2 Copyright (C) 1992-2006 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>
34 #include "canon-host.h"
37 #include "hard-locale.h"
40 #include "vasprintf.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "who"
45 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Michael Stone"
47 #ifndef MAXHOSTNAMELEN
48 # define MAXHOSTNAMELEN 64
52 # define UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
54 # define UT_TYPE_RUN_LVL(U) false
58 # define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
60 # define UT_TYPE_INIT_PROCESS(U) false
64 # define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
66 # define UT_TYPE_LOGIN_PROCESS(U) false
70 # define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
72 # define UT_TYPE_DEAD_PROCESS(U) false
76 # define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
78 # define UT_TYPE_NEW_TIME(U) false
83 #if HAVE_STRUCT_XTMP_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))
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) "??"
100 /* The name this program was run with. */
103 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
104 static bool do_lookup
;
106 /* If true, display only a list of usernames and count of
108 Ignored for `who am i'. */
109 static bool short_list
;
111 /* If true, display only name, line, and time fields. */
112 static bool short_output
;
114 /* If true, display the hours:minutes since each user has touched
115 the keyboard, or "." if within the last minute, or "old" if
116 not within the last day. */
117 static bool include_idle
;
119 /* If true, display a line at the top describing each field. */
120 static bool include_heading
;
122 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
123 or a `?' if their tty cannot be statted. */
124 static bool include_mesg
;
126 /* If true, display process termination & exit status. */
127 static bool include_exit
;
129 /* If true, display the last boot time. */
130 static bool need_boottime
;
132 /* If true, display dead processes. */
133 static bool need_deadprocs
;
135 /* If true, display processes waiting for user login. */
136 static bool need_login
;
138 /* If true, display processes started by init. */
139 static bool need_initspawn
;
141 /* If true, display the last clock change. */
142 static bool need_clockchange
;
144 /* If true, display the current runlevel. */
145 static bool need_runlevel
;
147 /* If true, display user processes. */
148 static bool need_users
;
150 /* If true, display info only for the controlling tty. */
151 static bool my_line_only
;
153 /* The strftime format to use for login times, and its expected
155 static char const *time_format
;
156 static int time_format_width
;
158 /* for long options with no corresponding short option, use enum */
161 LOOKUP_OPTION
= CHAR_MAX
+ 1
164 static struct option
const longopts
[] = {
165 {"all", no_argument
, NULL
, 'a'},
166 {"boot", no_argument
, NULL
, 'b'},
167 {"count", no_argument
, NULL
, 'q'},
168 {"dead", no_argument
, NULL
, 'd'},
169 {"heading", no_argument
, NULL
, 'H'},
170 {"idle", no_argument
, NULL
, 'i'}, /* FIXME: deprecated: remove in late 2006 */
171 {"login", no_argument
, NULL
, 'l'},
172 {"lookup", no_argument
, NULL
, LOOKUP_OPTION
},
173 {"message", no_argument
, NULL
, 'T'},
174 {"mesg", no_argument
, NULL
, 'T'},
175 {"process", no_argument
, NULL
, 'p'},
176 {"runlevel", no_argument
, NULL
, 'r'},
177 {"short", no_argument
, NULL
, 's'},
178 {"time", no_argument
, NULL
, 't'},
179 {"users", no_argument
, NULL
, 'u'},
180 {"writable", no_argument
, NULL
, 'T'},
181 {GETOPT_HELP_OPTION_DECL
},
182 {GETOPT_VERSION_OPTION_DECL
},
186 /* Return a string representing the time between WHEN and now.
187 BOOTTIME is the time of last reboot.
190 idle_string (time_t when
, time_t boottime
)
192 static time_t now
= TYPE_MINIMUM (time_t);
194 if (now
== TYPE_MINIMUM (time_t))
197 if (boottime
< when
&& now
- 24 * 60 * 60 < when
&& when
<= now
)
199 int seconds_idle
= now
- when
;
200 if (seconds_idle
< 60)
204 static char idle_hhmm
[IDLESTR_LEN
];
205 sprintf (idle_hhmm
, "%02d:%02d",
206 seconds_idle
/ (60 * 60),
207 (seconds_idle
% (60 * 60)) / 60);
215 /* Return a time string. */
217 time_string (const STRUCT_UTMP
*utmp_ent
)
219 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
221 /* Don't take the address of UT_TIME_MEMBER directly.
222 Ulrich Drepper wrote:
223 ``... GNU libc (and perhaps other libcs as well) have extended
224 utmp file formats which do not use a simple time_t ut_time field.
225 In glibc, ut_time is a macro which selects for backward compatibility
226 the tv_sec member of a struct timeval value.'' */
227 time_t t
= UT_TIME_MEMBER (utmp_ent
);
228 struct tm
*tmp
= localtime (&t
);
232 strftime (buf
, sizeof buf
, time_format
, tmp
);
236 return TYPE_SIGNED (time_t) ? imaxtostr (t
, buf
) : umaxtostr (t
, buf
);
239 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
240 will need tweaking if any of the localization stuff is done, or for 64 bit
243 print_line (int userlen
, const char *user
, const char state
,
244 int linelen
, const char *line
,
245 const char *time_str
, const char *idle
, const char *pid
,
246 const char *comment
, const char *exitstr
)
248 static char mesg
[3] = { ' ', 'x', '\0' };
250 char x_idle
[1 + IDLESTR_LEN
+ 1];
251 char x_pid
[1 + INT_STRLEN_BOUND (pid_t
) + 1];
257 if (include_idle
&& !short_output
&& strlen (idle
) < sizeof x_idle
- 1)
258 sprintf (x_idle
, " %-6s", idle
);
262 if (!short_output
&& strlen (pid
) < sizeof x_pid
- 1)
263 sprintf (x_pid
, " %10s", pid
);
267 x_exitstr
= xmalloc (include_exit
? 1 + MAX (12, strlen (exitstr
)) + 1 : 1);
269 sprintf (x_exitstr
, " %-12s", exitstr
);
273 err
= asprintf (&buf
,
283 userlen
, user
? user
: " .",
284 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. The most
314 recent boot time is BOOTTIME. */
316 print_user (const STRUCT_UTMP
*utmp_ent
, time_t boottime
)
321 char idlestr
[IDLESTR_LEN
+ 1];
322 static char *hoststr
;
323 static size_t hostlen
;
325 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
326 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
328 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
329 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
331 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
332 already an absolute file name. Some systems may put the full,
333 absolute file name in ut_line. */
334 if (utmp_ent
->ut_line
[0] == '/')
336 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
337 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
341 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
342 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
,
343 sizeof (utmp_ent
->ut_line
));
344 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
347 if (stat (line
, &stats
) == 0)
349 mesg
= (stats
.st_mode
& S_IWGRP
) ? '+' : '-';
350 last_change
= stats
.st_atime
;
359 sprintf (idlestr
, "%.*s", IDLESTR_LEN
, idle_string (last_change
, boottime
));
361 sprintf (idlestr
, " ?");
364 if (utmp_ent
->ut_host
[0])
366 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
368 char *display
= NULL
;
370 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
371 strncpy (ut_host
, utmp_ent
->ut_host
, sizeof (utmp_ent
->ut_host
));
372 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
374 /* Look for an X display. */
375 display
= strchr (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
= xrealloc (hoststr
, hostlen
);
395 sprintf (hoststr
, "(%s:%s)", host
, display
);
399 if (hostlen
< strlen (host
) + 3)
401 hostlen
= strlen (host
) + 3;
402 hoststr
= xrealloc (hoststr
, hostlen
);
404 sprintf (hoststr
, "(%s)", host
);
415 hoststr
= xrealloc (hoststr
, hostlen
);
421 print_line (sizeof UT_USER (utmp_ent
), UT_USER (utmp_ent
), mesg
,
422 sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
423 time_string (utmp_ent
), idlestr
, pidstr
,
424 hoststr
? hoststr
: "", "");
428 print_boottime (const STRUCT_UTMP
*utmp_ent
)
430 print_line (-1, "", ' ', -1, "system boot",
431 time_string (utmp_ent
), "", "", "", "");
435 make_id_equals_comment (STRUCT_UTMP
const *utmp_ent
)
437 char *comment
= xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent
) + 1);
439 strcpy (comment
, _("id="));
440 strncat (comment
, UT_ID (utmp_ent
), sizeof UT_ID (utmp_ent
));
445 print_deadprocs (const STRUCT_UTMP
*utmp_ent
)
447 static char *exitstr
;
448 char *comment
= make_id_equals_comment (utmp_ent
);
449 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
452 exitstr
= xmalloc (strlen (_("term="))
453 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent
)) + 1
454 + strlen (_("exit="))
455 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent
))
457 sprintf (exitstr
, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent
),
458 _("exit="), UT_EXIT_E_EXIT (utmp_ent
));
460 /* FIXME: add idle time? */
462 print_line (-1, "", ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
463 time_string (utmp_ent
), "", pidstr
, comment
, exitstr
);
468 print_login (const STRUCT_UTMP
*utmp_ent
)
470 char *comment
= make_id_equals_comment (utmp_ent
);
471 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
473 /* FIXME: add idle time? */
475 print_line (-1, "LOGIN", ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
476 time_string (utmp_ent
), "", pidstr
, comment
, "");
481 print_initspawn (const STRUCT_UTMP
*utmp_ent
)
483 char *comment
= make_id_equals_comment (utmp_ent
);
484 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
486 print_line (-1, "", ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
487 time_string (utmp_ent
), "", pidstr
, comment
, "");
492 print_clockchange (const STRUCT_UTMP
*utmp_ent
)
494 /* FIXME: handle NEW_TIME & OLD_TIME both */
495 print_line (-1, "", ' ', -1, _("clock change"),
496 time_string (utmp_ent
), "", "", "", "");
500 print_runlevel (const STRUCT_UTMP
*utmp_ent
)
502 static char *runlevline
, *comment
;
503 unsigned char last
= UT_PID (utmp_ent
) / 256;
504 unsigned char curr
= UT_PID (utmp_ent
) % 256;
507 runlevline
= xmalloc (strlen (_("run-level")) + 3);
508 sprintf (runlevline
, "%s %c", _("run-level"), curr
);
511 comment
= xmalloc (strlen (_("last=")) + 2);
512 sprintf (comment
, "%s%c", _("last="), (last
== 'N') ? 'S' : last
);
514 print_line (-1, "", ' ', -1, runlevline
, time_string (utmp_ent
),
515 "", "", comment
, "");
520 /* Print the username of each valid entry and the number of valid entries
521 in UTMP_BUF, which should have N elements. */
523 list_entries_who (size_t n
, const STRUCT_UTMP
*utmp_buf
)
525 unsigned long int entries
= 0;
526 char const *separator
= "";
530 if (IS_USER_PROCESS (utmp_buf
))
534 trimmed_name
= extract_trimmed_name (utmp_buf
);
536 printf ("%s%s", separator
, trimmed_name
);
543 printf (_("\n# users=%lu\n"), entries
);
549 print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
550 _("PID"), _("COMMENT"), _("EXIT"));
553 /* Display UTMP_BUF, which should have N entries. */
555 scan_entries (size_t n
, const STRUCT_UTMP
*utmp_buf
)
557 char *ttyname_b
IF_LINT ( = NULL
);
558 time_t boottime
= TYPE_MINIMUM (time_t);
565 ttyname_b
= ttyname (STDIN_FILENO
);
568 if (strncmp (ttyname_b
, DEV_DIR_WITH_TRAILING_SLASH
, DEV_DIR_LEN
) == 0)
569 ttyname_b
+= DEV_DIR_LEN
; /* Discard /dev/ prefix. */
575 strncmp (ttyname_b
, utmp_buf
->ut_line
,
576 sizeof (utmp_buf
->ut_line
)) == 0)
578 if (need_users
&& IS_USER_PROCESS (utmp_buf
))
579 print_user (utmp_buf
, boottime
);
580 else if (need_runlevel
&& UT_TYPE_RUN_LVL (utmp_buf
))
581 print_runlevel (utmp_buf
);
582 else if (need_boottime
&& UT_TYPE_BOOT_TIME (utmp_buf
))
583 print_boottime (utmp_buf
);
584 /* I've never seen one of these, so I don't know what it should
586 FIXME: handle OLD_TIME also, perhaps show the delta? */
587 else if (need_clockchange
&& UT_TYPE_NEW_TIME (utmp_buf
))
588 print_clockchange (utmp_buf
);
589 else if (need_initspawn
&& UT_TYPE_INIT_PROCESS (utmp_buf
))
590 print_initspawn (utmp_buf
);
591 else if (need_login
&& UT_TYPE_LOGIN_PROCESS (utmp_buf
))
592 print_login (utmp_buf
);
593 else if (need_deadprocs
&& UT_TYPE_DEAD_PROCESS (utmp_buf
))
594 print_deadprocs (utmp_buf
);
597 if (UT_TYPE_BOOT_TIME (utmp_buf
))
598 boottime
= UT_TIME_MEMBER (utmp_buf
);
604 /* Display a list of who is on the system, according to utmp file FILENAME.
605 Use read_utmp OPTIONS to read the file. */
607 who (const char *filename
, int options
)
610 STRUCT_UTMP
*utmp_buf
;
612 if (read_utmp (filename
, &n_users
, &utmp_buf
, options
) != 0)
613 error (EXIT_FAILURE
, errno
, "%s", filename
);
616 list_entries_who (n_users
, utmp_buf
);
618 scan_entries (n_users
, utmp_buf
);
626 if (status
!= EXIT_SUCCESS
)
627 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
631 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name
);
634 -a, --all same as -b -d --login -p -r -t -T -u\n\
635 -b, --boot time of last system boot\n\
636 -d, --dead print dead processes\n\
637 -H, --heading print line of column headings\n\
640 -l, --login print system login processes\n\
643 --lookup attempt to canonicalize hostnames via DNS\n\
644 -m only hostname and user associated with stdin\n\
645 -p, --process print active processes spawned by init\n\
648 -q, --count all login names and number of users logged on\n\
649 -r, --runlevel print current runlevel\n\
650 -s, --short print only name, line, and time (default)\n\
651 -t, --time print last system clock change\n\
654 -T, -w, --mesg add user's message status as +, - or ?\n\
655 -u, --users list users logged in\n\
656 --message same as -T\n\
657 --writable same as -T\n\
659 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
660 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
663 If FILE is not specified, use %s. %s as FILE is common.\n\
664 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
665 "), UTMP_FILE
, WTMP_FILE
);
666 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
672 main (int argc
, char **argv
)
675 bool assumptions
= true;
677 initialize_main (&argc
, &argv
);
678 program_name
= argv
[0];
679 setlocale (LC_ALL
, "");
680 bindtextdomain (PACKAGE
, LOCALEDIR
);
681 textdomain (PACKAGE
);
683 atexit (close_stdout
);
685 while ((optc
= getopt_long (argc
, argv
, "abdilmpqrstuwHT", longopts
, NULL
))
691 need_boottime
= true;
692 need_deadprocs
= true;
694 need_initspawn
= true;
695 need_runlevel
= true;
696 need_clockchange
= true;
705 need_boottime
= true;
710 need_deadprocs
= true;
717 include_heading
= true;
731 need_initspawn
= true;
740 need_runlevel
= true;
750 need_clockchange
= true;
761 _("Warning: -i will be removed in a future release; \
774 case_GETOPT_HELP_CHAR
;
776 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
779 usage (EXIT_FAILURE
);
791 short_output
= false;
794 if (hard_locale (LC_TIME
))
796 time_format
= "%Y-%m-%d %H:%M";
797 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
801 time_format
= "%b %e %H:%M";
802 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
805 switch (argc
- optind
)
807 case 2: /* who <blurf> <glop> */
812 who (UTMP_FILE
, READ_UTMP_CHECK_PIDS
);
815 case 1: /* who <utmp file> */
816 who (argv
[optind
], 0);
820 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
821 usage (EXIT_FAILURE
);