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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 9 | //config:config PIDOF |
| 10 | //config: bool "pidof" |
| 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: Pidof finds the process id's (pids) of the named programs. It prints |
| 14 | //config: those id's on the standard output. |
| 15 | //config: |
| 16 | //config:config FEATURE_PIDOF_SINGLE |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame^] | 17 | //config: bool "Enable single shot (-s)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 18 | //config: default y |
| 19 | //config: depends on PIDOF |
| 20 | //config: help |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame^] | 21 | //config: Support '-s' for returning only the first pid found. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 22 | //config: |
| 23 | //config:config FEATURE_PIDOF_OMIT |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame^] | 24 | //config: bool "Enable omitting pids (-o PID)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: depends on PIDOF |
| 27 | //config: help |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame^] | 28 | //config: Support '-o PID' for omitting the given pid(s) in output. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 29 | //config: The special pid %PPID can be used to name the parent process |
| 30 | //config: of the pidof, in other words the calling shell or shell script. |
| 31 | |
| 32 | //applet:IF_PIDOF(APPLET(pidof, BB_DIR_BIN, BB_SUID_DROP)) |
| 33 | |
| 34 | //kbuild:lib-$(CONFIG_PIDOF) += pidof.o |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 35 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 36 | //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT) |
| 37 | //usage:#define pidof_trivial_usage |
| 38 | //usage: "[OPTIONS] [NAME]..." |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 39 | //usage:#define USAGE_PIDOF "\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 40 | //usage:#else |
| 41 | //usage:#define pidof_trivial_usage |
| 42 | //usage: "[NAME]..." |
| 43 | //usage:#define USAGE_PIDOF /* none */ |
| 44 | //usage:#endif |
| 45 | //usage:#define pidof_full_usage "\n\n" |
| 46 | //usage: "List PIDs of all processes with names that match NAMEs" |
| 47 | //usage: USAGE_PIDOF |
| 48 | //usage: IF_FEATURE_PIDOF_SINGLE( |
| 49 | //usage: "\n -s Show only one PID" |
| 50 | //usage: ) |
| 51 | //usage: IF_FEATURE_PIDOF_OMIT( |
| 52 | //usage: "\n -o PID Omit given pid" |
| 53 | //usage: "\n Use %PPID to omit pid of pidof's parent" |
| 54 | //usage: ) |
| 55 | //usage: |
| 56 | //usage:#define pidof_example_usage |
| 57 | //usage: "$ pidof init\n" |
| 58 | //usage: "1\n" |
| 59 | //usage: IF_FEATURE_PIDOF_OMIT( |
| 60 | //usage: "$ pidof /bin/sh\n20351 5973 5950\n") |
| 61 | //usage: IF_FEATURE_PIDOF_OMIT( |
| 62 | //usage: "$ pidof /bin/sh -o %PPID\n20351 5950") |
| 63 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 64 | #include "libbb.h" |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 66 | enum { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 67 | IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) |
| 68 | IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,) |
| 69 | OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0, |
| 70 | OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 71 | }; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 73 | int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 74 | int pidof_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 76 | unsigned first = 1; |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 77 | unsigned opt; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 78 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 79 | llist_t *omits = NULL; /* list of pids to omit */ |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 80 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 81 | |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 82 | /* do unconditional option parsing */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 83 | opt = getopt32(argv, "" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 84 | IF_FEATURE_PIDOF_SINGLE ("s") |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 85 | IF_FEATURE_PIDOF_OMIT("o:*", &omits)); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 86 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 87 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 88 | /* fill omit list. */ |
| 89 | { |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 90 | llist_t *omits_p = omits; |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 91 | while (1) { |
| 92 | omits_p = llist_find_str(omits_p, "%PPID"); |
| 93 | if (!omits_p) |
| 94 | break; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 95 | /* are we asked to exclude the parent's process ID? */ |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 96 | omits_p->data = utoa((unsigned)getppid()); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 97 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 98 | } |
| 99 | #endif |
| 100 | /* Looks like everything is set to go. */ |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 101 | argv += optind; |
| 102 | while (*argv) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 103 | pid_t *pidList; |
| 104 | pid_t *pl; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 105 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 106 | /* reverse the pidlist like GNU pidof does. */ |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 107 | pidList = pidlist_reverse(find_pid_by_name(*argv)); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 108 | for (pl = pidList; *pl; pl++) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 109 | #if ENABLE_FEATURE_PIDOF_OMIT |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 110 | if (opt & OPT_OMIT) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 111 | llist_t *omits_p = omits; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 112 | while (omits_p) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 113 | if (xatoul(omits_p->data) == (unsigned long)(*pl)) { |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 114 | goto omitting; |
| 115 | } |
| 116 | omits_p = omits_p->link; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 117 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 118 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 119 | #endif |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 120 | printf(" %u" + first, (unsigned)*pl); |
| 121 | first = 0; |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 122 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 123 | break; |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 124 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 125 | omitting: ; |
| 126 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 127 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 128 | free(pidList); |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 129 | argv++; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 130 | } |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 131 | if (!first) |
| 132 | bb_putchar('\n'); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 133 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 134 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 135 | if (ENABLE_FEATURE_CLEAN_UP) |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 136 | llist_free(omits, NULL); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 137 | #endif |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 138 | return first; /* 1 (failure) - no processes found */ |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 139 | } |