2 Copyright (C) 1992-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by jla; revised by djm; revised again by mstone */
21 name [state] line time [activity] [pid] [comment] [exit]
23 name, line, time: not -q
31 #include <sys/types.h>
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "who"
40 #define AUTHORS N_ ("Joseph Arceneaux, David MacKenzie, and Michael Stone")
42 #ifndef MAXHOSTNAMELEN
43 # define MAXHOSTNAMELEN 64
51 # define USER_PROCESS INT_MAX
55 # define RUN_LVL INT_MAX
59 # define INIT_PROCESS INT_MAX
63 # define LOGIN_PROCESS INT_MAX
67 # define DEAD_PROCESS INT_MAX
80 #if HAVE_STRUCT_XTMP_UT_PID
81 # define UT_PID(U) ((U)->ut_pid)
82 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
83 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
84 sprintf (Var, "%d", (int) (Utmp_ent->ut_pid))
87 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
91 #if HAVE_STRUCT_XTMP_UT_ID
92 # define UT_ID(U) ((U)->ut_id)
94 /* Of course, sizeof "whatever" is the size of a pointer (often 4),
95 but that's ok, since the actual string has a length of only 2. */
96 # define UT_ID(U) "??"
99 #define UT_TYPE_UNDEF 255
101 #if HAVE_STRUCT_XTMP_UT_TYPE
102 # define UT_TYPE(U) ((U)->ut_type)
104 # define UT_TYPE(U) UT_TYPE_UNDEF
107 #define IS_USER_PROCESS(U) \
108 (UT_USER (utmp_buf)[0] \
109 && (UT_TYPE (utmp_buf) == USER_PROCESS \
110 || (UT_TYPE (utmp_buf) == UT_TYPE_UNDEF \
111 && UT_TIME_MEMBER (utmp_buf) != 0)))
117 /* The name this program was run with. */
120 /* If nonzero, attempt to canonicalize hostnames via a DNS lookup. */
121 static int do_lookup;
123 /* If nonzero, display only a list of usernames and count of
125 Ignored for `who am i'. */
126 static int short_list;
128 /* If nonzero, display only name, line, and time fields */
129 static int short_output;
131 /* If nonzero, display the hours:minutes since each user has touched
132 the keyboard, or "." if within the last minute, or "old" if
133 not within the last day. */
134 static int include_idle;
136 /* If nonzero, display a line at the top describing each field. */
137 static int include_heading;
139 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
140 or a `?' if their tty cannot be statted. */
141 static int include_mesg;
143 /* If nonzero, display process termination & exit status */
144 static int include_exit;
146 /* If nonzero, display the last boot time */
147 static int need_boottime;
149 /* If nonzero, display dead processes */
150 static int need_deadprocs;
152 /* If nonzero, display processes waiting for user login */
153 static int need_login;
155 /* If nonzero, display processes started by init */
156 static int need_initspawn;
158 /* If nonzero, display the last clock change */
159 static int need_clockchange;
161 /* If nonzero, display the current runlevel */
162 static int need_runlevel;
164 /* If nonzero, display user processes */
165 static int need_users;
167 /* If nonzero, display info only for the controlling tty */
168 static int my_line_only;
170 /* for long options with no corresponding short option, use enum */
173 LOOKUP_OPTION = CHAR_MAX + 1,
177 static struct option const longopts[] = {
178 {"all", no_argument, NULL, 'a'},
179 {"boot", no_argument, NULL, 'b'},
180 {"count", no_argument, NULL, 'q'},
181 {"dead", no_argument, NULL, 'd'},
182 {"heading", no_argument, NULL, 'H'},
183 {"idle", no_argument, NULL, 'i'},
184 {"login", no_argument, NULL, LOGIN_OPTION},
185 {"lookup", no_argument, NULL, LOOKUP_OPTION},
186 {"message", no_argument, NULL, 'T'},
187 {"mesg", no_argument, NULL, 'T'},
188 {"process", no_argument, NULL, 'p'},
189 {"runlevel", no_argument, NULL, 'r'},
190 {"short", no_argument, NULL, 's'},
191 {"time", no_argument, NULL, 't'},
192 {"users", no_argument, NULL, 'u'},
193 {"writable", no_argument, NULL, 'T'},
194 {GETOPT_HELP_OPTION_DECL},
195 {GETOPT_VERSION_OPTION_DECL},
199 /* Return a string representing the time between WHEN and the time
200 that this function is first run.
203 idle_string (time_t when)
205 static time_t now = 0;
206 static char idle_hhmm[IDLESTR_LEN];
212 seconds_idle = now - when;
213 if (seconds_idle < 60) /* One minute. */
215 if (seconds_idle < (24 * 60 * 60)) /* One day. */
217 sprintf (idle_hhmm, "%02d:%02d",
218 (int) (seconds_idle / (60 * 60)),
219 (int) ((seconds_idle % (60 * 60)) / 60));
220 return (const char *) idle_hhmm;
225 /* Return a standard time string, "mon dd hh:mm"
226 FIXME: handle localization */
228 time_string (const STRUCT_UTMP *utmp_ent)
230 /* Don't take the address of UT_TIME_MEMBER directly.
231 Ulrich Drepper wrote:
232 ``... GNU libc (and perhaps other libcs as well) have extended
233 utmp file formats which do not use a simple time_t ut_time field.
234 In glibc, ut_time is a macro which selects for backward compatibility
235 the tv_sec member of a struct timeval value.'' */
236 time_t tm = UT_TIME_MEMBER (utmp_ent);
238 char *ptr = ctime (&tm) + 4;
243 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
244 will need tweaking if any of the localization stuff is done, or for 64 bit
247 print_line (const char *user, const char state, const char *line,
248 const char *time_str, const char *idle, const char *pid,
249 const char *comment, const char *exitstr)
251 static char mesg[3] = { ' ', 'x', '\0' };
253 char x_idle[1 + IDLESTR_LEN + 1];
254 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
260 if (include_idle && !short_output)
261 sprintf (x_idle, " %-6s", idle);
266 sprintf (x_pid, " %10s", pid);
270 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
272 sprintf (x_exitstr, " %-12s", exitstr);
276 err = asprintf (&buf,
287 include_mesg ? mesg : "",
292 /* FIXME: it's not really clear whether the following
293 field should be in the short_output. A strict reading
294 of SUSv2 would suggest not, but I haven't seen any
295 implementations that actually work that way... */
303 /* Remove any trailing spaces. */
304 char *p = buf + strlen (buf);
315 /* Send properly parsed USER_PROCESS info to print_line */
317 print_user (const STRUCT_UTMP *utmp_ent)
322 char idlestr[IDLESTR_LEN];
323 static char *hoststr;
324 static size_t hostlen;
326 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
327 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
329 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
330 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
332 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
333 already an absolute pathname. Some system may put the full,
334 absolute pathname in ut_line. */
335 if (utmp_ent->ut_line[0] == '/')
337 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
338 line[sizeof (utmp_ent->ut_line)] = '\0';
342 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
343 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
344 sizeof (utmp_ent->ut_line));
345 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
348 if (stat (line, &stats) == 0)
350 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
351 last_change = stats.st_atime;
360 sprintf (idlestr, "%.6s", idle_string (last_change));
362 sprintf (idlestr, " ?");
365 if (utmp_ent->ut_host[0])
367 char ut_host[sizeof (utmp_ent->ut_host) + 1];
368 char *host = 0, *display = 0;
370 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
371 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
372 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
374 /* Look for an X display. */
375 display = strrchr (ut_host, ':');
379 if (*ut_host && do_lookup)
381 /* See if we can canonicalize it. */
382 host = canon_host (ut_host);
390 if (hostlen < strlen (host) + strlen (display) + 4)
392 hostlen = strlen (host) + strlen (display) + 4;
393 hoststr = (char *) realloc (hoststr, hostlen);
395 sprintf (hoststr, "(%s:%s)", host, display);
399 if (hostlen < strlen (host) + 3)
401 hostlen = strlen (host) + 3;
402 hoststr = (char *) realloc (hoststr, hostlen);
404 sprintf (hoststr, "(%s)", host);
412 hoststr = (char *) realloc (hoststr, hostlen);
414 stpcpy (hoststr, "");
418 print_line (UT_USER (utmp_ent), mesg, utmp_ent->ut_line,
419 time_string (utmp_ent), idlestr, pidstr,
420 hoststr ? hoststr : "", "");
424 print_boottime (const STRUCT_UTMP *utmp_ent)
426 print_line ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
430 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
432 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
434 /* Cast field width argument to `int' to avoid warning from gcc. */
435 sprintf (comment, "%s%.*s", _("id="), (int) sizeof UT_ID (utmp_ent),
441 print_deadprocs (const STRUCT_UTMP *utmp_ent)
443 static char *exitstr;
444 char *comment = make_id_equals_comment (utmp_ent);
445 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
448 exitstr = xmalloc (strlen (_("term="))
449 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
450 + strlen (_("exit="))
451 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
453 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
454 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
456 /* FIXME: add idle time? */
458 print_line ("", ' ', utmp_ent->ut_line,
459 time_string (utmp_ent), "", pidstr, comment, exitstr);
464 print_login (const STRUCT_UTMP *utmp_ent)
466 char *comment = make_id_equals_comment (utmp_ent);
467 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
469 /* FIXME: add idle time? */
471 print_line ("LOGIN", ' ', utmp_ent->ut_line,
472 time_string (utmp_ent), "", pidstr, comment, "");
477 print_initspawn (const STRUCT_UTMP *utmp_ent)
479 char *comment = make_id_equals_comment (utmp_ent);
480 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
482 print_line ("", ' ', utmp_ent->ut_line,
483 time_string (utmp_ent), "", pidstr, comment, "");
488 print_clockchange (const STRUCT_UTMP *utmp_ent)
490 /* FIXME: handle NEW_TIME & OLD_TIME both */
491 print_line ("", ' ', _("clock change"),
492 time_string (utmp_ent), "", "", "", "");
496 print_runlevel (const STRUCT_UTMP *utmp_ent)
498 static char *runlevline, *comment;
499 int last = UT_PID (utmp_ent) / 256;
500 int curr = UT_PID (utmp_ent) % 256;
503 runlevline = xmalloc (strlen (_("run-level")) + 3);
504 sprintf (runlevline, "%s %c", _("run-level"), curr);
507 comment = xmalloc (strlen (_("last=")) + 2);
508 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
510 print_line ("", ' ', runlevline, time_string (utmp_ent),
511 "", "", comment, "");
516 /* Print the username of each valid entry and the number of valid entries
517 in UTMP_BUF, which should have N elements. */
519 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
525 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
529 trimmed_name = extract_trimmed_name (utmp_buf);
531 printf ("%s ", trimmed_name);
537 printf (_("\n# users=%u\n"), entries);
543 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
544 _("COMMENT"), _("EXIT"));
547 /* Display UTMP_BUF, which should have N entries. */
549 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
551 char *ttyname_b IF_LINT ( = NULL);
558 ttyname_b = ttyname (0);
561 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
562 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
568 strncmp (ttyname_b, utmp_buf->ut_line,
569 sizeof (utmp_buf->ut_line)) == 0)
571 if (need_users && IS_USER_PROCESS (utmp_buf))
572 print_user (utmp_buf);
573 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
574 print_runlevel (utmp_buf);
575 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
576 print_boottime (utmp_buf);
577 /* I've never seen one of these, so I don't know what it should
579 FIXME: handle OLD_TIME also, perhaps show the delta? */
580 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
581 print_clockchange (utmp_buf);
582 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
583 print_initspawn (utmp_buf);
584 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
585 print_login (utmp_buf);
586 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
587 print_deadprocs (utmp_buf);
594 /* Display a list of who is on the system, according to utmp file filename. */
596 who (const char *filename)
599 STRUCT_UTMP *utmp_buf;
600 int fail = read_utmp (filename, &n_users, &utmp_buf);
603 error (EXIT_FAILURE, errno, "%s", filename);
606 list_entries_who (n_users, utmp_buf);
608 scan_entries (n_users, utmp_buf);
615 fprintf (stderr, _("Try `%s --help' for more information.\n"),
619 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
622 -a, --all same as -b -d --login -p -r -t -T -u\n\
623 -b, --boot time of last system boot\n\
624 -d, --dead print dead processes\n\
625 -H, --heading print line of column headings\n\
628 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
629 (deprecated, use -u)\n\
630 --login print system login processes\n\
631 (equivalent to SUS -l)\n\
634 -l, --lookup attempt to canonicalize hostnames via DNS\n\
635 (-l is deprecated, use --lookup)\n\
636 -m only hostname and user associated with stdin\n\
637 -p, --process print active processes spawned by init\n\
640 -q, --count all login names and number of users logged on\n\
641 -r, --runlevel print current runlevel\n\
642 -s, --short print only name, line, and time (default)\n\
643 -t, --time print last system clock change\n\
646 -T, -w, --mesg add user's message status as +, - or ?\n\
647 -u, --users list users logged in\n\
648 --message same as -T\n\
649 --writable same as -T\n\
651 fputs (HELP_OPTION_DESCRIPTION, stdout);
652 fputs (VERSION_OPTION_DESCRIPTION, stdout);
655 If FILE is not specified, use %s. %s as FILE is common.\n\
656 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
657 "), UTMP_FILE, WTMP_FILE);
658 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
664 main (int argc, char **argv)
669 initialize_main (&argc, &argv);
670 program_name = argv[0];
671 setlocale (LC_ALL, "");
672 bindtextdomain (PACKAGE, LOCALEDIR);
673 textdomain (PACKAGE);
675 atexit (close_stdout);
677 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts,
691 need_clockchange = 1;
715 /* FIXME: This should be -l in a future version */
746 need_clockchange = 1;
757 _("Warning: -i will be removed in a future release; \
768 _("Warning: the meaning of '-l' will change in a future\
769 release to conform to POSIX"));
774 case_GETOPT_HELP_CHAR;
776 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
779 usage (EXIT_FAILURE);
794 switch (argc - optind)
801 case 1: /* who <utmp file> */
805 case 2: /* who <blurf> <glop> */
811 error (0, 0, _("too many arguments"));
812 usage (EXIT_FAILURE);