proc_linux.c (5928B)
1 /* see LICENSE file for copyright and license details */ 2 #include <sys/stat.h> 3 4 #include <errno.h> 5 #include <fcntl.h> 6 #include <limits.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <unistd.h> 11 12 #include "../paths.h" 13 #include "../proc.h" 14 #include "../util.h" 15 16 #include <dirent.h> 17 #include <time.h> 18 19 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 20 21 int 22 parsecmdline(pid_t pid, char *buf, size_t siz) 23 { 24 int fd; 25 char path[PATH_MAX]; 26 ssize_t n, i; 27 28 snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%ld/cmdline", (long)pid); 29 fd = open(path, O_RDONLY); 30 if (fd < 0) 31 return -1; 32 n = read(fd, buf, siz > 0 ? siz - 1 : 0); 33 close(fd); 34 if (n <= 0) 35 return -1; 36 buf[n] = '\0'; 37 for (i = 0; i < n; i++) 38 if (buf[i] == '\0') 39 buf[i] = ' '; 40 return 0; 41 } 42 43 int 44 parsestat(pid_t pid, struct procstat *ps) 45 { 46 char path[PATH_MAX], line[BUFSIZ], *lp, *rp; 47 FILE *fp; 48 size_t len; 49 int n; 50 51 snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%d/stat", pid); 52 if (!(fp = fopen(path, "r"))) 53 return -1; 54 n = fgets(line, sizeof(line), fp) != NULL; 55 fclose(fp); 56 /* comm is whatever the process named itself, spaces and parens included, 57 * so it runs from the first ( to the last ) */ 58 if (!n || !(lp = strchr(line, '(')) || !(rp = strrchr(line, ')')) || rp < lp) { 59 errno = EINVAL; 60 return -1; 61 } 62 len = MIN((size_t)(rp - lp - 1), sizeof(ps->comm) - 1); 63 memcpy(ps->comm, lp + 1, len); 64 ps->comm[len] = '\0'; 65 ps->pid = pid; 66 n = sscanf( 67 rp + 1, 68 " %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu", 69 &ps->state, 70 &ps->ppid, 71 &ps->pgrp, 72 &ps->sid, 73 &ps->tty_nr, 74 &ps->tpgid, 75 &ps->flags, 76 &ps->minflt, 77 &ps->cminflt, 78 &ps->majflt, 79 &ps->cmajflt, 80 &ps->utime, 81 &ps->stime, 82 &ps->cutime, 83 &ps->cstime, 84 &ps->priority, 85 &ps->nice, 86 &ps->num_threads, 87 &ps->itrealvalue, 88 &ps->starttime, 89 &ps->vsize, 90 &ps->rss, 91 &ps->rsslim 92 ); 93 if (n != 23) { 94 errno = EINVAL; 95 return -1; 96 } 97 return 0; 98 } 99 100 int 101 parsestatus(pid_t pid, struct procstatus *pstatus) 102 { 103 char path[PATH_MAX]; 104 char buf[BUFSIZ], *off; 105 int fd; 106 ssize_t n; 107 108 snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%d/status", pid); 109 fd = open(path, O_RDONLY); 110 if (fd < 0) 111 return -1; 112 n = read(fd, buf, sizeof(buf) - 1); 113 close(fd); 114 if (n <= 0) 115 return -1; 116 buf[n] = '\0'; 117 off = strstr(buf, "Uid:"); 118 if (!off) 119 return -1; 120 sscanf(off, "Uid: %u %u", &pstatus->uid, &pstatus->euid); 121 off = strstr(buf, "Gid:"); 122 if (!off) 123 return -1; 124 sscanf(off, "Gid: %u %u", &pstatus->gid, &pstatus->egid); 125 return 0; 126 } 127 128 int 129 pidfile(const char *file) 130 { 131 char *end; 132 133 errno = 0; 134 strtol(file, &end, 10); 135 if (*end != '\0') 136 return 0; 137 if (errno != 0) 138 return 0; 139 return 1; 140 } 141 142 /* the terminal of a process as stat gives it: a device number in the 143 * kernel's own encoding */ 144 static void 145 tty_split(int nr, unsigned *major, unsigned *minor) 146 { 147 unsigned dev = (unsigned)nr; 148 149 *major = (dev & 0xfff00) >> 8; 150 *minor = (dev & 0xff) | ((dev >> 12) & 0xfff00); 151 } 152 153 /* when the system started, seconds since the epoch, from stat's btime */ 154 static time_t 155 boot_time(void) 156 { 157 static time_t boot; 158 char line[256]; 159 FILE *fp; 160 161 if (boot) 162 return boot; 163 if ((fp = fopen(ARUU_LINUX_PATH_PROC "/stat", "r"))) { 164 while (fgets(line, sizeof line, fp)) { 165 long long t; 166 167 if (sscanf(line, "btime %lld", &t) == 1) { 168 boot = (time_t)t; 169 break; 170 } 171 } 172 fclose(fp); 173 } 174 return boot; 175 } 176 177 int 178 proc_info(pid_t pid, struct procinfo *p) 179 { 180 struct procstat ps; 181 struct procstatus st; 182 long hz = sysconf(_SC_CLK_TCK); 183 long page = sysconf(_SC_PAGESIZE); 184 185 if (parsestat(pid, &ps) < 0) 186 return -1; 187 memset(&st, 0, sizeof st); 188 parsestatus(pid, &st); 189 if (hz <= 0) 190 hz = 100; 191 memset(p, 0, sizeof *p); 192 p->pid = pid; 193 p->ppid = ps.ppid; 194 p->pgid = ps.pgrp; 195 p->sid = ps.sid; 196 p->ruid = st.uid; 197 p->euid = st.euid; 198 p->rgid = st.gid; 199 p->egid = st.egid; 200 p->state = (char)ps.state; 201 p->nice = ps.nice; 202 p->priority = ps.priority; 203 p->nthreads = ps.num_threads; 204 p->start = boot_time() + (time_t)(ps.starttime / (unsigned long long)hz); 205 p->cpu = (ps.utime + ps.stime) / (unsigned long)hz; 206 p->vsize_kib = ps.vsize / 1024; 207 p->rss_kib = ps.rss * (page > 0 ? page / 1024 : 4); 208 if (ps.tty_nr) 209 tty_split(ps.tty_nr, &p->tty_major, &p->tty_minor); 210 strlcpy(p->comm, ps.comm, sizeof p->comm); 211 return 0; 212 } 213 214 int 215 proc_each(proc_fn *fn, void *arg) 216 { 217 struct dirent *e; 218 struct procinfo p; 219 DIR *d = opendir(ARUU_LINUX_PATH_PROC); 220 221 if (!d) 222 return -1; 223 while ((e = readdir(d))) { 224 if (!pidfile(e->d_name) || proc_info((pid_t)strtol(e->d_name, NULL, 10), &p) < 0) 225 continue; 226 if (fn(&p, arg)) 227 break; 228 } 229 closedir(d); 230 return 0; 231 } 232 233 int 234 proc_args(pid_t pid, char *buf, size_t n) 235 { 236 return parsecmdline(pid, buf, n); 237 } 238 239 int 240 proc_cwd(pid_t pid, char *buf, size_t n) 241 { 242 char path[PATH_MAX]; 243 ssize_t k; 244 245 snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%d/cwd", (int)pid); 246 if (n == 0 || (k = readlink(path, buf, n - 1)) < 0) 247 return -1; 248 buf[k] = '\0'; 249 return 0; 250 } 251 252 void 253 proc_ttyname(unsigned major, unsigned minor, char *buf, size_t n) 254 { 255 if (major == 136) 256 snprintf(buf, n, "pts/%u", minor); 257 else if (major == 4 && minor < 64) 258 snprintf(buf, n, "tty%u", minor); 259 else if (major == 4) 260 snprintf(buf, n, "ttyS%u", minor - 64); 261 else if (major == 5 && minor == 0) 262 snprintf(buf, n, "tty"); 263 else 264 snprintf(buf, n, "?"); 265 } 266 267 ssize_t 268 proc_selfexe(char *buf, size_t n) 269 { 270 return readlink(ARUU_LINUX_PATH_PROC "/self/exe", buf, n); 271 } 272 273 #endif