summaryrefslogtreecommitdiff
path: root/utils/ps.c
blob: da2a02fe67fb1804f3bf11578c1176662344498e (plain)
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
/* Show process information.

   Copyright (C) 1995 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 <assert.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>

#include "error.h"
#include "ps.h"
#include "common.h"

/* ---------------------------------------------------------------- */

static void
usage(status)
     int status;
{
  if (status != 0)
    fprintf(stderr, "Try `%s --help' for more information.\n",
	    program_invocation_short_name);
  else
    {
      printf("Usage: %s [OPTION...] [PID...]\n",
	     program_invocation_short_name);
      printf("\
\n\
  -a, --all-users            List other users' processes\n\
      --fmt=FMT              Use the output-format FMT\n\
			     FMT may be `default', `user', `vmem', `long',\n\
                             `hurd', `hurd-long', or a format-string\n\
  -g                         Include session leaders\n\
  -H, --no-header            Don't print a descriptive header line\n\
  -l                         Use the `long' output-format'\n\
      --login[=LID]	     Add the processes from the login collection LID\n\
                             (which defaults that of the current process)\n
  -P, --no-parent	     Include processes without parents\n\
  -Q, --all-fields	     Don't elide unusable fields (normally if there's\n\
                             some reason ps can't print a field for any\n\
                             process, it's removed from the output entirely)\n\
  -r, --reverse              Reverse the order of any sort\n\
      --session[=SID]	     Add the processes from the session SID (which\n\
                             defaults to the sid of the current process)\n
      --sort=FIELD           Sort the output with respect to FIELD\n\
  -t, --threads		     Show the threads for each process\n\
  -u                         Use the `user' output-format'\n\
  -v                         Use the `vmem' output-format'\n\
  -x                         Include orphaned processes\n\
");
    }

  exit(status);
}

int 
enum_name(char *arg, char **choices, char *kind, int allow_mismatches)
{
  int arglen = strlen(arg);
  char **p = choices;
  int partial_match = -1;

  while (*p != NULL)
    if (strcmp(*p, arg) == 0)
      return p - choices;
    else
      {
	if (strncmp(*p, arg, arglen) == 0)
	  if (partial_match >= 0)
	    {
	      fprintf(stderr, "%s: ambiguous %s", arg, kind);
	      usage(1);
	    }
	  else
	    partial_match = p - choices;
	p++;
      }

  if (partial_match >= 0 || allow_mismatches)
    return partial_match;
  else
    {
      fprintf(stderr, "%s: invalid %s", arg, kind);
      usage(1);
    }
}

/* ---------------------------------------------------------------- */

#define FILTER_OWN		0x01
#define FILTER_NSESSLDR		0x02
#define FILTER_CTTY    		0x04
#define FILTER_UNORPHANED	0x08
#define FILTER_PARENTED		0x10

enum procsets
{
  PROCSET_ALL, PROCSET_SESSION, PROCSET_LOGIN
};
char *procset_names[] =
{"all", "session", "login", 0};

/* -1 is EOF */
#define OPT_LOGIN	-2
#define OPT_SESSION	-3
#define OPT_SORT	-4
#define OPT_FMT		-5
#define OPT_HELP	-6

#define SHORT_OPTIONS "agHlPQrtuvx"

static struct option options[] =
{
  {"all-users", no_argument, 0, 'a'},
  {"all-fields", no_argument, 0, 'Q'},
  {"fmt", required_argument, 0, OPT_FMT},
  {"help", no_argument, 0, OPT_HELP},
  {"login", optional_argument, 0, OPT_LOGIN},
  {"no-header", no_argument, 0, 'H'},
  {"no-squash", no_argument, 0, 'Q'},
  {"no-parent", no_argument, 0, 'P'},
  {"session", optional_argument, 0, OPT_SESSION},
  {"threads", no_argument, 0, 't'},
  {"reverse", no_argument, 0, 'r'},
  {"sort", required_argument, 0, OPT_SORT},
  {0, 0, 0, 0}
};

