1
2 #include "os/NetBSD.h"
3
4 /* Given a path to a /proc/XXX/status file and a pointer to a procstat
5 struct, fill the struct */
get_procstat(char * path,struct procstat * prs)6 struct procstat* get_procstat( char* path, struct procstat* prs){
7 FILE* fp;
8
9 if( (fp = fopen( path, "r" )) != NULL ){
10 fscanf(fp,
11 "%s %d %d %d %d %d,%d %s %d,%d %d,%d %d,%d %s %d %d %d,%d,%s",
12 &prs->comm,
13 &prs->pid,
14 &prs->ppid,
15 &prs->pgid,
16 &prs->sid,
17 &prs->tdev_maj,
18 &prs->tdev_min,
19 &prs->flags,
20 &prs->start,
21 &prs->start_mic,
22 &prs->utime,
23 &prs->utime_mic,
24 &prs->stime,
25 &prs->stime_mic,
26 &prs->wchan,
27 &prs->euid,
28 &prs->ruid,
29 &prs->rgid,
30 &prs->egid,
31 &prs->groups
32 );
33 fclose(fp);
34 return(prs);
35 }
36 else{
37 return(NULL);
38 }
39 }
40
41 /* Make sure /proc is mounted and initialize some global vars */
OS_initialize()42 char* OS_initialize(){
43 char cbuf[1024];
44 char bbuf[32];
45 struct statfs sfs;
46
47 static char* no_proc = "/proc unavailable";
48 if( statfs("/proc", &sfs) == -1 ){
49 return no_proc;
50 }
51
52 return NULL;
53 }
54
OS_get_table()55 void OS_get_table(){
56 DIR *procdir;
57 struct dirent *procdirp;
58 FILE *fp;
59 char pathbuf[PATH_MAX];
60
61 /* for bless_into_proc */
62 struct procstat prs;
63 static char format[F_LASTFIELD + 2];
64 char cmndline[ARG_MAX];
65
66 if( (procdir = opendir("/proc")) == NULL ){
67 return;
68 }
69
70 /* Iterate through all the process entries (numeric) under /proc */
71 while( (procdirp = readdir(procdir)) != NULL ){
72
73 /* Only look at this file if it's a proc id; that is, all numbers */
74 if( strtok(procdirp->d_name, "0123456789") == NULL ){
75 /* zero our format */
76 strcpy(format, Defaultformat);
77
78 sprintf(pathbuf, "%s%s", "/proc/", procdirp->d_name);
79
80 /* get stuff out of /proc/PROC_ID/status */
81 memset(&prs, 0, sizeof(prs));
82 strcat( pathbuf, "/status" );
83 if (get_procstat(pathbuf, &prs) != NULL) {
84 int i;
85 char utime[20];
86 double utime_f;
87 char stime[20];
88 double stime_f;
89 char time[20];
90 double time_f;
91 char start[20];
92 double start_f;
93 char *ttydev;
94 int ttynum;
95
96 utime_f = prs.utime + prs.utime_mic/1000000;
97 stime_f = prs.stime + prs.stime_mic/1000000;
98 time_f = utime_f + stime_f;
99 start_f = prs.start + prs.start_mic/1000000;
100
101 sprintf(utime, "%f", utime_f);
102 sprintf(stime, "%f", stime_f);
103 sprintf(time, "%f", time_f);
104 sprintf(start, "%f", start_f);
105
106 ttynum = makedev(prs.tdev_maj, prs.tdev_min);
107 ttydev = devname(ttynum, S_IFCHR);
108 if (ttydev == NULL) ttydev = "??";
109
110 /* get stuff out of /proc/PROC_ID/cmdline */
111 sprintf(pathbuf, "%s%s%s", "/proc/", procdirp->d_name, "/cmdline");
112 if( (fp = fopen( pathbuf, "r" )) != NULL ){
113 size_t got;
114 if( (got = fread(cmndline, sizeof(char), ARG_MAX, fp)) > 0 ){
115 size_t i;
116 for(i = 0; i < got; i++){
117 if( cmndline[i] == '\0' ) cmndline[i] = ' ';
118 }
119 cmndline[got] = '\0'; /* necessary? */
120
121 format[F_CMNDLINE] = tolower(format[F_CMNDLINE]);
122 }
123 fclose(fp);
124 }
125
126 bless_into_proc( format,
127 Fields,
128 prs.ruid,
129 prs.rgid,
130 prs.pid,
131 prs.ppid,
132 prs.pgid,
133 prs.sid,
134 prs.flags,
135 utime,
136 stime,
137 time,
138 prs.wchan,
139 start,
140 prs.euid,
141 prs.egid,
142 prs.comm,
143 prs.wchan,
144 ttydev,
145 ttynum,
146 cmndline
147 );
148 }
149 }
150 }
151 closedir(procdir);
152 }
153
154