]> mj.ucw.cz Git - ywho.git/blob - ywho.c
Bugfix.
[ywho.git] / ywho.c
1 /*
2  *      Extended `who' command, version 1.11.
3  *
4  *      (c) 1996--2003 Martin Mares <mj@ucw.cz>
5  *
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.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <utmp.h>
16 #include <time.h>
17 #include <dirent.h>
18 #include <pwd.h>
19 #include <sys/stat.h>
20 #include <asm/param.h>
21 #include <sys/ioctl.h>
22 #include <ctype.h>
23
24 #undef DEBUG
25 #define MAXHOSTSIZE 24
26
27 #ifdef DEBUG
28 #define DBG(x,y...) printf(x,##y)
29 #else
30 #define DBG(x,y...) do { } while(0)
31 #endif
32
33 struct procrec {
34   struct procrec *next;
35   int pid, ppid, pgrp, sess;
36   int uid, state, distance;
37   time_t ctim, tim;
38   struct utrec *user;
39   char cmd_line[1];
40 };
41
42 static struct procrec *first_procrec;
43
44 struct utrec {
45   struct utrec *next;
46   pid_t login_pid;
47   char line[UT_LINESIZE];
48   time_t login_time, click_time, total_time;
49   char user_name[UT_NAMESIZE+1];
50   int uid;
51   char host_name[MAXHOSTSIZE+1];
52   int mesg, detach;
53   struct procrec *proc;
54 };
55
56 static struct utrec *first_utrec;
57 static int usercnt, zombies, maxhost = 10;
58 static time_t now;
59
60 static void *
61 xmalloc(int i)
62 {
63   void *z;
64   z = malloc(i);
65   if (!z)
66     {
67       fprintf(stderr, "ywho: out of memory!\n");
68       exit(1);
69     }
70   return z;
71 }
72
73 static void
74 strcpy_padded(char *to, char *from, int size)
75 {
76   memcpy(to, from, size);
77   to[size] = 0;
78 }
79
80 static void
81 readutmp(void)
82 {
83   static struct utmp *ut;
84   static struct passwd *pw;
85   char *z;
86   utmpname(UTMP_FILE);
87   while (ut = getutent())
88     if (ut->ut_type == USER_PROCESS && ut->ut_user[0])
89       {
90         struct utrec *u = xmalloc(sizeof(struct utrec));
91         struct stat st;
92         char device[32];
93         size_t hlen;
94         bzero(u, sizeof(*u));
95         u->next = first_utrec;
96         first_utrec = u;
97         u->login_pid = ut->ut_pid;
98         z = ut->ut_line;
99         if (!strncmp(z, "tty", 3))
100           {
101             if (z[3] >= '0' && z[3] <= '9')
102               sprintf(u->line, "c%s", z+3);
103             else
104               strcpy(u->line, z+3);
105           }
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);
111         if (pw)
112           u->uid = pw->pw_uid;
113         else
114           u->uid = -1;
115         strcpy_padded(u->host_name, ut->ut_host,
116           (MAXHOSTSIZE > UT_HOSTSIZE ? UT_HOSTSIZE : MAXHOSTSIZE));
117         if ((hlen = strlen(u->host_name)) > maxhost)
118           maxhost = hlen;
119         strcpy(u->line, ut->ut_line);
120         if (*u->line)
121           {
122             char *z = u->line;
123             if (!strncmp(z, "tty", 3))
124               {
125                 if (isdigit(z[3]))
126                   sprintf(u->line, "c%s", z+3);
127                 else
128                   strcpy(u->line, z+3);
129               }
130             else if (!strncmp(z, "pts/", 4))
131               strcpy(u->line, z+4);
132             else
133               strcpy(u->line, z);
134           }
135         strcpy(device, "/dev/");
136         strcat(device, ut->ut_line);
137         if (stat(device, &st))
138           u->mesg = 2;
139         else
140           {
141             u->mesg = !!(S_IWGRP & st.st_mode);
142             u->click_time = st.st_atime;
143 #if 0
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;
148 #endif
149           }
150         u->detach = 0;
151         usercnt++;
152         DBG("UTMP: %s %s %d %d\n", u->user_name, u->host_name,
153             u->uid, u->login_pid);
154       }
155   endutent();
156 }
157
158 static void
159 puttime(int s)
160 {
161   int d, h, m;
162   if (s < 100)
163     printf("%4ds", s);
164   else
165     {
166       m = s/60;
167       s %= 60;
168       h = m/60;
169       m %= 60;
170       if (h < 100)
171         printf("%02d.%02d", h, m);
172       else
173         {
174           d = h/24;
175           h %= 24;
176           if (d < 100)
177             printf("%2dd%02d", d, h);
178           else
179             printf("%4dd", d);
180         }
181     }
182 }
183
184 static void
185 memory(unsigned int i)
186 {
187   if (i < 10240)
188     printf("%dB", i);
189   else
190     {
191       i = (i+511U)/1024U;
192       if (i < 2048)
193         printf("%dK", i);
194       else
195         {
196           i = (i+511)/1024;
197           if (i < 2048)
198             printf("%dM", i);
199           else
200             printf("%dG", (i+511U)/1024U);
201         }
202     }
203 }
204
205 static void
206 dispsys(void)
207 {
208   FILE *f;
209   char line[256];
210   char *z, *w;
211   w = NULL;
212   if (f = fopen("/proc/version", "r"))
213     {
214       fgets(line, 256, f);
215       z = strchr(line, '(');
216       if (z)
217         {
218           while (z != line && z[-1] == ' ')
219             z--;
220         }
221       else
222         z = line + strlen(line) - 1;
223       *z = 0;
224       z = strstr(line, " version");
225       if (z)
226         memmove(z, z+8, strlen(z+8)+1);
227       printf(line);
228       fclose(f);
229     }
230   if (f = fopen("/proc/cpuinfo", "r"))
231     {
232       char a[32], b[32], c[32];
233       strcpy(a, "???");
234       b[0] = 0;
235       c[0] = 0;
236       for(;;)
237         {
238           fgets(line, 256, f);
239           if (feof(f))
240             break;
241           if (!strncmp(line, "cpu\t\t: ", 7))
242             {
243               z = line+7;
244               w = a;
245             }
246           else if (!strncmp(line, "cpu family\t: ",13))
247             {
248               z = line+13; strcpy(z + 1, "86\n");
249               w = a;
250             }
251           else if (!strncmp(line, "model\t\t: ", 9))
252             {
253               z = line+9;
254               w = b;
255             }
256           else if (!strncmp(line, "model name\t: ", 13))
257             {
258               z = line+13;
259               if (!strncmp(z, "AMD-K6", 6))
260                 {
261                   z[3] = ' ';
262                   if ((w = strstr(z, "tm w/ multimedia")))
263                     strcpy(w, "-1\n");
264                   else if ((w = strstr(z, "(tm) 3D")))
265                     strcpy(w, "-2\n");
266                 }
267               w = b;
268             }
269           else if (!strncmp(line, "mask\t\t: ", 8))
270             {
271               z = line+8;
272               w = c;
273             }
274           else
275             z = NULL;
276           if (z)
277             {
278               line[strlen(line)-1] = 0;
279               strcpy(w, z);
280               if (!strcmp(w, "Unknown"))
281                 w[0] = 0;
282             }
283         }
284       printf(" on %s", *b ? b : a);
285       if (*c)
286         printf("/%s", c);
287       fclose(f);
288     }
289   if (f = fopen("/proc/uptime", "r"))
290     {
291       int p,q,p1,q1;
292       fgets(line, 256, f);
293       sscanf(line, "%d.%d%d.%d", &p, &p1, &q, &q1);
294       printf(" up ");
295       puttime(p);
296       printf(" run ");
297       puttime(p-q);
298       fclose(f);
299     }
300   {
301     struct tm *tm;
302     char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
303     now = time(NULL);
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);
308   }
309   if (f = fopen("/proc/loadavg", "r"))
310     {
311       int i = 3;
312       fgets(line, 256, f);
313       *strchr(line, '\n') = 0;
314       z = line-1;
315       while (i-- && z)
316         {
317           *z = '/';
318           z = strchr(z, ' ');
319         }
320       if (z)
321         {
322           char *k;
323           *z++ = 0;
324           k = strchr(z, ' ');
325           if (k)
326             *k = 0;
327           printf("R/T=%s, ", z);
328         }
329       else
330         z = line;
331       printf("LAV=%s", line);
332       fclose(f);
333     }
334   if (f = fopen("/proc/meminfo", "r"))
335     {
336       int free, buffers, stotal, sfree;
337       free = buffers = stotal = sfree = 0;
338       while (fgets(line, 256, f))
339         {
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);
348         }
349       printf(", free ");
350       memory(1024*(free+buffers));
351       printf(" of RAM");
352       if (stotal != sfree)
353         {
354           printf(", used ");
355           memory(1024*(stotal - sfree));
356           printf(" of swap");
357         }
358     }
359   printf(", %d user%s.\n", usercnt, usercnt == 1 ? "" : "s");
360 }
361
362 static void
363 readproc(void)
364 {
365   DIR *d;
366   struct dirent *e;
367   struct winsize win;
368   unsigned cmdcols;
369 #define LEFTSIZE 35
370   
371   if (!ioctl(1, TIOCGWINSZ, &win) && win.ws_col >= LEFTSIZE + maxhost + 10 && win.ws_col < 1024)
372     cmdcols = win.ws_col;
373   else
374     cmdcols = 80;
375   cmdcols -= LEFTSIZE + maxhost;
376   if (d = opendir("/proc"))
377     {
378       while (e = readdir(d))
379         {
380           int i;
381           if (sscanf(e->d_name, "%d", &i)==1)
382             {
383               struct procrec *p = xmalloc(sizeof(struct procrec) + cmdcols);
384               struct stat st;
385               char name[256], eman[1024];
386               int fil;
387               bzero(p, sizeof(*p));
388               p->pid = i;
389               strcpy(name, "/proc/");
390               strcat(name, e->d_name);
391               if (!stat(name, &st))
392                 {
393                   p->uid = st.st_uid;
394                   strcpy(eman, name);
395                   strcat(eman, "/cmdline");
396                   if ((fil = open(eman, O_RDONLY)) >= 0)
397                     {
398                       int y, z = read(fil, p->cmd_line, cmdcols);
399                       if (z < 0)
400                         z = 0;
401                       for (y=0; y<z; y++)
402                         if (!p->cmd_line[y])
403                           p->cmd_line[y] = ' ';
404                       p->cmd_line[z] = 0;
405                       close(fil);
406                     }
407                   strcpy(eman, name);
408                   strcat(eman, "/stat");
409                   if ((fil = open(eman, O_RDONLY)) >= 0)
410                     {
411                       int z = read(fil, eman, 1023);
412                       char *j, *k;
413                       int trash, utim, stim, cutim, cstim;
414                       if (z < 0)
415                         z = 0;
416                       eman[z] = 0;
417                       if ((j = strchr(eman, '(')) && (k = strchr(j+1, ')')))
418                         {
419                           *k = 0;
420                           if (!p->cmd_line[0])
421                             {
422                               strncpy(p->cmd_line, j+1, cmdcols);
423                               p->cmd_line[cmdcols] = 0;
424                             }
425                           p->state = k[2];
426                           if (p->state == 'Z')
427                             zombies++;
428                           sscanf(k+4, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
429                                  &p->ppid, &p->pgrp, &p->sess, &trash, &trash,
430                                  &trash, &trash, &trash, &trash, &trash, &utim,
431                                  &stim, &cutim, &cstim);
432                           DBG("PROC: pid=%d ppid=%d pgrp=%d sess=%d uid=%d\n", p->pid, p->ppid, p->pgrp, p->sess, p->uid);
433                           p->next = first_procrec;
434                           first_procrec = p;
435                         }
436                       p->tim = utim + stim;
437                       p->ctim = cutim + cstim;
438                       close(fil);
439                     }
440                 }
441             }
442         }
443       closedir(d);
444     }
445   else
446     {
447       perror("unable to scan /proc");
448       exit(1);
449     }
450 }
451
452 static void
453 solve(void)
454 {
455   struct utrec *u;
456   struct procrec *m, *n;
457   struct passwd *pw;
458   int cnt;
459
460   /* Recognize login session process trees and find leaves of these trees */
461   for (u=first_utrec; u; u=u->next)
462     {
463       for (m=first_procrec; m; m=m->next)
464         if (m->pid == u->login_pid)
465           {
466             m->user = u;
467             u->total_time = m->ctim;
468             u->proc = m;
469             DBG("** %d is login process for %s@%s\n", m->pid, u->user_name, u->line);
470           }
471     }
472   do
473     {
474       cnt = 0;
475       for (m=first_procrec; m; m=m->next)
476         if (!m->user)
477           for (n=first_procrec; n; n=n->next)
478             if (n->pid == m->ppid && n->user)
479               {
480                 u = m->user = n->user;
481                 m->distance = n->distance + 1;
482                 if (!u->proc || u->proc->distance < m->distance)
483                   u->proc = m;
484                 cnt++;
485                 DBG("** %d at distance %d for %s@%s\n", m->pid, m->distance, u->user_name, u->line);
486                 break;
487               }
488     }
489   while (cnt);
490
491   /* Process the remaining processes */
492   for(m=first_procrec; m; m=m->next)
493     {
494       if (m->user)
495         continue;
496       for(u=first_utrec; u; u=u->next)
497         if (u->uid == m->uid)
498           break;
499       if (!u)
500         {
501           if (m->uid < 2 || m->ppid != 1)
502             continue;
503           u = xmalloc(sizeof(struct utrec));
504           bzero(u, sizeof(*u));
505           u->next = first_utrec;
506           first_utrec = u;
507           pw = getpwuid(u->uid = m->uid);
508           if (pw)
509             strcpy(u->user_name, pw->pw_name);
510           else
511             sprintf(u->user_name, "<%d>", m->uid);
512           u->mesg = 3;
513         }
514       if (!u->proc)
515         u->proc = m;
516       u->total_time += m->ctim;
517       u->detach++;
518       DBG("** daemon %s (%s)\n", m->cmd_line, u->user_name);
519     }
520 }
521
522 static inline int
523 comp(struct utrec *a, struct utrec *b)
524 {
525   int k;
526   if (b->mesg == 3)
527     return -1;
528   k = strcmp(a->user_name, b->user_name);
529   if (k)
530     return k;
531   if (!a->host_name[0] && b->host_name[0])
532     return -1;
533   return strcmp(a->line, b->line);
534 }
535
536 static void
537 sort(void)
538 {
539   struct utrec *fi = NULL;
540   struct utrec *z, **y, **m, **l;
541   l = &fi;
542   while (first_utrec)
543     {
544       y = &first_utrec;
545       m = y;
546       while (z = *y)
547         {
548           if (comp(z, *m) < 0)
549             m = y;
550           y = &z->next;
551         }
552       z = *m;
553       *m = z->next;
554       *l = z;
555       z->next = NULL;
556       l = &z->next;
557     }
558   first_utrec = fi;
559 }
560
561 static void
562 show(void)
563 {
564   struct utrec *u;
565   unsigned char *c;
566   printf("Name     Li  MD %-*s LogT  IdleT RunT  Command\n", maxhost, "From");
567   for(u=first_utrec; u; u=u->next)
568     {
569       if (!u->line[0])
570         strcpy(u->line, "--");
571       printf("%-8.8s %-3s %c%c %-*.*s ",
572              u->user_name,
573              u->line,
574              u->mesg ? ' ' : '-',
575              u->detach ? 'D' : ' ',
576              maxhost,
577              maxhost,
578              u->host_name);
579       if (u->mesg <= 1)
580         {
581           if (u->login_time > now)
582             u->login_time = now;
583           puttime(now - u->login_time);
584           putchar(' ');
585           if (u->click_time > now)
586             u->click_time = now;
587           if (now - u->click_time >= 60)
588             puttime(now - u->click_time);
589           else
590             printf("     ");
591         }
592       else
593         printf("?????      ");
594       if (u->proc)
595         {
596           putchar(' ');
597           puttime((unsigned)u->total_time/(unsigned)HZ);
598           putchar(' ');
599           for (c = u->proc->cmd_line; *c; c++)
600             {
601               if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
602                 putchar(' ');
603               else
604                 putchar(*c);
605             }
606         }
607       else
608         printf(" <nothing>");
609       putchar('\n');
610     }
611 }
612
613 int main(void)
614 {
615   readutmp();
616   dispsys();
617   readproc();
618   solve();
619   sort();
620   show();
621   if (zombies)
622     {
623       printf("!!! There %s %d zombie%s walking around.\n",
624              zombies == 1 ? "is" : "are",
625              zombies,
626              zombies == 1 ? "" : "s" );
627     }
628   return 0;
629 }