/* The names of various predefined output formats.  */
char *fmt_names[] =
  {"default", "user", "vmem", "long", "hurd", "hurd-long", 0};
/* How each of those formats should be sorted; */
char *fmt_sortkeys[] =
  {"pid", "%cpu", "%mem", "pid", "pid", "pid"};
/* and whether the sort should be backwards or forwards; */ 
bool fmt_sortrev[] =
  {FALSE, TRUE, TRUE, FALSE, FALSE, FALSE};
/* and the actual format strings.  */
char *fmts[] =
{
  "~PID ~TH# ~TT=tty ~STAT=state ~TIME ~COMMAND=args",
  "~USER ~PID ~TH# ~%CPU ~%MEM ~SZ=vsize ~RSS=rsize ~TT=tty ~STAT=state ~COMMAND=args",
  "~PID ~TH# ~STAT=state ~SL=sleep ~PAGEIN=pgins ~FAULTS=pgflts ~COWFLT=cowflts ~ZFILLS ~SIZE=vsize ~RSS=rsize ~%CPU ~%MEM ~COMMAND=args",
  "~UID ~PID ~TH# ~PPID ~PRI ~NI=bpri ~TH=nth ~MSGIN=msgsin ~MSGOUT=msgsout ~SZ=vsize ~RSS=rsize ~STAT=state ~TT=tty ~TIME ~COMMAND=args",
  "~PID ~Th# ~UID ~NTh ~VMem=vsize ~RSS=rsize ~User=utime ~System=stime ~Args",
  "~PID ~Th# ~UID ~PPID ~PGRP ~Sess ~NTh ~VMem=vsize ~RSS=rsize ~%CPU ~User=utime ~System=stime ~Args"
};

