c_cpp BrickstorOS和illumos系统的替代ps命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp BrickstorOS和illumos系统的替代ps命令相关的知识,希望对你有一定的参考价值。

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
// #include <sys/task.h>
// #include <sys/contract.h>

#include <signal.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <wchar.h>
#include <limits.h>
#include <procfs.h>
// #include <project.h>
#include <pwd.h>
#include <grp.h>
// #include <zone.h>

//
// Build on OmniOS with: /opt/gcc-6/bin/gcc -m64 -lumem -Wall -o pscsv ./pscsv.c
//
const char *dirpath = "/proc";
const char *psinfo_tpl = "%s/%s/psinfo";

static DIR *open_proc_dir(const char *dirpath)
{
	struct stat buf;
	DIR *dirp;

	if ((dirp = opendir(dirpath)) == NULL) {
		printf(gettext("Failed to open %s"), dirpath);
		return (NULL);
	}

	if (fstat(dirp->dd_fd, &buf) == -1) {
		printf(gettext("Failed to stat %s"), dirpath);
		(void) closedir(dirp);
		return (NULL);
	}

	if (strcmp(buf.st_fstype, "proc") != 0) {
		printf(gettext("%s is not a procfs mount point\n"), dirpath);
		(void) closedir(dirp);
		return (NULL);
	}

	return (dirp);
}

void print_process(const psinfo_t *p) {
    timestruc_t st = p->pr_start;
    printf("%d,%d,%ld.%ld,%d,%s,%s\n",
        p->pr_pid, p->pr_ppid, st.tv_sec, st.tv_nsec,
        p->pr_nlwp, p->pr_fname, p->pr_psargs);
}

int main(void) {
	char procpath[MAXPATHLEN];
	psinfo_t ps;
	dirent_t *dent;
	int procfd;
    DIR *dirp;
	
    if ((dirp = open_proc_dir(dirpath)) == NULL) return (1);

    while ((dent = readdir(dirp)) != NULL) {
		if (dent->d_name[0] == '.')
			continue;
        (void) snprintf(procpath, sizeof (procpath), psinfo_tpl,
		    dirpath, dent->d_name);
        if ((procfd = open(procpath, O_RDONLY)) == -1) exit(1);

        if (read(procfd, &ps, sizeof (ps)) == sizeof(psinfo_t)) {
            (void)print_process(&ps);
        }
    }
    return(closedir(dirp));
}

以上是关于c_cpp BrickstorOS和illumos系统的替代ps命令的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp BrickstorOS的快速单文件网络统计实用程序

c_cpp [illumos和bsros datalink layer]用于观察dladm和libdladm行为的Dtrace片段#tags:dladm,datalink,数据链管理,

sh BrickstorOS的容量,使用和配额

sh BrickStorOS基本健康和系统信息收集脚本

sh BrickstorOS启动池(bp)设置和修改

sh BrickStorOS配置调整脚本