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);
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 char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
262 tm = localtime(&now);
263 printf(" on %s %02d-%02d-%02d %02d.%02d:%02d", days[tm->tm_wday], tm->tm_mday, tm->tm_mon+1,
264 tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec);
265 printf(" (%u UE)\n", (unsigned int) now);
276 if (f = fopen("/proc/loadavg", "r"))
280 *strchr(line, '\n') = 0;
294 printf("R/T=%s, ", z);
298 printf("LAV=%s", line);
302 if (f = fopen("/proc/meminfo", "r"))
304 int free, buffers, cached, stotal, sfree;
305 free = buffers = stotal = sfree = 0;
306 while (fgets(line, 256, f))
308 if (!strncmp(line, "MemFree:", 8))
309 sscanf(line+8, "%d", &free);
310 else if (!strncmp(line, "Buffers:", 8))
311 sscanf(line+8, "%d", &buffers);
312 else if (!strncmp(line, "Cached:", 7))
313 sscanf(line+7, "%d", &cached);
314 else if (!strncmp(line, "SwapTotal:", 10))
315 sscanf(line+10, "%d", &stotal);
316 else if (!strncmp(line, "SwapFree:", 9))
317 sscanf(line+9, "%d", &sfree);
320 memory(1024*(free+buffers+cached));
325 memory(1024*(stotal - sfree));
329 printf(", %d user%s.\n", usercnt, usercnt == 1 ? "" : "s");
341 if (!ioctl(1, TIOCGWINSZ, &win) && win.ws_col >= LEFTSIZE + maxhost + 10 && win.ws_col < 1024)
342 cmdcols = win.ws_col;
345 cmdcols -= LEFTSIZE + maxhost;
346 if (d = opendir("/proc"))
348 while (e = readdir(d))
351 if (sscanf(e->d_name, "%d", &i)==1)
353 struct procrec *p = xmalloc(sizeof(struct procrec) + cmdcols);
355 char name[256], eman[1024];
357 bzero(p, sizeof(*p));
359 strcpy(name, "/proc/");
360 strcat(name, e->d_name);
361 if (!stat(name, &st))
365 strcat(eman, "/cmdline");
366 if ((fil = open(eman, O_RDONLY)) >= 0)
368 int y, z = read(fil, p->cmd_line, cmdcols);
373 p->cmd_line[y] = ' ';
378 strcat(eman, "/stat");
379 if ((fil = open(eman, O_RDONLY)) >= 0)
381 int z = read(fil, eman, 1023);
383 int trash, utim, stim, cutim, cstim;
387 if ((j = strchr(eman, '(')) && (k = strchr(j+1, ')')))
392 strncpy(p->cmd_line, j+1, cmdcols);
393 p->cmd_line[cmdcols] = 0;
398 sscanf(k+4, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
399 &p->ppid, &p->pgrp, &p->sess, &trash, &trash,
400 &trash, &trash, &trash, &trash, &trash, &utim,
401 &stim, &cutim, &cstim);
402 DBG("PROC: pid=%d ppid=%d pgrp=%d sess=%d uid=%d\n", p->pid, p->ppid, p->pgrp, p->sess, p->uid);
403 p->next = first_procrec;
406 p->tim = utim + stim;
407 p->ctim = cutim + cstim;
417 perror("unable to scan /proc");
426 struct procrec *m, *n;
430 /* Recognize login session process trees and find leaves of these trees */
431 for (u=first_utrec; u; u=u->next)
433 for (m=first_procrec; m; m=m->next)
434 if (m->pid == u->login_pid)
437 u->total_time = m->ctim;
439 DBG("** %d is login process for %s@%s\n", m->pid, u->user_name, u->line);
445 for (m=first_procrec; m; m=m->next)
447 for (n=first_procrec; n; n=n->next)
448 if (n->pid == m->ppid && n->user)
450 u = m->user = n->user;
451 m->distance = n->distance + 1;
452 if (!u->proc || u->proc->distance < m->distance)
455 DBG("** %d at distance %d for %s@%s\n", m->pid, m->distance, u->user_name, u->line);
461 /* Process the remaining processes */
462 for(m=first_procrec; m; m=m->next)
466 for(u=first_utrec; u; u=u->next)
467 if (u->uid == m->uid)
471 if (m->uid < 2 || m->ppid != 1)
473 u = xmalloc(sizeof(struct utrec));
474 bzero(u, sizeof(*u));
475 u->next = first_utrec;
477 pw = getpwuid(u->uid = m->uid);
479 strcpy(u->user_name, pw->pw_name);
481 sprintf(u->user_name, "<%d>", m->uid);
486 u->total_time += m->ctim;
488 DBG("** daemon %s (%s)\n", m->cmd_line, u->user_name);
493 comp(struct utrec *a, struct utrec *b)
498 k = strcmp(a->user_name, b->user_name);
501 if (!a->host_name[0] && b->host_name[0])
503 return strcmp(a->line, b->line);
509 struct utrec *fi = NULL;
510 struct utrec *z, **y, **m, **l;
536 printf("Name Li MD %-*s LogT IdleT RunT Command\n", maxhost, "From");
537 for(u=first_utrec; u; u=u->next)
540 strcpy(u->line, "--");
541 printf("%-8.8s %-3s %c%c %-*.*s ",
545 u->detach ? 'D' : ' ',
551 if (u->login_time > now)
553 puttime(now - u->login_time);
555 if (u->click_time > now)
557 if (now - u->click_time >= 60)
558 puttime(now - u->click_time);
567 puttime((unsigned)u->total_time/(unsigned)HZ);
569 for (c = u->proc->cmd_line; *c; c++)
571 if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
578 printf(" <nothing>");
594 printf("!!! There %s %d zombie%s walking around.\n",
595 zombies == 1 ? "is" : "are",
597 zombies == 1 ? "" : "s" );