summaryrefslogtreecommitdiff
blob: 9d68b7b12cda4d693a9d5e55719e1f538b93cf34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/agent/mibgroup/ucd-snmp/proc.c,v
retrieving revision 5.4
retrieving revision 5.6
diff -u -r5.4 -r5.6
--- net-snmp/net-snmp/agent/mibgroup/ucd-snmp/proc.c	2002/12/19 15:07:33	5.4
+++ net-snmp/net-snmp/agent/mibgroup/ucd-snmp/proc.c	2003/02/07 05:17:42	5.6
@@ -413,13 +413,15 @@
 sh_count_procs(char *procname)
 {
     DIR *dir;
-    char cmdline[512];
+    char cmdline[512], *tmpc;
     struct dirent *ent;
     int fd,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;
@@ -430,6 +432,30 @@
       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;
+      DEBUGMSGTL(("proc","Comparing wanted %s against %s\n",
+                  procname, tmpc));
+      if(!strncmp(tmpc,procname,plen)) {
+          total++;
+          DEBUGMSGTL(("proc", " Matched.  total count now=%d\n", total));
+      }
+#endif      
     }
     closedir(dir);
     return total;
@@ -662,8 +688,14 @@
             /*
              * Zombie process 
              */
-        } else if (!strcmp(procname, info.pr_fname))
-            total++;
+        } 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);
     }