blob: 5b28f3b86212b44a84a497ac7cc883f892aa6ed2 [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-Fischer7fee0c42006-09-13 16:39:19 +00007 * Licensed under the GPL v2 or later, 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#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>
Eric Andersen221b2ea2001-07-31 19:06:07 +000020
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000021#if ENABLE_FEATURE_PIDOF_SINGLE
22#define _SINGLE_COMPL(a) a
23#define SINGLE (1<<0)
24#else
25#define _SINGLE_COMPL(a)
26#define SINGLE (0)
27#endif
28
29#if ENABLE_FEATURE_PIDOF_OMIT
30#define _OMIT_COMPL(a) a
31#define _OMIT(a) ,a
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000032#if ENABLE_FEATURE_PIDOF_SINGLE
33#define OMIT (1<<1)
34#else
35#define OMIT (1<<0)
36#endif
37#else
38#define _OMIT_COMPL(a) ""
39#define _OMIT(a)
40#define OMIT (0)
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000041#define omitted (0)
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000042#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000043
Rob Landleydfba7412006-03-06 20:47:33 +000044int pidof_main(int argc, char **argv)
Eric Andersen221b2ea2001-07-31 19:06:07 +000045{
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000046 unsigned n = 0;
47 unsigned fail = 1;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000048 unsigned long int opt;
49#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000050 llist_t *omits = NULL; /* list of pids to omit */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000051 bb_opt_complementally = _OMIT_COMPL("o::");
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000052#endif
Eric Andersen221b2ea2001-07-31 19:06:07 +000053
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000054 /* do unconditional option parsing */
55 opt = bb_getopt_ulflags(argc, argv,
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000056 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000057 _OMIT(&omits));
Eric Andersen221b2ea2001-07-31 19:06:07 +000058
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000059#if ENABLE_FEATURE_PIDOF_OMIT
60 /* fill omit list. */
61 {
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +000062 char getppid_str[32];
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000063 llist_t * omits_p = omits;
64 while (omits_p) {
65 /* are we asked to exclude the parent's process ID? */
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000066 if (!strncmp(omits_p->data, "%PPID", 5)) {
Rob Landleya6b5b602006-05-08 19:03:07 +000067 llist_pop(&omits_p);
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000068 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
Rob Landley8bb50782006-05-26 23:44:51 +000069 llist_add_to(&omits_p, getppid_str);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000070 }
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000071 omits_p = omits_p->link;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000072 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000073 }
74#endif
75 /* Looks like everything is set to go. */
Eric Andersen221b2ea2001-07-31 19:06:07 +000076 while(optind < argc) {
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000077 long *pidList;
78 long *pl;
Eric Andersen221b2ea2001-07-31 19:06:07 +000079
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000080 /* reverse the pidlist like GNU pidof does. */
81 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +000082 for(pl = pidList; *pl > 0; pl++) {
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000083#if ENABLE_FEATURE_PIDOF_OMIT
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000084 unsigned omitted = 0;
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000085 if (opt & OMIT) {
86 llist_t *omits_p = omits;
87 while (omits_p)
88 if (strtol(omits_p->data, NULL, 10) == *pl) {
89 omitted = 1; break;
90 } else
91 omits_p = omits_p->link;
92 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +000093#endif
Bernhard Reutner-Fischerab52db82005-10-07 15:44:37 +000094 if (!omitted) {
95 if (n) {
96 putchar(' ');
97 } else {
98 n = 1;
99 }
100 printf("%ld", *pl);
101 }
102 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
103
104 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
Robert Griebld11edf92002-05-22 23:38:12 +0000105 break;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000106 }
Eric Andersen44608e92002-10-22 12:21:15 +0000107 free(pidList);
Eric Andersen221b2ea2001-07-31 19:06:07 +0000108 optind++;
109 }
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000110 putchar('\n');
Eric Andersen221b2ea2001-07-31 19:06:07 +0000111
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000112#if ENABLE_FEATURE_PIDOF_OMIT
113 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleya6b5b602006-05-08 19:03:07 +0000114 llist_free(omits, NULL);
Bernhard Reutner-Fischer81c3a512005-10-06 15:37:02 +0000115#endif
Robert Griebld11edf92002-05-22 23:38:12 +0000116 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
Eric Andersen221b2ea2001-07-31 19:06:07 +0000117}