blob: 76c58df858c5a078c41cf76e8c60e9e1ec445d49 [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 *
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +00007 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Eric Andersen221b2ea2001-07-31 19:06:07 +00008 */
9
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <errno.h>
14#include <unistd.h>
15#include <signal.h>
16#include <ctype.h>
17#include <string.h>
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000018#include <sys/types.h>
Eric Andersen221b2ea2001-07-31 19:06:07 +000019#include <unistd.h>
20#include "busybox.h"
21
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000022#if ENABLE_FEATURE_PIDOF_SINGLE
23#define _SINGLE_COMPL(a) a
24#define SINGLE (1<<0)
25#else
26#define _SINGLE_COMPL(a)
27#define SINGLE (0)
28#endif
29
30#if ENABLE_FEATURE_PIDOF_OMIT
31#define _OMIT_COMPL(a) a
32#define _OMIT(a) ,a
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000033#if ENABLE_FEATURE_PIDOF_SINGLE
34#define OMIT (1<<1)
35#else
36#define OMIT (1<<0)
37#endif
38#else
39#define _OMIT_COMPL(a) ""
40#define _OMIT(a)
41#define OMIT (0)
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000042#define omitted (0)
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000043#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000044
45extern int pidof_main(int argc, char **argv)
46{
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000047 unsigned n = 0;
48 unsigned fail = 1;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000049 unsigned long int opt;
50#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000051 llist_t *omits = NULL; /* list of pids to omit */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000052 bb_opt_complementally = _OMIT_COMPL("o*");
53#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000054
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000055 /* do unconditional option parsing */
56 opt = bb_getopt_ulflags(argc, argv,
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000057 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000058 _OMIT(&omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000059
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000060#if ENABLE_FEATURE_PIDOF_OMIT
61 /* fill omit list. */
62 {
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000063 RESERVE_CONFIG_BUFFER(getppid_str, 32);
64 llist_t * omits_p = omits;
65 while (omits_p) {
66 /* are we asked to exclude the parent's process ID? */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000067 if (!strncmp(omits_p->data, "%PPID", 5)) {
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000068 omits_p = llist_free_one(omits_p);
69 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
70 omits_p = llist_add_to(omits_p, getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000071#if 0
72 } else {
73 bb_error_msg_and_die("illegal omit pid value (%s)!\n",
74 omits_p->data);
75#endif
76 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000077 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000078 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000079 if (ENABLE_FEATURE_CLEAN_UP)
80 RELEASE_CONFIG_BUFFER(getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000081 }
82#endif
83 /* Looks like everything is set to go. */
Eric Andersen221b2ea2001-07-31 19:06:07 +000084 while(optind < argc) {
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000085 long *pidList;
86 long *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000087
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000088 /* reverse the pidlist like GNU pidof does. */
89 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000090 for(pl = pidList; *pl > 0; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000091#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000092 unsigned omitted = 0;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000093 if (opt & OMIT) {
94 llist_t *omits_p = omits;
95 while (omits_p)
96 if (strtol(omits_p->data, NULL, 10) == *pl) {
97 omitted = 1; break;
98 } else
99 omits_p = omits_p->link;
100 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000101#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +0000102 if (!omitted) {
103 if (n) {
104 putchar(' ');
105 } else {
106 n = 1;
107 }
108 printf("%ld", *pl);
109 }
110 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
111
112 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +0000113 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000114 }
Eric Andersen44608e92002-10-22 12:21:15 +0000115 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +0000116 optind++;
117 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000118 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000119
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000120#if ENABLE_FEATURE_PIDOF_OMIT
121 if (ENABLE_FEATURE_CLEAN_UP)
122 llist_free(omits);
123#endif
Robert Griebld11edf92002-05-22 23:38:12 +0000124 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000125}