void 
main(int argc, char *argv[])
{
  int opt;
  error_t err;
  ps_fmt_t fmt;
  proc_stat_list_t procset;
  process_t cur_proc = getproc();
  int cur_pid = getpid();
  char *fmt_string = "default", *sort_key_name = NULL;
  int filter_mask =
    FILTER_OWN | FILTER_NSESSLDR | FILTER_UNORPHANED | FILTER_PARENTED;
  bool sort_reverse = FALSE, print_heading = TRUE, squash_bogus_fields = TRUE;
  bool show_threads = FALSE;

  program_invocation_short_name = argv[0];

  err = proc_stat_list_create(cur_proc, &procset);
  if (err)
    error(1, err, "proc_stat_list_create");

  while ((opt = getopt_long(argc, argv, SHORT_OPTIONS, options, 0)) != EOF)
    switch (opt)
      {
      case 'a':
	filter_mask &= ~FILTER_OWN; break;
      case 'g':
	filter_mask &= ~FILTER_NSESSLDR; break;
      case 'x':
	filter_mask &= ~FILTER_UNORPHANED; break;
      case 'P':
	filter_mask &= ~FILTER_PARENTED; break;
      case 'u':
	fmt_string = "user"; break;
      case 'v':
	fmt_string = "vmem"; break;
      case 'l':
	fmt_string = "long"; break;
      case 'H':
	print_heading = 0; break;
      case 'Q':
	squash_bogus_fields = FALSE; break;
      case 't':
	show_threads = TRUE; break;
      case OPT_FMT:
	fmt_string = optarg; break;
      case OPT_SORT:
	sort_key_name = optarg; break;
      case 'r':
	sort_reverse = TRUE; break;
      case OPT_SESSION:
	{
	  int sid = (optarg == NULL ? -1 : atoi(optarg));

	  if (sid < 0) {
	    /* Get the current session; a bit of a pain */
	    err = proc_getsid(cur_proc, cur_pid, &sid);
	    if (err)
	      error(2, err, "Couldn't get current session id");
	  }

	  err = proc_stat_list_add_session(procset, sid);
	  if (err)
	    error(2, err, "Couldn't add session pids");
	}
	break;
      case OPT_LOGIN:
	{
	  int login_coll = (optarg == NULL ? -1 : atoi(optarg));

	  if (login_coll < 0) {
	    err = proc_getloginid(cur_proc, cur_pid, &login_coll);
	    if (err)
	      error(2, err, "Couldn't get current login collection");
	  }

	  err = proc_stat_list_add_login_coll(procset, login_coll);
	  if (err)
	    error(2, err, "Couldn't add login collection pids");
	}
	break;
      case OPT_HELP:
	usage(0);
      default:
	usage(1);
      }

  {
    int fmt_index = enum_name(fmt_string, fmt_names, "format type", 1);
    if (fmt_index >= 0)
      {
	fmt_string = fmts[fmt_index];
	if (sort_key_name == NULL)
	  {
	    sort_key_name = fmt_sortkeys[fmt_index];
	    sort_reverse = fmt_sortrev[fmt_index];
	  }
      }
  }

  while (argv[optind] != NULL)
    {
      int pid = atoi(argv[optind]);

      if (pid == 0 && strcmp(argv[optind], "0") != 0)
	error(3, 0, "%s: invalid process id", argv[optind]);

      err = proc_stat_list_add_pid(procset, pid);
      if (err)
	error(3, err, "%d: can't add process id", pid);

      optind++;
    }

  if (proc_stat_list_num_procs(procset) == 0)
    {
      err = proc_stat_list_add_all(procset);
      if (err)
	error(2, err, "Couldn't get process list");
    }

  if (filter_mask & FILTER_OWN)
    proc_stat_list_filter(procset, &ps_own_filter, FALSE);
  if (filter_mask & FILTER_NSESSLDR)
    proc_stat_list_filter(procset, &ps_not_sess_leader_filter, FALSE);
  if (filter_mask & FILTER_UNORPHANED)
    proc_stat_list_filter(procset, &ps_unorphaned_filter, FALSE);
  if (filter_mask & FILTER_PARENTED)
    proc_stat_list_filter(procset, &ps_parent_filter, FALSE);

  err = ps_fmt_create(fmt_string, ps_std_fmt_specs, &fmt);
  if (err)
    error(4, err, "Can't create output format");

  if (show_threads)
    proc_stat_list_add_threads(procset);

  if (sort_key_name != NULL)
    /* Sort on the given field; we look in both the user-named fields and
       the system named fields for the name given, as the user may only know
       the printed title and not the official name. */
    {
      ps_fmt_spec_t sort_key = NULL;
      int nfields = ps_fmt_num_fields(fmt);
      ps_fmt_field_t field = ps_fmt_fields(fmt);

      /* first, look at the actual printed titles in the current format */
      while (nfields-- > 0)
	if (strcasecmp(sort_key_name, ps_fmt_field_title(field)) == 0)
	  {
	    sort_key = ps_fmt_field_fmt_spec(field);
	    break;
	  }
	else
	  field++;

      /* Then, if not there, look at the actual ps_fmt_spec names (this way
	 the user can sort on a key that's not actually displayed). */
      if (sort_key == NULL)
	{
	  sort_key = find_ps_fmt_spec(sort_key_name, ps_std_fmt_specs);
	  if (sort_key == NULL)
	    error(3, 0, "%s: bad sort key", sort_key_name);
	}

      err = proc_stat_list_sort(procset, sort_key, sort_reverse);
      if (err)
	/* Give an error message, but don't exit.  */
	error(0, err, "Couldn't sort processes");
    }

  if (squash_bogus_fields)
    /* Remove any fields that we can't print anyway (because of system
       bugs/protection violations?).  */
    {
      int bogus_flags = ps_fmt_needs(fmt);

      err = proc_stat_list_find_bogus_flags(procset, &bogus_flags);
      if (err)
	error(0, err, "Couldn't remove bogus fields");
      else
	ps_fmt_squash(fmt, bogus_flags);
    }

  if (print_heading)
    {
      err = ps_fmt_write_titles(fmt, stdout, NULL);
      if (err)
	error(0, err, "Can't print titles");
      putc('\n', stdout);
    }

  err = proc_stat_list_fmt(procset, fmt, stdout, NULL);
  if (err)
    error(5, err, "Couldn't output process status");

  exit(0);
}