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>
36 #include "vasprintf.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "who"
41 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Michael Stone"
43 #ifndef MAXHOSTNAMELEN
44 # define MAXHOSTNAMELEN 64
52 # define USER_PROCESS INT_MAX
56 # define RUN_LVL INT_MAX
60 # define INIT_PROCESS INT_MAX
64 # define LOGIN_PROCESS INT_MAX
68 # define DEAD_PROCESS INT_MAX
81 #if HAVE_STRUCT_XTMP_UT_PID
82 # define UT_PID(U) ((U)->ut_pid)
83 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
84 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
85 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
88 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
92 #if HAVE_STRUCT_XTMP_UT_ID
93 # define UT_ID(U) ((U)->ut_id)
95 # define UT_ID(U) "??"
98 #define UT_TYPE_UNDEF 255
100 #if HAVE_STRUCT_XTMP_UT_TYPE
101 # define UT_TYPE(U) ((U)->ut_type)
103 # define UT_TYPE(U) UT_TYPE_UNDEF
106 #define IS_USER_PROCESS(U) \
107 (UT_USER (utmp_buf)[0] \
108 && (UT_TYPE (utmp_buf) == USER_PROCESS \
109 || (UT_TYPE (utmp_buf) == UT_TYPE_UNDEF \
110 && UT_TIME_MEMBER (utmp_buf) != 0)))
116 /* The name this program was run with. */
119 /* If nonzero, attempt to canonicalize hostnames via a DNS lookup. */
120 static int do_lookup;
122 /* If nonzero, display only a list of usernames and count of
124 Ignored for `who am i'. */
125 static int short_list;
127 /* If nonzero, display only name, line, and time fields */
128 static int short_output;
130 /* If nonzero, display the hours:minutes since each user has touched
131 the keyboard, or "." if within the last minute, or "old" if
132 not within the last day. */
133 static int include_idle;
135 /* If nonzero, display a line at the top describing each field. */
136 static int include_heading;
138 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
139 or a `?' if their tty cannot be statted. */
140 static int include_mesg;
142 /* If nonzero, display process termination & exit status */
143 static int include_exit;
145 /* If nonzero, display the last boot time */
146 static int need_boottime;
148 /* If nonzero, display dead processes */
149 static int need_deadprocs;
151 /* If nonzero, display processes waiting for user login */
152 static int need_login;
154 /* If nonzero, display processes started by init */
155 static int need_initspawn;
157 /* If nonzero, display the last clock change */
158 static int need_clockchange;
160 /* If nonzero, display the current runlevel */
161 static int need_runlevel;
163 /* If nonzero, display user processes */
164 static int need_users;
166 /* If nonzero, display info only for the controlling tty */
167 static int my_line_only;
169 /* for long options with no corresponding short option, use enum */
172 LOOKUP_OPTION = CHAR_MAX + 1
175 static struct option const longopts[] = {
176 {"all", no_argument, NULL, 'a'},
177 {"boot", no_argument, NULL, 'b'},
178 {"count", no_argument, NULL, 'q'},
179 {"dead", no_argument, NULL, 'd'},
180 {"heading", no_argument, NULL, 'H'},
181 {"idle", no_argument, NULL, 'i'},
182 {"login", no_argument, NULL, 'l'},
183 {"lookup", no_argument, NULL, LOOKUP_OPTION},
184 {"message", no_argument, NULL, 'T'},
185 {"mesg", no_argument, NULL, 'T'},
186 {"process", no_argument, NULL, 'p'},
187 {"runlevel", no_argument, NULL, 'r'},
188 {"short", no_argument, NULL, 's'},
189 {"time", no_argument, NULL, 't'},
190 {"users", no_argument, NULL, 'u'},
191 {"writable", no_argument, NULL, 'T'},
192 {GETOPT_HELP_OPTION_DECL},
193 {GETOPT_VERSION_OPTION_DECL},
197 /* Return a string representing the time between WHEN and the time
198 that this function is first run.
201 idle_string (time_t when)
203 static time_t now = 0;
204 static char idle_hhmm[IDLESTR_LEN];
210 seconds_idle = now - when;
211 if (seconds_idle < 60) /* One minute. */
213 if (seconds_idle < (24 * 60 * 60)) /* One day. */
215 sprintf (idle_hhmm, "%02d:%02d",
216 (int) (seconds_idle / (60 * 60)),
217 (int) ((seconds_idle % (60 * 60)) / 60));
218 return (const char *) idle_hhmm;
223 /* Return a standard time string, "mon dd hh:mm"
224 FIXME: handle localization */
226 time_string (const STRUCT_UTMP *utmp_ent)
228 /* Don't take the address of UT_TIME_MEMBER directly.
229 Ulrich Drepper wrote:
230 ``... GNU libc (and perhaps other libcs as well) have extended
231 utmp file formats which do not use a simple time_t ut_time field.
232 In glibc, ut_time is a macro which selects for backward compatibility
233 the tv_sec member of a struct timeval value.'' */
234 time_t tm = UT_TIME_MEMBER (utmp_ent);
236 char *ptr = ctime (&tm) + 4;
241 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
242 will need tweaking if any of the localization stuff is done, or for 64 bit
245 print_line (const char *user, const char state, const char *line,
246 const char *time_str, const char *idle, const char *pid,
247 const char *comment, const char *exitstr)
249 static char mesg[3] = { ' ', 'x', '\0' };
251 char x_idle[1 + IDLESTR_LEN + 1];
252 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
258 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
259 sprintf (x_idle, " %-6s", idle);
263 if (!short_output && strlen (pid) < sizeof x_pid - 1)
264 sprintf (x_pid, " %10s", pid);
268 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
270 sprintf (x_exitstr, " %-12s", exitstr);
274 err = asprintf (&buf,
285 include_mesg ? mesg : "",
290 /* FIXME: it's not really clear whether the following
291 field should be in the short_output. A strict reading
292 of SUSv2 would suggest not, but I haven't seen any
293 implementations that actually work that way... */
301 /* Remove any trailing spaces. */
302 char *p = buf + strlen (buf);
313 /* Send properly parsed USER_PROCESS info to print_line */
315 print_user (const STRUCT_UTMP *utmp_ent)
320 char idlestr[IDLESTR_LEN + 1];
321 static char *hoststr;
322 static size_t hostlen;
324 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
325 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
327 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
328 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
330 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
331 already an absolute pathname. Some system may put the full,
332 absolute pathname in ut_line. */
333 if (utmp_ent->ut_line[0] == '/')
335 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
336 line[sizeof (utmp_ent->ut_line)] = '\0';
340 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
341 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
342 sizeof (utmp_ent->ut_line));
343 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
346 if (stat (line, &stats) == 0)
348 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
349 last_change = stats.st_atime;
358 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change));
360 sprintf (idlestr, " ?");
363 if (utmp_ent->ut_host[0])
365 char ut_host[sizeof (utmp_ent->ut_host) + 1];
366 char *host = 0, *display = 0;
368 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
369 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
370 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
372 /* Look for an X display. */
373 display = strchr (ut_host, ':');
377 if (*ut_host && do_lookup)
379 /* See if we can canonicalize it. */
380 host = canon_host (ut_host);
388 if (hostlen < strlen (host) + strlen (display) + 4)
390 hostlen = strlen (host) + strlen (display) + 4;
391 hoststr = xrealloc (hoststr, hostlen);
393 sprintf (hoststr, "(%s:%s)", host, display);
397 if (hostlen < strlen (host) + 3)
399 hostlen = strlen (host) + 3;
400 hoststr = xrealloc (hoststr, hostlen);
402 sprintf (hoststr, "(%s)", host);
410 hoststr = xrealloc (hoststr, hostlen);
412 stpcpy (hoststr, "");
416 print_line (UT_USER (utmp_ent), mesg, utmp_ent->ut_line,
417 time_string (utmp_ent), idlestr, pidstr,
418 hoststr ? hoststr : "", "");
422 print_boottime (const STRUCT_UTMP *utmp_ent)
424 print_line ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
428 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
430 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
432 strcpy (comment, _("id="));
433 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
438 print_deadprocs (const STRUCT_UTMP *utmp_ent)
440 static char *exitstr;
441 char *comment = make_id_equals_comment (utmp_ent);
442 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
445 exitstr = xmalloc (strlen (_("term="))
446 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
447 + strlen (_("exit="))
448 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
450 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
451 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
453 /* FIXME: add idle time? */
455 print_line ("", ' ', utmp_ent->ut_line,
456 time_string (utmp_ent), "", pidstr, comment, exitstr);
461 print_login (const STRUCT_UTMP *utmp_ent)
463 char *comment = make_id_equals_comment (utmp_ent);
464 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
466 /* FIXME: add idle time? */
468 print_line ("LOGIN", ' ', utmp_ent->ut_line,
469 time_string (utmp_ent), "", pidstr, comment, "");
474 print_initspawn (const STRUCT_UTMP *utmp_ent)
476 char *comment = make_id_equals_comment (utmp_ent);
477 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
479 print_line ("", ' ', utmp_ent->ut_line,
480 time_string (utmp_ent), "", pidstr, comment, "");
485 print_clockchange (const STRUCT_UTMP *utmp_ent)
487 /* FIXME: handle NEW_TIME & OLD_TIME both */
488 print_line ("", ' ', _("clock change"),
489 time_string (utmp_ent), "", "", "", "");
493 print_runlevel (const STRUCT_UTMP *utmp_ent)
495 static char *runlevline, *comment;
496 int last = UT_PID (utmp_ent) / 256;
497 int curr = UT_PID (utmp_ent) % 256;
500 runlevline = xmalloc (strlen (_("run-level")) + 3);
501 sprintf (runlevline, "%s %c", _("run-level"), curr);
504 comment = xmalloc (strlen (_("last=")) + 2);
505 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
507 print_line ("", ' ', runlevline, time_string (utmp_ent),
508 "", "", comment, "");
513 /* Print the username of each valid entry and the number of valid entries
514 in UTMP_BUF, which should have N elements. */
516 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
519 char const *separator = "";
523 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
527 trimmed_name = extract_trimmed_name (utmp_buf);
529 printf ("%s%s", separator, trimmed_name);
536 printf (_("\n# users=%u\n"), entries);
542 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
543 _("COMMENT"), _("EXIT"));
546 /* Display UTMP_BUF, which should have N entries. */
548 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
550 char *ttyname_b IF_LINT ( = NULL);
557 ttyname_b = ttyname (0);
560 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
561 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
567 strncmp (ttyname_b, utmp_buf->ut_line,
568 sizeof (utmp_buf->ut_line)) == 0)
570 if (need_users && IS_USER_PROCESS (utmp_buf))
571 print_user (utmp_buf);
572 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
573 print_runlevel (utmp_buf);
574 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
575 print_boottime (utmp_buf);
576 /* I've never seen one of these, so I don't know what it should
578 FIXME: handle OLD_TIME also, perhaps show the delta? */
579 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
580 print_clockchange (utmp_buf);
581 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
582 print_initspawn (utmp_buf);
583 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
584 print_login (utmp_buf);
585 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
586 print_deadprocs (utmp_buf);
593 /* Display a list of who is on the system, according to utmp file filename. */
595 who (const char *filename)
598 STRUCT_UTMP *utmp_buf;
599 int fail = read_utmp (filename, &n_users, &utmp_buf);
602 error (EXIT_FAILURE, errno, "%s", filename);
605 list_entries_who (n_users, utmp_buf);
607 scan_entries (n_users, utmp_buf);
615 if (status != EXIT_SUCCESS)
616 fprintf (stderr, _("Try `%s --help' for more information.\n"),
620 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
623 -a, --all same as -b -d --login -p -r -t -T -u\n\
624 -b, --boot time of last system boot\n\
625 -d, --dead print dead processes\n\
626 -H, --heading print line of column headings\n\
629 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
630 (deprecated, use -u)\n\
631 -l, --login print system login processes\n\
634 --lookup attempt to canonicalize hostnames via DNS\n\
635 -m only hostname and user associated with stdin\n\
636 -p, --process print active processes spawned by init\n\
639 -q, --count all login names and number of users logged on\n\
640 -r, --runlevel print current runlevel\n\
641 -s, --short print only name, line, and time (default)\n\
642 -t, --time print last system clock change\n\
645 -T, -w, --mesg add user's message status as +, - or ?\n\
646 -u, --users list users logged in\n\
647 --message same as -T\n\
648 --writable same as -T\n\
650 fputs (HELP_OPTION_DESCRIPTION, stdout);
651 fputs (VERSION_OPTION_DESCRIPTION, stdout);
654 If FILE is not specified, use %s. %s as FILE is common.\n\
655 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
656 "), UTMP_FILE, WTMP_FILE);
657 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
663 main (int argc, char **argv)
668 initialize_main (&argc, &argv);
669 program_name = argv[0];
670 setlocale (LC_ALL, "");
671 bindtextdomain (PACKAGE, LOCALEDIR);
672 textdomain (PACKAGE);
674 atexit (close_stdout);
676 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts,
690 need_clockchange = 1;
744 need_clockchange = 1;
755 _("Warning: -i will be removed in a future release; \
768 case_GETOPT_HELP_CHAR;
770 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
773 usage (EXIT_FAILURE);
788 switch (argc - optind)
795 case 1: /* who <utmp file> */
799 case 2: /* who <blurf> <glop> */
805 error (0, 0, _("too many arguments"));
806 usage (EXIT_FAILURE);