2 * Extended `who' command, version 1.11.
4 * (c) 1996--2003 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);
212 if (f = fopen("/proc/version", "r"))
215 z = strchr(line, '(');
218 while (z != line && z[-1] == ' ')
222 z = line + strlen(line) - 1;
224 z = strstr(line, " version");
226 memmove(z, z+8, strlen(z+8)+1);
230 if (f = fopen("/proc/cpuinfo", "r"))
232 char a[32], b[32], c[32];
241 if (!strncmp(line, "cpu\t\t: ", 7))
246 else if (!strncmp(line, "cpu family\t: ",13))
248 z = line+13; strcpy(z + 1, "86\n");
251 else if (!strncmp(line, "model\t\t: ", 9))
256 else if (!strncmp(line, "model name\t: ", 13))
259 if (!strncmp(z, "AMD-K6", 6))
262 if ((w = strstr(z, "tm w/ multimedia")))
264 else if ((w = strstr(z, "(tm) 3D")))
269 else if (!strncmp(line, "mask\t\t: ", 8))
278 line[strlen(line)-1] = 0;
280 if (!strcmp(w, "Unknown"))
284 printf(" on %s", *b ? b : a);
289 if (f = fopen("/proc/uptime", "r"))
293 sscanf(line, "%d.%d%d.%d", &p, &p1, &q, &q1);
302 char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
304 tm = localtime(&now);
305 printf(" on %s %02d-%02d-%02d %02d.%02d:%02d", days[tm->tm_wday], tm->tm_mday, tm->tm_mon+1,
306 tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec);
307 printf(" (%u UE)\n", (unsigned int) now);
309 if (f = fopen("/proc/loadavg", "r"))
313 *strchr(line, '\n') = 0;
327 printf("R/T=%s, ", z);
331 printf("LAV=%s", line);
334 if (f = fopen("/proc/meminfo", "r"))
336 int free, buffers, cached, stotal, sfree;
337 free = buffers = stotal = sfree = 0;
338 while (fgets(line, 256, f))
340 if (!strncmp(line, "MemFree:", 8))
341 sscanf(line+8, "%d", &free);
342 else if (!strncmp(line, "Buffers:", 8))
343 sscanf(line+8, "%d", &buffers);
344 else if (!strncmp(line, "Cached:", 7))
345 sscanf(line+7, "%d", &cached);
346 else if (!strncmp(line, "SwapTotal:", 10))
347 sscanf(line+10, "%d", &stotal);
348 else if (!strncmp(line, "SwapFree:", 9))
349 sscanf(line+9, "%d", &sfree);
352 memory(1024*(free+buffers+cached));
357 memory(1024*(stotal - sfree));
361 printf(", %d user%s.\n", usercnt, usercnt == 1 ? "" : "s");
373 if (!ioctl(1, TIOCGWINSZ, &win) && win.ws_col >= LEFTSIZE + maxhost + 10 && win.ws_col < 1024)
374 cmdcols = win.ws_col;
377 cmdcols -= LEFTSIZE + maxhost;
378 if (d = opendir("/proc"))
380 while (e = readdir(d))
383 if (sscanf(e->d_name, "%d", &i)==1)
385 struct procrec *p = xmalloc(sizeof(struct procrec) + cmdcols);
387 char name[256], eman[1024];
389 bzero(p, sizeof(*p));
391 strcpy(name, "/proc/");
392 strcat(name, e->d_name);
393 if (!stat(name, &st))
397 strcat(eman, "/cmdline");
398 if ((fil = open(eman, O_RDONLY)) >= 0)
400 int y, z = read(fil, p->cmd_line, cmdcols);
405 p->cmd_line[y] = ' ';
410 strcat(eman, "/stat");
411 if ((fil = open(eman, O_RDONLY)) >= 0)
413 int z = read(fil, eman, 1023);
415 int trash, utim, stim, cutim, cstim;
419 if ((j = strchr(eman, '(')) && (k = strchr(j+1, ')')))
424 strncpy(p->cmd_line, j+1, cmdcols);
425 p->cmd_line[cmdcols] = 0;
430 sscanf(k+4, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
431 &p->ppid, &p->pgrp, &p->sess, &trash, &trash,
432 &trash, &trash, &trash, &trash, &trash, &utim,
433 &stim, &cutim, &cstim);
434 DBG("PROC: pid=%d ppid=%d pgrp=%d sess=%d uid=%d\n", p->pid, p->ppid, p->pgrp, p->sess, p->uid);
435 p->next = first_procrec;
438 p->tim = utim + stim;
439 p->ctim = cutim + cstim;
449 perror("unable to scan /proc");
458 struct procrec *m, *n;
462 /* Recognize login session process trees and find leaves of these trees */
463 for (u=first_utrec; u; u=u->next)
465 for (m=first_procrec; m; m=m->next)
466 if (m->pid == u->login_pid)
469 u->total_time = m->ctim;
471 DBG("** %d is login process for %s@%s\n", m->pid, u->user_name, u->line);
477 for (m=first_procrec; m; m=m->next)
479 for (n=first_procrec; n; n=n->next)
480 if (n->pid == m->ppid && n->user)
482 u = m->user = n->user;
483 m->distance = n->distance + 1;
484 if (!u->proc || u->proc->distance < m->distance)
487 DBG("** %d at distance %d for %s@%s\n", m->pid, m->distance, u->user_name, u->line);
493 /* Process the remaining processes */
494 for(m=first_procrec; m; m=m->next)
498 for(u=first_utrec; u; u=u->next)
499 if (u->uid == m->uid)
503 if (m->uid < 2 || m->ppid != 1)
505 u = xmalloc(sizeof(struct utrec));
506 bzero(u, sizeof(*u));
507 u->next = first_utrec;
509 pw = getpwuid(u->uid = m->uid);
511 strcpy(u->user_name, pw->pw_name);
513 sprintf(u->user_name, "<%d>", m->uid);
518 u->total_time += m->ctim;
520 DBG("** daemon %s (%s)\n", m->cmd_line, u->user_name);
525 comp(struct utrec *a, struct utrec *b)
530 k = strcmp(a->user_name, b->user_name);
533 if (!a->host_name[0] && b->host_name[0])
535 return strcmp(a->line, b->line);
541 struct utrec *fi = NULL;
542 struct utrec *z, **y, **m, **l;
568 printf("Name Li MD %-*s LogT IdleT RunT Command\n", maxhost, "From");
569 for(u=first_utrec; u; u=u->next)
572 strcpy(u->line, "--");
573 printf("%-8.8s %-3s %c%c %-*.*s ",
577 u->detach ? 'D' : ' ',
583 if (u->login_time > now)
585 puttime(now - u->login_time);
587 if (u->click_time > now)
589 if (now - u->click_time >= 60)
590 puttime(now - u->click_time);
599 puttime((unsigned)u->total_time/(unsigned)HZ);
601 for (c = u->proc->cmd_line; *c; c++)
603 if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
610 printf(" <nothing>");
625 printf("!!! There %s %d zombie%s walking around.\n",
626 zombies == 1 ? "is" : "are",
628 zombies == 1 ? "" : "s" );