blob: bf5e7841aad8c61ac1b6e466db273beb38279fe6 [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 {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000013 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
14 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
15 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
16 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
Denis Vlasenko048c93c2006-11-01 09:17:47 +000017};
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 Vlasenkoa60f84e2008-07-05 09:18:54 +000020int pidof_main(int argc UNUSED_PARAM, 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
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000025 llist_t *omits = NULL; /* list of pids to omit */
Denis Vlasenko048c93c2006-11-01 09:17:47 +000026 opt_complementary = "o::";
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000027#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000028
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000029 /* do unconditional option parsing */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000030 opt = getopt32(argv, ""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000031 IF_FEATURE_PIDOF_SINGLE ("s")
32 IF_FEATURE_PIDOF_OMIT("o:", &omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000033
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000034#if ENABLE_FEATURE_PIDOF_OMIT
35 /* fill omit list. */
36 {
Denis Vlasenko198bada2007-06-23 14:56:43 +000037 llist_t *omits_p = omits;
Denis Vlasenko0b791d92009-04-13 20:52:00 +000038 while (1) {
39 omits_p = llist_find_str(omits_p, "%PPID");
40 if (!omits_p)
41 break;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000042 /* are we asked to exclude the parent's process ID? */
Denis Vlasenko0b791d92009-04-13 20:52:00 +000043 omits_p->data = utoa((unsigned)getppid());
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000044 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000045 }
46#endif
47 /* Looks like everything is set to go. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000048 argv += optind;
49 while (*argv) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000050 pid_t *pidList;
51 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000052
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000053 /* reverse the pidlist like GNU pidof does. */
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000054 pidList = pidlist_reverse(find_pid_by_name(*argv));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000055 for (pl = pidList; *pl; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000056#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000057 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000058 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000059 while (omits_p) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000060 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
Denis Vlasenko198bada2007-06-23 14:56:43 +000061 goto omitting;
62 }
63 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000064 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000065 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000066#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000067 printf(" %u" + first, (unsigned)*pl);
68 first = 0;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000069 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000070 break;
Denis Vlasenko198bada2007-06-23 14:56:43 +000071#if ENABLE_FEATURE_PIDOF_OMIT
72 omitting: ;
73#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000074 }
Eric Andersen44608e92002-10-22 12:21:15 +000075 free(pidList);
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000076 argv++;
Eric Andersen221b2ea2001-07-31 19:06:07 +000077 }
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000078 if (!first)
79 bb_putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +000080
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000081#if ENABLE_FEATURE_PIDOF_OMIT
82 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +000083 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000084#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000085 return first; /* 1 (failure) - no processes found */
Eric Andersen221b2ea2001-07-31 19:06:07 +000086}