| 1 |
--- a/src/w.c |
| 2 |
+++ b/src/w.c |
| 3 |
@@ -229,6 +229,12 @@ |
| 4 |
#ifndef __CYGWIN__ |
| 5 |
int32_t ut_addr_v6[4]; /* IP address of the remote host */ |
| 6 |
|
| 7 |
+ if (!u) { |
| 8 |
+ /* Both systemd session and utmp not available */ |
| 9 |
+ print_host("", 0, fromlen); |
| 10 |
+ return; |
| 11 |
+ } |
| 12 |
+ |
| 13 |
if (ip_addresses) { /* -i switch used */ |
| 14 |
memcpy(&ut_addr_v6, &u->ut_addr_v6, sizeof(ut_addr_v6)); |
| 15 |
if (IN6_IS_ADDR_V4MAPPED(&ut_addr_v6)) { |
| 16 |
@@ -378,6 +384,32 @@ |
| 17 |
return -1; |
| 18 |
} |
| 19 |
|
| 20 |
+static struct pids_fetch *cache_pids(struct pids_info **info) |
| 21 |
+{ |
| 22 |
+ struct pids_fetch *reap; |
| 23 |
+ *info = NULL; |
| 24 |
+ |
| 25 |
+ enum pids_item items[] = { |
| 26 |
+ PIDS_ID_PID, |
| 27 |
+ PIDS_ID_TGID, |
| 28 |
+ PIDS_TICS_BEGAN, |
| 29 |
+ PIDS_ID_EUID, |
| 30 |
+ PIDS_ID_RUID, |
| 31 |
+ PIDS_ID_TPGID, |
| 32 |
+ PIDS_ID_PGRP, |
| 33 |
+ PIDS_TTY, |
| 34 |
+ PIDS_TICS_ALL, |
| 35 |
+ PIDS_CMDLINE}; |
| 36 |
+ |
| 37 |
+ if (procps_pids_new(info, items, 10) < 0) |
| 38 |
+ xerrx(EXIT_FAILURE, |
| 39 |
+ _("Unable to create pid info structure")); |
| 40 |
+ if ((reap = procps_pids_reap(*info, PIDS_FETCH_TASKS_ONLY)) == NULL) |
| 41 |
+ xerrx(EXIT_FAILURE, |
| 42 |
+ _("Unable to load process information")); |
| 43 |
+ return reap; |
| 44 |
+} |
| 45 |
+ |
| 46 |
/* |
| 47 |
* This function scans the process table accumulating total cpu |
| 48 |
* times for any processes "associated" with this login session. |
| 49 |
@@ -394,7 +426,8 @@ |
| 50 |
unsigned long long *restrict const jcpu, |
| 51 |
unsigned long long *restrict const pcpu, |
| 52 |
char *cmdline, |
| 53 |
- pid_t *pid) |
| 54 |
+ pid_t *pid, |
| 55 |
+ struct pids_fetch *reap) |
| 56 |
{ |
| 57 |
#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[i], info) |
| 58 |
#define PIDS_GETUNT(e) PIDS_VAL(EU_ ## e, u_int, reap->stacks[i], info) |
| 59 |
@@ -407,19 +440,7 @@ |
| 60 |
unsigned long long best_time = 0; |
| 61 |
unsigned long long secondbest_time = 0; |
| 62 |
|
| 63 |
- struct pids_info *info=NULL; |
| 64 |
- struct pids_fetch *reap; |
| 65 |
- enum pids_item items[] = { |
| 66 |
- PIDS_ID_PID, |
| 67 |
- PIDS_ID_TGID, |
| 68 |
- PIDS_TICS_BEGAN, |
| 69 |
- PIDS_ID_EUID, |
| 70 |
- PIDS_ID_RUID, |
| 71 |
- PIDS_ID_TPGID, |
| 72 |
- PIDS_ID_PGRP, |
| 73 |
- PIDS_TTY, |
| 74 |
- PIDS_TICS_ALL, |
| 75 |
- PIDS_CMDLINE}; |
| 76 |
+ /* Must match items in cache_pids */ |
| 77 |
enum rel_items { |
| 78 |
EU_PID, EU_TGID, EU_START, EU_EUID, EU_RUID, EU_TPGID, EU_PGRP, EU_TTY, |
| 79 |
EU_TICS_ALL, EU_CMDLINE}; |
| 80 |
@@ -448,12 +469,6 @@ |
| 81 |
|
| 82 |
line = get_tty_device(tty); |
| 83 |
|
| 84 |
- if (procps_pids_new(&info, items, 10) < 0) |
| 85 |
- xerrx(EXIT_FAILURE, |
| 86 |
- _("Unable to create pid info structure")); |
| 87 |
- if ((reap = procps_pids_reap(info, PIDS_FETCH_TASKS_ONLY)) == NULL) |
| 88 |
- xerrx(EXIT_FAILURE, |
| 89 |
- _("Unable to load process information")); |
| 90 |
total_procs = reap->counts->total; |
| 91 |
|
| 92 |
if (u) |
| 93 |
@@ -498,7 +513,6 @@ |
| 94 |
*pid = PIDS_GETULL(PID); |
| 95 |
*pcpu = PIDS_GETULL(TICS_ALL); |
| 96 |
} |
| 97 |
- procps_pids_unref(&info); |
| 98 |
return found_utpid; |
| 99 |
#undef PIDS_GETINT |
| 100 |
#undef PIDS_GETUNT |
| 101 |
@@ -511,7 +525,9 @@ |
| 102 |
const char *session, const char *name, |
| 103 |
utmp_t * u, const int longform, int maxcmd, int from, |
| 104 |
const int userlen, const int fromlen, const int ip_addresses, |
| 105 |
- const int pids) |
| 106 |
+ const int pids, |
| 107 |
+ struct pids_fetch *reap) |
| 108 |
+ |
| 109 |
{ |
| 110 |
unsigned long long jcpu, pcpu; |
| 111 |
unsigned i; |
| 112 |
@@ -554,7 +570,7 @@ |
| 113 |
#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 114 |
session, |
| 115 |
#endif |
| 116 |
- u, tty + 5, &jcpu, &pcpu, cmdline, &best_pid) == 0) |
| 117 |
+ u, tty + 5, &jcpu, &pcpu, cmdline, &best_pid, reap) == 0) |
| 118 |
/* |
| 119 |
* just skip if stale utmp entry (i.e. login proc doesn't |
| 120 |
* exist). If there is a desire a cmdline flag could be |
| 121 |
@@ -563,18 +579,14 @@ |
| 122 |
*/ |
| 123 |
return; |
| 124 |
|
| 125 |
-#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 126 |
if (name) |
| 127 |
strncpy(uname, name, UT_NAMESIZE); |
| 128 |
- else |
| 129 |
-#endif |
| 130 |
- strncpy(uname, u->ut_user, UT_NAMESIZE); |
| 131 |
/* force NUL term for printf */ |
| 132 |
uname[UT_NAMESIZE] = '\0'; |
| 133 |
|
| 134 |
printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, tty + 5); |
| 135 |
if (from) |
| 136 |
- print_from(session, NULL, ip_addresses, fromlen); |
| 137 |
+ print_from(session, u, ip_addresses, fromlen); |
| 138 |
|
| 139 |
/* login time */ |
| 140 |
if (longform) { |
| 141 |
@@ -657,7 +669,7 @@ |
| 142 |
|
| 143 |
int main(int argc, char **argv) |
| 144 |
{ |
| 145 |
- char *user = NULL, *p; |
| 146 |
+ char *match_user = NULL, *p; |
| 147 |
utmp_t *u; |
| 148 |
struct winsize win; |
| 149 |
int ch; |
| 150 |
@@ -672,6 +684,8 @@ |
| 151 |
int from = 1; |
| 152 |
int ip_addresses = 0; |
| 153 |
int pids = 0; |
| 154 |
+ struct pids_info *info = NULL; |
| 155 |
+ struct pids_fetch *pids_cache = NULL; |
| 156 |
|
| 157 |
enum { |
| 158 |
HELP_OPTION = CHAR_MAX + 1 |
| 159 |
@@ -737,7 +751,7 @@ |
| 160 |
} |
| 161 |
|
| 162 |
if ((argv[optind])) |
| 163 |
- user = (argv[optind]); |
| 164 |
+ match_user = (argv[optind]); |
| 165 |
|
| 166 |
/* Get user field length from environment */ |
| 167 |
if ((env_var = getenv("PROCPS_USERLEN")) != NULL) { |
| 168 |
@@ -775,6 +789,9 @@ |
| 169 |
CLAMP_CMD_WIDTH(maxcmd); |
| 170 |
#undef CLAMP_CMD_WIDTH |
| 171 |
|
| 172 |
+ if ( (pids_cache = cache_pids(&info)) == NULL) { |
| 173 |
+ return(EXIT_FAILURE); |
| 174 |
+ } |
| 175 |
|
| 176 |
if (header) { |
| 177 |
/* print uptime and headers */ |
| 178 |
@@ -806,16 +823,11 @@ |
| 179 |
|
| 180 |
if ((r = sd_session_get_username(sessions_list[i], &name)) < 0) |
| 181 |
error(EXIT_FAILURE, -r, _("get user name failed")); |
| 182 |
- |
| 183 |
- if (user) { |
| 184 |
- if (!strcmp(name, user)) |
| 185 |
- showinfo(sessions_list[i], name, NULL, longform, |
| 186 |
- maxcmd, from, userlen, fromlen, |
| 187 |
- ip_addresses, pids); |
| 188 |
- } else { |
| 189 |
+ if (!match_user || (0 == strcmp(name, match_user))) |
| 190 |
showinfo(sessions_list[i], name, NULL, longform, maxcmd, |
| 191 |
- from, userlen, fromlen, ip_addresses, pids); |
| 192 |
- } |
| 193 |
+ from, userlen, fromlen, ip_addresses, pids, |
| 194 |
+ pids_cache); |
| 195 |
+ |
| 196 |
free(name); |
| 197 |
free(sessions_list[i]); |
| 198 |
} |
| 199 |
@@ -829,40 +841,22 @@ |
| 200 |
utmpname(UTMP_FILE); |
| 201 |
setutent(); |
| 202 |
#endif |
| 203 |
- if (user) { |
| 204 |
- for (;;) { |
| 205 |
+ for (;;) { |
| 206 |
#ifdef HAVE_UTMPX_H |
| 207 |
- u = getutxent(); |
| 208 |
+ u = getutxent(); |
| 209 |
#else |
| 210 |
- u = getutent(); |
| 211 |
+ u = getutent(); |
| 212 |
#endif |
| 213 |
- if (!u) |
| 214 |
- break; |
| 215 |
- if (u->ut_type != USER_PROCESS) |
| 216 |
- continue; |
| 217 |
- if (!strncmp(u->ut_user, user, UT_NAMESIZE)) |
| 218 |
- showinfo( |
| 219 |
- NULL, NULL, |
| 220 |
- u, longform, maxcmd, from, userlen, |
| 221 |
- fromlen, ip_addresses, pids); |
| 222 |
- } |
| 223 |
- } else { |
| 224 |
- for (;;) { |
| 225 |
-#ifdef HAVE_UTMPX_H |
| 226 |
- u = getutxent(); |
| 227 |
-#else |
| 228 |
- u = getutent(); |
| 229 |
-#endif |
| 230 |
- if (!u) |
| 231 |
- break; |
| 232 |
- if (u->ut_type != USER_PROCESS) |
| 233 |
- continue; |
| 234 |
- if (*u->ut_user) |
| 235 |
- showinfo( |
| 236 |
- NULL, NULL, |
| 237 |
- u, longform, maxcmd, from, userlen, |
| 238 |
- fromlen, ip_addresses, pids); |
| 239 |
- } |
| 240 |
+ if (!u) |
| 241 |
+ break; |
| 242 |
+ if (u->ut_type != USER_PROCESS || (NULL == u->ut_user)) |
| 243 |
+ continue; |
| 244 |
+ if (!match_user || |
| 245 |
+ (0 == strncmp(u->ut_user, match_user, UT_NAMESIZE))) |
| 246 |
+ showinfo( |
| 247 |
+ NULL, u->ut_user, |
| 248 |
+ u, longform, maxcmd, from, userlen, |
| 249 |
+ fromlen, ip_addresses, pids, pids_cache); |
| 250 |
} |
| 251 |
#ifdef HAVE_UTMPX_H |
| 252 |
endutxent(); |