Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * pidof implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 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-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 20 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 21 | #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-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 32 | #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-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 41 | #define omitted (0) |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 42 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 43 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 44 | int pidof_main(int argc, char **argv) |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 45 | { |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 46 | unsigned n = 0; |
| 47 | unsigned fail = 1; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 48 | unsigned long int opt; |
| 49 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 50 | llist_t *omits = NULL; /* list of pids to omit */ |
"Vladimir N. Oleynik" | f704b27 | 2005-10-14 09:56:52 +0000 | [diff] [blame] | 51 | bb_opt_complementally = _OMIT_COMPL("o::"); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 52 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 53 | |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 54 | /* do unconditional option parsing */ |
| 55 | opt = bb_getopt_ulflags(argc, argv, |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 56 | _SINGLE_COMPL("s") _OMIT_COMPL("o:") |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 57 | _OMIT(&omits)); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 58 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 59 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 60 | /* fill omit list. */ |
| 61 | { |
"Vladimir N. Oleynik" | a2eec60 | 2005-10-15 13:45:32 +0000 | [diff] [blame] | 62 | char getppid_str[32]; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 63 | llist_t * omits_p = omits; |
| 64 | while (omits_p) { |
| 65 | /* are we asked to exclude the parent's process ID? */ |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 66 | if (!strncmp(omits_p->data, "%PPID", 5)) { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 67 | llist_pop(&omits_p); |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 68 | snprintf(getppid_str, sizeof(getppid_str), "%d", getppid()); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 69 | llist_add_to(&omits_p, getppid_str); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 70 | } |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 71 | omits_p = omits_p->link; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 72 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 73 | } |
| 74 | #endif |
| 75 | /* Looks like everything is set to go. */ |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 76 | while(optind < argc) { |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 77 | long *pidList; |
| 78 | long *pl; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 79 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 80 | /* reverse the pidlist like GNU pidof does. */ |
| 81 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 82 | for(pl = pidList; *pl > 0; pl++) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 83 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 84 | unsigned omitted = 0; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 85 | 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-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 93 | #endif |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 94 | 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 Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 105 | break; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 106 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 107 | free(pidList); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 108 | optind++; |
| 109 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 110 | putchar('\n'); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 111 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 112 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 113 | if (ENABLE_FEATURE_CLEAN_UP) |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 114 | llist_free(omits, NULL); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 115 | #endif |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 116 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 117 | } |