2 Copyright (C) 1992-2004 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 #include "vasprintf.h"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "who"
42 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Michael Stone"
44 #ifndef MAXHOSTNAMELEN
45 # define MAXHOSTNAMELEN 64
53 # define USER_PROCESS INT_MAX
57 # define RUN_LVL INT_MAX
61 # define INIT_PROCESS INT_MAX
65 # define LOGIN_PROCESS INT_MAX
69 # define DEAD_PROCESS INT_MAX
82 #if HAVE_STRUCT_XTMP_UT_PID
83 # define UT_PID(U) ((U)->ut_pid)
84 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
85 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
86 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
89 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
93 #if HAVE_STRUCT_XTMP_UT_ID
94 # define UT_ID(U) ((U)->ut_id)
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
176 static struct option const longopts[] = {
177 {"all", no_argument, NULL, 'a'},
178 {"boot", no_argument, NULL, 'b'},
179 {"count", no_argument, NULL, 'q'},
180 {"dead", no_argument, NULL, 'd'},
181 {"heading", no_argument, NULL, 'H'},
182 {"idle", no_argument, NULL, 'i'},
183 {"login", no_argument, NULL, 'l'},
184 {"lookup", no_argument, NULL, LOOKUP_OPTION},
185 {"message", no_argument, NULL, 'T'},
186 {"mesg", no_argument, NULL, 'T'},
187 {"process", no_argument, NULL, 'p'},
188 {"runlevel", no_argument, NULL, 'r'},
189 {"short", no_argument, NULL, 's'},
190 {"time", no_argument, NULL, 't'},
191 {"users", no_argument, NULL, 'u'},
192 {"writable", no_argument, NULL, 'T'},
193 {GETOPT_HELP_OPTION_DECL},
194 {GETOPT_VERSION_OPTION_DECL},
198 /* Return a string representing the time between WHEN and the time
199 that this function is first run.
202 idle_string (time_t when)
204 static time_t now = 0;
205 static char idle_hhmm[IDLESTR_LEN];
211 seconds_idle = now - when;
212 if (seconds_idle < 60) /* One minute. */
214 if (seconds_idle < (24 * 60 * 60)) /* One day. */
216 sprintf (idle_hhmm, "%02d:%02d",
217 (int) (seconds_idle / (60 * 60)),
218 (int) ((seconds_idle % (60 * 60)) / 60));
219 return (const char *) idle_hhmm;
224 /* Return a standard time string, "mon dd hh:mm"
225 FIXME: handle localization */
227 time_string (const STRUCT_UTMP *utmp_ent)
229 /* Don't take the address of UT_TIME_MEMBER directly.
230 Ulrich Drepper wrote:
231 ``... GNU libc (and perhaps other libcs as well) have extended
232 utmp file formats which do not use a simple time_t ut_time field.
233 In glibc, ut_time is a macro which selects for backward compatibility
234 the tv_sec member of a struct timeval value.'' */
235 time_t tm = UT_TIME_MEMBER (utmp_ent);
237 char *ptr = ctime (&tm);
245 static char buf[INT_BUFSIZE_BOUND (intmax_t)];
246 ptr = (TYPE_SIGNED (time_t) ? imaxtostr (tm, buf) : umaxtostr (tm, buf));
251 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
252 will need tweaking if any of the localization stuff is done, or for 64 bit
255 print_line (const char *user, const char state, const char *line,
256 const char *time_str, const char *idle, const char *pid,
257 const char *comment, const char *exitstr)
259 static char mesg[3] = { ' ', 'x', '\0' };
261 char x_idle[1 + IDLESTR_LEN + 1];
262 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
268 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
269 sprintf (x_idle, " %-6s", idle);
273 if (!short_output && strlen (pid) < sizeof x_pid - 1)
274 sprintf (x_pid, " %10s", pid);
278 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
280 sprintf (x_exitstr, " %-12s", exitstr);
284 err = asprintf (&buf,
295 include_mesg ? mesg : "",
300 /* FIXME: it's not really clear whether the following
301 field should be in the short_output. A strict reading
302 of SUSv2 would suggest not, but I haven't seen any
303 implementations that actually work that way... */
311 /* Remove any trailing spaces. */
312 char *p = buf + strlen (buf);
323 /* Send properly parsed USER_PROCESS info to print_line */
325 print_user (const STRUCT_UTMP *utmp_ent)
330 char idlestr[IDLESTR_LEN + 1];
331 static char *hoststr;
332 static size_t hostlen;
334 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
335 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
337 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
338 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
340 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
341 already an absolute pathname. Some system may put the full,
342 absolute pathname in ut_line. */
343 if (utmp_ent->ut_line[0] == '/')
345 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
346 line[sizeof (utmp_ent->ut_line)] = '\0';
350 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
351 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
352 sizeof (utmp_ent->ut_line));
353 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
356 if (stat (line, &stats) == 0)
358 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
359 last_change = stats.st_atime;
368 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change));
370 sprintf (idlestr, " ?");
373 if (utmp_ent->ut_host[0])
375 char ut_host[sizeof (utmp_ent->ut_host) + 1];
376 char *host = 0, *display = 0;
378 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
379 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
380 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
382 /* Look for an X display. */
383 display = strchr (ut_host, ':');
387 if (*ut_host && do_lookup)
389 /* See if we can canonicalize it. */
390 host = canon_host (ut_host);
398 if (hostlen < strlen (host) + strlen (display) + 4)
400 hostlen = strlen (host) + strlen (display) + 4;
401 hoststr = xrealloc (hoststr, hostlen);
403 sprintf (hoststr, "(%s:%s)", host, display);
407 if (hostlen < strlen (host) + 3)
409 hostlen = strlen (host) + 3;
410 hoststr = xrealloc (hoststr, hostlen);
412 sprintf (hoststr, "(%s)", host);
420 hoststr = xrealloc (hoststr, hostlen);
422 stpcpy (hoststr, "");
426 print_line (UT_USER (utmp_ent), mesg, 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 ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
438 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
440 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
442 strcpy (comment, _("id="));
443 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
448 print_deadprocs (const STRUCT_UTMP *utmp_ent)
450 static char *exitstr;
451 char *comment = make_id_equals_comment (utmp_ent);
452 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
455 exitstr = xmalloc (strlen (_("term="))
456 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
457 + strlen (_("exit="))
458 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
460 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
461 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
463 /* FIXME: add idle time? */
465 print_line ("", ' ', utmp_ent->ut_line,
466 time_string (utmp_ent), "", pidstr, comment, exitstr);
471 print_login (const STRUCT_UTMP *utmp_ent)
473 char *comment = make_id_equals_comment (utmp_ent);
474 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
476 /* FIXME: add idle time? */
478 print_line ("LOGIN", ' ', utmp_ent->ut_line,
479 time_string (utmp_ent), "", pidstr, comment, "");
484 print_initspawn (const STRUCT_UTMP *utmp_ent)
486 char *comment = make_id_equals_comment (utmp_ent);
487 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
489 print_line ("", ' ', utmp_ent->ut_line,
490 time_string (utmp_ent), "", pidstr, comment, "");
495 print_clockchange (const STRUCT_UTMP *utmp_ent)
497 /* FIXME: handle NEW_TIME & OLD_TIME both */
498 print_line ("", ' ', _("clock change"),
499 time_string (utmp_ent), "", "", "", "");
503 print_runlevel (const STRUCT_UTMP *utmp_ent)
505 static char *runlevline, *comment;
506 int last = UT_PID (utmp_ent) / 256;
507 int curr = UT_PID (utmp_ent) % 256;
510 runlevline = xmalloc (strlen (_("run-level")) + 3);
511 sprintf (runlevline, "%s %c", _("run-level"), curr);
514 comment = xmalloc (strlen (_("last=")) + 2);
515 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
517 print_line ("", ' ', runlevline, time_string (utmp_ent),
518 "", "", comment, "");
523 /* Print the username of each valid entry and the number of valid entries
524 in UTMP_BUF, which should have N elements. */
526 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
529 char const *separator = "";
533 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
537 trimmed_name = extract_trimmed_name (utmp_buf);
539 printf ("%s%s", separator, trimmed_name);
546 printf (_("\n# users=%u\n"), entries);
552 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
553 _("COMMENT"), _("EXIT"));
556 /* Display UTMP_BUF, which should have N entries. */
558 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
560 char *ttyname_b IF_LINT ( = NULL);
567 ttyname_b = ttyname (0);
570 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
571 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
577 strncmp (ttyname_b, utmp_buf->ut_line,
578 sizeof (utmp_buf->ut_line)) == 0)
580 if (need_users && IS_USER_PROCESS (utmp_buf))
581 print_user (utmp_buf);
582 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
583 print_runlevel (utmp_buf);
584 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
585 print_boottime (utmp_buf);
586 /* I've never seen one of these, so I don't know what it should
588 FIXME: handle OLD_TIME also, perhaps show the delta? */
589 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
590 print_clockchange (utmp_buf);
591 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
592 print_initspawn (utmp_buf);
593 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
594 print_login (utmp_buf);
595 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
596 print_deadprocs (utmp_buf);
603 /* Display a list of who is on the system, according to utmp file filename. */
605 who (const char *filename)
608 STRUCT_UTMP *utmp_buf;
609 int fail = read_utmp (filename, &n_users, &utmp_buf);
612 error (EXIT_FAILURE, errno, "%s", filename);
615 list_entries_who (n_users, utmp_buf);
617 scan_entries (n_users, utmp_buf);
625 if (status != EXIT_SUCCESS)
626 fprintf (stderr, _("Try `%s --help' for more information.\n"),
630 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
633 -a, --all same as -b -d --login -p -r -t -T -u\n\
634 -b, --boot time of last system boot\n\
635 -d, --dead print dead processes\n\
636 -H, --heading print line of column headings\n\
639 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
640 (deprecated, use -u)\n\
641 -l, --login print system login processes\n\
644 --lookup attempt to canonicalize hostnames via DNS\n\
645 -m only hostname and user associated with stdin\n\
646 -p, --process print active processes spawned by init\n\
649 -q, --count all login names and number of users logged on\n\
650 -r, --runlevel print current runlevel\n\
651 -s, --short print only name, line, and time (default)\n\
652 -t, --time print last system clock change\n\
655 -T, -w, --mesg add user's message status as +, - or ?\n\
656 -u, --users list users logged in\n\
657 --message same as -T\n\
658 --writable same as -T\n\
660 fputs (HELP_OPTION_DESCRIPTION, stdout);
661 fputs (VERSION_OPTION_DESCRIPTION, stdout);
664 If FILE is not specified, use %s. %s as FILE is common.\n\
665 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
666 "), UTMP_FILE, WTMP_FILE);
667 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
673 main (int argc, char **argv)
678 initialize_main (&argc, &argv);
679 program_name = argv[0];
680 setlocale (LC_ALL, "");
681 bindtextdomain (PACKAGE, LOCALEDIR);
682 textdomain (PACKAGE);
684 atexit (close_stdout);
686 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts,
700 need_clockchange = 1;
754 need_clockchange = 1;
765 _("Warning: -i will be removed in a future release; \
778 case_GETOPT_HELP_CHAR;
780 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
783 usage (EXIT_FAILURE);
798 switch (argc - optind)
805 case 1: /* who <utmp file> */
809 case 2: /* who <blurf> <glop> */
815 error (0, 0, _("too many arguments"));
816 usage (EXIT_FAILURE);