blob: e6c954843571de7087d815e9610ca59b22cc8d8d [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
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000012#if ENABLE_FEATURE_PIDOF_SINGLE
13#define _SINGLE_COMPL(a) a
14#define SINGLE (1<<0)
15#else
16#define _SINGLE_COMPL(a)
17#define SINGLE (0)
18#endif
19
20#if ENABLE_FEATURE_PIDOF_OMIT
21#define _OMIT_COMPL(a) a
22#define _OMIT(a) ,a
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000023#if ENABLE_FEATURE_PIDOF_SINGLE
24#define OMIT (1<<1)
25#else
26#define OMIT (1<<0)
27#endif
28#else
29#define _OMIT_COMPL(a) ""
30#define _OMIT(a)
31#define OMIT (0)
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000032#define omitted (0)
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000033#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000034
Rob Landleydfba7412006-03-06 20:47:33 +000035int pidof_main(int argc, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000036{
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000037 unsigned n = 0;
38 unsigned fail = 1;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000039 unsigned long int opt;
40#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000041 llist_t *omits = NULL; /* list of pids to omit */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000042 opt_complementary = _OMIT_COMPL("o::");
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000043#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000044
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000045 /* do unconditional option parsing */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000046 opt = getopt32(argc, argv,
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000047 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000048 _OMIT(&omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000049
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000050#if ENABLE_FEATURE_PIDOF_OMIT
51 /* fill omit list. */
52 {
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +000053 char getppid_str[32];
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000054 llist_t * omits_p = omits;
55 while (omits_p) {
56 /* are we asked to exclude the parent's process ID? */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000057 if (!strncmp(omits_p->data, "%PPID", 5)) {
Rob Landleya6b5b602006-05-08 19:03:07 +000058 llist_pop(&omits_p);
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000059 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
Rob Landley8bb50782006-05-26 23:44:51 +000060 llist_add_to(&omits_p, getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000061 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000062 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000063 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000064 }
65#endif
66 /* Looks like everything is set to go. */
Eric Andersen221b2ea2001-07-31 19:06:07 +000067 while(optind < argc) {
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000068 long *pidList;
69 long *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000070
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000071 /* reverse the pidlist like GNU pidof does. */
72 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000073 for(pl = pidList; *pl > 0; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000074#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000075 unsigned omitted = 0;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000076 if (opt & OMIT) {
77 llist_t *omits_p = omits;
78 while (omits_p)
79 if (strtol(omits_p->data, NULL, 10) == *pl) {
80 omitted = 1; break;
81 } else
82 omits_p = omits_p->link;
83 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000084#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000085 if (!omitted) {
86 if (n) {
87 putchar(' ');
88 } else {
89 n = 1;
90 }
91 printf("%ld", *pl);
92 }
93 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
94
95 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000096 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000097 }
Eric Andersen44608e92002-10-22 12:21:15 +000098 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +000099 optind++;
100 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000101 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000102
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000103#if ENABLE_FEATURE_PIDOF_OMIT
104 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +0000105 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000106#endif
Robert Griebld11edf92002-05-22 23:38:12 +0000107 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000108}