blob: 8ed5a2155a8b7665d87832820952055bf57de325 [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersen221b2ea2001-07-31 19:06:07 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Eric Andersen221b2ea2001-07-31 19:06:07 +000011
Denis Vlasenko048c93c2006-11-01 09:17:47 +000012enum {
13 USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
14 USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
15 OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
16 OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
17};
Eric Andersen221b2ea2001-07-31 19:06:07 +000018
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000019int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000020int pidof_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000021{
Denis Vlasenko048c93c2006-11-01 09:17:47 +000022 unsigned first = 1;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000023 unsigned opt;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000024#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko198bada2007-06-23 14:56:43 +000025 char ppid_str[sizeof(int)*3 + 1];
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000026 llist_t *omits = NULL; /* list of pids to omit */
Denis Vlasenko048c93c2006-11-01 09:17:47 +000027 opt_complementary = "o::";
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000028#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000029
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000030 /* do unconditional option parsing */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000031 opt = getopt32(argv, ""
Denis Vlasenko048c93c2006-11-01 09:17:47 +000032 USE_FEATURE_PIDOF_SINGLE ("s")
33 USE_FEATURE_PIDOF_OMIT("o:", &omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000034
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000035#if ENABLE_FEATURE_PIDOF_OMIT
36 /* fill omit list. */
37 {
Denis Vlasenko198bada2007-06-23 14:56:43 +000038 llist_t *omits_p = omits;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000039 while (omits_p) {
40 /* are we asked to exclude the parent's process ID? */
Denis Vlasenko198bada2007-06-23 14:56:43 +000041 if (strcmp(omits_p->data, "%PPID") == 0) {
42 sprintf(ppid_str, "%u", (unsigned)getppid());
43 omits_p->data = ppid_str;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000044 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000045 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000046 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000047 }
48#endif
49 /* Looks like everything is set to go. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000050 argv += optind;
51 while (*argv) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000052 pid_t *pidList;
53 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000054
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000055 /* reverse the pidlist like GNU pidof does. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000056 pidList = pidlist_reverse(find_pid_by_name(*argv));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000057 for (pl = pidList; *pl; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000058#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000059 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000060 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000061 while (omits_p) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000062 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
Denis Vlasenko198bada2007-06-23 14:56:43 +000063 goto omitting;
64 }
65 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000066 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000067 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000068#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000069 printf(" %u" + first, (unsigned)*pl);
70 first = 0;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000071 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000072 break;
Denis Vlasenko198bada2007-06-23 14:56:43 +000073#if ENABLE_FEATURE_PIDOF_OMIT
74 omitting: ;
75#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000076 }
Eric Andersen44608e92002-10-22 12:21:15 +000077 free(pidList);
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000078 argv++;
Eric Andersen221b2ea2001-07-31 19:06:07 +000079 }
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000080 if (!first)
81 bb_putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +000082
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000083#if ENABLE_FEATURE_PIDOF_OMIT
84 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +000085 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000086#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000087 return first; /* 1 (failure) - no processes found */
Eric Andersen221b2ea2001-07-31 19:06:07 +000088}