2 * Extended `who' command, version 1.12.
4 * (c) 1996--2010 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License. See file COPYING in any of the GNU packages.
20 #include <asm/param.h>
21 #include <sys/ioctl.h>
25 #define MAXHOSTSIZE 24
28 #define DBG(x,y...) printf(x,##y)
30 #define DBG(x,y...) do { } while(0)
35 int pid, ppid, pgrp, sess;
36 int uid, state, distance;
42 static struct procrec *first_procrec;
47 char line[UT_LINESIZE];
48 time_t login_time, click_time, total_time;
49 char user_name[UT_NAMESIZE+1];
51 char host_name[MAXHOSTSIZE+1];
56 static struct utrec *first_utrec;
57 static int usercnt, zombies, maxhost = 10;
67 fprintf(stderr, "ywho: out of memory!\n");
74 strcpy_padded(char *to, char *from, int size)
76 memcpy(to, from, size);
83 static struct utmp *ut;
84 static struct passwd *pw;
87 while (ut = getutent())
88 if (ut->ut_type == USER_PROCESS && ut->ut_user[0])
90 struct utrec *u = xmalloc(sizeof(struct utrec));
95 u->next = first_utrec;
97 u->login_pid = ut->ut_pid;
99 if (!strncmp(z, "tty", 3))
101 if (z[3] >= '0' && z[3] <= '9')
102 sprintf(u->line, "c%s", z+3);
104 strcpy(u->line, z+3);
106 else if (!strncmp(z, "pts/", 4))
107 strcpy(u->line, z+4);
108 u->login_time = ut->ut_time;
109 strcpy_padded(u->user_name, ut->ut_user, UT_NAMESIZE);
110 pw = getpwnam(u->user_name);
115 strcpy_padded(u->host_name, ut->ut_host,
116 (MAXHOSTSIZE > UT_HOSTSIZE ? UT_HOSTSIZE : MAXHOSTSIZE));
117 if ((hlen = strlen(u->host_name)) > maxhost)
119 strcpy(u->line, ut->ut_line);
123 if (!strncmp(z, "tty", 3))
126 sprintf(u->line, "c%s", z+3);
128 strcpy(u->line, z+3);
130 else if (!strncmp(z, "pts/", 4))
131 strcpy(u->line, z+4);
135 strcpy(device, "/dev/");
136 strcat(device, ut->ut_line);
137 if (stat(device, &st))
141 u->mesg = !!(S_IWGRP & st.st_mode);
142 u->click_time = st.st_atime;
144 if (u->click_time < st.st_mtime)
145 u->click_time = st.st_mtime;
146 if (u->click_time < st.st_ctime)
147 u->click_time = st.st_ctime;
152 DBG("UTMP: %s %s %d %d\n", u->user_name, u->host_name,
153 u->uid, u->login_pid);
171 printf("%02d.%02d", h, m);
177 printf("%2dd%02d", d, h);
185 memory(unsigned int i)
200 printf("%dG", (i+511U)/1024U);
213 if (f = fopen("/proc/version", "r"))
216 z = strchr(line, '(');
219 while (z != line && z[-1] == ' ')
223 z = line + strlen(line) - 1;
225 z = strstr(line, " version");
227 memmove(z, z+8, strlen(z+8)+1);
232 if (gethostname(line, sizeof(line)) >= 0)
233 printf(" (%s)", line);
235 if (f = fopen("/proc/cpuinfo", "r"))
237 while (fgets(line, 256, f))
239 if (!strncmp(line, "processor\t", 9))
242 printf(" [%d CPU%s]", num_cpus, (num_cpus != 1 ? "s" : ""));
246 if (f = fopen("/proc/uptime", "r"))
250 sscanf(line, "%d.%d%d.%d", &p, &p1, &q, &q1);
254 puttime(num_cpus*p - q);
260 struct tm *tm = localtime(&now);
261 strftime(line, sizeof(line), "%a %Y-%m-%d %H.%M:%S", tm);
262 printf(" on %s\n", line);
273 if (f = fopen("/proc/loadavg", "r"))
277 *strchr(line, '\n') = 0;
291 printf("R/T=%s, ", z);
295 printf("LAV=%s", line);
299 if (f = fopen("/proc/meminfo", "r"))
301 int free, buffers, cached, stotal, sfree;
302 free = buffers = stotal = sfree = 0;
303 while (fgets(line, 256, f))
305 if (!strncmp(line, "MemFree:", 8))
306 sscanf(line+8, "%d", &free);
307 else if (!strncmp(line, "Buffers:", 8))
308 sscanf(line+8, "%d", &buffers);
309 else if (!strncmp(line, "Cached:", 7))
310 sscanf(line+7, "%d", &cached);
311 else if (!strncmp(line, "SwapTotal:", 10))
312 sscanf(line+10, "%d", &stotal);
313 else if (!strncmp(line, "SwapFree:", 9))
314 sscanf(line+9, "%d", &sfree);
317 memory(1024*(free+buffers+cached));
322 memory(1024*(stotal - sfree));
326 printf(", %d user%s\n", usercnt, usercnt == 1 ? "" : "s");
338 if (!ioctl(1, TIOCGWINSZ, &win) && win.ws_col >= LEFTSIZE + maxhost + 10 && win.ws_col < 1024)
339 cmdcols = win.ws_col;
342 cmdcols -= LEFTSIZE + maxhost;
343 if (d = opendir("/proc"))
345 while (e = readdir(d))
348 if (sscanf(e->d_name, "%d", &i)==1)
350 struct procrec *p = xmalloc(sizeof(struct procrec) + cmdcols);
352 char name[256], eman[1024];
354 bzero(p, sizeof(*p));
356 strcpy(name, "/proc/");
357 strcat(name, e->d_name);
358 if (!stat(name, &st))
362 strcat(eman, "/cmdline");
363 if ((fil = open(eman, O_RDONLY)) >= 0)
365 int y, z = read(fil, p->cmd_line, cmdcols);
370 p->cmd_line[y] = ' ';
375 strcat(eman, "/stat");
376 if ((fil = open(eman, O_RDONLY)) >= 0)
378 int z = read(fil, eman, 1023);
380 int trash, utim, stim, cutim, cstim;
384 if ((j = strchr(eman, '(')) && (k = strchr(j+1, ')')))
389 strncpy(p->cmd_line, j+1, cmdcols);
390 p->cmd_line[cmdcols] = 0;
395 sscanf(k+4, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
396 &p->ppid, &p->pgrp, &p->sess, &trash, &trash,
397 &trash, &trash, &trash, &trash, &trash, &utim,
398 &stim, &cutim, &cstim);
399 DBG("PROC: pid=%d ppid=%d pgrp=%d sess=%d uid=%d\n", p->pid, p->ppid, p->pgrp, p->sess, p->uid);
400 p->next = first_procrec;
403 p->tim = utim + stim;
404 p->ctim = cutim + cstim;
414 perror("unable to scan /proc");
423 struct procrec *m, *n;
427 /* Recognize login session process trees and find leaves of these trees */
428 for (u=first_utrec; u; u=u->next)
430 for (m=first_procrec; m; m=m->next)
431 if (m->pid == u->login_pid)
434 u->total_time = m->ctim;
436 DBG("** %d is login process for %s@%s\n", m->pid, u->user_name, u->line);
442 for (m=first_procrec; m; m=m->next)
444 for (n=first_procrec; n; n=n->next)
445 if (n->pid == m->ppid && n->user)
447 u = m->user = n->user;
448 m->distance = n->distance + 1;
449 if (!u->proc || u->proc->distance < m->distance)
452 DBG("** %d at distance %d for %s@%s\n", m->pid, m->distance, u->user_name, u->line);
458 /* Process the remaining processes */
459 for(m=first_procrec; m; m=m->next)
463 for(u=first_utrec; u; u=u->next)
464 if (u->uid == m->uid)
468 if (m->uid < 2 || m->ppid != 1)
470 u = xmalloc(sizeof(struct utrec));
471 bzero(u, sizeof(*u));
472 u->next = first_utrec;
474 pw = getpwuid(u->uid = m->uid);
476 strcpy(u->user_name, pw->pw_name);
478 sprintf(u->user_name, "<%d>", m->uid);
483 u->total_time += m->ctim;
485 DBG("** daemon %s (%s)\n", m->cmd_line, u->user_name);
490 comp(struct utrec *a, struct utrec *b)
495 k = strcmp(a->user_name, b->user_name);
498 if (!a->host_name[0] && b->host_name[0])
500 return strcmp(a->line, b->line);
506 struct utrec *fi = NULL;
507 struct utrec *z, **y, **m, **l;
533 printf("Name Li MD %-*s LogT IdleT RunT Command\n", maxhost, "From");
534 for(u=first_utrec; u; u=u->next)
537 strcpy(u->line, "--");
538 printf("%-8.8s %-3s %c%c %-*.*s ",
542 u->detach ? 'D' : ' ',
548 if (u->login_time > now)
550 puttime(now - u->login_time);
552 if (u->click_time > now)
554 if (now - u->click_time >= 60)
555 puttime(now - u->click_time);
564 puttime((unsigned)u->total_time/(unsigned)HZ);
566 for (c = u->proc->cmd_line; *c; c++)
568 if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
575 printf(" <nothing>");
591 printf("!!! There %s %d zombie%s walking around.\n",
592 zombies == 1 ? "is" : "are",
594 zombies == 1 ? "" : "s" );