blob: ea2520a4f0e446c1421e970ce3295bacb71eb05e [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.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;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000023 unsigned fail = 1;
Denis Vlasenko048c93c2006-11-01 09:17:47 +000024 unsigned opt;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000025#if ENABLE_FEATURE_PIDOF_OMIT
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 Vlasenko048c93c2006-11-01 09:17:47 +000031 opt = getopt32(argc, argv, ""
32 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 Vlasenko048c93c2006-11-01 09:17:47 +000038 char getppid_str[sizeof(int)*3 + 1];
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000039 llist_t * omits_p = omits;
40 while (omits_p) {
41 /* are we asked to exclude the parent's process ID? */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000042 if (!strncmp(omits_p->data, "%PPID", 5)) {
Rob Landleya6b5b602006-05-08 19:03:07 +000043 llist_pop(&omits_p);
Denis Vlasenko048c93c2006-11-01 09:17:47 +000044 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
Rob Landley8bb50782006-05-26 23:44:51 +000045 llist_add_to(&omits_p, getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000046 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000047 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000048 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000049 }
50#endif
51 /* Looks like everything is set to go. */
Denis Vlasenko13858992006-10-08 12:49:22 +000052 while (optind < argc) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000053 pid_t *pidList;
54 pid_t *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000055
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000056 /* reverse the pidlist like GNU pidof does. */
57 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000058 for (pl = pidList; *pl; pl++) {
Denis Vlasenko048c93c2006-11-01 09:17:47 +000059 SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000060#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000061 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000062 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000063 while (omits_p) {
Denis Vlasenko13858992006-10-08 12:49:22 +000064 if (xatoul(omits_p->data) == *pl) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000065 omitted = 1;
66 break;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000067 } else
68 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000069 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000070 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000071#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000072 if (!omitted) {
Denis Vlasenko048c93c2006-11-01 09:17:47 +000073 printf(" %u" + first, (unsigned)*pl);
74 first = 0;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000075 }
76 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
77
Denis Vlasenko048c93c2006-11-01 09:17:47 +000078 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000079 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000080 }
Eric Andersen44608e92002-10-22 12:21:15 +000081 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +000082 optind++;
83 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000084 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +000085
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000086#if ENABLE_FEATURE_PIDOF_OMIT
87 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +000088 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000089#endif
Robert Griebld11edf92002-05-22 23:38:12 +000090 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +000091}