blob: b64f0cbd6482e5e7e02043eb26111ee487c41e12 [file] [log] [blame]
Eric Andersen221b2ea2001-07-31 19:06:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * pidof implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen221b2ea2001-07-31 19:06:07 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen221b2ea2001-07-31 19:06:07 +00008 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +01009//config:config PIDOF
10//config: bool "pidof"
11//config: default y
12//config: help
13//config: Pidof finds the process id's (pids) of the named programs. It prints
14//config: those id's on the standard output.
15//config:
16//config:config FEATURE_PIDOF_SINGLE
Denys Vlasenkof5604222017-01-10 14:58:54 +010017//config: bool "Enable single shot (-s)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010018//config: default y
19//config: depends on PIDOF
20//config: help
Denys Vlasenkof5604222017-01-10 14:58:54 +010021//config: Support '-s' for returning only the first pid found.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010022//config:
23//config:config FEATURE_PIDOF_OMIT
Denys Vlasenkof5604222017-01-10 14:58:54 +010024//config: bool "Enable omitting pids (-o PID)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010025//config: default y
26//config: depends on PIDOF
27//config: help
Denys Vlasenkof5604222017-01-10 14:58:54 +010028//config: Support '-o PID' for omitting the given pid(s) in output.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010029//config: The special pid %PPID can be used to name the parent process
30//config: of the pidof, in other words the calling shell or shell script.
31
32//applet:IF_PIDOF(APPLET(pidof, BB_DIR_BIN, BB_SUID_DROP))
33
34//kbuild:lib-$(CONFIG_PIDOF) += pidof.o
Eric Andersen221b2ea2001-07-31 19:06:07 +000035
Pere Orga5bc8c002011-04-11 03:29:49 +020036//usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
37//usage:#define pidof_trivial_usage
38//usage: "[OPTIONS] [NAME]..."
Denys Vlasenko66426762011-06-05 03:58:28 +020039//usage:#define USAGE_PIDOF "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020040//usage:#else
41//usage:#define pidof_trivial_usage
42//usage: "[NAME]..."
43//usage:#define USAGE_PIDOF /* none */
44//usage:#endif
45//usage:#define pidof_full_usage "\n\n"
46//usage: "List PIDs of all processes with names that match NAMEs"
47//usage: USAGE_PIDOF
48//usage: IF_FEATURE_PIDOF_SINGLE(
49//usage: "\n -s Show only one PID"
50//usage: )
51//usage: IF_FEATURE_PIDOF_OMIT(
52//usage: "\n -o PID Omit given pid"
53//usage: "\n Use %PPID to omit pid of pidof's parent"
54//usage: )
55//usage:
56//usage:#define pidof_example_usage
57//usage: "$ pidof init\n"
58//usage: "1\n"
59//usage: IF_FEATURE_PIDOF_OMIT(
60//usage: "$ pidof /bin/sh\n20351 5973 5950\n")
61//usage: IF_FEATURE_PIDOF_OMIT(
62//usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
63
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000064#include "libbb.h"
Eric Andersen221b2ea2001-07-31 19:06:07 +000065
Denis Vlasenko048c93c2006-11-01 09:17:47 +000066enum {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000067 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
68 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
69 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
70 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
Denis Vlasenko048c93c2006-11-01 09:17:47 +000071};
Eric Andersen221b2ea2001-07-31 19:06:07 +000072
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000073int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000074int pidof_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000075{
Denis Vlasenko048c93c2006-11-01 09:17:47 +000076 unsigned first = 1;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000077 unsigned opt;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000078#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000079 llist_t *omits = NULL; /* list of pids to omit */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000080#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000081
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000082 /* do unconditional option parsing */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000083 opt = getopt32(argv, ""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000084 IF_FEATURE_PIDOF_SINGLE ("s")
Denys Vlasenko237bedd2016-07-06 21:58:02 +020085 IF_FEATURE_PIDOF_OMIT("o:*", &omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000086
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000087#if ENABLE_FEATURE_PIDOF_OMIT
88 /* fill omit list. */
89 {
Denis Vlasenko198bada2007-06-23 14:56:43 +000090 llist_t *omits_p = omits;
Denis Vlasenko0b791d92009-04-13 20:52:00 +000091 while (1) {
92 omits_p = llist_find_str(omits_p, "%PPID");
93 if (!omits_p)
94 break;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000095 /* are we asked to exclude the parent's process ID? */
Denis Vlasenko0b791d92009-04-13 20:52:00 +000096 omits_p->data = utoa((unsigned)getppid());
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000097 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000098 }
99#endif
100 /* Looks like everything is set to go. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000101 argv += optind;
102 while (*argv) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000103 pid_t *pidList;
104 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000105
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000106 /* reverse the pidlist like GNU pidof does. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000107 pidList = pidlist_reverse(find_pid_by_name(*argv));
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000108 for (pl = pidList; *pl; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000109#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +0000110 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000111 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000112 while (omits_p) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000113 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
Denis Vlasenko198bada2007-06-23 14:56:43 +0000114 goto omitting;
115 }
116 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000117 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000118 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000119#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +0000120 printf(" %u" + first, (unsigned)*pl);
121 first = 0;
Denis Vlasenko048c93c2006-11-01 09:17:47 +0000122 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +0000123 break;
Denis Vlasenko198bada2007-06-23 14:56:43 +0000124#if ENABLE_FEATURE_PIDOF_OMIT
125 omitting: ;
126#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +0000127 }
Eric Andersen44608e92002-10-22 12:21:15 +0000128 free(pidList);
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000129 argv++;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000130 }
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000131 if (!first)
132 bb_putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000133
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000134#if ENABLE_FEATURE_PIDOF_OMIT
135 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +0000136 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000137#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +0000138 return first; /* 1 (failure) - no processes found */
Eric Andersen221b2ea2001-07-31 19:06:07 +0000139}