blob: 28c5c04e292c14338cd7789fade5633333eca717 [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)
Denis Vlasenko13858992006-10-08 12:49:22 +000017#define SINGLE 0
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000018#endif
19
20#if ENABLE_FEATURE_PIDOF_OMIT
Denis Vlasenko35fb5122006-11-01 09:16:49 +000021# define _OMIT_COMPL(a) a
22# define _OMIT(a) ,a
23# if ENABLE_FEATURE_PIDOF_SINGLE
24# define OMIT (1<<1)
25# else
26# define OMIT (1<<0)
27# endif
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000028#else
Denis Vlasenko35fb5122006-11-01 09:16:49 +000029# define _OMIT_COMPL(a) ""
30# define _OMIT(a)
31# define OMIT (0)
32# 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);
Denis Vlasenko13858992006-10-08 12:49:22 +000059 snprintf(getppid_str, sizeof(getppid_str), "%ld", (long)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. */
Denis Vlasenko13858992006-10-08 12:49:22 +000067 while (optind < argc) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000068 pid_t *pidList;
69 pid_t *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]));
Denis Vlasenko35fb5122006-11-01 09:16:49 +000073 for (pl = pidList; *pl; 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;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000078 while (omits_p) {
Denis Vlasenko13858992006-10-08 12:49:22 +000079 if (xatoul(omits_p->data) == *pl) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000080 omitted = 1;
81 break;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000082 } else
83 omits_p = omits_p->link;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000084 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000085 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000086#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000087 if (!omitted) {
88 if (n) {
89 putchar(' ');
90 } else {
91 n = 1;
92 }
Denis Vlasenko35fb5122006-11-01 09:16:49 +000093 printf("%u", (unsigned)*pl);
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000094 }
95 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
96
97 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +000098 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +000099 }
Eric Andersen44608e92002-10-22 12:21:15 +0000100 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +0000101 optind++;
102 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000103 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000104
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000105#if ENABLE_FEATURE_PIDOF_OMIT
106 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +0000107 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000108#endif
Robert Griebld11edf92002-05-22 23:38:12 +0000109 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000110}