blob: 1d97189453d153e03780acf0380f9bc43ac66e54 [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
Rob Landleydfba7412006-03-06 20:47:33 +000019int pidof_main(int argc, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000020{
Denis Vlasenko048c93c2006-11-01 09:17:47 +000021 unsigned first = 1;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000022 unsigned fail = 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 Vlasenko048c93c2006-11-01 09:17:47 +000030 opt = getopt32(argc, argv, ""
31 USE_FEATURE_PIDOF_SINGLE ("s")
32 USE_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 Vlasenko048c93c2006-11-01 09:17:47 +000037 char getppid_str[sizeof(int)*3 + 1];
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000038 llist_t * omits_p = omits;
39 while (omits_p) {
40 /* are we asked to exclude the parent's process ID? */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000041 if (!strncmp(omits_p->data, "%PPID", 5)) {
Rob Landleya6b5b602006-05-08 19:03:07 +000042 llist_pop(&omits_p);
Denis Vlasenko048c93c2006-11-01 09:17:47 +000043 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
Rob Landley8bb50782006-05-26 23:44:51 +000044 llist_add_to(&omits_p, getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000045 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000046 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000047 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000048 }
49#endif
50 /* Looks like everything is set to go. */
Denis Vlasenko13858992006-10-08 12:49:22 +000051 while (optind < argc) {
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. */
56 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000057 for (pl = pidList; *pl; pl++) {
Denis Vlasenko048c93c2006-11-01 09:17:47 +000058 SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000059#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko048c93c2006-11-01 09:17:47 +000060 if (opt & OPT_OMIT) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000061 llist_t *omits_p = omits;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000062 while (omits_p) {
Denis Vlasenko13858992006-10-08 12:49:22 +000063 if (xatoul(omits_p->data) == *pl) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000064 omitted = 1;
65 break;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000066 } else
67 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000068 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000069 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000070#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000071 if (!omitted) {
Denis Vlasenko048c93c2006-11-01 09:17:47 +000072 printf(" %u" + first, (unsigned)*pl);
73 first = 0;
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000074 }
75 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
76
Denis Vlasenko048c93c2006-11-01 09:17:47 +000077 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000078 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000079 }
Eric Andersen44608e92002-10-22 12:21:15 +000080 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +000081 optind++;
82 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000083 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +000084
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000085#if ENABLE_FEATURE_PIDOF_OMIT
86 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +000087 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000088#endif
Robert Griebld11edf92002-05-22 23:38:12 +000089 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +000090}