sysvinit-2.88-omit.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. diff --git a/man/pidof.8 b/man/pidof.8
  2. index 276a93c..ac5f58a 100644
  3. --- a/man/pidof.8
  4. +++ b/man/pidof.8
  5. @@ -24,6 +24,7 @@ pidof -- find the process ID of a running program.
  6. .RB [ \-c ]
  7. .RB [ \-n ]
  8. .RB [ \-x ]
  9. +.RB [ \-m ]
  10. .RB [ \-o
  11. .IR omitpid[,omitpid..] ]
  12. .RB [ \-o
  13. @@ -63,6 +64,11 @@ shells running the named scripts.
  14. Tells \fIpidof\fP to omit processes with that process id. The special
  15. pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP
  16. program, in other words the calling shell or shell script.
  17. +.IP -m
  18. +When used with -o, will also omit any processes that have the same
  19. +argv[0] and argv[1] as any explicitly omitted process ids. This can be
  20. +used to avoid multiple shell scripts concurrently calling pidof returning
  21. +each other's pids.
  22. .SH "EXIT STATUS"
  23. .TP
  24. .B 0
  25. diff --git a/src/killall5.c b/src/killall5.c
  26. index 5937d98..e73885e 100644
  27. --- a/src/killall5.c
  28. +++ b/src/killall5.c
  29. @@ -118,6 +118,7 @@ typedef struct _s_nfs
  30. /* List of processes. */
  31. PROC *plist;
  32. +PROC *olist;
  33. /* List of processes to omit. */
  34. OMIT *omit;
  35. @@ -345,6 +346,20 @@ static void clear_mnt(void)
  36. }
  37. }
  38. +static void clear_omit(void)
  39. +{
  40. + OMIT *o;
  41. + PROC *p;
  42. + for (o = omit; o; o = omit) {
  43. + omit = omit->next;
  44. + free(o);
  45. + }
  46. + for (p = olist; p; p = olist) {
  47. + olist = olist->next;
  48. + free(p);
  49. + }
  50. +}
  51. +
  52. /*
  53. * Check if path is ia shadow off a NFS partition.
  54. */
  55. @@ -452,6 +467,7 @@ int readproc(int do_stat)
  56. DIR *dir;
  57. FILE *fp;
  58. PROC *p, *n;
  59. + OMIT *o, *m;
  60. struct dirent *d;
  61. struct stat st;
  62. char path[PATH_MAX+1];
  63. @@ -624,6 +640,17 @@ int readproc(int do_stat)
  64. p->next = plist;
  65. plist = p;
  66. p->pid = pid;
  67. + /* Could be smarter, but it's a small list. */
  68. + m = omit;
  69. + for (o = omit; m; o = m) {
  70. + m = o->next;
  71. + if (o->pid == p->pid) {
  72. + n = (PROC*)xmalloc(sizeof(PROC));
  73. + *n = *p;
  74. + n->next = olist;
  75. + olist = n;
  76. + }
  77. + }
  78. }
  79. closedir(dir);
  80. @@ -813,6 +840,26 @@ PIDQ_HEAD *pidof(char *prog)
  81. return q;
  82. }
  83. +int matches(PROC *o, PROC *p)
  84. +{
  85. + int ret = 0;
  86. + char *oargv1, *pargv1;
  87. + if ((o->argv0 && p->argv0 && !strcmp(o->argv0,p->argv0))) {
  88. + if (o->argv1 && p->argv1) {
  89. + if ((oargv1 = canonicalize_file_name(o->argv1)) == NULL)
  90. + oargv1 = strdup(o->argv1);
  91. + if ((pargv1 = canonicalize_file_name(p->argv1)) == NULL)
  92. + pargv1 = strdup(p->argv1);
  93. + if (! strcmp(oargv1, pargv1)) {
  94. + ret = 1;
  95. + }
  96. + free(oargv1);
  97. + free(pargv1);
  98. + }
  99. + }
  100. + return ret;
  101. +}
  102. +
  103. /* Give usage message and exit. */
  104. void usage(void)
  105. {
  106. @@ -845,6 +892,7 @@ void nsyslog(int pri, char *fmt, ...)
  107. #define PIDOF_SINGLE 0x01
  108. #define PIDOF_OMIT 0x02
  109. #define PIDOF_NETFS 0x04
  110. +#define PIDOF_OMIT_OMIT_MATCHES 0x08
  111. /*
  112. * Pidof functionality.
  113. @@ -861,6 +909,7 @@ int main_pidof(int argc, char **argv)
  114. struct stat st;
  115. char tmp[512];
  116. + olist = (PROC*)0;
  117. omit = (OMIT*)0;
  118. nlist = (NFS*)0;
  119. opterr = 0;
  120. @@ -868,7 +917,7 @@ int main_pidof(int argc, char **argv)
  121. if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
  122. flags |= PIDOF_NETFS;
  123. - while ((opt = getopt(argc,argv,"hco:sxn")) != EOF) switch (opt) {
  124. + while ((opt = getopt(argc,argv,"hcmo:sxn")) != EOF) switch (opt) {
  125. case '?':
  126. nsyslog(LOG_ERR,"invalid options on command line!\n");
  127. closelog();
  128. @@ -907,6 +956,9 @@ int main_pidof(int argc, char **argv)
  129. case 'x':
  130. scripts_too++;
  131. break;
  132. + case 'm':
  133. + flags |= PIDOF_OMIT_OMIT_MATCHES;
  134. + break;
  135. case 'n':
  136. flags |= PIDOF_NETFS;
  137. break;
  138. @@ -938,10 +990,13 @@ int main_pidof(int argc, char **argv)
  139. pid_t spid = 0;
  140. while ((p = get_next_from_pid_q(q))) {
  141. if ((flags & PIDOF_OMIT) && omit) {
  142. - OMIT * optr;
  143. - for (optr = omit; optr; optr = optr->next) {
  144. + PROC * optr;
  145. + for (optr = olist; optr; optr = optr->next) {
  146. if (optr->pid == p->pid)
  147. break;
  148. + if (flags & PIDOF_OMIT_OMIT_MATCHES)
  149. + if (matches(optr, p))
  150. + break;
  151. }
  152. /*
  153. @@ -977,6 +1032,7 @@ int main_pidof(int argc, char **argv)
  154. if (!first)
  155. printf("\n");
  156. + clear_omit();
  157. clear_mnt();
  158. closelog();