📄 proc.c
字号:
static int mib[] = { CTL_KERN, KERN_PROC , KERN_PROC_ALL }; if (sysctl(mib, 3, NULL, &nproc, NULL, 0) < 0) return 0; if(nproc > onproc || !pbase) { if((pbase = (struct kinfo_proc*) realloc(pbase, nproc + sizeof(struct kinfo_proc))) == 0) return -1; onproc = nproc; memset(pbase,0,nproc + sizeof(struct kinfo_proc)); } if (sysctl(mib, 3, pbase, &nproc, NULL, 0) < 0) return -1; for (pp = pbase, i = 0; i < nproc / sizeof(struct kinfo_proc); pp++, i++) { if (PP(pp, p_stat) != 0 && (((PP(pp, p_flag) & P_SYSTEM) == 0))) { if (PP(pp, p_stat) != SZOMB && !strcmp(PP(pp,p_comm),procname)) ret++; } } return ret;}#elif OSTYPE == ULTRIXID#define NPROCS 32 /* number of proces to read at once */extern int kmem, mem, swap;#include <sys/user.h>#include <sys/proc.h>#include <sys/file.h>#include <sys/vm.h>#include <machine/pte.h>#ifdef HAVE_NLIST_H#include <nlist.h>#endifstatic struct user *getuser (struct proc *);static int getword (off_t);static int getstruct (off_t, char *, off_t, int);static struct nlist proc_nl[] = { { "_nproc" },#define X_NPROC 0 { "_proc" },#define X_PROC 1 { "_proc_bitmap" },#define X_PROC_BITMAP 2 { NULL }};intsh_count_procs(char *procname){ int total, proc_active, nproc; int thisproc = 0; int absolute_proc_number = -1; struct user *auser; struct proc *aproc, *procp; unsigned bitmap; struct proc procs[NPROCS], *procsp; static int inited = 0; procp = (struct proc *)getword(proc_nl[X_PROC].n_value); nproc = getword(proc_nl[X_NPROC].n_value); total = 0; for (;;) { do { while (thisproc == 0) { int nread; int psize; if (nproc == 0) return(total); thisproc = MIN(NPROCS, nproc); psize = thisproc * sizeof(struct proc); nproc -= thisproc; if (lseek(kmem, (off_t)procp, L_SET) == -1 || (nread = read(kmem, (char *)procs, psize)) < 0) { /* warn("read proc"); */ return(total); } else if (nread != psize) { thisproc = nread / sizeof(struct proc); nproc = 0; /* warn("read proc: short read"); */ } procsp = procs; procp += thisproc; } aproc = procsp++; thisproc--; absolute_proc_number++; if ((absolute_proc_number % 32) == 0) bitmap = getword((unsigned int)proc_nl[X_PROC_BITMAP].n_value + ((absolute_proc_number / 32) * 4)); proc_active = (bitmap & (1 << (absolute_proc_number % 32))) != 0; if (proc_active && aproc->p_stat != SZOMB && !(aproc->p_type & SWEXIT)) auser = getuser(aproc); } while (!proc_active || auser == NULL); if (strcmp(auser->u_comm, procname) == 0) total ++; }}#define SW_UADDR dtob(getword((off_t)dmap.dm_ptdaddr))#define SW_UBYTES sizeof(struct user)#define SKRD(file, src, dst, size) \ (lseek(file, (off_t)(src), L_SET) == -1) || \ (read(file, (char *)(dst), (size)) != (size))static struct user *getuser(struct proc *aproc){ static union { struct user user; char upgs[UPAGES][NBPG]; } u; static struct pte uptes[UPAGES]; static struct dmap dmap; int i, nbytes; /* * If process is not in core, we simply snarf it's user struct * from the swap device. */ if ((aproc->p_sched & SLOAD) == 0) { if (!getstruct((off_t)aproc->p_smap, "aproc->p_smap", (off_t)&dmap, sizeof(dmap))) { /* warnx("can't read dmap for pid %d from %s", aproc->p_pid, _PATH_DRUM); */ return(NULL); } if (SKRD(swap, SW_UADDR, &u.user, SW_UBYTES)) { /* warnx("can't read u for pid %d from %s", aproc->p_pid, _PATH_DRUM); */ return(NULL); } return (&u.user); } /* * Process is in core. Follow p_addr to read in the page * table entries that map the u-area and then read in the * physical pages that comprise the u-area. * * If at any time, an lseek() or read() fails, print a warning * message and return NULL. */ if (SKRD(kmem, aproc->p_addr, uptes, sizeof(uptes))) { /* warnx("can't read user pt for pid %d from %s", aproc->p_pid, _PATH_DRUM); */ return(NULL); } nbytes = sizeof(struct user); for (i = 0; i < UPAGES && nbytes > 0; i++) { if (SKRD(mem, ptob(uptes[i].pg_pfnum), u.upgs[i], NBPG)) { /* warnx("can't read user page %u for pid %d from %s", uptes[i].pg_pfnum, aproc->p_pid, _PATH_MEM); */ return(NULL); } nbytes -= NBPG; } return(&u.user);}static intgetword(off_t loc){ int val; if (SKRD(kmem, loc, &val, sizeof(val))) exit(1); return(val);}static intgetstruct(off_t loc, char *name, off_t dest, int size){ if(SKRD(kmem,loc,dest,size)) return(0); return(1);}#elif OSTYPE == SOLARISID#ifdef _SLASH_PROC_METHOD_#include <fcntl.h>#include <dirent.h>#include <procfs.h>/* * Gets process information from /proc/.../psinfo */ intsh_count_procs(char *procname){ int fd,total = 0; struct psinfo info; char fbuf[32]; struct dirent *ent; DIR *dir; if (!(dir = opendir("/proc"))) return -1; while ((ent = readdir(dir))) { if (!strcmp(ent->d_name,"..") || !strcmp(ent->d_name,".")) continue; snprintf(fbuf,sizeof fbuf,"/proc/%s/psinfo",ent->d_name); if ((fd = open(fbuf,O_RDONLY)) < 0) { /* Continue or return error? */ closedir(dir); return -1; } if (read(fd,(char*)&info,sizeof(struct psinfo)) != sizeof(struct psinfo)) { close(fd); closedir(dir); return -1; } if (!info.pr_nlwp && !info.pr_lwp.pr_lwpid) { /* Zombie process */ } else if (!strcmp(procname,info.pr_fname)) total++; close(fd); } closedir(dir); return total;} #else /* _SLASH_PROC_METHOD_ */#define _KMEMUSER /* Needed by <sys/user.h> */#include <kvm.h>#include <fcntl.h>#include <sys/user.h>#include <sys/proc.h>intsh_count_procs(char *procname){ struct proc *p; struct user *u; int total; if (kd == NULL) { return -1; } if( kvm_setproc(kd) < 0 ) { return( -1 ); } kvm_setproc(kd); total = 0; while ((p = kvm_nextproc(kd)) != NULL) { if( !p ) { return( -1 ); } u = kvm_getu(kd, p); /* Skip this entry if u or u->u_comm is a NULL pointer */ if( !u ) { continue; } if (strcmp(procname, u->u_comm) == 0) total++; } return(total);}#endif /* _SLASH_PROC_METHOD_ */#elseint sh_count_procs(char *procname){ char line[STRMAX], *cptr, *cp; int ret=0, fd; FILE *file;#ifndef EXCACHETIME#endif struct extensible ex; int slow = strstr (PSCMD, "ax") != NULL; strcpy(ex.command,PSCMD); if ((fd = get_exec_output(&ex)) > 0) { if ((file = fdopen(fd,"r")) == NULL) { setPerrorstatus("fdopen"); close(fd); return (-1); } while(fgets(line,sizeof(line),file) != NULL) { if (slow) { cptr = find_field(line, 5); cp = strrchr(cptr, '/'); if (cp) cptr = cp+1; else if (*cptr == '-') cptr++; else if (*cptr == '[') { cptr++; cp = strchr(cptr, ']'); if (cp) *cp = 0; } copy_word(cptr, line); cp = line+strlen(line)-1; if (*cp == ':') *cp = 0; } else { if ((cptr = find_field(line,LASTFIELD)) == NULL) continue; copy_word(cptr,line); } if (!strcmp(line,procname)) ret++; } if (ftell(file) < 2) {#ifdef USING_UCD_SNMP_ERRORMIB_MODULE seterrorstatus("process list unreasonable short (mem?)",2);#endif ret = -1; } fclose(file); wait_on_exec(&ex); } else { ret = -1; } return(ret);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -