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
|
/* Hurdish w
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <paths.h>
#include <ctype.h>
#include <utmp.h>
#include <pwd.h>
#include <grp.h>
#include <netdb.h>
#include <sys/fcntl.h>
#include <argp.h>
#include <argz.h>
#include <envz.h>
#include <idvec.h>
#include <ps.h>
#include <timefmt.h>
#include <error.h>
#define DEFAULT_FMT_STRING "~USER ~TTY ~FROM ~LOGIN ~IDLE ~WHAT"
extern char *canon_host (char *host);
extern char *shared_domain (char *host1, char *host2);
extern char *localhost ();
/* Long options without corresponding short ones. -1 is EOF. */
#define OPT_SORT -4
#define OPT_FMT -5
#define OA OPTION_ARG_OPTIONAL
static struct argp_option options[] =
{
{"fmt", OPT_FMT, "FMT", 0, "Use the custom output-format FMT"},
{"no-header", 'H', 0, 0, "Don't print a descriptive header line"},
{0, 'h', 0, OPTION_ALIAS | OPTION_HIDDEN}, /* BSD compat */
{"reverse", 'r', 0, 0, "Reverse the order of any sort"},
{"sort", OPT_SORT,"FIELD",0, "Sort the output with respect to FIELD,"
" backwards if FIELD is prefixed by `-'"},
{0, 'i', 0, 0, "Sort output by idle time"},
{"tty", 't', "TTY", OA, "Only show entries for terminal TTY"},
{"width", 'w', "WIDTH",OA, "If WIDTH is given, try to format the"
" output for WIDTH columns, otherwise,"
" remove the default limit"},
{"uptime", 'u', 0, 0, "Only show the uptime and load info"},
{"no-uptime", 'U', 0, 0, "Don't show the uptime and load info"},
{"raw-hosts", 'n', 0, 0, "Show network addresses as numbers"},
{0, 0}
};
static char *args_doc = "[USER...]";
static char *doc = 0;
/* The current time of day. */
static struct timeval now;
/* True if we shouldn't attempt to show names for host addresses. */
static int raw_hosts = 0;
struct w_hook
{
struct utmp utmp;
struct timeval idle;
char *host; /* A malloced host name. */
};
#define W_PSTAT_USER (PSTAT_USER_BASE << 0)
#define W_PSTAT_HOST (PSTAT_USER_BASE << 1)
#define W_PSTAT_IDLE (PSTAT_USER_BASE << 2)
#define W_PSTAT_LOGIN (PSTAT_USER_BASE << 3)
static ps_flags_t
w_fetch (struct proc_stat *ps, ps_flags_t need, ps_flags_t have)
{
struct w_hook *hook = ps->hook;
if (! hook)
return 0; /* Can't do anything without w specific info.*/
if (need & W_PSTAT_HOST)
{
struct utmp *utmp = &hook->utmp;
if (utmp->ut_host[0])
{
/* UTMP->ut_host might not be '\0' terminated; this copy is. */
char ut_host[sizeof utmp->ut_host + 1];
strncpy (ut_host, utmp->ut_host, sizeof utmp->ut_host);
ut_host[sizeof utmp->ut_host] = '\0';
if (raw_hosts)
hook->host = strdup (ut_host);
else
{
char *sd;
hook->host = strdup (canon_host (ut_host) ?: ut_host);
sd = shared_domain (hook->host, localhost ());
if (sd)
*sd = '\0';
}
have |= W_PSTAT_HOST;
}
}
if (need & W_PSTAT_IDLE)
{
struct stat stat;
struct ps_tty *tty = ps->tty;
hook->idle.tv_usec = 0;
if (io_stat (tty->port, &stat) == 0)
{
hook->idle.tv_sec = now.tv_sec - stat.st_mtime;
if (hook->idle.tv_sec > 0)
have |= W_PSTAT_IDLE;
}
}
/* We can always return these. */
have |= (need & W_PSTAT_LOGIN);
return have;
}
static void
w_cleanup (struct proc_stat *ps)
{
if (ps->hook)
{
if (ps->flags & W_PSTAT_HOST)
free (((struct w_hook *)ps->hook)->host);
free (ps->hook);
}
}
static void
w_get_idle (struct proc_stat *ps, struct timeval *tv)
{
struct w_hook *hook = ps->hook;
*tv = hook->idle;
}
const struct ps_getter w_idle_getter =
{"idle_time", W_PSTAT_IDLE, w_get_idle};
static void
w_get_login (struct proc_stat *ps, struct timeval *tv)
{
struct w_hook *hook = ps->hook;
tv->tv_usec = 0;
tv->tv_sec = hook ? hook->utmp.ut_time : 0;
}
const struct ps_getter w_login_getter =
{"login_time", 0, w_get_login};
static void
w_get_user (struct proc_stat *ps, char **user, unsigned *user_len)
{
struct w_hook *hook = ps->hook;
*user = hook->utmp.ut_name;
*user_len = ((char *)memchr (*user, 0, UT_NAMESIZE) ?: *user) - *user;
}
const struct ps_getter w_user_getter =
{"user", 0, w_get_user};
static void
w_get_host (struct proc_stat *ps, char **host, unsigned *host_len)
{
struct w_hook *hook = ps->hook;
*host = hook->host;
*host_len = strlen (*host);
}
const struct ps_getter w_host_getter =
{"host", W_PSTAT_HOST, w_get_host};
extern error_t ps_emit_past_time (), ps_emit_string (), ps_emit_minutes ();
extern int ps_cmp_times (), ps_cmp_strings ();
const struct ps_fmt_spec _w_specs[] =
{
{"USER", 0, UT_NAMESIZE, &w_user_getter, ps_emit_string, ps_cmp_strings},
{"LOGIN", "LOGIN@", -7, &w_login_getter, ps_emit_past_time, ps_cmp_times},
{"FROM", 0, UT_HOSTSIZE, &w_host_getter, ps_emit_string, ps_cmp_strings},
{"IDLE", 0, -5, &w_idle_getter, ps_emit_minutes, ps_cmp_times},
{"WHAT=args"},
{0}
};
struct ps_fmt_specs w_specs = {_w_specs, &ps_std_fmt_specs};
/* Add to PROCS any processes in the foreground process group corresponding
to U, attaching a struct w_hook to which ever process is deemed the most
noteworthy. */
static void
add_utmp_procs (struct proc_stat_list *procs, struct utmp *u)
{
/* The tty name, with space for '\0' termination and an
appropiate prefix. */
char tty[sizeof _PATH_DEV + sizeof u->ut_line];
io_t tty_node;
error_t err;
pid_t pid;
int pos;
struct proc_stat *ps;
strncpy (tty, u->ut_line, sizeof u->ut_line);
tty[sizeof u->ut_line] = '\0'; /* Ensure it's '\0' terminated. */
if (*tty != '/')
/* Not an absolute path -- it must be in /dev, which insert. */
{
bcopy (tty, tty + sizeof _PATH_DEV - 1, strlen (tty) + 1);
bcopy (_PATH_DEV, tty, sizeof _PATH_DEV - 1);
}
/* Now find which process group is the in foreground for TTY. */
tty_node = file_name_lookup (tty, 0, 0);
if (tty_node == MACH_PORT_NULL)
{
error (0, errno, "%s", tty);
return;
}
err = io_get_owner (tty_node, &pid);
if (err)
{
error (0, err, "%s", tty);
return;
}
/* The new process will get added at the end, so look for it there. */
pos = proc_stat_list_num_procs (procs);
if (pid >= 0)
err = proc_stat_list_add_pid (procs, pid, &ps);
else
{
struct proc_stat **pgrp_procs;
unsigned num_procs;
err = proc_stat_list_add_pgrp (procs, -pid, &pgrp_procs, &num_procs);
if (! err)
{
if (num_procs > 0)
ps = pgrp_procs[0]; /* Use the first one. */
else
ps = 0;
free (pgrp_procs);
}
}
if (err)
{
error (0, err, "%s (owner %s %d)",
tty, pid < 0 ? "pgrp" : "pid", pid < 0 ? -pid : pid);
return;
}
if (ps)
{
ps->hook = malloc (sizeof (struct w_hook));
if (ps->hook)
bcopy (u, &((struct w_hook *)ps->hook)->utmp, sizeof *u);
}
else
error (0, 0, "%s (owner %s %d): No processes",
tty, pid < 0 ? "pgrp" : "pid", pid < 0 ? -pid : pid);
}
/* Read into PROCS from the utmp file called NAME. */
static void
read_utmp_procs (struct proc_stat_list *procs, char *name)
{
int rd, fd;
struct utmp buf[64];
fd = open (name, O_RDONLY);
if (fd < 0)
error (9, errno, "%s", name);
while ((rd = read (fd, buf, sizeof (buf))) > 0)
{
struct utmp *u = buf;
while (rd >= sizeof (*u))
{
if (*u->ut_name && *u->ut_line)
/* An active entry. */
add_utmp_procs (procs, u);
rd -= sizeof (*u);
u++;
}
}
close (fd);
}
static void
uptime (struct proc_stat_list *procs)
{
struct stat st;
char uptime_rep[20], tod_rep[20];
struct host_load_info *load;
unsigned nusers = 0;
int maybe_add_user (struct proc_stat *ps) { if (ps->hook) nusers++; return 0; }
proc_stat_list_for_each (procs, maybe_add_user);
/* Until we get a better way of finding out how long the system's been
up... XXX */
if (stat ("/var/run/uptime", &st) != 0)
strcpy (uptime_rep, "chuck");
else
{
struct timeval uptime = { now.tv_sec - st.st_ctime, 0 };
fmt_named_interval (&uptime, 0, uptime_rep, sizeof (uptime_rep));
}
strftime (tod_rep, sizeof (tod_rep), "%r", localtime (&now.tv_sec));
if (tod_rep[0] == '0')
tod_rep[0] = ' '; /* Get rid of bletcherous leading 0. */
errno = ps_host_load_info (&load);
if (errno)
error (0, errno, "ps_host_load_info");
printf ("%s up %s, %u user%s, load averages: %.2f, %.2f, %.2f\n",
tod_rep, uptime_rep, nusers, nusers == 1 ? "" : "s",
(double)load->avenrun[0] / (double)LOAD_SCALE,
(double)load->avenrun[1] / (double)LOAD_SCALE,
(double)load->avenrun[2] / (double)LOAD_SCALE);
}
extern void psout ();
void
main(int argc, char *argv[])
{
error_t err;
struct ps_context *context;
int output_width = -1;
char *fmt_string = DEFAULT_FMT_STRING, *sort_key_name = NULL;
int sort_reverse = 0, print_heading = 1, show_entries = 1, show_uptime = 1;
int squash_bogus_fields = 1, squash_nominal_fields = 1;
struct proc_stat_list *procs;
#if 0
char *tty_names = 0;
unsigned num_tty_names = 0;
struct idvec *only_uids = make_idvec (), *not_uids = make_idvec ();
#endif
struct ps_user_hooks ps_hooks = { 0, w_fetch, w_cleanup };
int has_hook (struct proc_stat *ps) { return ps->hook != 0; }
/* Parse our options... */
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'H': case 'h': print_heading = 0; break;
case 'i': sort_key_name = "idle"; break;
case OPT_SORT: sort_key_name = arg; break;
case OPT_FMT: fmt_string = arg; break;
case 'r': sort_reverse = 1; break;
case 'u': show_entries = 0; break;
case 'U': show_uptime = 0; break;
case 'n': raw_hosts = 1; break;
case ARGP_KEY_ARG:
break;
default: return EINVAL;
}
return 0;
}
struct argp argp = {options, parse_opt, args_doc, doc};
if (gettimeofday (&now, 0) < 0)
error (0, errno, "gettimeofday");
err = ps_context_create (getproc (), &context);
if (err)
error (2, err, "ps_context_create");
err = proc_stat_list_create (context, &procs);
if (err)
error (3, err, "proc_stat_list_create");
context->user_hooks = &ps_hooks;
/* Parse our options. */
argp_parse (&argp, argc, argv, 0, 0, 0);
read_utmp_procs (procs, _PATH_UTMP);
/* Keep only processes that have our hooks attached. */
proc_stat_list_filter1 (procs, has_hook, 0, 0);
if (show_uptime)
uptime (procs);
if (show_entries)
psout (procs, fmt_string, &w_specs, sort_key_name, sort_reverse,
output_width, print_heading,
squash_bogus_fields, squash_nominal_fields);
exit(0);
}
|