PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/pidof.c

https://gitlab.com/tpetazzoni/procps
C | 378 lines | 259 code | 79 blank | 40 comment | 57 complexity | a9b5df1e0be3e310bb0e9cfa21ba2d12 MD5 | raw file
  1. /*
  2. * pidof.c - Utility for listing pids of running processes
  3. *
  4. * Copyright (C) 2013 Jaromir Capik <jcapik@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include "c.h"
  23. #include "fileutils.h"
  24. #include "nls.h"
  25. #include "xalloc.h"
  26. #include "proc/readproc.h"
  27. #include "proc/version.h" /* procps_version */
  28. #define grow_size(x) (x = x * 5 / 4 + 1024)
  29. #define safe_free(x) if (x) { free(x); x=NULL; }
  30. struct el {
  31. pid_t pid;
  32. };
  33. struct el *procs = NULL;
  34. static int proc_count = 0;
  35. struct el *omitted_procs = NULL;
  36. static int omit_count = 0;
  37. static char *program = NULL;
  38. /* switch flags */
  39. static int opt_single_shot = 0; /* -s */
  40. static int opt_scripts_too = 0; /* -x */
  41. static int opt_rootdir_check = 0; /* -c */
  42. static char *pidof_root = NULL;
  43. static int __attribute__ ((__noreturn__)) usage(int opt)
  44. {
  45. int err = (opt == '?');
  46. FILE *fp = err ? stderr : stdout;
  47. fputs(USAGE_HEADER, fp);
  48. fprintf(fp, _(" %s [options] [program [...]]\n"), program_invocation_short_name);
  49. fputs(USAGE_OPTIONS, fp);
  50. fputs(_(" -s, --single-shot return one PID only\n"), fp);
  51. fputs(_(" -c, --check-root omit processes with different root\n"), fp);
  52. fputs(_(" -x also find shells running the named scripts\n"), fp);
  53. fputs(_(" -o, --omit-pid <PID,...> omit processes with PID\n"), fp);
  54. fputs(USAGE_SEPARATOR, fp);
  55. fputs(USAGE_HELP, fp);
  56. fputs(USAGE_VERSION, fp);
  57. fprintf(fp, USAGE_MAN_TAIL("pidof(1)"));
  58. exit(fp == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
  59. }
  60. static int is_omitted (pid_t pid)
  61. {
  62. int i;
  63. for (i = 0; i < omit_count; i++) {
  64. if (pid == omitted_procs[i].pid) return 1;
  65. }
  66. return 0;
  67. }
  68. static char *get_basename (char *filename)
  69. {
  70. char *pos;
  71. char *result;
  72. pos = result = filename;
  73. while (*pos != '\0') {
  74. if (*(pos++) == '/') result = pos;
  75. }
  76. return result;
  77. }
  78. static char *pid_link (pid_t pid, const char *base_name)
  79. {
  80. char link [PROCPATHLEN];
  81. char *result;
  82. ssize_t path_alloc_size;
  83. ssize_t len;
  84. snprintf(link, sizeof(link), "/proc/%d/%s", pid, base_name);
  85. len = path_alloc_size = 0;
  86. result = NULL;
  87. do {
  88. grow_size(path_alloc_size);
  89. result = xrealloc(result, path_alloc_size);
  90. if ((len = readlink(link, result, path_alloc_size)) < 0) {
  91. len = 0;
  92. break;
  93. }
  94. } while (len == path_alloc_size);
  95. result[len] = '\0';
  96. return result;
  97. }
  98. static void select_procs (void)
  99. {
  100. PROCTAB *ptp;
  101. proc_t task;
  102. int match;
  103. static int size = 0;
  104. char *cmd_arg0, *cmd_arg0base;
  105. char *cmd_arg1, *cmd_arg1base;
  106. char *pos;
  107. char *program_base;
  108. char *root_link;
  109. char *exe_link;
  110. char *exe_link_base;
  111. /* get the input base name */
  112. program_base = get_basename(program);
  113. ptp = openproc (PROC_FILLCOM | PROC_FILLSTAT);
  114. exe_link = root_link = NULL;
  115. memset(&task, 0, sizeof (task));
  116. while(readproc(ptp, &task)) {
  117. if (opt_rootdir_check) {
  118. /* get the /proc/<pid>/root symlink value */
  119. root_link = pid_link(task.XXXID, "root");
  120. match = !strcmp(pidof_root, root_link);
  121. safe_free(root_link);
  122. if (!match) { /* root check failed */
  123. memset (&task, 0, sizeof (task));
  124. continue;
  125. }
  126. }
  127. if (!is_omitted(task.XXXID) && task.cmdline) {
  128. cmd_arg0 = *task.cmdline;
  129. /* processes starting with '-' are login shells */
  130. if (*cmd_arg0 == '-') {
  131. cmd_arg0++;
  132. }
  133. /* get the argv0 base name */
  134. cmd_arg0base = get_basename(cmd_arg0);
  135. /* get the /proc/<pid>/exe symlink value */
  136. exe_link = pid_link(task.XXXID, "exe");
  137. /* get the exe_link base name */
  138. exe_link_base = get_basename(exe_link);
  139. match = 0;
  140. if (!strcmp(program, cmd_arg0base) ||
  141. !strcmp(program_base, cmd_arg0) ||
  142. !strcmp(program, cmd_arg0) ||
  143. !strcmp(program, exe_link_base) ||
  144. !strcmp(program, exe_link))
  145. {
  146. match = 1;
  147. } else if (opt_scripts_too && *(task.cmdline+1)) {
  148. pos = cmd_arg1base = cmd_arg1 = *(task.cmdline+1);
  149. /* get the arg1 base name */
  150. while (*pos != '\0') {
  151. if (*(pos++) == '/') cmd_arg1base = pos;
  152. }
  153. /* if script, then task.cmd = argv1, otherwise task.cmd = argv0 */
  154. if (task.cmd &&
  155. !strncmp(task.cmd, cmd_arg1base, strlen(task.cmd)) &&
  156. (!strcmp(program, cmd_arg1base) ||
  157. !strcmp(program_base, cmd_arg1) ||
  158. !strcmp(program, cmd_arg1)))
  159. {
  160. match = 1;
  161. }
  162. }
  163. /* If there is a space in arg0 then process probably has
  164. * setproctitle so use the cmdline
  165. */
  166. if (!match && strchr(cmd_arg0, ' ')) {
  167. match = (strcmp(program, task.cmd)==0);
  168. }
  169. safe_free(exe_link);
  170. if (match) {
  171. if (proc_count == size) {
  172. grow_size(size);
  173. procs = xrealloc(procs, size * (sizeof *procs));
  174. }
  175. if (procs) {
  176. procs[proc_count++].pid = task.XXXID;
  177. } else {
  178. xerrx(EXIT_FAILURE, _("internal error"));
  179. }
  180. }
  181. }
  182. memset (&task, 0, sizeof (task));
  183. }
  184. closeproc (ptp);
  185. }
  186. static void add_to_omit_list (char *input_arg)
  187. {
  188. static int omit_size = 0;
  189. char *omit_str;
  190. char *endptr;
  191. pid_t omit_pid;
  192. omit_str = NULL;
  193. omit_str = strtok(input_arg, ",;:");
  194. while (omit_str) {
  195. if (!strcmp(omit_str,"%PPID")) { /* keeping this %PPID garbage for backward compatibility only */
  196. omit_pid = getppid(); /* ... as it can be replaced with $$ in common shells */
  197. endptr = omit_str + sizeof("%PPID") - 1;
  198. } else {
  199. omit_pid = strtoul(omit_str, &endptr, 10);
  200. }
  201. if (*endptr == '\0') {
  202. if (omit_count == omit_size) {
  203. grow_size(omit_size);
  204. omitted_procs = xrealloc(omitted_procs, omit_size * sizeof(*omitted_procs));
  205. }
  206. if (omitted_procs) {
  207. omitted_procs[omit_count++].pid = omit_pid;
  208. } else {
  209. xerrx(EXIT_FAILURE, _("internal error"));
  210. }
  211. } else {
  212. xwarnx(_("illegal omit pid value (%s)!\n"), omit_str);
  213. }
  214. omit_str = strtok(NULL, ",;:");
  215. }
  216. }
  217. int main (int argc, char **argv)
  218. {
  219. int opt;
  220. signed int i;
  221. int found = 0;
  222. int first_pid = 1;
  223. const char *opts = "scnxmo:?Vh";
  224. static const struct option longopts[] = {
  225. {"check-root", no_argument, NULL, 'c'},
  226. {"single-shot", no_argument, NULL, 's'},
  227. {"omit-pid", required_argument, NULL, 'o'},
  228. {"help", no_argument, NULL, 'h'},
  229. {"version", no_argument, NULL, 'V'},
  230. {NULL, 0, NULL, 0}
  231. };
  232. #ifdef HAVE_PROGRAM_INVOCATION_NAME
  233. program_invocation_name = program_invocation_short_name;
  234. #endif
  235. setlocale (LC_ALL, "");
  236. bindtextdomain (PACKAGE, LOCALEDIR);
  237. textdomain (PACKAGE);
  238. atexit (close_stdout);
  239. /* process command-line options */
  240. while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != -1) {
  241. switch (opt) {
  242. case 's':
  243. opt_single_shot = 1;
  244. break;
  245. case 'o':
  246. add_to_omit_list (optarg);
  247. break;
  248. case 'x':
  249. opt_scripts_too = 1;
  250. break;
  251. case 'c':
  252. if (geteuid() == 0) {
  253. opt_rootdir_check = 1;
  254. pidof_root = pid_link(getpid(), "root");
  255. }
  256. break;
  257. case 'V':
  258. printf (PROCPS_NG_VERSION);
  259. exit (EXIT_SUCCESS);
  260. case 'h':
  261. case '?':
  262. usage (opt);
  263. break;
  264. /* compatibility-only switches */
  265. case 'n': /* avoiding stat(2) on NFS volumes doesn't make any sense anymore ... */
  266. /* ... as this reworked solution does not use stat(2) at all */
  267. case 'm': /* omitting relatives with argv[0] & argv[1] matching the argv[0] & argv[1] ...*/
  268. /* ... of explicitly omitted PIDs is too 'expensive' and as we don't know */
  269. /* ... wheter it is still needed, we won't re-implement it unless ... */
  270. /* ... somebody gives us a good reason to do so :) */
  271. break;
  272. }
  273. }
  274. /* main loop */
  275. while (argc - optind) { /* for each program */
  276. program = argv[optind++];
  277. select_procs(); /* get the list of matching processes */
  278. if (proc_count) {
  279. found = 1;
  280. for (i = proc_count - 1; i >= 0; i--) { /* and display their PIDs */
  281. if (first_pid) {
  282. first_pid = 0;
  283. printf ("%ld", (long) procs[i].pid);
  284. } else {
  285. printf (" %ld", (long) procs[i].pid);
  286. }
  287. if (opt_single_shot) break;
  288. }
  289. proc_count = 0;
  290. }
  291. }
  292. /* final line feed */
  293. if (found) printf("\n");
  294. /* some cleaning */
  295. safe_free(procs);
  296. safe_free(omitted_procs);
  297. safe_free(pidof_root);
  298. return !found;
  299. }