blob: c989076bb63aa104939784c1331c4f71dd808b9f [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 Vlasenko06af2162007-02-03 17:28:39 +000019int pidof_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000020int pidof_main(int argc, 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 Vlasenko13858992006-10-08 12:49:22 +000050 while (optind < argc) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000051 pid_t *pidList;
52 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000053
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000054 /* reverse the pidlist like GNU pidof does. */
55 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000056 for (pl = pidList; *pl; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000057#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000058 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000059 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000060 while (omits_p) {
Denis Vlasenko13858992006-10-08 12:49:22 +000061 if (xatoul(omits_p->data) == *pl) {
Denis Vlasenko198bada2007-06-23 14:56:43 +000062 goto omitting;
63 }
64 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000065 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000066 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000067#endif
Denis Vlasenko198bada2007-06-23 14:56:43 +000068 printf(" %u" + first, (unsigned)*pl);
69 first = 0;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000070 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000071 break;
Denis Vlasenko198bada2007-06-23 14:56:43 +000072#if ENABLE_FEATURE_PIDOF_OMIT
73 omitting: ;
74#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000075 }
Eric Andersen44608e92002-10-22 12:21:15 +000076 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +000077 optind++;
78 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000079 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}