blob: 6d7b591093e363222928895a270bd2731596fa9d [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 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
11//usage:#define pidof_trivial_usage
12//usage: "[OPTIONS] [NAME]..."
Denys Vlasenko66426762011-06-05 03:58:28 +020013//usage:#define USAGE_PIDOF "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage:#else
15//usage:#define pidof_trivial_usage
16//usage: "[NAME]..."
17//usage:#define USAGE_PIDOF /* none */
18//usage:#endif
19//usage:#define pidof_full_usage "\n\n"
20//usage: "List PIDs of all processes with names that match NAMEs"
21//usage: USAGE_PIDOF
22//usage: IF_FEATURE_PIDOF_SINGLE(
23//usage: "\n -s Show only one PID"
24//usage: )
25//usage: IF_FEATURE_PIDOF_OMIT(
26//usage: "\n -o PID Omit given pid"
27//usage: "\n Use %PPID to omit pid of pidof's parent"
28//usage: )
29//usage:
30//usage:#define pidof_example_usage
31//usage: "$ pidof init\n"
32//usage: "1\n"
33//usage: IF_FEATURE_PIDOF_OMIT(
34//usage: "$ pidof /bin/sh\n20351 5973 5950\n")
35//usage: IF_FEATURE_PIDOF_OMIT(
36//usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Eric Andersen221b2ea2001-07-31 19:06:07 +000039
Denis Vlasenko048c93c2006-11-01 09:17:47 +000040enum {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000041 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
42 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
43 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
44 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
Denis Vlasenko048c93c2006-11-01 09:17:47 +000045};
Eric Andersen221b2ea2001-07-31 19:06:07 +000046
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048int pidof_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000049{
Denis Vlasenko048c93c2006-11-01 09:17:47 +000050 unsigned first = 1;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000051 unsigned opt;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000052#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000053 llist_t *omits = NULL; /* list of pids to omit */
Denis Vlasenko048c93c2006-11-01 09:17:47 +000054 opt_complementary = "o::";
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000055#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000056
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000057 /* do unconditional option parsing */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000058 opt = getopt32(argv, ""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000059 IF_FEATURE_PIDOF_SINGLE ("s")
60 IF_FEATURE_PIDOF_OMIT("o:", &omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000061
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000062#if ENABLE_FEATURE_PIDOF_OMIT
63 /* fill omit list. */
64 {
Denis Vlasenko198bada2007-06-23 14:56:43 +000065 llist_t *omits_p = omits;
Denis Vlasenko0b791d92009-04-13 20:52:00 +000066 while (1) {
67 omits_p = llist_find_str(omits_p, "%PPID");
68 if (!omits_p)
69 break;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000070 /* are we asked to exclude the parent's process ID? */
Denis Vlasenko0b791d92009-04-13 20:52:00 +000071 omits_p->data = utoa((unsigned)getppid());
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000072 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000073 }
74#endif
75 /* Looks like everything is set to go. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000076 argv += optind;
77 while (*argv) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000078 pid_t *pidList;
79 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000080
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000081 /* reverse the pidlist like GNU pidof does. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000082 pidList = pidlist_reverse(find_pid_by_name(*argv));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000083 for (pl = pidList; *pl; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000084#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000085 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000086 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000087 while (omits_p) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000088 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
Denis Vlasenko198bada2007-06-23 14:56:43 +000089 goto omitting;
90 }
91 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000092 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000093 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000094#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000095 printf(" %u" + first, (unsigned)*pl);
96 first = 0;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000097 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000098 break;
Denis Vlasenko198bada2007-06-23 14:56:43 +000099#if ENABLE_FEATURE_PIDOF_OMIT
100 omitting: ;
101#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +0000102 }
Eric Andersen44608e92002-10-22 12:21:15 +0000103 free(pidList);
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000104 argv++;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000105 }
Denis Vlasenko6446c2d2007-11-25 04:54:13 +0000106 if (!first)
107 bb_putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000108
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000109#if ENABLE_FEATURE_PIDOF_OMIT
110 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +0000111 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000112#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +0000113 return first; /* 1 (failure) - no processes found */
Eric Andersen221b2ea2001-07-31 19:06:07 +0000114}