📄 mom_mach.c
字号:
break; } if (i == nproc) { rm_errno = RM_ERR_EXIST; return NULL; } sprintf(ret_string, "%ukb", (pi->pr_rssize * page_size) >> 10); /* KB */ return ret_string;}static char *resi(attrib)struct rm_attribute *attrib;{ char *id = "resi"; int value; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") == 0) return (resi_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (resi_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}static char *sessions(attrib)struct rm_attribute *attrib;{ char *id = "sessions"; int i, j; prpsinfo_t *pi; char *fmt; int njids = 0; pid_t *jids, *hold; static int maxjid = 200; register pid_t jobid; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if ((jids = (pid_t *)calloc(maxjid, sizeof(pid_t))) == NULL) { log_err(errno, id, "no memory"); rm_errno = RM_ERR_SYSTEM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } /* ** Search for members of job */ for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (pi->pr_uid == 0) continue; if ((jobid = pi->pr_sid) == 0) continue; DBPRT(("%s[%d]: pid %d sid %d\n", id, njids, pi->pr_pid, jobid)) for (j=0; j<njids; j++) { if (jids[j] == jobid) break; } if (j == njids) { /* not found */ if (njids == maxjid) { /* need more space */ maxjid += 100; hold = (pid_t *)realloc(jids, maxjid); if (hold == NULL) { log_err(errno, id, "realloc"); rm_errno = RM_ERR_SYSTEM; free(jids); return NULL; } jids = hold; } jids[njids++] = jobid; /* add jobid to list */ } } fmt = ret_string; for (j=0; j<njids; j++) { checkret(&fmt, 100); sprintf(fmt, " %d", (int)jids[j]); fmt += strlen(fmt); } free(jids); return ret_string;}static char *nsessions(attrib)struct rm_attribute *attrib;{ char *result, *ch; int num = 0; if ((result = sessions(attrib)) == NULL) return result; for (ch=result; *ch; ch++) { if (*ch == ' ') /* count blanks */ num++; } sprintf(ret_string, "%d", num); return ret_string;}static char *pids(attrib)struct rm_attribute *attrib;{ char *id = "pids"; pid_t jobid; int i, j; prpsinfo_t *pi; char *fmt; int num_pids; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((jobid = (pid_t)atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") != 0) { rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } /* ** Search for members of session */ fmt = ret_string; num_pids = 0; for (i=0; i<nproc; i++) { pi = &proc_info[i]; DBPRT(("%s[%d]: pid: %d sid %d\n", id, num_pids, pi->pr_pid, pi->pr_sid)) if (jobid != pi->pr_sid) continue; sprintf(fmt, "%d ", pi->pr_pid); fmt += strlen(fmt); num_pids++; } if (num_pids == 0) { rm_errno = RM_ERR_EXIST; return NULL; } return ret_string;}static char *nusers(attrib)struct rm_attribute *attrib;{ char *id = "nusers"; int i, j; prpsinfo_t *pi; int nuids = 0; uid_t *uids, *hold; static int maxuid = 200; register uid_t uid; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if ((uids = (uid_t *)calloc(maxuid, sizeof(uid_t))) == NULL) { log_err(errno, id, "no memory"); rm_errno = RM_ERR_SYSTEM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } for (i=0; i<nproc; i++) { pi = &proc_info[i]; if ((uid = pi->pr_uid) == 0) continue; DBPRT(("%s[%d]: pid %d uid %d\n", id, nuids, pi->pr_pid, uid)) for (j=0; j<nuids; j++) { if (uids[j] == uid) break; } if (j == nuids) { /* not found */ if (nuids == maxuid) { /* need more space */ maxuid += 100; hold = (uid_t *)realloc(uids, maxuid); if (hold == NULL) { log_err(errno, id, "realloc"); rm_errno = RM_ERR_SYSTEM; free(uids); return NULL; } uids = hold; } uids[nuids++] = uid; /* add uid to list */ } } sprintf(ret_string, "%d", nuids); free(uids); return ret_string;}static char *ncpus(attrib)struct rm_attribute *attrib;{ char *id = "ncpus"; unsigned int pmem; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } sprintf(ret_string, "%d", sysconf(_SC_NPROCESSORS_ONLN)); return ret_string;}static char *physmem(attrib)struct rm_attribute *attrib;{ char *id = "physmem"; unsigned int pmem; int kbytes; int start; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } getsysinfo(GSI_PHYSMEM, (caddr_t) &kbytes, sizeof(kbytes), &start); sprintf(ret_string, "%ukb", kbytes); /* KB */ return ret_string;}static char *size_fs(param)char *param;{ char *id = "size_fs"; FILE *mf; struct mntent *mp; struct statvfs fsbuf; if (param[0] != '/') { sprintf(log_buffer, "%s: not full path filesystem name: %s\n", id, param); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (statvfs(param, &fsbuf) == -1) { log_err(errno, id, "statvfs"); rm_errno = RM_ERR_BADPARAM; return NULL; } /* in KB */ sprintf(ret_string, "%lukb", (unsigned long)(((double)fsbuf.f_bsize * (double)fsbuf.f_bfree) / 1024.0)); return ret_string;}static char *size_file(param)char *param;{ char *id = "size_file"; struct stat sbuf; if (param[0] != '/') { sprintf(log_buffer, "%s: not full path filesystem name: %s\n", id, param); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (stat(param, &sbuf) == -1) { log_err(errno, id, "stat"); rm_errno = RM_ERR_BADPARAM; return NULL; } sprintf(ret_string, "%ukb", sbuf.st_size >> 10); /* in KB */ return ret_string;}static char *size(attrib)struct rm_attribute *attrib;{ char *id = "size"; char *param; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } param = attrib->a_value; if (strcmp(attrib->a_qualifier, "file") == 0) return (size_file(param)); else if (strcmp(attrib->a_qualifier, "fs") == 0) return (size_fs(param)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}time_t maxtm;static voidsetmax(dev)char *dev;{ struct stat sb; if (stat(dev, &sb) == -1) return; if (maxtm < sb.st_atime) maxtm = sb.st_atime; return;}static char *idletime(attrib)struct rm_attribute *attrib;{ char *id = "idletime"; DIR *dp; struct dirent *de; char ttyname[50]; time_t curtm; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if ((dp=opendir("/dev/pts")) == NULL) { log_err(errno, id, "opendir /dev"); rm_errno = RM_ERR_SYSTEM; return NULL; } maxtm = 0; curtm = time(NULL); setmax("/dev/mouse"); while ((de=readdir(dp)) != NULL) { char *name = de->d_name; if (maxtm >= curtm) break; if (*name == '.') continue; sprintf(ttyname, "/dev/pts/%s", name); setmax(ttyname); } closedir(dp); sprintf(ret_string, "%d", MAX(0, curtm - maxtm)); return ret_string;}static char *walltime(attrib)struct rm_attribute *attrib;{ char *id = "walltime"; int i; int value, job, found = 0; time_t now, start; prpsinfo_t *pi; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "proc") == 0) job = 0; else if (strcmp(attrib->a_qualifier, "session") == 0) job = 1; else { rm_errno = RM_ERR_BADPARAM; return NULL; } if ((now = time(NULL)) <= 0) { log_err(errno, id, "time"); rm_errno = RM_ERR_SYSTEM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } start = now; for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (job) { if (value != pi->pr_sid) continue; } else { if ((pid_t)value != pi->pr_pid) continue; } found = 1; start = MIN(start, pi->pr_start.tv_sec); } if (found) { sprintf(ret_string, "%ld", (long)((double)(now - start) * wallfactor)); return ret_string; } rm_errno = RM_ERR_EXIST; return NULL;}intget_la(rv) double *rv;{ char *id = "get_la"; double avg; struct tbl_loadavg info; int ret; ret = table(TBL_LOADAVG, 0L, &info, 1, sizeof(info)); if ( ret < 0 ) { log_err(-1, id, extra_parm); return (rm_errno = RM_ERR_SYSTEM); } if ( info.tl_lscale == 0 ) { avg = info.tl_avenrun.d[0]; } else { avg = (double) info.tl_avenrun.l[0]; avg /= (double) info.tl_lscale; } *rv = (double)avg; return 0;}static char *cpuspeed(attrib)struct rm_attribute *attrib;{ char *id = "cpuspeed"; double avg; struct tbl_loadavg info; int ret; double scale = -1; int err; double freq; struct rpb rpbbuf; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } err = getsysinfo(GSI_GET_HWRPB, &rpbbuf, sizeof(rpbbuf)); if (err < 0) { freq = 233; } else { freq = rpbbuf.rpb_counter; } /* * Scale is the 1 over the clock frequency, in Mhz */ scale = ((double) freq / (1000 * 1000)); sprintf(ret_string, "%d", (int) scale); return ret_string;}static char *cputype(attrib)struct rm_attribute *attrib;{ char *id = "cputype"; double avg; struct tbl_loadavg info; int ret; static long cputype = -1; static char *cputypename; static char errbuf[128]; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if ( cputype == -1 ) { int err; double freq; err = getsysinfo(GSI_PROC_TYPE, &cputype, sizeof(cputype)); if (err < 0) { cputype = -1; sprintf(errbuf, "[errno = %d]", errno); cputypename = errbuf; } else { CPU_TYPE_TO_TEXT(cputype & 0xfffff, cputypename); if ( strcmp(cputypename,"") == 0 ) { sprintf(errbuf, "[error, cputype = %d]", cputype); } } } return cputypename;}static char *platform(attrib)struct rm_attribute *attrib;{ char *id = "platform"; int err; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } err = getsysinfo(GSI_PLATFORM_NAME, ret_string, ret_size-1); if (err < 0) { return "unknown [error]"; } else { return ret_string; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -