📄 mom_mach.c
字号:
task *ptask; char *file;{ return (-1);}#define dsecs(val) ( (double)(val).tv_sec + ((double)(val).tv_nsec * 1.0e-9) )char *cput_job(jobid)pid_t jobid;{ char *id = "cput_job"; int found = 0; int fd; struct dirent *dent; char procname[100]; double cputime, addtime; prstatus_t ps; cputime = 0.0; rewinddir(pdir); for (fd = -1; (dent = readdir(pdir)) != NULL; close(fd)) { if (!isdigit(dent->d_name[0])) continue; sprintf(procname, procfmts, dent->d_name); if ((fd = open(procname, O_RDONLY)) == -1) continue; if (ioctl(fd, PIOCSTATUS, &ps) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCSTATUS)", procname); log_err(errno, id, log_buffer); continue; } if (jobid != ps.pr_sid) continue; found = 1; addtime = dsecs(ps.pr_utime) + dsecs(ps.pr_stime) + dsecs(ps.pr_cutime) + dsecs(ps.pr_cstime); cputime += addtime; DBPRT(("%s: total %.2f pid %d %.2f\n", id, cputime, ps.pr_pid, addtime)) } if (found) { sprintf(ret_string, "%.2f", cputime * cputfactor); return ret_string; } rm_errno = RM_ERR_EXIST; return NULL;}char *cput_proc(pid)pid_t pid;{ char *id = "cput_pid"; char procname[100]; double cputime; int fd; prstatus_t ps; cputime = 0.0; sprintf(procname, procfmtd, pid); if ((fd = open(procname, O_RDONLY)) == -1) { rm_errno = RM_ERR_EXIST; return NULL; } if (ioctl(fd, PIOCSTATUS, &ps) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCSTATUS)", procname); log_err(errno, id, log_buffer); close (fd); rm_errno = RM_ERR_SYSTEM; return NULL; } cputime = dsecs(ps.pr_utime) + dsecs(ps.pr_stime) + dsecs(ps.pr_cutime) + dsecs(ps.pr_cstime); close(fd); sprintf(ret_string, "%.2f", cputime * cputfactor); return ret_string;}char *cput(attrib)struct rm_attribute *attrib;{ char *id = "cput"; 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 (cput_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (cput_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}char *mem_job(sid)pid_t sid;{ ulong memsize; static list_head taskhead; static task onetask; static int first = 1; if (first) { CLEAR_HEAD(taskhead); CLEAR_LINK(onetask.ti_jobtask); append_link(&taskhead, &onetask.ti_jobtask, &onetask); first = 0; } onetask.ti_qs.ti_sid = sid; memsize = mem_sum(&taskhead); if (memsize == 0) { rm_errno = RM_ERR_EXIST; return NULL; } else { sprintf(ret_string, "%lukb", memsize >> 10); /* in KB */ return ret_string; }}char *mem_proc(pid)pid_t pid;{ char *id = "mem_proc"; char procname[100]; int fd; prpsinfo_t pi; sprintf(procname, procfmtd, pid); if ((fd = open(procname, O_RDONLY)) == -1) { rm_errno = RM_ERR_EXIST; return NULL; } if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); close(fd); rm_errno = RM_ERR_SYSTEM; return NULL; } close(fd); sprintf(ret_string, "%lukb", ((ulong)pi.pr_size * (ulong)pagesize) >> 10); /* in KB */ return ret_string;}char *mem(attrib)struct rm_attribute *attrib;{ char *id = "mem"; 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 (mem_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (mem_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}static char *resi_job(jobid)pid_t jobid;{ char *id = "resi_job"; ulong resisize; int fd; int found = 0; char procname[100]; struct dirent *dent; prpsinfo_t pi; resisize = 0; rewinddir(pdir); for (fd = -1; (dent = readdir(pdir)) != NULL; close(fd)) { if (!isdigit(dent->d_name[0])) continue; sprintf(procname, procfmts, dent->d_name); if ((fd = open(procname, O_RDONLY)) == -1) continue; if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); continue; } if (jobid != pi.pr_sid) continue; found = 1; resisize += pi.pr_rssize; } if (found) { /* in KB */ sprintf(ret_string, "%lukb",(resisize * (ulong)pagesize) >> 10); return ret_string; } rm_errno = RM_ERR_EXIST; return NULL;}static char *resi_proc(pid)pid_t pid;{ char *id = "resi_proc"; char procname[100]; int fd; prpsinfo_t pi; sprintf(procname, procfmtd, pid); if ((fd = open(procname, O_RDONLY)) == -1) { rm_errno = RM_ERR_EXIST; return NULL; } if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); close(fd); rm_errno = RM_ERR_SYSTEM; return NULL; } close(fd); sprintf(ret_string, "%lukb", ((ulong)pi.pr_rssize * (ulong)pagesize) >> 1024); /* 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; }}char *sessions(attrib)struct rm_attribute *attrib;{ char *id = "sessions"; int fd, j; struct dirent *dent; prpsinfo_t pi; char procname[100]; 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; } /* ** Search for members of session */ rewinddir(pdir); for (fd = -1; (dent = readdir(pdir)) != NULL; close(fd)) { if (!isdigit(dent->d_name[0])) continue; sprintf(procname, procfmts, dent->d_name); if ((fd = open(procname, O_RDONLY)) == -1) continue; if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); continue; } 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;}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;}char *pids(attrib)struct rm_attribute *attrib;{ char *id = "pids"; pid_t jobid; int fd; char procname[100]; struct dirent *dent; prpsinfo_t pi; int i; 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; } /* ** Search for members of session */ rewinddir(pdir); fmt = ret_string; for (fd=-1, num_pids=0; (dent = readdir(pdir)) != NULL; close(fd)) { if (!isdigit(dent->d_name[0])) continue; sprintf(procname, procfmts, dent->d_name); if ((fd = open(procname, O_RDONLY)) == -1) continue; if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); continue; } DBPRT(("%s[%d]: pid: %s sid %d\n", id, num_pids, dent->d_name, pi.pr_sid)) if (jobid != pi.pr_sid) continue; sprintf(fmt, "%s ", dent->d_name); fmt += strlen(fmt); num_pids++; } if (num_pids == 0) { rm_errno = RM_ERR_EXIST; return NULL; } return ret_string;}char *nusers(attrib)struct rm_attribute *attrib;{ char *id = "nusers"; int fd, j; struct dirent *dent; prpsinfo_t pi; char procname[100]; 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; } rewinddir(pdir); for (fd = -1; (dent = readdir(pdir)) != NULL; close(fd)) { if (!isdigit(dent->d_name[0])) continue; sprintf(procname, procfmts, dent->d_name); if ((fd = open(procname, O_RDONLY)) == -1) continue; if (ioctl(fd, PIOCPSINFO, &pi) == -1) { sprintf(log_buffer, "%s: ioctl(PIOCPSINFO)", procname); log_err(errno, id, log_buffer); continue; } 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -