/*===========================================================================* * rdlink_hook * *===========================================================================*/ int rdlink_hook(struct inode *node, char *ptr, size_t max, cbdata_t UNUSED(cbdata)) { /* Symbolic link resolution hook. Not used yet. */ struct inode *parent; /* Get the parent inode. */ parent = get_parent_inode(node); /* If the parent inode is a pid directory, call the pid handler. */ if (parent != NULL && dir_is_pid(parent)) pid_link(node, ptr, max); return OK; }
static void select_procs(void) { static size_t size = 0; PROCTAB* ptp; proc_t* task; size_t match; char* cmd_arg0; char* cmd_arg0_base; char* cmd_arg1; char* cmd_arg1_base; char* program_base; char* root_link; char* exe_link; char* exe_link_base; program_base = basename(program); /* get the input base name */ ptp = openproc(PROC_FILLCOM | PROC_FILLSTAT); while ((task = readproc(ptp, NULL))) { if (epidof_root) { /* get the /proc/<pid>/root symlink value */ root_link = pid_link(task->XXXID, "root"); match = !strcmp(epidof_root, root_link); xfree(root_link); if (!match) /* root check failed */ { freeproc(task); continue; } } if (!is_omitted(task->XXXID) && task->cmdline) { cmd_arg0 = task->cmdline[0]; /* processes starting with '-' are login shells */ if (*cmd_arg0 == '-') cmd_arg0++; cmd_arg0_base = basename(cmd_arg0); /* get the argv0 basename */ exe_link = pid_link(task->XXXID, "exe"); /* get the /proc/<pid>/exe symlink value */ exe_link_base = basename(exe_link); /* get the exe_link basename */ match = 0; #define __test(p, c) (!strcmp(p, c##_base) || !strcmp(p, c) || !strcmp(p##_base, c)) if (__test(program, cmd_arg0) || __test(program, exe_link)) match = 1; else if (opt_scripts_too && task->cmdline[1]) { cmd_arg1 = task->cmdline[1]; cmd_arg1_base = basename(cmd_arg1); /* if script, then task->cmd = argv1, otherwise task->cmd = argv0 */ if (task->cmd && !strncmp(task->cmd, cmd_arg1_base, strlen(task->cmd)) && __test(program, cmd_arg1)) match = 1; } #undef __test xfree(exe_link); if (match && environment_test(task->XXXID, *argv)) { if (proc_count == size) procs = xrealloc(procs, __grow(size) * sizeof(*procs)); procs[proc_count++] = task->XXXID; } } freeproc(task); } closeproc(ptp); }
int main(int argc, char** argv_) { int found = 0; int first_pid = 1; char* usage_str; ssize_t i, n; argv = argv_; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); n = (ssize_t)(strlen(_(" [options] [program...]")) + strlen(*argv) + 1); usage_str = alloca((size_t)n * sizeof(char)); sprintf(usage_str, "%s%s", *argv, _(" [options] [program...]")); args_init(!strcmp(argv[0], "dpidof") ? _("epidof with display isolation") : _("pidof with environment constraints"), usage_str, NULL, 0, 1, 0, args_standard_abbreviations); args_add_option(args_new_argumentless(NULL, 0, "-c", "--check-root", NULL), _("Restrict to processes running under the same root")); args_add_option(args_new_argumentless(NULL, 0, "-s", "--single-shot", NULL), _("Return only one process ID")); args_add_option(args_new_argumentless(NULL, 0, "-x", "--scripts", NULL), _("Test the name of scripts")); args_add_option(args_new_argumented (NULL, _("PID"), 0, "-o", "--omit-pid", NULL), _("Do not return a specific process ID")); args_add_option(args_new_argumentless(NULL, 0, "-h", "--help", NULL), _("Display this help information")); args_add_option(args_new_argumentless(NULL, 0, "-V", "--version", NULL), _("Print the name and version of this program")); environment_parse(&argc, argv); args_parse(argc, argv); if (args_unrecognised_count || args_opts_used("-h")) args_help(), fprintf(stderr, "%s\n\n", _(environment_synopsis)); else if (args_opts_used("-V")) printf("%s " VERSION, !strcmp(argv[0], "dpidof") ? "dpidof" : "epidof"); else goto cont; return args_unrecognised_count ? EXIT_FAILURE : EXIT_SUCCESS; cont: /* process command-line options */ if (args_opts_used("-s")) opt_single_shot = 1; if (args_opts_used("-x")) opt_scripts_too = 1; if (args_opts_used("-c") && (geteuid() == 0)) epidof_root = pid_link(getpid(), "root"); if (args_opts_used("-o")) { char** arr = args_opts_get("-o"); for (i = 0, n = (ssize_t)args_opts_get_count("-o"); i < n; i++) add_to_omit_list(arr[i]); } /* main loop */ for (n = 0; n < args_files_count; n++) /* for each program */ { program = args_files[n]; proc_count = 0; select_procs(); /* get the list of matching processes */ if (proc_count > 0) { found = 1; for (i = (ssize_t)proc_count - 1; i >= 0; i--) /* and display their PIDs */ { printf(first_pid ? "%ld" : " %ld", (long)(procs[i])); first_pid = 0; if (opt_single_shot) break; } } } /* final line feed */ if (found) printf("\n"); xfree(procs); xfree(omitted_procs); xfree(epidof_root); args_dispose(); environment_dispose(); return !found; }