pidof.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * pidof - Find and print process IDs
  7. *
  8. */
  9. #include <sys/stat.h>
  10. #include <sys/signal.h>
  11. #include <fcntl.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <dirent.h>
  18. #include <signal.h>
  19. typedef struct process {
  20. int pid;
  21. int ppid;
  22. int tgid;
  23. char name[100];
  24. char path[200];
  25. } p_t;
  26. #define LINE_LEN 4096
  27. p_t * build_entry(struct dirent * dent) {
  28. char tmp[256];
  29. FILE * f;
  30. char line[LINE_LEN];
  31. sprintf(tmp, "/proc/%s/status", dent->d_name);
  32. f = fopen(tmp, "r");
  33. p_t * proc = malloc(sizeof(p_t));
  34. while (fgets(line, LINE_LEN, f) != NULL) {
  35. char * n = strstr(line,"\n");
  36. if (n) { *n = '\0'; }
  37. char * tab = strstr(line,"\t");
  38. if (tab) {
  39. *tab = '\0';
  40. tab++;
  41. }
  42. if (strstr(line, "Pid:") == line) {
  43. proc->pid = atoi(tab);
  44. } else if (strstr(line, "PPid:") == line) {
  45. proc->ppid = atoi(tab);
  46. } else if (strstr(line, "Tgid:") == line) {
  47. proc->tgid = atoi(tab);
  48. } else if (strstr(line, "Name:") == line) {
  49. strcpy(proc->name, tab);
  50. } else if (strstr(line, "Path:") == line) {
  51. strcpy(proc->path, tab);
  52. }
  53. }
  54. if (strstr(proc->name,"python") == proc->name) {
  55. char * name = proc->path + strlen(proc->path) - 1;
  56. while (1) {
  57. if (*name == '/') {
  58. name++;
  59. break;
  60. }
  61. if (name == proc->name) break;
  62. name--;
  63. }
  64. memcpy(proc->name, name, strlen(name)+1);
  65. }
  66. if (proc->tgid != proc->pid) {
  67. char tmp[100] = {0};
  68. sprintf(tmp, "{%s}", proc->name);
  69. memcpy(proc->name, tmp, strlen(tmp)+1);
  70. }
  71. fclose(f);
  72. return proc;
  73. }
  74. int main (int argc, char * argv[]) {
  75. if (argc < 2) return 1;
  76. int space = 0;
  77. /* Open the directory */
  78. DIR * dirp = opendir("/proc");
  79. int found_something = 0;
  80. struct dirent * ent = readdir(dirp);
  81. while (ent != NULL) {
  82. if (ent->d_name[0] >= '0' && ent->d_name[0] <= '9') {
  83. p_t * proc = build_entry(ent);
  84. if (!strcmp(proc->name, argv[optind])) {
  85. if (space++) printf(" ");
  86. printf("%d", proc->pid);
  87. found_something = 1;
  88. }
  89. }
  90. ent = readdir(dirp);
  91. }
  92. closedir(dirp);
  93. if (!found_something) {
  94. return 1;
  95. }
  96. printf("\n");
  97. return 0;
  98. }