📄 mom_mach.c
字号:
char *nusers(attrib)struct rm_attribute *attrib;{ char *id = "nusers"; int i, j; int nuids = 0; uid_t *uids, uid; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } if ((uids = (uid_t *)calloc(nproc, sizeof(uid_t))) == NULL) { log_err(errno, id, "no memory"); rm_errno = RM_ERR_SYSTEM; return NULL; } for (i=0; i<nproc; i++) { struct kinfo_proc *pp = &proc_tbl[i]; if ((uid = pp->kp_eproc.e_pcred.p_ruid) == 0) continue; DBPRT(("%s: pid %d uid %u\n", id, (int)pp->kp_proc.p_pid, uid)) for (j=0; j<nuids; j++) { if (uids[j] == uid) break; } if (j == nuids) /* not found */ uids[nuids++] = uid; /* so add it to list */ } sprintf(ret_string, "%d", nuids); free(uids); return ret_string;}static char *physmem(attrib)struct rm_attribute *attrib;{ char *id = "physmem"; int mib[2]; int physmem; size_t len = sizeof(physmem); if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } mib[0] = CTL_HW; mib[1] = HW_PHYSMEM; if (sysctl(mib, 2, &physmem, &len, NULL, 0) < 0) { log_err(errno, id, "sysctl"); rm_errno = RM_ERR_SYSTEM; return NULL; } sprintf(ret_string, "%ukb", physmem >> 10); /* in KB */ return ret_string;}char *size_fs(param)char *param;{ char *id = "size_fs"; FILE *mf; struct mntent *mp; struct statfs fsbuf; if (param[0] != '/') { sprintf(log_buffer, "%s: not full path filesystem name: %s", id, param); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (statfs(param, &fsbuf) == -1) { log_err(errno, id, "statfs"); rm_errno = RM_ERR_BADPARAM; return NULL; } /* in KB */ sprintf(ret_string, "%lukb", (unsigned long)(((double)fsbuf.f_bsize * (double)fsbuf.f_bavail) / 1024.0)); return ret_string;}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", 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;}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;voidsetmax(dev)char *dev;{ struct stat sb; if (stat(dev, &sb) == -1) return; if (maxtm < sb.st_atime) { DBPRT(("setmax: dev %s access %d replaces max %d\n", dev, sb.st_atime, maxtm)) maxtm = sb.st_atime; } return;}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")) == 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) { if (maxtm >= curtm) break; if (strncmp(de->d_name, "tty", 3)) continue; sprintf(ttyname, "/dev/%s", de->d_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"; struct pstats ps; pid_t value; int i, job, found = 0; time_t now, start; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = (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, "proc") == 0) job = 0; else if (strcmp(attrib->a_qualifier, "session") == 0) job = 1; else { rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } if ((now = time(NULL)) <= 0) { log_err(errno, id, "time"); rm_errno = RM_ERR_SYSTEM; return NULL; } start = now; for (i=0; i<nproc; i++) { struct kinfo_proc *pp = &proc_tbl[i]; if (job) { if (value != sess_tbl[i]) continue; } else { if (value != pp->kp_proc.p_pid) continue; } if (pp->kp_proc.p_stats == NULL) { rm_errno = RM_ERR_SYSTEM; return NULL; } if (kvm_read(kd, (u_long)pp->kp_proc.p_stats, &ps, sizeof(ps)) != sizeof(ps)) { log_err(errno, id, "kvm_read(pstats)"); rm_errno = RM_ERR_SYSTEM; return NULL; } found = 1; start = MIN(start, ps.p_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"; int mib[2]; struct loadavg la; size_t len = sizeof(la); mib[0] = CTL_VM; mib[1] = VM_LOADAVG; if (sysctl(mib, 2, &la, &len, NULL, 0) < 0) { log_err(errno, id, "sysctl(VM_LOADAVG)"); return (rm_errno = RM_ERR_SYSTEM); } *rv = (double)la.ldavg[0]/la.fscale; return 0;}u_longgracetime(secs)u_long secs;{ time_t now = time((time_t *)NULL); if (secs > now) /* time is in the future */ return (secs - now); else return 0;}static char *quota(attrib)struct rm_attribute *attrib;{ char *id = "quota"; int type; dev_t dirdev; uid_t uid; struct stat sb; struct fstab *fs; struct dqblk qi; struct passwd *pw; static char *type_array[] = { "harddata", "softdata", "currdata", "hardfile", "softfile", "currfile", "timedata", "timefile", }; enum type_name { harddata, softdata, currdata, hardfile, softfile, currfile, timedata, timefile, type_end }; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "type")) { sprintf(log_buffer, "unknown qualifier %s", attrib->a_qualifier); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } for (type=0; type<type_end; type++) { if (strcmp(attrib->a_value, type_array[type]) == 0) break; } if (type == type_end) { /* check to see if command is legal */ sprintf(log_buffer, "bad param: %s=%s", attrib->a_qualifier, attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if ((attrib = momgetattr(NULL)) == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "dir") != 0) { sprintf(log_buffer, "bad param: %s=%s", attrib->a_qualifier, attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (attrib->a_value[0] != '/') { /* must be absolute path */ sprintf(log_buffer, "not an absolute path: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (stat(attrib->a_value, &sb) == -1) { sprintf(log_buffer, "stat: %s", attrib->a_value); log_err(errno, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } dirdev = sb.st_dev; DBPRT(("dir has devnum %d\n", dirdev)) if (setfsent() == NULL) { log_err(errno, id, "setfsent"); rm_errno = RM_ERR_SYSTEM; return NULL; } while ((fs = getfsent()) != NULL) { if (strcmp(fs->fs_type, FSTAB_XX) == 0 || strcmp(fs->fs_type, FSTAB_SW) == 0) continue; if (stat(fs->fs_file, &sb) == -1) { sprintf(log_buffer, "stat: %s", fs->fs_file); log_err(errno, id, log_buffer); continue; } DBPRT(("%s\t%s\t%d\n", fs->fs_spec, fs->fs_file, sb.st_dev)) if (sb.st_dev == dirdev) break; } endfsent(); if (fs == NULL) { sprintf(log_buffer, "filesystem %s not found", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } if ((attrib = momgetattr(NULL)) == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "user") != 0) { sprintf(log_buffer, "bad param: %s=%s", attrib->a_qualifier, attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if ((uid = (uid_t)atoi(attrib->a_value)) == 0) { if ((pw = getpwnam(attrib->a_value)) == NULL) { sprintf(log_buffer, "user not found: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } uid = pw->pw_uid; } if (quotactl(fs->fs_file, Q_GETQUOTA, uid, (char *)&qi) == -1) { log_err(errno, id, "quotactl"); rm_errno = RM_ERR_SYSTEM; return NULL; } switch (type) { case harddata: sprintf(ret_string, "%u", dbtob(qi.dqb_bhardlimit)); break; case softdata: sprintf(ret_string, "%u", dbtob(qi.dqb_bsoftlimit)); break; case currdata: sprintf(ret_string, "%u", dbtob(qi.dqb_curblocks)); break; case hardfile: sprintf(ret_string, "%u", qi.dqb_ihardlimit); break; case softfile: sprintf(ret_string, "%u", qi.dqb_isoftlimit); break; case currfile: sprintf(ret_string, "%u", qi.dqb_curinodes); break; case timedata: sprintf(ret_string, "%u", gracetime(qi.dqb_btime)); break; case timefile: sprintf(ret_string, "%u", gracetime(qi.dqb_itime)); break; } return ret_string;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -