blob: 4adead77e260dff51cf10b6f1c0c73731f171b0e [file] [log] [blame]
Eric Andersen00a6a752002-04-26 23:53:10 +00001/* vi: set sw=4 ts=4: */
2/*----------------------------------------------------------------------
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Mini who is used to display user name, login time,
Eric Andersen00a6a752002-04-26 23:53:10 +00004 * idle time and host name.
5 *
6 * Author: Da Chen <dchen@ayrnetworks.com>
7 *
8 * This is a free document; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation:
11 * http://www.gnu.org/copyleft/gpl.html
12 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013 * Copyright (c) 2002 AYR Networks, Inc.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000014 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020015 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000016 *
Eric Andersen00a6a752002-04-26 23:53:10 +000017 *----------------------------------------------------------------------
18 */
Tito Ragusa7926b982011-08-09 04:37:50 +020019//config:config WHO
20//config: bool "who"
21//config: default y
22//config: depends on FEATURE_UTMP
23//config: help
24//config: who is used to show who is logged on.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010025//config:
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +020026// procps-ng has this variation of "who":
27//config:config W
28//config: bool "w"
29//config: default y
30//config: depends on FEATURE_UTMP
31//config: help
32//config: w is used to show who is logged on.
33//config:
Tito Ragusa7926b982011-08-09 04:37:50 +020034//config:config USERS
35//config: bool "users"
36//config: default y
37//config: depends on FEATURE_UTMP
38//config: help
39//config: Print users currently logged on.
40
Denys Vlasenko205d48e2017-01-29 14:57:33 +010041// APPLET_ODDNAME:name main location suid_type help
Tito Ragusa7926b982011-08-09 04:37:50 +020042//applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +020043//applet:IF_W( APPLET_ODDNAME(w, who, BB_DIR_USR_BIN, BB_SUID_DROP, w))
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +020044//applet:IF_WHO( APPLET( who, BB_DIR_USR_BIN, BB_SUID_DROP))
Tito Ragusa7926b982011-08-09 04:37:50 +020045
46//kbuild:lib-$(CONFIG_USERS) += who.o
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +020047//kbuild:lib-$(CONFIG_W) += who.o
48//kbuild:lib-$(CONFIG_WHO) += who.o
Tito Ragusa7926b982011-08-09 04:37:50 +020049
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010050/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
51
Tito Ragusa7926b982011-08-09 04:37:50 +020052//usage:#define users_trivial_usage
53//usage: ""
54//usage:#define users_full_usage "\n\n"
55//usage: "Print the users currently logged on"
56
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +020057//usage:#define w_trivial_usage
58//usage: ""
59//usage:#define w_full_usage "\n\n"
60//usage: "Show who is logged on"
61//
62// procps-ng 3.3.10:
63// "\n -h, --no-header"
64// "\n -u, --no-current"
65// Ignores the username while figuring out the current process
66// and cpu times. To demonstrate this, do a "su" and do a "w" and a "w -u".
67// "\n -s, --short"
68// Short format. Don't print the login time, JCPU or PCPU times.
69// "\n -f, --from"
70// Toggle printing the from (remote hostname) field.
71// The default is for the from field to not be printed
72// "\n -i, --ip-addr"
73// Display IP address instead of hostname for from field.
74// "\n -o, --old-style"
75// Old style output. Prints blank space for idle times less than one minute.
76// Example output:
77// 17:28:00 up 4 days, 22:41, 4 users, load average: 0.84, 0.97, 0.90
78// USER TTY LOGIN@ IDLE JCPU PCPU WHAT
79// root tty1 Thu18 4days 4:33m 0.07s /bin/sh /etc/xdg/xfce4/xinitrc -- vt
80// root pts/1 Mon13 3:24m 1:01 0.01s w
81
Pere Orga34425382011-03-31 14:43:25 +020082//usage:#define who_trivial_usage
83//usage: "[-a]"
84//usage:#define who_full_usage "\n\n"
85//usage: "Show who is logged on\n"
Pere Orga34425382011-03-31 14:43:25 +020086//usage: "\n -a Show all"
Denys Vlasenkob110e1f2012-04-18 14:38:15 +020087//usage: "\n -H Print column headers"
Pere Orga34425382011-03-31 14:43:25 +020088
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000089#include "libbb.h"
Eric Andersen00a6a752002-04-26 23:53:10 +000090
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000091static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000092{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000093 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000094
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000095 /*if (t < 60) {
96 str6[0] = '.';
97 str6[1] = '\0';
98 return;
99 }*/
100 if (t >= 0 && t < (24 * 60 * 60)) {
101 sprintf(str6, "%02d:%02d",
102 (int) (t / (60 * 60)),
103 (int) ((t % (60 * 60)) / 60));
104 return;
Rob Landleye01d7462006-03-12 19:26:01 +0000105 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +0000106 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +0000107}
108
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000109int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000110int who_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +0000111{
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +0200112#define CNT_APPLET (ENABLE_USERS + ENABLE_W + ENABLE_WHO)
113 int do_users = (ENABLE_USERS && (CNT_APPLET == 1 || applet_name[0] == 'u'));
114 int do_w = (ENABLE_W && (CNT_APPLET == 1 || applet_name[1] == '\0'));
115 int do_who = (ENABLE_WHO && (CNT_APPLET == 1 || applet_name[1] == 'h'));
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200116 struct utmpx *ut;
Denis Vlasenko01cd9572007-11-16 05:24:43 +0000117 unsigned opt;
Tito Ragusa7926b982011-08-09 04:37:50 +0200118 const char *fmt = "%s";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000119
Denis Vlasenko01cd9572007-11-16 05:24:43 +0000120 opt_complementary = "=0";
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +0200121 opt = getopt32(argv, do_who ? "aH" : "");
122 if ((opt & 2) || do_w) /* -H or we are w */
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200123 puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000124
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200125 setutxent();
126 while ((ut = getutxent()) != NULL) {
Denys Vlasenko4c721042010-04-04 23:45:09 +0200127 if (ut->ut_user[0]
128 && ((opt & 1) || ut->ut_type == USER_PROCESS)
129 ) {
Tito Ragusa7926b982011-08-09 04:37:50 +0200130 if (!do_users) {
131 char str6[6];
132 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
133 struct stat st;
134 time_t seconds;
Denys Vlasenko4c721042010-04-04 23:45:09 +0200135
Tito Ragusa7926b982011-08-09 04:37:50 +0200136 str6[0] = '?';
137 str6[1] = '\0';
138 strcpy(name, "/dev/");
139 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
140 ut->ut_line,
141 sizeof(ut->ut_line)+1
142 );
143 if (stat(name, &st) == 0)
144 idle_string(str6, st.st_atime);
145 /* manpages say ut_tv.tv_sec *is* time_t,
146 * but some systems have it wrong */
147 seconds = ut->ut_tv.tv_sec;
148 /* How wide time field can be?
149 * "Nov 10 19:33:20": 15 chars
150 * "2010-11-10 19:33": 16 chars
151 */
152 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
153 (int)sizeof(ut->ut_user), ut->ut_user,
154 (int)sizeof(ut->ut_line), ut->ut_line,
155 str6,
Denys Vlasenko1bc0bd12017-04-11 18:17:03 +0200156// TODO: with LANG=en_US.UTF-8, who from coreutils 8.25 shows
157// TIME col as "2017-04-06 18:47" (the default format is "Apr 6 18:47").
158// The former format looks saner to me. Switch to it unconditionally?
Tito Ragusa7926b982011-08-09 04:37:50 +0200159 ctime(&seconds) + 4,
160 (int)sizeof(ut->ut_host), ut->ut_host
161 );
162 } else {
163 printf(fmt, ut->ut_user);
164 fmt = " %s";
165 }
Rob Landleye01d7462006-03-12 19:26:01 +0000166 }
167 }
Tito Ragusa7926b982011-08-09 04:37:50 +0200168 if (do_users)
169 bb_putchar('\n');
Denis Vlasenko41cca2b2007-03-07 00:07:42 +0000170 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200171 endutxent();
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +0000172 return EXIT_SUCCESS;
Eric Andersen00a6a752002-04-26 23:53:10 +0000173}