2 * Extended `who' command, version 1.10.
4 * (c) 1996--2002 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, 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, "SwapTotal:", 10))
345 sscanf(line+10, "%d", &stotal);
346 else if (!strncmp(line, "SwapFree:", 9))
347 sscanf(line+9, "%d", &sfree);
350 memory(1024*(free+buffers));
355 memory(1024*(stotal - sfree));
359 printf(", %d user%s.\n", usercnt, usercnt == 1 ? "" : "s");
368 unsigned cmdcols = 0;
371 if (!ioctl(1, TIOCGWINSZ, &win) && win.ws_col >= LEFTSIZE + maxhost + 10 && win.ws_col < 1024)
372 cmdcols = win.ws_col;
374 cmdcols -= LEFTSIZE + maxhost;
375 if (d = opendir("/proc"))
377 while (e = readdir(d))
380 if (sscanf(e->d_name, "%d", &i)==1)
382 struct procrec *p = xmalloc(sizeof(struct procrec) + cmdcols);
384 char name[256], eman[1024];
386 bzero(p, sizeof(*p));
388 strcpy(name, "/proc/");
389 strcat(name, e->d_name);
390 if (!stat(name, &st))
394 strcat(eman, "/cmdline");
395 if ((fil = open(eman, O_RDONLY)) >= 0)
397 int y, z = read(fil, p->cmd_line, cmdcols);
402 p->cmd_line[y] = ' ';
407 strcat(eman, "/stat");
408 if ((fil = open(eman, O_RDONLY)) >= 0)
410 int z = read(fil, eman, 1023);
412 int trash, utim, stim, cutim, cstim;
416 if ((j = strchr(eman, '(')) && (k = strchr(j+1, ')')))
421 strncpy(p->cmd_line, j+1, cmdcols);
422 p->cmd_line[cmdcols] = 0;
427 sscanf(k+4, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
428 &p->ppid, &p->pgrp, &p->sess, &trash, &trash,
429 &trash, &trash, &trash, &trash, &trash, &utim,
430 &stim, &cutim, &cstim);
431 DBG("PROC: pid=%d ppid=%d pgrp=%d sess=%d uid=%d\n", p->pid, p->ppid, p->pgrp, p->sess, p->uid);
432 p->next = first_procrec;
435 p->tim = utim + stim;
436 p->ctim = cutim + cstim;
446 perror("unable to scan /proc");
455 struct procrec *m, *n;
459 /* Recognize login session process trees and find leaves of these trees */
460 for (u=first_utrec; u; u=u->next)
462 for (m=first_procrec; m; m=m->next)
463 if (m->pid == u->login_pid)
466 u->total_time = m->ctim;
468 DBG("** %d is login process for %s@%s\n", m->pid, u->user_name, u->line);
474 for (m=first_procrec; m; m=m->next)
476 for (n=first_procrec; n; n=n->next)
477 if (n->pid == m->ppid && n->user)
479 u = m->user = n->user;
480 m->distance = n->distance + 1;
481 if (!u->proc || u->proc->distance < m->distance)
484 DBG("** %d at distance %d for %s@%s\n", m->pid, m->distance, u->user_name, u->line);
490 /* Process the remaining processes */
491 for(m=first_procrec; m; m=m->next)
495 for(u=first_utrec; u; u=u->next)
496 if (u->uid == m->uid)
500 if (m->uid < 2 || m->ppid != 1)
502 u = xmalloc(sizeof(struct utrec));
503 bzero(u, sizeof(*u));
504 u->next = first_utrec;
506 pw = getpwuid(u->uid = m->uid);
508 strcpy(u->user_name, pw->pw_name);
510 sprintf(u->user_name, "<%d>", m->uid);
515 u->total_time += m->ctim;
517 DBG("** daemon %s (%s)\n", m->cmd_line, u->user_name);
522 comp(struct utrec *a, struct utrec *b)
527 k = strcmp(a->user_name, b->user_name);
530 if (!a->host_name[0] && b->host_name[0])
532 return strcmp(a->line, b->line);
538 struct utrec *fi = NULL;
539 struct utrec *z, **y, **m, **l;
565 printf("Name Li MD %-*s LogT IdleT RunT Command\n", maxhost, "From");
566 for(u=first_utrec; u; u=u->next)
569 strcpy(u->line, "--");
570 printf("%-8.8s %-3s %c%c %-*.*s ",
574 u->detach ? 'D' : ' ',
580 if (u->login_time > now)
582 puttime(now - u->login_time);
584 if (u->click_time > now)
586 if (now - u->click_time >= 60)
587 puttime(now - u->click_time);
596 puttime((unsigned)u->total_time/(unsigned)HZ);
598 for (c = u->proc->cmd_line; *c; c++)
600 if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
607 printf(" <nothing>");
622 printf("!!! There %s %d zombie%s walking around.\n",
623 zombies == 1 ? "is" : "are",
625 zombies == 1 ? "" : "s" );