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 | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 8 | */ |
| 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-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> |
| 20 | #include "busybox.h" |
| 21 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 22 | #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-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 33 | #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-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 42 | #define omitted (0) |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 43 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 44 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 45 | int pidof_main(int argc, char **argv) |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 46 | { |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 47 | unsigned n = 0; |
| 48 | unsigned fail = 1; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 49 | unsigned long int opt; |
| 50 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 51 | llist_t *omits = NULL; /* list of pids to omit */ |
"Vladimir N. Oleynik" | f704b27 | 2005-10-14 09:56:52 +0000 | [diff] [blame] | 52 | bb_opt_complementally = _OMIT_COMPL("o::"); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 53 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 54 | |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 55 | /* do unconditional option parsing */ |
| 56 | opt = bb_getopt_ulflags(argc, argv, |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 57 | _SINGLE_COMPL("s") _OMIT_COMPL("o:") |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 58 | _OMIT(&omits)); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 59 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 60 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 61 | /* fill omit list. */ |
| 62 | { |
"Vladimir N. Oleynik" | a2eec60 | 2005-10-15 13:45:32 +0000 | [diff] [blame] | 63 | char getppid_str[32]; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 64 | llist_t * omits_p = omits; |
| 65 | while (omits_p) { |
| 66 | /* are we asked to exclude the parent's process ID? */ |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 67 | if (!strncmp(omits_p->data, "%PPID", 5)) { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 68 | llist_pop(&omits_p); |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 69 | snprintf(getppid_str, sizeof(getppid_str), "%d", getppid()); |
| 70 | omits_p = llist_add_to(omits_p, getppid_str); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 71 | #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-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 77 | omits_p = omits_p->link; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 78 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 79 | } |
| 80 | #endif |
| 81 | /* Looks like everything is set to go. */ |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 82 | while(optind < argc) { |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 83 | long *pidList; |
| 84 | long *pl; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 85 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 86 | /* reverse the pidlist like GNU pidof does. */ |
| 87 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 88 | for(pl = pidList; *pl > 0; pl++) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 89 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 90 | unsigned omitted = 0; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 91 | if (opt & OMIT) { |
| 92 | llist_t *omits_p = omits; |
| 93 | while (omits_p) |
| 94 | if (strtol(omits_p->data, NULL, 10) == *pl) { |
| 95 | omitted = 1; break; |
| 96 | } else |
| 97 | omits_p = omits_p->link; |
| 98 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 99 | #endif |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 100 | if (!omitted) { |
| 101 | if (n) { |
| 102 | putchar(' '); |
| 103 | } else { |
| 104 | n = 1; |
| 105 | } |
| 106 | printf("%ld", *pl); |
| 107 | } |
| 108 | fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); |
| 109 | |
| 110 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE)) |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 111 | break; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 112 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 113 | free(pidList); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 114 | optind++; |
| 115 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 116 | putchar('\n'); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 117 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 118 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 119 | if (ENABLE_FEATURE_CLEAN_UP) |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 120 | llist_free(omits, NULL); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 121 | #endif |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 122 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 123 | } |