⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proc.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <unistd.h>intsh_count_procs(char *procname){    DIR *dir;    char cmdline[512], *tmpc;    struct dirent *ent;#ifdef USE_PROC_CMDLINE    int fd;#endif    int len,plen=strlen(procname),total = 0;    FILE *status;    if ((dir = opendir("/proc")) == NULL) return -1;    while (NULL != (ent = readdir(dir))) {      if(!(ent->d_name[0] >= '0' && ent->d_name[0] <= '9')) continue;#ifdef USE_PROC_CMDLINE  /* old method */      /* read /proc/XX/cmdline */      sprintf(cmdline,"/proc/%s/cmdline",ent->d_name);      if((fd = open(cmdline, O_RDONLY)) < 0) break;      len = read(fd,cmdline,sizeof(cmdline) - 1);      close(fd);      if(len <= 0) continue;      cmdline[len] = 0;      while(--len && !cmdline[len]);      while(--len) if(!cmdline[len]) cmdline[len] = ' ';      if(!strncmp(cmdline,procname,plen)) total++;#else      /* read /proc/XX/status */      sprintf(cmdline,"/proc/%s/status",ent->d_name);      if ((status = fopen(cmdline, "r")) == NULL)          break;      if (fgets(cmdline, sizeof(cmdline), status) == NULL) {          fclose(status);          break;      }      fclose(status);      cmdline[sizeof(cmdline)-1] = '\0';      /* XXX: assumes Name: is first */      if (strncmp("Name:",cmdline, 5) != 0)          break;      tmpc = skip_token(cmdline);      if (!tmpc)          break;      for (len=0;; len++) {	if (tmpc[len] && isgraph(tmpc[len])) continue;	tmpc[len]='\0';	break;      }      DEBUGMSGTL(("proc","Comparing wanted %s against %s\n",                  procname, tmpc));      if(len==plen && !strncmp(tmpc,procname,plen)) {          total++;          DEBUGMSGTL(("proc", " Matched.  total count now=%d\n", total));      }#endif          }    closedir(dir);    return total;}#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"))) {        snmp_perror("/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? */            snmp_perror(fbuf);	    continue;        }        if (read(fd, (char *) &info, sizeof(struct psinfo)) !=            sizeof(struct psinfo)) {            snmp_perror(fbuf);            close(fd);            closedir(dir);            return -1;        }        if (!info.pr_nlwp && !info.pr_lwp.pr_lwpid) {            /*             * Zombie process              */        } else {            DEBUGMSGTL(("proc","Comparing wanted %s against %s\n",                        procname, info.pr_fname));            if (!strcmp(procname, info.pr_fname)) {                total++;                DEBUGMSGTL(("proc", " Matched.  total count now=%d\n", 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_ */#elseintsh_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_nword(cptr, line, sizeof(line));                cp = line + strlen(line) - 1;                if (*cp == ':')                    *cp = 0;            } else {                if ((cptr = find_field(line, LASTFIELD)) == NULL)                    continue;                copy_nword(cptr, line, sizeof(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 + -