Debian Codegraph @

agent/mibgroup/host/data_access/swrun_procfs_psinfo.c

raw
1 /*
2 * swrun_procfs_psinfo.c:
3 * hrSWRunTable data access:
4 * /proc/{pid}/psinfo interface - Solaris
5 */
6 #include <net-snmp/net-snmp-config.h>
7
8 #include <stdio.h>
9 #ifdef HAVE_STDLIB_H
10 #include <stdlib.h>
11 #endif
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18
19 #ifdef HAVE_DIRENT_H
20 #include <dirent.h>
21 #endif
22 #ifdef HAVE_FCNTL_H
23 #include <fcntl.h>
24 #endif
25
26 #include <procfs.h>
27 #ifdef HAVE_SYS_PROC_H
28 #include <sys/proc.h>
29 #endif
30
31 #include <sys/processor.h>
32 #include <sys/procset.h>
33 #include <thread.h>
34
35 #include <net-snmp/net-snmp-includes.h>
36 #include <net-snmp/agent/net-snmp-agent-includes.h>
37 #include <net-snmp/library/container.h>
38 #include <net-snmp/library/snmp_debug.h>
39 #include <net-snmp/data_access/swrun.h>
40 #include "swrun_private.h"
41
42 /* ---------------------------------------------------------------------
43 */
44 void
45 netsnmp_arch_swrun_init(void)
46 {
47 /* Nothing to do */
48 return;
49 }
50
51 /* ---------------------------------------------------------------------
52 */
53 int
54 netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
55 {
56 DIR *procdir = NULL;
57 struct dirent *procentry_p;
58 psinfo_t psinfo;
59 int pid, fd;
60 char *cp, buf[512];
61 netsnmp_swrun_entry *entry;
62
63 procdir = opendir("/proc");
64 if ( NULL == procdir ) {
65 snmp_log( LOG_ERR, "Failed to open /proc" );
66 return -1;
67 }
68
69 /*
70 * Walk through the list of processes in the /proc tree
71 */
72 while ( NULL != (procentry_p = readdir( procdir ))) {
73 pid = atoi( procentry_p->d_name );
74 if ( 0 == pid )
75 continue; /* Presumably '.' or '..' */
76
77 entry = netsnmp_swrun_entry_create(pid);
78 if (NULL == entry)
79 continue; /* error already logged by function */
80 CONTAINER_INSERT(container, entry);
81
82 /*
83 * Now extract the interesting information
84 * from the various /proc{PID}/ interface files
85 */
86
87 snprintf( buf, sizeof(buf), "/proc/%d/psinfo", pid );
88 fd = open( buf, O_RDONLY );
89 read( fd, &psinfo, sizeof(psinfo));
90 close(fd);
91
92 entry->hrSWRunName_len
93 = sprintf(entry->hrSWRunName, "%.*s",
94 (int)sizeof(entry->hrSWRunName) - 1,
95 psinfo.pr_fname);
96 /*
97 * Split pr_psargs into two:
98 * argv[0] is hrSWRunPath
99 * argv[1..] is hrSWRunParameters
100 */
101 cp = strchr(psinfo.pr_psargs, ' ');
102 if (cp)
103 *cp = '\0'; /* End of argv[0] */
104 entry->hrSWRunPath_len = sprintf(entry->hrSWRunPath, "%.*s",
105 (int)sizeof(entry->hrSWRunPath) - 1, psinfo.pr_psargs);
106 if (cp) {
107 entry->hrSWRunParameters_len = sprintf(entry->hrSWRunParameters,
108 "%.*s", (int)sizeof(entry->hrSWRunParameters) - 1, cp+1);
109 *cp = ' '; /* Restore pr_psargs value */
110 }
111
112 /*
113 * check for system processes
114 */
115 entry->hrSWRunType = (PR_ISSYS & psinfo.pr_flag)
116 ? 2 /* kernel process */
117 : 4 /* application */
118 ;
119
120 switch (psinfo.pr_lwp.pr_state) {
121 case SRUN:
122 case SONPROC: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
123 break;
124 case SSLEEP: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
125 break;
126 case SSTOP: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
127 break;
128 case SIDL:
129 case SZOMB:
130 default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
131 break;
132 }
133
134 entry->hrSWRunPerfCPU = (psinfo.pr_time.tv_sec * 100);
135 entry->hrSWRunPerfCPU += (psinfo.pr_time.tv_nsec / 10000000);
136 entry->hrSWRunPerfMem = psinfo.pr_rssize;
137 /* XXX - is this reported in kB? */
138 }
139 closedir( procdir );
140
141 DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n",
142 (int)CONTAINER_SIZE(container)));
143
144 return 0;
145 }