forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_who.c
576 lines (570 loc) · 16.5 KB
/
kernel_who.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <config.h>
#include <getopt.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include "system.h"
#include "c-ctype.h"
#include "canon-host.h"
#include "readutmp.h"
#include "die.h"
#include "error.h"
#include "hard-locale.h"
#include "quote.h"
#ifdef TTY_GROUP_NAME
# include <grp.h>
#endif
#define PROGRAM_NAME "who"
#ifdef RUN_LVL
# define UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
#else
# define UT_TYPE_RUN_LVL(U) false
#endif
#ifdef INIT_PROCESS
# define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
#else
# define UT_TYPE_INIT_PROCESS(U) false
#endif
#ifdef LOGIN_PROCESS
# define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
#else
# define UT_TYPE_LOGIN_PROCESS(U) false
#endif
#ifdef DEAD_PROCESS
# define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
#else
# define UT_TYPE_DEAD_PROCESS(U) false
#endif
#ifdef NEW_TIME
# define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
#else
# define UT_TYPE_NEW_TIME(U) false
#endif
#define IDLESTR_LEN 6
#if HAVE_STRUCT_XTMP_UT_PID
# define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
#else
# define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
char const *Var = ""
#endif
#if HAVE_STRUCT_XTMP_UT_ID
# define UT_ID(U) ((U)->ut_id)
#else
# define UT_ID(U) "??"
#endif
static bool do_lookup;
static bool short_list;
static bool short_output;
static bool include_idle;
static bool include_heading;
static bool include_mesg;
static bool include_exit;
static bool need_boottime;
static bool need_deadprocs;
static bool need_login;
static bool need_initspawn;
static bool need_clockchange;
static bool need_runlevel;
static bool need_users;
static bool my_line_only;
static char const *time_format;
static int time_format_width;
enum
{
LOOKUP_OPTION = CHAR_MAX + 1
};
static struct option const longopts[] =
{
{"all", no_argument, NULL, 'a'},
{"boot", no_argument, NULL, 'b'},
{"count", no_argument, NULL, 'q'},
{"dead", no_argument, NULL, 'd'},
{"heading", no_argument, NULL, 'H'},
{"login", no_argument, NULL, 'l'},
{"lookup", no_argument, NULL, LOOKUP_OPTION},
{"message", no_argument, NULL, 'T'},
{"mesg", no_argument, NULL, 'T'},
{"process", no_argument, NULL, 'p'},
{"runlevel", no_argument, NULL, 'r'},
{"short", no_argument, NULL, 's'},
{"time", no_argument, NULL, 't'},
{"users", no_argument, NULL, 'u'},
{"writable", no_argument, NULL, 'T'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
static char const *idle_string (time_t when, time_t boottime)
{
static time_t now = TYPE_MINIMUM (time_t);
if (now == TYPE_MINIMUM (time_t))
time (&now);
if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
{
int seconds_idle = now - when;
if (seconds_idle < 60)
return " . ";
else
{
static char idle_hhmm[IDLESTR_LEN];
assert (seconds_idle / (60 * 60) < 24);
sprintf (idle_hhmm, "%02d:%02d", seconds_idle / (60 * 60), (seconds_idle % (60 * 60)) / 60);
return idle_hhmm;
}
}
return _(" old ");
}
static char const *time_string (const STRUCT_UTMP *utmp_ent)
{
static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
time_t t = UT_TIME_MEMBER (utmp_ent);
struct tm *tmp = localtime (&t);
if (tmp)
{
strftime (buf, sizeof buf, time_format, tmp);
return buf;
}
else
return timetostr (t, buf);
}
static void print_line (int userlen, char const *user, const char state, int linelen, char const *line, char const *time_str, char const *idle, char const *pid, char const *comment, char const *exitstr)
{
static char mesg[3] = { ' ', 'x', '\0' };
char *buf;
char x_idle[1 + IDLESTR_LEN + 1];
char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
char *x_exitstr;
int err;
mesg[1] = state;
if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
sprintf (x_idle, " %-6s", idle);
else
*x_idle = '\0';
if (!short_output && strlen (pid) < sizeof x_pid - 1)
sprintf (x_pid, " %10s", pid);
else
*x_pid = '\0';
x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
if (include_exit)
sprintf (x_exitstr, " %-12s", exitstr);
else
*x_exitstr = '\0';
err = asprintf (&buf,
"%-8.*s"
"%s"
" %-12.*s"
" %-*s"
"%s"
"%s"
" %-8s"
"%s"
,
userlen, user ? user : " .", include_mesg ? mesg : "", linelen, line, time_format_width, time_str, x_idle, x_pid, comment, x_exitstr);
if (err == -1)
xalloc_die ();
{
char *p = buf + strlen (buf);
while (*--p == ' ')
;
*(p + 1) = '\0';
}
puts (buf);
free (buf);
free (x_exitstr);
}
static bool
is_tty_writable (struct stat const *pstat)
{
#ifdef TTY_GROUP_NAME
struct group *ttygr = getgrnam (TTY_GROUP_NAME);
if (!ttygr || (pstat->st_gid != ttygr->gr_gid))
return false;
#endif
return pstat->st_mode & S_IWGRP;
}
static void
print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
{
struct stat stats;
time_t last_change;
char mesg;
char idlestr[IDLESTR_LEN + 1];
static char *hoststr;
#if HAVE_UT_HOST
static size_t hostlen;
#endif
#define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
#define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
char *p = line;
PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
stzncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
if (stat (line, &stats) == 0)
{
mesg = is_tty_writable (&stats) ? '+' : '-';
last_change = stats.st_atime;
}
else
{
mesg = '?';
last_change = 0;
}
if (last_change)
sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
else
sprintf (idlestr, " ?");
#if HAVE_UT_HOST
if (utmp_ent->ut_host[0])
{
char ut_host[sizeof (utmp_ent->ut_host) + 1];
char *host = NULL;
char *display = NULL;
stzncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
display = strchr (ut_host, ':');
if (display)
*display++ = '\0';
if (*ut_host && do_lookup)
{
host = canon_host (ut_host);
}
if (! host)
host = ut_host;
if (display)
{
if (hostlen < strlen (host) + strlen (display) + 4)
{
hostlen = strlen (host) + strlen (display) + 4;
free (hoststr);
hoststr = xmalloc (hostlen);
}
sprintf (hoststr, "(%s:%s)", host, display);
}
else
{
if (hostlen < strlen (host) + 3)
{
hostlen = strlen (host) + 3;
free (hoststr);
hoststr = xmalloc (hostlen);
}
sprintf (hoststr, "(%s)", host);
}
if (host != ut_host)
free (host);
}
else
{
if (hostlen < 1)
{
hostlen = 1;
free (hoststr);
hoststr = xmalloc (hostlen);
}
*hoststr = '\0';
}
#endif
print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg, sizeof utmp_ent->ut_line, utmp_ent->ut_line, time_string (utmp_ent), idlestr, pidstr, hoststr ? hoststr : "", "");
}
static void print_boottime (const STRUCT_UTMP *utmp_ent)
{
print_line (-1, "", ' ', -1, _("system boot"), time_string (utmp_ent), "", "", "", "");
}
static char *make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
{
size_t utmpsize = sizeof UT_ID (utmp_ent);
char *comment = xmalloc (strlen (_("id=")) + utmpsize + 1);
char *p = stpcpy (comment, _("id="));
stzncpy (p, UT_ID (utmp_ent), utmpsize);
return comment;
}
static void print_deadprocs (const STRUCT_UTMP *utmp_ent)
{
static char *exitstr;
char *comment = make_id_equals_comment (utmp_ent);
PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
if (!exitstr)
exitstr = xmalloc (strlen (_("term=")) + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1 + strlen (_("exit=")) + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent)) + 1);
sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent), _("exit="), UT_EXIT_E_EXIT (utmp_ent));
print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line, time_string (utmp_ent), "", pidstr, comment, exitstr);
free (comment);
}
static void print_login (const STRUCT_UTMP *utmp_ent)
{
char *comment = make_id_equals_comment (utmp_ent);
PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
print_line (-1, _("LOGIN"), ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line, time_string (utmp_ent), "", pidstr, comment, "");
free (comment);
}
static void print_initspawn (const STRUCT_UTMP *utmp_ent)
{
char *comment = make_id_equals_comment (utmp_ent);
PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line, time_string (utmp_ent), "", pidstr, comment, "");
free (comment);
}
static void print_clockchange (const STRUCT_UTMP *utmp_ent)
{
print_line (-1, "", ' ', -1, _("clock change"), time_string (utmp_ent), "", "", "", "");
}
static void print_runlevel (const STRUCT_UTMP *utmp_ent)
{
static char *runlevline, *comment;
unsigned char last = UT_PID (utmp_ent) / 256;
unsigned char curr = UT_PID (utmp_ent) % 256;
if (!runlevline)
runlevline = xmalloc (strlen (_("run-level")) + 3);
sprintf (runlevline, "%s %c", _("run-level"), curr);
if (!comment)
comment = xmalloc (strlen (_("last=")) + 2);
sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent), "", "", c_isprint (last) ? comment : "", "");
return;
}
static void list_entries_who (size_t n, const STRUCT_UTMP *utmp_buf)
{
unsigned long int entries = 0;
char const *separator = "";
while (n--)
{
if (IS_USER_PROCESS (utmp_buf))
{
char *trimmed_name;
trimmed_name = extract_trimmed_name (utmp_buf);
printf ("%s%s", separator, trimmed_name);
free (trimmed_name);
separator = " ";
entries++;
}
utmp_buf++;
}
printf (_("\n# users=%lu\n"), entries);
}
static void print_heading (void)
{
print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"), _("PID"), _("COMMENT"), _("EXIT"));
}
static void scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
{
char *ttyname_b IF_LINT ( = NULL);
time_t boottime = TYPE_MINIMUM (time_t);
if (include_heading)
print_heading ();
if (my_line_only)
{
ttyname_b = ttyname (STDIN_FILENO);
if (!ttyname_b)
return;
if (STRNCMP_LIT (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH) == 0)
ttyname_b += DEV_DIR_LEN;
}
while (n--)
{
if (!my_line_only
|| STREQ_LEN (ttyname_b, utmp_buf->ut_line, sizeof (utmp_buf->ut_line)))
{
if (need_users && IS_USER_PROCESS (utmp_buf))
print_user (utmp_buf, boottime);
else if (need_runlevel && UT_TYPE_RUN_LVL (utmp_buf))
print_runlevel (utmp_buf);
else if (need_boottime && UT_TYPE_BOOT_TIME (utmp_buf))
print_boottime (utmp_buf);
else if (need_clockchange && UT_TYPE_NEW_TIME (utmp_buf))
print_clockchange (utmp_buf);
else if (need_initspawn && UT_TYPE_INIT_PROCESS (utmp_buf))
print_initspawn (utmp_buf);
else if (need_login && UT_TYPE_LOGIN_PROCESS (utmp_buf))
print_login (utmp_buf);
else if (need_deadprocs && UT_TYPE_DEAD_PROCESS (utmp_buf))
print_deadprocs (utmp_buf);
}
if (UT_TYPE_BOOT_TIME (utmp_buf))
boottime = UT_TIME_MEMBER (utmp_buf);
utmp_buf++;
}
}
static void who (char const *filename, int options)
{
size_t n_users;
STRUCT_UTMP *utmp_buf;
if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
die (EXIT_FAILURE, errno, "%s", quotef (filename));
if (short_list)
list_entries_who (n_users, utmp_buf);
else
scan_entries (n_users, utmp_buf);
free (utmp_buf);
}
void usage (int status)
{
if (status != EXIT_SUCCESS)
emit_try_help ();
else
{
printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
fputs (_("\
Print information about users who are currently logged in.\n\
"), stdout);
fputs (_("\
\n\
-a, --all same as -b -d --login -p -r -t -T -u\n\
-b, --boot time of last system boot\n\
-d, --dead print dead processes\n\
-H, --heading print line of column headings\n\
"), stdout);
fputs (_("\
-l, --login print system login processes\n\
"), stdout);
fputs (_("\
--lookup attempt to canonicalize hostnames via DNS\n\
-m only hostname and user associated with stdin\n\
-p, --process print active processes spawned by init\n\
"), stdout);
fputs (_("\
-q, --count all login names and number of users logged on\n\
-r, --runlevel print current runlevel\n\
-s, --short print only name, line, and time (default)\n\
-t, --time print last system clock change\n\
"), stdout);
fputs (_("\
-T, -w, --mesg add user's message status as +, - or ?\n\
-u, --users list users logged in\n\
--message same as -T\n\
--writable same as -T\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\
\n\
If FILE is not specified, use %s. %s as FILE is common.\n\
If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual.\n\
"), UTMP_FILE, WTMP_FILE);
emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
int main (int argc, char **argv)
{
int optc;
bool assumptions = true;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
while ((optc = getopt_long (argc, argv, "abdlmpqrstuwHT", longopts, NULL)) != -1)
{
switch (optc)
{
case 'a':
need_boottime = true;
need_deadprocs = true;
need_login = true;
need_initspawn = true;
need_runlevel = true;
need_clockchange = true;
need_users = true;
include_mesg = true;
include_idle = true;
include_exit = true;
assumptions = false;
break;
case 'b':
need_boottime = true;
assumptions = false;
break;
case 'd':
need_deadprocs = true;
include_idle = true;
include_exit = true;
assumptions = false;
break;
case 'H':
include_heading = true;
break;
case 'l':
need_login = true;
include_idle = true;
assumptions = false;
break;
case 'm':
my_line_only = true;
break;
case 'p':
need_initspawn = true;
assumptions = false;
break;
case 'q':
short_list = true;
break;
case 'r':
need_runlevel = true;
include_idle = true;
assumptions = false;
break;
case 's':
short_output = true;
break;
case 't':
need_clockchange = true;
assumptions = false;
break;
case 'T':
case 'w':
include_mesg = true;
break;
case 'u':
need_users = true;
include_idle = true;
assumptions = false;
break;
case LOOKUP_OPTION:
do_lookup = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (assumptions)
{
need_users = true;
short_output = true;
}
if (include_exit)
{
short_output = false;
}
if (hard_locale (LC_TIME))
{
time_format = "%Y-%m-%d %H:%M";
time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
}
else
{
time_format = "%b %e %H:%M";
time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
}
switch (argc - optind)
{
case 2:
my_line_only = true;
FALLTHROUGH;
case -1:
case 0:
who (UTMP_FILE, READ_UTMP_CHECK_PIDS);
break;
case 1:
who (argv[optind], 0);
break;
default:
error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
usage (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}