Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 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. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 16 | * |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 17 | *---------------------------------------------------------------------- |
| 18 | */ |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 19 | //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 Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 25 | //config: |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 26 | //config:config USERS |
| 27 | //config: bool "users" |
| 28 | //config: default y |
| 29 | //config: depends on FEATURE_UTMP |
| 30 | //config: help |
| 31 | //config: Print users currently logged on. |
| 32 | |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 33 | // APPLET_ODDNAME:name main location suid_type help |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 34 | //applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users)) |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 35 | //applet:IF_WHO(APPLET(who, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 36 | |
| 37 | //kbuild:lib-$(CONFIG_USERS) += who.o |
| 38 | //kbuild:lib-$(CONFIG_WHO) += who.o |
| 39 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 40 | /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */ |
| 41 | |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 42 | //usage:#define users_trivial_usage |
| 43 | //usage: "" |
| 44 | //usage:#define users_full_usage "\n\n" |
| 45 | //usage: "Print the users currently logged on" |
| 46 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 47 | //usage:#define who_trivial_usage |
| 48 | //usage: "[-a]" |
| 49 | //usage:#define who_full_usage "\n\n" |
| 50 | //usage: "Show who is logged on\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 51 | //usage: "\n -a Show all" |
Denys Vlasenko | b110e1f | 2012-04-18 14:38:15 +0200 | [diff] [blame] | 52 | //usage: "\n -H Print column headers" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 53 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 54 | #include "libbb.h" |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 56 | static void idle_string(char *str6, time_t t) |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 57 | { |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 58 | t = time(NULL) - t; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 60 | /*if (t < 60) { |
| 61 | str6[0] = '.'; |
| 62 | str6[1] = '\0'; |
| 63 | return; |
| 64 | }*/ |
| 65 | if (t >= 0 && t < (24 * 60 * 60)) { |
| 66 | sprintf(str6, "%02d:%02d", |
| 67 | (int) (t / (60 * 60)), |
| 68 | (int) ((t % (60 * 60)) / 60)); |
| 69 | return; |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 71 | strcpy(str6, "old"); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 74 | int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 75 | int who_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 76 | { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 77 | struct utmpx *ut; |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 78 | unsigned opt; |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 79 | int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u')); |
| 80 | const char *fmt = "%s"; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 82 | opt_complementary = "=0"; |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 83 | opt = getopt32(argv, do_users ? "" : "aH"); |
Denys Vlasenko | 4c72104 | 2010-04-04 23:45:09 +0200 | [diff] [blame] | 84 | if (opt & 2) // -H |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 85 | puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST"); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 86 | |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 87 | setutxent(); |
| 88 | while ((ut = getutxent()) != NULL) { |
Denys Vlasenko | 4c72104 | 2010-04-04 23:45:09 +0200 | [diff] [blame] | 89 | if (ut->ut_user[0] |
| 90 | && ((opt & 1) || ut->ut_type == USER_PROCESS) |
| 91 | ) { |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 92 | if (!do_users) { |
| 93 | char str6[6]; |
| 94 | char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1]; |
| 95 | struct stat st; |
| 96 | time_t seconds; |
Denys Vlasenko | 4c72104 | 2010-04-04 23:45:09 +0200 | [diff] [blame] | 97 | |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 98 | str6[0] = '?'; |
| 99 | str6[1] = '\0'; |
| 100 | strcpy(name, "/dev/"); |
| 101 | safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1, |
| 102 | ut->ut_line, |
| 103 | sizeof(ut->ut_line)+1 |
| 104 | ); |
| 105 | if (stat(name, &st) == 0) |
| 106 | idle_string(str6, st.st_atime); |
| 107 | /* manpages say ut_tv.tv_sec *is* time_t, |
| 108 | * but some systems have it wrong */ |
| 109 | seconds = ut->ut_tv.tv_sec; |
| 110 | /* How wide time field can be? |
| 111 | * "Nov 10 19:33:20": 15 chars |
| 112 | * "2010-11-10 19:33": 16 chars |
| 113 | */ |
| 114 | printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n", |
| 115 | (int)sizeof(ut->ut_user), ut->ut_user, |
| 116 | (int)sizeof(ut->ut_line), ut->ut_line, |
| 117 | str6, |
| 118 | ctime(&seconds) + 4, |
| 119 | (int)sizeof(ut->ut_host), ut->ut_host |
| 120 | ); |
| 121 | } else { |
| 122 | printf(fmt, ut->ut_user); |
| 123 | fmt = " %s"; |
| 124 | } |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Tito Ragusa | 7926b98 | 2011-08-09 04:37:50 +0200 | [diff] [blame] | 127 | if (do_users) |
| 128 | bb_putchar('\n'); |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 129 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 130 | endutxent(); |
Bernhard Reutner-Fischer | e897988 | 2007-11-16 11:52:42 +0000 | [diff] [blame] | 131 | return EXIT_SUCCESS; |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 132 | } |