2 Copyright (C) 1992-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
36 #include "hard-locale.h"
39 #include "vasprintf.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "who"
44 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Michael Stone"
46 #ifndef MAXHOSTNAMELEN
47 # define MAXHOSTNAMELEN 64
55 # define UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
57 # define UT_TYPE_RUN_LVL(U) false
61 # define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
63 # define UT_TYPE_INIT_PROCESS(U) false
67 # define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
69 # define UT_TYPE_LOGIN_PROCESS(U) false
73 # define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
75 # define UT_TYPE_DEAD_PROCESS(U) false
79 # define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
81 # define UT_TYPE_NEW_TIME(U) false
86 #if HAVE_STRUCT_XTMP_UT_PID
87 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
88 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
89 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
91 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
95 #if HAVE_STRUCT_XTMP_UT_ID
96 # define UT_ID(U) ((U)->ut_id)
98 # define UT_ID(U) "??"
104 /* The name this program was run with. */
107 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
108 static bool do_lookup;
110 /* If true, display only a list of usernames and count of
112 Ignored for `who am i'. */
113 static bool short_list;
115 /* If true, display only name, line, and time fields. */
116 static bool short_output;
118 /* If true, display the hours:minutes since each user has touched
119 the keyboard, or "." if within the last minute, or "old" if
120 not within the last day. */
121 static bool include_idle;
123 /* If true, display a line at the top describing each field. */
124 static bool include_heading;
126 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
127 or a `?' if their tty cannot be statted. */
128 static bool include_mesg;
130 /* If true, display process termination & exit status. */
131 static bool include_exit;
133 /* If true, display the last boot time. */
134 static bool need_boottime;
136 /* If true, display dead processes. */
137 static bool need_deadprocs;
139 /* If true, display processes waiting for user login. */
140 static bool need_login;
142 /* If true, display processes started by init. */
143 static bool need_initspawn;
145 /* If true, display the last clock change. */
146 static bool need_clockchange;
148 /* If true, display the current runlevel. */
149 static bool need_runlevel;
151 /* If true, display user processes. */
152 static bool need_users;
154 /* If true, display info only for the controlling tty. */
155 static bool my_line_only;
157 /* The strftime format to use for login times, and its expected
159 static char const *time_format;
160 static int time_format_width;
162 /* for long options with no corresponding short option, use enum */
165 LOOKUP_OPTION = CHAR_MAX + 1
168 static struct option const longopts[] = {
169 {"all", no_argument, NULL, 'a'},
170 {"boot", no_argument, NULL, 'b'},
171 {"count", no_argument, NULL, 'q'},
172 {"dead", no_argument, NULL, 'd'},
173 {"heading", no_argument, NULL, 'H'},
174 {"idle", no_argument, NULL, 'i'},
175 {"login", no_argument, NULL, 'l'},
176 {"lookup", no_argument, NULL, LOOKUP_OPTION},
177 {"message", no_argument, NULL, 'T'},
178 {"mesg", no_argument, NULL, 'T'},
179 {"process", no_argument, NULL, 'p'},
180 {"runlevel", no_argument, NULL, 'r'},
181 {"short", no_argument, NULL, 's'},
182 {"time", no_argument, NULL, 't'},
183 {"users", no_argument, NULL, 'u'},
184 {"writable", no_argument, NULL, 'T'},
185 {GETOPT_HELP_OPTION_DECL},
186 {GETOPT_VERSION_OPTION_DECL},
190 /* Return a string representing the time between WHEN and now.
191 BOOTTIME is the time of last reboot.
194 idle_string (time_t when, time_t boottime)
196 static time_t now = TYPE_MINIMUM (time_t);
198 if (now == TYPE_MINIMUM (time_t))
201 if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
203 int seconds_idle = now - when;
204 if (seconds_idle < 60)
208 static char idle_hhmm[IDLESTR_LEN];
209 sprintf (idle_hhmm, "%02d:%02d",
210 seconds_idle / (60 * 60),
211 (seconds_idle % (60 * 60)) / 60);
219 /* Return a time string. */
221 time_string (const STRUCT_UTMP *utmp_ent)
223 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
225 /* Don't take the address of UT_TIME_MEMBER directly.
226 Ulrich Drepper wrote:
227 ``... GNU libc (and perhaps other libcs as well) have extended
228 utmp file formats which do not use a simple time_t ut_time field.
229 In glibc, ut_time is a macro which selects for backward compatibility
230 the tv_sec member of a struct timeval value.'' */
231 time_t t = UT_TIME_MEMBER (utmp_ent);
232 struct tm *tmp = localtime (&t);
236 strftime (buf, sizeof buf, time_format, tmp);
240 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
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 (int userlen, const char *user, const char state,
248 int linelen, const char *line,
249 const char *time_str, const char *idle, const char *pid,
250 const char *comment, const char *exitstr)
252 static char mesg[3] = { ' ', 'x', '\0' };
254 char x_idle[1 + IDLESTR_LEN + 1];
255 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
261 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
262 sprintf (x_idle, " %-6s", idle);
266 if (!short_output && strlen (pid) < sizeof x_pid - 1)
267 sprintf (x_pid, " %10s", pid);
271 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
273 sprintf (x_exitstr, " %-12s", exitstr);
277 err = asprintf (&buf,
287 userlen, user ? user : " .",
288 include_mesg ? mesg : "",
294 /* FIXME: it's not really clear whether the following
295 field should be in the short_output. A strict reading
296 of SUSv2 would suggest not, but I haven't seen any
297 implementations that actually work that way... */
305 /* Remove any trailing spaces. */
306 char *p = buf + strlen (buf);
317 /* Send properly parsed USER_PROCESS info to print_line. The most
318 recent boot time is BOOTTIME. */
320 print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
325 char idlestr[IDLESTR_LEN + 1];
326 static char *hoststr;
327 static size_t hostlen;
329 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
330 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
332 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
333 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
335 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
336 already an absolute pathname. Some system may put the full,
337 absolute pathname in ut_line. */
338 if (utmp_ent->ut_line[0] == '/')
340 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
341 line[sizeof (utmp_ent->ut_line)] = '\0';
345 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
346 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
347 sizeof (utmp_ent->ut_line));
348 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
351 if (stat (line, &stats) == 0)
353 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
354 last_change = stats.st_atime;
363 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
365 sprintf (idlestr, " ?");
368 if (utmp_ent->ut_host[0])
370 char ut_host[sizeof (utmp_ent->ut_host) + 1];
372 char *display = NULL;
374 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
375 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
376 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
378 /* Look for an X display. */
379 display = strchr (ut_host, ':');
383 if (*ut_host && do_lookup)
385 /* See if we can canonicalize it. */
386 host = canon_host (ut_host);
394 if (hostlen < strlen (host) + strlen (display) + 4)
396 hostlen = strlen (host) + strlen (display) + 4;
397 hoststr = xrealloc (hoststr, hostlen);
399 sprintf (hoststr, "(%s:%s)", host, display);
403 if (hostlen < strlen (host) + 3)
405 hostlen = strlen (host) + 3;
406 hoststr = xrealloc (hoststr, hostlen);
408 sprintf (hoststr, "(%s)", host);
419 hoststr = xrealloc (hoststr, hostlen);
421 stpcpy (hoststr, "");
425 print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg,
426 sizeof utmp_ent->ut_line, utmp_ent->ut_line,
427 time_string (utmp_ent), idlestr, pidstr,
428 hoststr ? hoststr : "", "");
432 print_boottime (const STRUCT_UTMP *utmp_ent)
434 print_line (-1, "", ' ', -1, "system boot",
435 time_string (utmp_ent), "", "", "", "");
439 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
441 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
443 strcpy (comment, _("id="));
444 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
449 print_deadprocs (const STRUCT_UTMP *utmp_ent)
451 static char *exitstr;
452 char *comment = make_id_equals_comment (utmp_ent);
453 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
456 exitstr = xmalloc (strlen (_("term="))
457 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
458 + strlen (_("exit="))
459 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
461 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
462 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
464 /* FIXME: add idle time? */
466 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
467 time_string (utmp_ent), "", pidstr, comment, exitstr);
472 print_login (const STRUCT_UTMP *utmp_ent)
474 char *comment = make_id_equals_comment (utmp_ent);
475 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
477 /* FIXME: add idle time? */
479 print_line (-1, "LOGIN", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
480 time_string (utmp_ent), "", pidstr, comment, "");
485 print_initspawn (const STRUCT_UTMP *utmp_ent)
487 char *comment = make_id_equals_comment (utmp_ent);
488 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
490 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
491 time_string (utmp_ent), "", pidstr, comment, "");
496 print_clockchange (const STRUCT_UTMP *utmp_ent)
498 /* FIXME: handle NEW_TIME & OLD_TIME both */
499 print_line (-1, "", ' ', -1, _("clock change"),
500 time_string (utmp_ent), "", "", "", "");
504 print_runlevel (const STRUCT_UTMP *utmp_ent)
506 static char *runlevline, *comment;
507 unsigned char last = UT_PID (utmp_ent) / 256;
508 unsigned char curr = UT_PID (utmp_ent) % 256;
511 runlevline = xmalloc (strlen (_("run-level")) + 3);
512 sprintf (runlevline, "%s %c", _("run-level"), curr);
515 comment = xmalloc (strlen (_("last=")) + 2);
516 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
518 print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent),
519 "", "", comment, "");
524 /* Print the username of each valid entry and the number of valid entries
525 in UTMP_BUF, which should have N elements. */
527 list_entries_who (size_t n, const STRUCT_UTMP *utmp_buf)
529 unsigned long int entries = 0;
530 char const *separator = "";
534 if (IS_USER_PROCESS (utmp_buf))
538 trimmed_name = extract_trimmed_name (utmp_buf);
540 printf ("%s%s", separator, trimmed_name);
547 printf (_("\n# users=%lu\n"), entries);
553 print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
554 _("PID"), _("COMMENT"), _("EXIT"));
557 /* Display UTMP_BUF, which should have N entries. */
559 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
561 char *ttyname_b IF_LINT ( = NULL);
562 time_t boottime = TYPE_MINIMUM (time_t);
569 ttyname_b = ttyname (STDIN_FILENO);
572 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
573 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
579 strncmp (ttyname_b, utmp_buf->ut_line,
580 sizeof (utmp_buf->ut_line)) == 0)
582 if (need_users && IS_USER_PROCESS (utmp_buf))
583 print_user (utmp_buf, boottime);
584 else if (need_runlevel && UT_TYPE_RUN_LVL (utmp_buf))
585 print_runlevel (utmp_buf);
586 else if (need_boottime && UT_TYPE_BOOT_TIME (utmp_buf))
587 print_boottime (utmp_buf);
588 /* I've never seen one of these, so I don't know what it should
590 FIXME: handle OLD_TIME also, perhaps show the delta? */
591 else if (need_clockchange && UT_TYPE_NEW_TIME (utmp_buf))
592 print_clockchange (utmp_buf);
593 else if (need_initspawn && UT_TYPE_INIT_PROCESS (utmp_buf))
594 print_initspawn (utmp_buf);
595 else if (need_login && UT_TYPE_LOGIN_PROCESS (utmp_buf))
596 print_login (utmp_buf);
597 else if (need_deadprocs && UT_TYPE_DEAD_PROCESS (utmp_buf))
598 print_deadprocs (utmp_buf);
601 if (UT_TYPE_BOOT_TIME (utmp_buf))
602 boottime = UT_TIME_MEMBER (utmp_buf);
608 /* Display a list of who is on the system, according to utmp file FILENAME.
609 Use read_utmp OPTIONS to read the file. */
611 who (const char *filename, int options)
614 STRUCT_UTMP *utmp_buf;
616 if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
617 error (EXIT_FAILURE, errno, "%s", filename);
620 list_entries_who (n_users, utmp_buf);
622 scan_entries (n_users, utmp_buf);
630 if (status != EXIT_SUCCESS)
631 fprintf (stderr, _("Try `%s --help' for more information.\n"),
635 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
638 -a, --all same as -b -d --login -p -r -t -T -u\n\
639 -b, --boot time of last system boot\n\
640 -d, --dead print dead processes\n\
641 -H, --heading print line of column headings\n\
644 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
645 (deprecated, use -u)\n\
646 -l, --login print system login processes\n\
649 --lookup attempt to canonicalize hostnames via DNS\n\
650 -m only hostname and user associated with stdin\n\
651 -p, --process print active processes spawned by init\n\
654 -q, --count all login names and number of users logged on\n\
655 -r, --runlevel print current runlevel\n\
656 -s, --short print only name, line, and time (default)\n\
657 -t, --time print last system clock change\n\
660 -T, -w, --mesg add user's message status as +, - or ?\n\
661 -u, --users list users logged in\n\
662 --message same as -T\n\
663 --writable same as -T\n\
665 fputs (HELP_OPTION_DESCRIPTION, stdout);
666 fputs (VERSION_OPTION_DESCRIPTION, stdout);
669 If FILE is not specified, use %s. %s as FILE is common.\n\
670 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
671 "), UTMP_FILE, WTMP_FILE);
672 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
678 main (int argc, char **argv)
681 bool assumptions = true;
683 initialize_main (&argc, &argv);
684 program_name = argv[0];
685 setlocale (LC_ALL, "");
686 bindtextdomain (PACKAGE, LOCALEDIR);
687 textdomain (PACKAGE);
689 atexit (close_stdout);
691 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts, NULL))
697 need_boottime = true;
698 need_deadprocs = true;
700 need_initspawn = true;
701 need_runlevel = true;
702 need_clockchange = true;
711 need_boottime = true;
716 need_deadprocs = true;
723 include_heading = true;
737 need_initspawn = true;
746 need_runlevel = true;
756 need_clockchange = true;
767 _("Warning: -i will be removed in a future release; \
780 case_GETOPT_HELP_CHAR;
782 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
785 usage (EXIT_FAILURE);
797 short_output = false;
800 if (hard_locale (LC_TIME))
802 time_format = "%Y-%m-%d %H:%M";
803 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
807 time_format = "%b %e %H:%M";
808 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
811 switch (argc - optind)
813 case 2: /* who <blurf> <glop> */
818 who (UTMP_FILE, READ_UTMP_CHECK_PIDS);
821 case 1: /* who <utmp file> */
822 who (argv[optind], 0);
826 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
827 usage (EXIT_FAILURE);