Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a02a4e9 | 2017-10-05 15:19:25 +0200 | [diff] [blame] | 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * Mini who is used to display user name, login time, |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 4 | * 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * Copyright (c) 2002 AYR Networks, Inc. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 14 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 15 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 16 | */ |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 17 | //config:config WHO |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 18 | //config: bool "who (3.9 kb)" |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: default y |
| 20 | //config: depends on FEATURE_UTMP |
| 21 | //config: help |
| 22 | //config: Print users currently logged on. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 23 | //config: |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 24 | // procps-ng has this variation of "who": |
| 25 | //config:config W |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 26 | //config: bool "w (3.8 kb)" |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 27 | //config: default y |
| 28 | //config: depends on FEATURE_UTMP |
| 29 | //config: help |
| 30 | //config: Print users currently logged on. |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 31 | //config: |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 32 | //config:config USERS |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 33 | //config: bool "users (3.4 kb)" |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 34 | //config: default y |
| 35 | //config: depends on FEATURE_UTMP |
| 36 | //config: help |
| 37 | //config: Print users currently logged on. |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 38 | |
Denys Vlasenko | 1a1203f | 2017-08-07 16:47:34 +0200 | [diff] [blame] | 39 | // APPLET_NOEXEC:name main location suid_type help |
| 40 | //applet:IF_USERS(APPLET_NOEXEC(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users)) |
| 41 | //applet:IF_W( APPLET_NOEXEC(w, who, BB_DIR_USR_BIN, BB_SUID_DROP, w)) |
| 42 | //applet:IF_WHO( APPLET_NOEXEC(who, who, BB_DIR_USR_BIN, BB_SUID_DROP, who)) |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 43 | |
| 44 | //kbuild:lib-$(CONFIG_USERS) += who.o |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 45 | //kbuild:lib-$(CONFIG_W) += who.o |
| 46 | //kbuild:lib-$(CONFIG_WHO) += who.o |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 47 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 48 | /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */ |
| 49 | |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 50 | //usage:#define users_trivial_usage |
| 51 | //usage: "" |
| 52 | //usage:#define users_full_usage "\n\n" |
| 53 | //usage: "Print the users currently logged on" |
| 54 | |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 55 | //usage:#define w_trivial_usage |
| 56 | //usage: "" |
| 57 | //usage:#define w_full_usage "\n\n" |
| 58 | //usage: "Show who is logged on" |
| 59 | // |
| 60 | // procps-ng 3.3.10: |
| 61 | // "\n -h, --no-header" |
| 62 | // "\n -u, --no-current" |
| 63 | // Ignores the username while figuring out the current process |
| 64 | // and cpu times. To demonstrate this, do a "su" and do a "w" and a "w -u". |
| 65 | // "\n -s, --short" |
| 66 | // Short format. Don't print the login time, JCPU or PCPU times. |
| 67 | // "\n -f, --from" |
| 68 | // Toggle printing the from (remote hostname) field. |
| 69 | // The default is for the from field to not be printed |
| 70 | // "\n -i, --ip-addr" |
| 71 | // Display IP address instead of hostname for from field. |
| 72 | // "\n -o, --old-style" |
| 73 | // Old style output. Prints blank space for idle times less than one minute. |
| 74 | // Example output: |
| 75 | // 17:28:00 up 4 days, 22:41, 4 users, load average: 0.84, 0.97, 0.90 |
| 76 | // USER TTY LOGIN@ IDLE JCPU PCPU WHAT |
| 77 | // root tty1 Thu18 4days 4:33m 0.07s /bin/sh /etc/xdg/xfce4/xinitrc -- vt |
| 78 | // root pts/1 Mon13 3:24m 1:01 0.01s w |
| 79 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 80 | //usage:#define who_trivial_usage |
Denys Vlasenko | 1f60d88 | 2021-06-15 10:00:18 +0200 | [diff] [blame] | 81 | //usage: "[-aH]" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 82 | //usage:#define who_full_usage "\n\n" |
| 83 | //usage: "Show who is logged on\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 84 | //usage: "\n -a Show all" |
Denys Vlasenko | b110e1f | 2012-04-18 14:38:15 +0200 | [diff] [blame] | 85 | //usage: "\n -H Print column headers" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 86 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 87 | #include "libbb.h" |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 89 | static void idle_string(char *str6, time_t t) |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 90 | { |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 91 | t = time(NULL) - t; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 93 | /*if (t < 60) { |
| 94 | str6[0] = '.'; |
| 95 | str6[1] = '\0'; |
| 96 | return; |
| 97 | }*/ |
| 98 | if (t >= 0 && t < (24 * 60 * 60)) { |
| 99 | sprintf(str6, "%02d:%02d", |
| 100 | (int) (t / (60 * 60)), |
| 101 | (int) ((t % (60 * 60)) / 60)); |
| 102 | return; |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 103 | } |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 104 | strcpy(str6, "old"); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 107 | int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 108 | int who_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 109 | { |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 110 | #define CNT_APPLET (ENABLE_USERS + ENABLE_W + ENABLE_WHO) |
| 111 | int do_users = (ENABLE_USERS && (CNT_APPLET == 1 || applet_name[0] == 'u')); |
| 112 | int do_w = (ENABLE_W && (CNT_APPLET == 1 || applet_name[1] == '\0')); |
| 113 | int do_who = (ENABLE_WHO && (CNT_APPLET == 1 || applet_name[1] == 'h')); |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 114 | struct utmpx *ut; |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 115 | unsigned opt; |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 116 | const char *fmt = "%s"; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 117 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 118 | opt = getopt32(argv, do_who ? "^" "aH" "\0" "=0": "^" "" "\0" "=0"); |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 119 | if ((opt & 2) || do_w) /* -H or we are w */ |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 120 | puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST"); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 121 | |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 122 | setutxent(); |
| 123 | while ((ut = getutxent()) != NULL) { |
Denys Vlasenko | 4c72104 | 2010-04-04 23:45:09 +0200 | [diff] [blame] | 124 | if (ut->ut_user[0] |
| 125 | && ((opt & 1) || ut->ut_type == USER_PROCESS) |
| 126 | ) { |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 127 | if (!do_users) { |
| 128 | char str6[6]; |
| 129 | char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1]; |
| 130 | struct stat st; |
| 131 | time_t seconds; |
Denys Vlasenko | 4c72104 | 2010-04-04 23:45:09 +0200 | [diff] [blame] | 132 | |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 133 | str6[0] = '?'; |
| 134 | str6[1] = '\0'; |
| 135 | strcpy(name, "/dev/"); |
| 136 | safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1, |
| 137 | ut->ut_line, |
| 138 | sizeof(ut->ut_line)+1 |
| 139 | ); |
| 140 | if (stat(name, &st) == 0) |
| 141 | idle_string(str6, st.st_atime); |
| 142 | /* manpages say ut_tv.tv_sec *is* time_t, |
| 143 | * but some systems have it wrong */ |
| 144 | seconds = ut->ut_tv.tv_sec; |
| 145 | /* How wide time field can be? |
| 146 | * "Nov 10 19:33:20": 15 chars |
| 147 | * "2010-11-10 19:33": 16 chars |
| 148 | */ |
| 149 | printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n", |
| 150 | (int)sizeof(ut->ut_user), ut->ut_user, |
| 151 | (int)sizeof(ut->ut_line), ut->ut_line, |
| 152 | str6, |
Denys Vlasenko | 1bc0bd1 | 2017-04-11 18:17:03 +0200 | [diff] [blame] | 153 | // TODO: with LANG=en_US.UTF-8, who from coreutils 8.25 shows |
| 154 | // TIME col as "2017-04-06 18:47" (the default format is "Apr 6 18:47"). |
| 155 | // The former format looks saner to me. Switch to it unconditionally? |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 156 | ctime(&seconds) + 4, |
| 157 | (int)sizeof(ut->ut_host), ut->ut_host |
| 158 | ); |
| 159 | } else { |
| 160 | printf(fmt, ut->ut_user); |
| 161 | fmt = " %s"; |
| 162 | } |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 165 | if (do_users) |
| 166 | bb_putchar('\n'); |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 167 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 168 | endutxent(); |
Bernhard Reutner-Fischer | e897988 | 2007-11-16 11:52:42 +0000 | [diff] [blame] | 169 | return EXIT_SUCCESS; |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 170 | } |