2013-10-05 00:05:01 +05:30
|
|
|
/*
|
|
|
|
* pidof.c - Utility for listing pids of running processes
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Jaromir Capik <jcapik@redhat.com>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-09-26 10:07:38 +05:30
|
|
|
#include <unistd.h>
|
2013-10-05 00:05:01 +05:30
|
|
|
#include <getopt.h>
|
1970-01-01 05:30:00 +05:30
|
|
|
#include <limits.h>
|
2015-09-26 10:07:38 +05:30
|
|
|
#include <sys/types.h>
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include "fileutils.h"
|
|
|
|
#include "nls.h"
|
|
|
|
#include "xalloc.h"
|
2020-06-30 10:30:00 +05:30
|
|
|
|
|
|
|
#include <proc/pids.h>
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
|
1970-01-01 05:30:00 +05:30
|
|
|
#define grow_size(x) do { \
|
|
|
|
if ((x) < 0 || (size_t)(x) >= INT_MAX / 5 / sizeof(struct el)) \
|
|
|
|
xerrx(EXIT_FAILURE, _("integer overflow")); \
|
|
|
|
(x) = (x) * 5 / 4 + 1024; \
|
|
|
|
} while (0)
|
|
|
|
|
2013-10-05 00:05:01 +05:30
|
|
|
#define safe_free(x) if (x) { free(x); x=NULL; }
|
|
|
|
|
|
|
|
|
|
|
|
struct el {
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct el *procs = NULL;
|
|
|
|
static int proc_count = 0;
|
|
|
|
|
|
|
|
struct el *omitted_procs = NULL;
|
|
|
|
static int omit_count = 0;
|
|
|
|
|
|
|
|
static char *program = NULL;
|
|
|
|
|
|
|
|
/* switch flags */
|
|
|
|
static int opt_single_shot = 0; /* -s */
|
|
|
|
static int opt_scripts_too = 0; /* -x */
|
|
|
|
static int opt_rootdir_check = 0; /* -c */
|
|
|
|
|
|
|
|
static char *pidof_root = NULL;
|
|
|
|
|
|
|
|
static int __attribute__ ((__noreturn__)) usage(int opt)
|
|
|
|
{
|
|
|
|
int err = (opt == '?');
|
|
|
|
FILE *fp = err ? stderr : stdout;
|
|
|
|
|
|
|
|
fputs(USAGE_HEADER, fp);
|
|
|
|
fprintf(fp, _(" %s [options] [program [...]]\n"), program_invocation_short_name);
|
|
|
|
fputs(USAGE_OPTIONS, fp);
|
2013-12-28 03:55:39 +05:30
|
|
|
fputs(_(" -s, --single-shot return one PID only\n"), fp);
|
|
|
|
fputs(_(" -c, --check-root omit processes with different root\n"), fp);
|
2014-02-02 12:43:01 +05:30
|
|
|
fputs(_(" -x also find shells running the named scripts\n"), fp);
|
2013-12-28 03:55:39 +05:30
|
|
|
fputs(_(" -o, --omit-pid <PID,...> omit processes with PID\n"), fp);
|
pidof: allow to change a separator put between pids
I frequency use pidof command with strace system call tracer.
strace can trace MULTIPLE processes specified with "-p $PID"
arguments like:
strace -p 1 -p 1030 -p 3043
Sometimes I want to do as following
strace -p $(pidof httpd)
However, above command line doesn't work because -p option
is needed for specifying a pid. pidof uses a whitespace as
a separator. For passing the output to strace, the separator
should be replaced with ' -p '.
This maybe not a special to my use case.
This commit introduces -S option that allows a user to specify a
separator the one wants.
$ ./pidof bash
./pidof bash
24624 18790 12786 11898 11546 10766 7654 5095
$ ./pidof -S ',' bash
./pidof -S ',' bash
24624,18790,12786,11898,11546,10766,7654,5095
$ ./pidof -S '-p ' bash
./pidof -S '-p ' bash
24624-p 18790-p 12786-p 11898-p 11546-p 10766-p 7654-p 5095
$ ./pidof -S ' -p ' bash
./pidof -S ' -p ' bash
24624 -p 18790 -p 12786 -p 11898 -p 11546 -p 10766 -p 7654 -p 5095
$ strace -p $(./pidof -S ' -p ' bash)
strace -p $(./pidof -S ' -p ' bash)
strace: Process 24624 attached
strace: Process 18790 attached
strace: Process 12786 attached
...
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2018-02-24 14:33:11 +05:30
|
|
|
fputs(_(" -S, --separator SEP use SEP as separator put between PIDs"), fp);
|
2013-10-05 00:05:01 +05:30
|
|
|
fputs(USAGE_SEPARATOR, fp);
|
|
|
|
fputs(USAGE_HELP, fp);
|
|
|
|
fputs(USAGE_VERSION, fp);
|
|
|
|
fprintf(fp, USAGE_MAN_TAIL("pidof(1)"));
|
|
|
|
|
|
|
|
exit(fp == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int is_omitted (pid_t pid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < omit_count; i++) {
|
|
|
|
if (pid == omitted_procs[i].pid) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *get_basename (char *filename)
|
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
pos = result = filename;
|
|
|
|
while (*pos != '\0') {
|
|
|
|
if (*(pos++) == '/') result = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *pid_link (pid_t pid, const char *base_name)
|
|
|
|
{
|
2015-09-26 10:07:38 +05:30
|
|
|
char link [1000];
|
2013-10-05 00:05:01 +05:30
|
|
|
char *result;
|
2015-07-12 01:00:31 +05:30
|
|
|
ssize_t path_alloc_size;
|
|
|
|
ssize_t len;
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
snprintf(link, sizeof(link), "/proc/%d/%s", pid, base_name);
|
|
|
|
|
|
|
|
len = path_alloc_size = 0;
|
|
|
|
result = NULL;
|
|
|
|
do {
|
2015-07-12 01:00:31 +05:30
|
|
|
grow_size(path_alloc_size);
|
|
|
|
result = xrealloc(result, path_alloc_size);
|
2013-10-05 00:05:01 +05:30
|
|
|
|
2015-07-12 01:00:31 +05:30
|
|
|
if ((len = readlink(link, result, path_alloc_size)) < 0) {
|
2013-10-05 00:05:01 +05:30
|
|
|
len = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (len == path_alloc_size);
|
|
|
|
|
|
|
|
result[len] = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void select_procs (void)
|
|
|
|
{
|
2016-07-21 10:30:00 +05:30
|
|
|
enum pids_item items[] = { PIDS_ID_PID, PIDS_CMD, PIDS_CMDLINE_V };
|
2015-08-26 10:30:00 +05:30
|
|
|
enum rel_items { rel_pid, rel_cmd, rel_cmdline };
|
2016-07-21 10:30:00 +05:30
|
|
|
struct pids_info *info = NULL;
|
2015-08-26 10:30:00 +05:30
|
|
|
struct pids_stack *stack;
|
2013-11-04 20:56:19 +05:30
|
|
|
int match;
|
2013-10-05 00:05:01 +05:30
|
|
|
static int size = 0;
|
|
|
|
char *cmd_arg0, *cmd_arg0base;
|
|
|
|
char *cmd_arg1, *cmd_arg1base;
|
|
|
|
char *program_base;
|
|
|
|
char *root_link;
|
|
|
|
char *exe_link;
|
|
|
|
char *exe_link_base;
|
|
|
|
|
|
|
|
/* get the input base name */
|
|
|
|
program_base = get_basename(program);
|
|
|
|
|
2016-05-14 10:30:00 +05:30
|
|
|
procps_pids_new(&info, items, 3);
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
exe_link = root_link = NULL;
|
2016-07-21 10:30:00 +05:30
|
|
|
while ((stack = procps_pids_get(info, PIDS_FETCH_TASKS_ONLY))) {
|
2016-08-05 10:30:00 +05:30
|
|
|
char *p_cmd = PIDS_VAL(rel_cmd, str, stack, info),
|
|
|
|
**p_cmdline = PIDS_VAL(rel_cmdline, strv, stack, info);
|
|
|
|
int tid = PIDS_VAL(rel_pid, s_int, stack, info);
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
if (opt_rootdir_check) {
|
|
|
|
/* get the /proc/<pid>/root symlink value */
|
2015-08-26 10:30:00 +05:30
|
|
|
root_link = pid_link(tid, "root");
|
2013-10-05 00:05:01 +05:30
|
|
|
match = !strcmp(pidof_root, root_link);
|
|
|
|
safe_free(root_link);
|
|
|
|
|
|
|
|
if (!match) { /* root check failed */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:11:48 +05:30
|
|
|
if (!is_omitted(tid)) {
|
2013-10-05 00:05:01 +05:30
|
|
|
|
2020-04-07 17:11:48 +05:30
|
|
|
cmd_arg0 = (p_cmdline && *p_cmdline) ? *p_cmdline : "\0";
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
/* processes starting with '-' are login shells */
|
|
|
|
if (*cmd_arg0 == '-') {
|
|
|
|
cmd_arg0++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the argv0 base name */
|
|
|
|
cmd_arg0base = get_basename(cmd_arg0);
|
|
|
|
|
|
|
|
/* get the /proc/<pid>/exe symlink value */
|
2015-08-26 10:30:00 +05:30
|
|
|
exe_link = pid_link(tid, "exe");
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
/* get the exe_link base name */
|
|
|
|
exe_link_base = get_basename(exe_link);
|
|
|
|
|
|
|
|
match = 0;
|
|
|
|
|
|
|
|
if (!strcmp(program, cmd_arg0base) ||
|
|
|
|
!strcmp(program_base, cmd_arg0) ||
|
|
|
|
!strcmp(program, cmd_arg0) ||
|
|
|
|
!strcmp(program, exe_link_base) ||
|
|
|
|
!strcmp(program, exe_link))
|
|
|
|
{
|
|
|
|
match = 1;
|
|
|
|
|
2020-04-07 17:11:48 +05:30
|
|
|
} else if (opt_scripts_too && p_cmdline && *(p_cmdline+1)) {
|
2013-10-05 00:05:01 +05:30
|
|
|
|
1970-01-01 05:30:00 +05:30
|
|
|
cmd_arg1 = *(p_cmdline+1);
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
/* get the arg1 base name */
|
1970-01-01 05:30:00 +05:30
|
|
|
cmd_arg1base = get_basename(cmd_arg1);
|
2013-10-05 00:05:01 +05:30
|
|
|
|
2015-08-26 10:30:00 +05:30
|
|
|
/* if script, then cmd = argv1, otherwise cmd = argv0 */
|
|
|
|
if (p_cmd &&
|
|
|
|
!strncmp(p_cmd, cmd_arg1base, strlen(p_cmd)) &&
|
2013-10-05 00:05:01 +05:30
|
|
|
(!strcmp(program, cmd_arg1base) ||
|
|
|
|
!strcmp(program_base, cmd_arg1) ||
|
|
|
|
!strcmp(program, cmd_arg1)))
|
|
|
|
{
|
|
|
|
match = 1;
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 12:25:44 +05:30
|
|
|
/* If there is a space in arg0 then process probably has
|
|
|
|
* setproctitle so use the cmdline
|
|
|
|
*/
|
|
|
|
if (!match && strchr(cmd_arg0, ' ')) {
|
|
|
|
match = (strcmp(program, p_cmd)==0);
|
|
|
|
}
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
safe_free(exe_link);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
if (proc_count == size) {
|
|
|
|
grow_size(size);
|
|
|
|
procs = xrealloc(procs, size * (sizeof *procs));
|
|
|
|
}
|
|
|
|
if (procs) {
|
2015-08-26 10:30:00 +05:30
|
|
|
procs[proc_count++].pid = tid;
|
2013-10-05 00:05:01 +05:30
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("internal error"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:30:00 +05:30
|
|
|
procps_pids_unref(&info);
|
2013-10-05 00:05:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_to_omit_list (char *input_arg)
|
|
|
|
{
|
|
|
|
static int omit_size = 0;
|
|
|
|
|
|
|
|
char *omit_str;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
pid_t omit_pid;
|
|
|
|
|
|
|
|
omit_str = NULL;
|
2013-10-14 19:08:33 +05:30
|
|
|
omit_str = strtok(input_arg, ",;:");
|
2013-10-05 00:05:01 +05:30
|
|
|
while (omit_str) {
|
|
|
|
|
2013-10-14 19:08:33 +05:30
|
|
|
if (!strcmp(omit_str,"%PPID")) { /* keeping this %PPID garbage for backward compatibility only */
|
|
|
|
omit_pid = getppid(); /* ... as it can be replaced with $$ in common shells */
|
|
|
|
endptr = omit_str + sizeof("%PPID") - 1;
|
|
|
|
} else {
|
|
|
|
omit_pid = strtoul(omit_str, &endptr, 10);
|
|
|
|
}
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
if (*endptr == '\0') {
|
|
|
|
if (omit_count == omit_size) {
|
|
|
|
grow_size(omit_size);
|
|
|
|
omitted_procs = xrealloc(omitted_procs, omit_size * sizeof(*omitted_procs));
|
|
|
|
}
|
|
|
|
if (omitted_procs) {
|
|
|
|
omitted_procs[omit_count++].pid = omit_pid;
|
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("internal error"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xwarnx(_("illegal omit pid value (%s)!\n"), omit_str);
|
|
|
|
}
|
|
|
|
|
2013-10-14 19:08:33 +05:30
|
|
|
omit_str = strtok(NULL, ",;:");
|
2013-10-05 00:05:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
signed int i;
|
|
|
|
int found = 0;
|
|
|
|
int first_pid = 1;
|
|
|
|
|
pidof: allow to change a separator put between pids
I frequency use pidof command with strace system call tracer.
strace can trace MULTIPLE processes specified with "-p $PID"
arguments like:
strace -p 1 -p 1030 -p 3043
Sometimes I want to do as following
strace -p $(pidof httpd)
However, above command line doesn't work because -p option
is needed for specifying a pid. pidof uses a whitespace as
a separator. For passing the output to strace, the separator
should be replaced with ' -p '.
This maybe not a special to my use case.
This commit introduces -S option that allows a user to specify a
separator the one wants.
$ ./pidof bash
./pidof bash
24624 18790 12786 11898 11546 10766 7654 5095
$ ./pidof -S ',' bash
./pidof -S ',' bash
24624,18790,12786,11898,11546,10766,7654,5095
$ ./pidof -S '-p ' bash
./pidof -S '-p ' bash
24624-p 18790-p 12786-p 11898-p 11546-p 10766-p 7654-p 5095
$ ./pidof -S ' -p ' bash
./pidof -S ' -p ' bash
24624 -p 18790 -p 12786 -p 11898 -p 11546 -p 10766 -p 7654 -p 5095
$ strace -p $(./pidof -S ' -p ' bash)
strace -p $(./pidof -S ' -p ' bash)
strace: Process 24624 attached
strace: Process 18790 attached
strace: Process 12786 attached
...
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2018-02-24 14:33:11 +05:30
|
|
|
const char *separator = " ";
|
2019-09-21 11:47:05 +05:30
|
|
|
const char *opts = "scdnxmo:S:?Vh";
|
2013-10-05 00:05:01 +05:30
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
2013-10-07 21:47:19 +05:30
|
|
|
{"check-root", no_argument, NULL, 'c'},
|
2013-10-05 00:05:01 +05:30
|
|
|
{"single-shot", no_argument, NULL, 's'},
|
|
|
|
{"omit-pid", required_argument, NULL, 'o'},
|
2019-09-21 11:47:05 +05:30
|
|
|
{"separator", required_argument, NULL, 'S'},
|
2013-10-05 00:05:01 +05:30
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
|
|
|
program_invocation_name = program_invocation_short_name;
|
|
|
|
#endif
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
|
|
/* process command-line options */
|
|
|
|
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
opt_single_shot = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
add_to_omit_list (optarg);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
opt_scripts_too = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
opt_rootdir_check = 1;
|
1970-01-01 05:30:00 +05:30
|
|
|
safe_free(pidof_root);
|
2013-10-05 00:05:01 +05:30
|
|
|
pidof_root = pid_link(getpid(), "root");
|
|
|
|
}
|
|
|
|
break;
|
2019-09-21 11:47:05 +05:30
|
|
|
case 'd': /* sysv pidof uses this for S */
|
pidof: allow to change a separator put between pids
I frequency use pidof command with strace system call tracer.
strace can trace MULTIPLE processes specified with "-p $PID"
arguments like:
strace -p 1 -p 1030 -p 3043
Sometimes I want to do as following
strace -p $(pidof httpd)
However, above command line doesn't work because -p option
is needed for specifying a pid. pidof uses a whitespace as
a separator. For passing the output to strace, the separator
should be replaced with ' -p '.
This maybe not a special to my use case.
This commit introduces -S option that allows a user to specify a
separator the one wants.
$ ./pidof bash
./pidof bash
24624 18790 12786 11898 11546 10766 7654 5095
$ ./pidof -S ',' bash
./pidof -S ',' bash
24624,18790,12786,11898,11546,10766,7654,5095
$ ./pidof -S '-p ' bash
./pidof -S '-p ' bash
24624-p 18790-p 12786-p 11898-p 11546-p 10766-p 7654-p 5095
$ ./pidof -S ' -p ' bash
./pidof -S ' -p ' bash
24624 -p 18790 -p 12786 -p 11898 -p 11546 -p 10766 -p 7654 -p 5095
$ strace -p $(./pidof -S ' -p ' bash)
strace -p $(./pidof -S ' -p ' bash)
strace: Process 24624 attached
strace: Process 18790 attached
strace: Process 12786 attached
...
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2018-02-24 14:33:11 +05:30
|
|
|
case 'S':
|
|
|
|
separator = optarg;
|
|
|
|
break;
|
2013-10-05 00:05:01 +05:30
|
|
|
case 'V':
|
|
|
|
printf (PROCPS_NG_VERSION);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage (opt);
|
|
|
|
break;
|
|
|
|
/* compatibility-only switches */
|
|
|
|
case 'n': /* avoiding stat(2) on NFS volumes doesn't make any sense anymore ... */
|
|
|
|
/* ... as this reworked solution does not use stat(2) at all */
|
|
|
|
case 'm': /* omitting relatives with argv[0] & argv[1] matching the argv[0] & argv[1] ...*/
|
|
|
|
/* ... of explicitly omitted PIDs is too 'expensive' and as we don't know */
|
|
|
|
/* ... wheter it is still needed, we won't re-implement it unless ... */
|
|
|
|
/* ... somebody gives us a good reason to do so :) */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* main loop */
|
|
|
|
while (argc - optind) { /* for each program */
|
|
|
|
|
|
|
|
program = argv[optind++];
|
|
|
|
|
|
|
|
select_procs(); /* get the list of matching processes */
|
|
|
|
|
|
|
|
if (proc_count) {
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
for (i = proc_count - 1; i >= 0; i--) { /* and display their PIDs */
|
|
|
|
if (first_pid) {
|
|
|
|
first_pid = 0;
|
2013-11-04 20:56:19 +05:30
|
|
|
printf ("%ld", (long) procs[i].pid);
|
2013-10-05 00:05:01 +05:30
|
|
|
} else {
|
pidof: allow to change a separator put between pids
I frequency use pidof command with strace system call tracer.
strace can trace MULTIPLE processes specified with "-p $PID"
arguments like:
strace -p 1 -p 1030 -p 3043
Sometimes I want to do as following
strace -p $(pidof httpd)
However, above command line doesn't work because -p option
is needed for specifying a pid. pidof uses a whitespace as
a separator. For passing the output to strace, the separator
should be replaced with ' -p '.
This maybe not a special to my use case.
This commit introduces -S option that allows a user to specify a
separator the one wants.
$ ./pidof bash
./pidof bash
24624 18790 12786 11898 11546 10766 7654 5095
$ ./pidof -S ',' bash
./pidof -S ',' bash
24624,18790,12786,11898,11546,10766,7654,5095
$ ./pidof -S '-p ' bash
./pidof -S '-p ' bash
24624-p 18790-p 12786-p 11898-p 11546-p 10766-p 7654-p 5095
$ ./pidof -S ' -p ' bash
./pidof -S ' -p ' bash
24624 -p 18790 -p 12786 -p 11898 -p 11546 -p 10766 -p 7654 -p 5095
$ strace -p $(./pidof -S ' -p ' bash)
strace -p $(./pidof -S ' -p ' bash)
strace: Process 24624 attached
strace: Process 18790 attached
strace: Process 12786 attached
...
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2018-02-24 14:33:11 +05:30
|
|
|
printf ("%s%ld", separator, (long) procs[i].pid);
|
2013-10-05 00:05:01 +05:30
|
|
|
}
|
|
|
|
if (opt_single_shot) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* final line feed */
|
|
|
|
if (found) printf("\n");
|
|
|
|
|
|
|
|
/* some cleaning */
|
|
|
|
safe_free(procs);
|
|
|
|
safe_free(omitted_procs);
|
|
|
|
safe_free(pidof_root);
|
|
|
|
|
|
|
|
return !found;
|
|
|
|
}
|