diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2015-01-03 13:58:15 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2015-01-03 13:58:15 +0100 |
commit | 4aca87515a5083ae0e31ce3177189fd43b6d05ac (patch) | |
tree | 7b1d9a31393ca090757dc6f0d3859b4fcd93f271 /release/src/router/busybox/procps/pidof.c | |
parent | 008d0be72b2f160382c6e880765e96b64a050c65 (diff) | |
download | tomato-4aca87515a5083ae0e31ce3177189fd43b6d05ac.tar.gz tomato-4aca87515a5083ae0e31ce3177189fd43b6d05ac.tar.bz2 |
patch to Vanilla Tomato 1.28
Diffstat (limited to 'release/src/router/busybox/procps/pidof.c')
-rw-r--r-- | release/src/router/busybox/procps/pidof.c | 115 |
1 files changed, 65 insertions, 50 deletions
diff --git a/release/src/router/busybox/procps/pidof.c b/release/src/router/busybox/procps/pidof.c index 2fe8ecd2..19423996 100644 --- a/release/src/router/busybox/procps/pidof.c +++ b/release/src/router/busybox/procps/pidof.c @@ -2,70 +2,85 @@ /* * pidof implementation for busybox * - * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> * + * Licensed under the GPL version 2, see the file LICENSE in this tarball. */ +#include "libbb.h" -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <unistd.h> -#include <signal.h> -#include <ctype.h> -#include <string.h> -#include <unistd.h> -#include "busybox.h" - +enum { + USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) + USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,) + OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0, + OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, +}; -extern int pidof_main(int argc, char **argv) +int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int pidof_main(int argc UNUSED_PARAM, char **argv) { - int opt, n = 0; - int single_flag = 0; - int fail = 1; + unsigned first = 1; + unsigned opt; +#if ENABLE_FEATURE_PIDOF_OMIT + llist_t *omits = NULL; /* list of pids to omit */ + opt_complementary = "o::"; +#endif - /* do normal option parsing */ - while ((opt = getopt(argc, argv, "s")) > 0) { - switch (opt) { - case 's': - single_flag = 1; + /* do unconditional option parsing */ + opt = getopt32(argv, "" + USE_FEATURE_PIDOF_SINGLE ("s") + USE_FEATURE_PIDOF_OMIT("o:", &omits)); + +#if ENABLE_FEATURE_PIDOF_OMIT + /* fill omit list. */ + { + llist_t *omits_p = omits; + while (1) { + omits_p = llist_find_str(omits_p, "%PPID"); + if (!omits_p) break; - default: - bb_show_usage(); + /* are we asked to exclude the parent's process ID? */ + omits_p->data = utoa((unsigned)getppid()); } } +#endif + /* Looks like everything is set to go. */ + argv += optind; + while (*argv) { + pid_t *pidList; + pid_t *pl; - /* Looks like everything is set to go. */ - while(optind < argc) { - long *pidList; - long *pl; - - pidList = find_pid_by_name(argv[optind]); - for(pl = pidList; *pl > 0; pl++) { - printf("%s%ld", (n++ ? " " : ""), *pl); - fail = 0; - if (single_flag) + /* reverse the pidlist like GNU pidof does. */ + pidList = pidlist_reverse(find_pid_by_name(*argv)); + for (pl = pidList; *pl; pl++) { +#if ENABLE_FEATURE_PIDOF_OMIT + if (opt & OPT_OMIT) { + llist_t *omits_p = omits; + while (omits_p) { + if (xatoul(omits_p->data) == (unsigned long)(*pl)) { + goto omitting; + } + omits_p = omits_p->link; + } + } +#endif + printf(" %u" + first, (unsigned)*pl); + first = 0; + if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) break; +#if ENABLE_FEATURE_PIDOF_OMIT + omitting: ; +#endif } free(pidList); - optind++; - + argv++; } - printf("\n"); + if (!first) + bb_putchar('\n'); - return fail ? EXIT_FAILURE : EXIT_SUCCESS; +#if ENABLE_FEATURE_PIDOF_OMIT + if (ENABLE_FEATURE_CLEAN_UP) + llist_free(omits, NULL); +#endif + return first; /* 1 (failure) - no processes found */ } |