1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1992-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
23 #define FORMAT "PID=%(pid)d PPID=%(ppid)d PGID=%(pgid)d TID=%(tid)d SID=%(sid)d"
25 static const char usage
[] =
26 "[-?\n@(#)$Id: pids (AT&T Research) 2008-04-01 $\n]"
28 "[+NAME?pids - list calling shell process ids]"
29 "[+DESCRIPTION?When invoked as a shell builtin, \bpids\b lists one or "
30 "more of the calling process ids determined by \bgetpid\b(2), "
31 "\bgetppid\b(2), \bgetpgrp\b(2), \btcgetpgrp\b(2) and \bgetsid\b(2). "
32 "Unknown or invalid ids have the value \b-1\b.]"
33 "[f:format?List the ids specified by \aformat\a. \aformat\a follows "
34 "\bprintf\b(3) conventions, except that \bsfio\b(3) inline ids are used "
35 "instead of arguments: "
36 "%[-+]][\awidth\a[.\aprecis\a[.\abase\a]]]]]](\aid\a)\achar\a. The "
37 "supported \aid\as are:]:[format:=" FORMAT
"]"
39 "[+pid?The process id.]"
40 "[+pgid?The process group id.]"
41 "[+ppid?The parent process id.]"
42 "[+tid|tty?The controlling terminal id.]"
43 "[+sid?The session id.]"
45 "[+SEE ALSO?\bgetpid\b(2), \bgetppid\b(2), \bgetpgrp\b(2), "
46 "\btcgetpgrp\b(2), \bgetsid\b(2)]"
54 * sfkeyprintf() lookup
55 * handle==0 for heading
59 key(void* handle
, Sffmt_t
* fp
, const char* arg
, char** ps
, Sflong_t
* pn
)
65 if (!(s
= fp
->t_str
) || streq(s
, "pid"))
67 else if (streq(s
, "pgid"))
69 else if (streq(s
, "ppid"))
71 else if (streq(s
, "tid") || streq(s
, "tty"))
73 for (fd
= 0; fd
< 3; fd
++)
74 if ((tid
= tcgetpgrp(fd
)) >= 0)
78 else if (streq(s
, "sid"))
80 else if (streq(s
, "format"))
84 error(2, "%s: unknown format identifier", s
);
91 b_pids(int argc
, char** argv
, void* context
)
95 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, 0);
98 switch (optget(argv
, usage
))
101 format
= opt_info
.arg
;
104 error(ERROR_USAGE
|4, "%s", opt_info
.arg
);
107 error(2, "%s", opt_info
.arg
);
112 argv
+= opt_info
.index
;
113 if (error_info
.errors
|| *argv
)
114 error(ERROR_USAGE
|4, "%s", optusage(NiL
));
117 sfkeyprintf(sfstdout
, format
, format
, key
, NiL
);
118 sfprintf(sfstdout
, "\n");