Blame SOURCES/sysvinit-2.88-omit.patch

b1e7a3
diff --git a/man/pidof.8 b/man/pidof.8
b1e7a3
index 276a93c..ac5f58a 100644
b1e7a3
--- a/man/pidof.8
b1e7a3
+++ b/man/pidof.8
b1e7a3
@@ -24,6 +24,7 @@ pidof -- find the process ID of a running program.
b1e7a3
 .RB [ \-c ]
b1e7a3
 .RB [ \-n ]
b1e7a3
 .RB [ \-x ]
b1e7a3
+.RB [ \-m ]
b1e7a3
 .RB [ \-o
b1e7a3
 .IR omitpid[,omitpid..] ]
b1e7a3
 .RB [ \-o
b1e7a3
@@ -63,6 +64,11 @@ shells running the named scripts.
b1e7a3
 Tells \fIpidof\fP to omit processes with that process id. The special
b1e7a3
 pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP
b1e7a3
 program, in other words the calling shell or shell script.
b1e7a3
+.IP -m
b1e7a3
+When used with -o, will also omit any processes that have the same
b1e7a3
+argv[0] and argv[1] as any explicitly omitted process ids. This can be
b1e7a3
+used to avoid multiple shell scripts concurrently calling pidof returning
b1e7a3
+each other's pids.
b1e7a3
 .SH "EXIT STATUS"
b1e7a3
 .TP
b1e7a3
 .B 0
b1e7a3
diff --git a/src/killall5.c b/src/killall5.c
b1e7a3
index 5937d98..e73885e 100644
b1e7a3
--- a/src/killall5.c
b1e7a3
+++ b/src/killall5.c
b1e7a3
@@ -118,6 +118,7 @@ typedef struct _s_nfs
b1e7a3
 
b1e7a3
 /* List of processes. */
b1e7a3
 PROC *plist;
b1e7a3
+PROC *olist;
b1e7a3
 
b1e7a3
 /* List of processes to omit. */
b1e7a3
 OMIT *omit;
b1e7a3
@@ -345,6 +346,20 @@ static void clear_mnt(void)
b1e7a3
 	}
b1e7a3
 }
b1e7a3
 
b1e7a3
+static void clear_omit(void)
b1e7a3
+{
b1e7a3
+	OMIT *o;
b1e7a3
+	PROC *p;
b1e7a3
+	for (o = omit; o; o = omit) {
b1e7a3
+		omit = omit->next;
b1e7a3
+		free(o);
b1e7a3
+	}
b1e7a3
+	for (p = olist; p; p = olist) {
b1e7a3
+		olist = olist->next;
b1e7a3
+		free(p);
b1e7a3
+	}
b1e7a3
+}
b1e7a3
+
b1e7a3
 /*
b1e7a3
  *     Check if path is ia shadow off a NFS partition.
b1e7a3
  */
b1e7a3
@@ -452,6 +467,7 @@ int readproc(int do_stat)
b1e7a3
 	DIR		*dir;
b1e7a3
 	FILE		*fp;
b1e7a3
 	PROC		*p, *n;
b1e7a3
+	OMIT		*o, *m;
b1e7a3
 	struct dirent	*d;
b1e7a3
 	struct stat	st;
b1e7a3
 	char		path[PATH_MAX+1];
b1e7a3
@@ -624,6 +640,17 @@ int readproc(int do_stat)
b1e7a3
 		p->next = plist;
b1e7a3
 		plist = p;
b1e7a3
 		p->pid = pid;
b1e7a3
+		/* Could be smarter, but it's a small list. */
b1e7a3
+		m = omit;
b1e7a3
+		for (o = omit; m; o = m) {
b1e7a3
+			m = o->next;
b1e7a3
+			if (o->pid == p->pid) {
b1e7a3
+				n = (PROC*)xmalloc(sizeof(PROC));
b1e7a3
+				*n = *p;
b1e7a3
+				n->next = olist;
b1e7a3
+				olist = n;
b1e7a3
+			}
b1e7a3
+		}
b1e7a3
 	}
b1e7a3
 	closedir(dir);
b1e7a3
 
b1e7a3
@@ -813,6 +840,26 @@ PIDQ_HEAD *pidof(char *prog)
b1e7a3
 	return q;
b1e7a3
 }
b1e7a3
 
b1e7a3
+int matches(PROC *o, PROC *p)
b1e7a3
+{
b1e7a3
+	int ret = 0;
b1e7a3
+	char *oargv1, *pargv1;
b1e7a3
+	if ((o->argv0 && p->argv0 && !strcmp(o->argv0,p->argv0))) {
b1e7a3
+		if (o->argv1 && p->argv1) {
b1e7a3
+			if ((oargv1 = canonicalize_file_name(o->argv1)) == NULL)
b1e7a3
+				oargv1 = strdup(o->argv1);
b1e7a3
+			if ((pargv1 = canonicalize_file_name(p->argv1)) == NULL)
b1e7a3
+				pargv1 = strdup(p->argv1);
b1e7a3
+			if (! strcmp(oargv1, pargv1)) {
b1e7a3
+				ret = 1;
b1e7a3
+			}
b1e7a3
+			free(oargv1);
b1e7a3
+			free(pargv1);
b1e7a3
+		}
b1e7a3
+	}
b1e7a3
+	return ret;
b1e7a3
+}
b1e7a3
+
b1e7a3
 /* Give usage message and exit. */
b1e7a3
 void usage(void)
b1e7a3
 {
b1e7a3
@@ -845,6 +892,7 @@ void nsyslog(int pri, char *fmt, ...)
b1e7a3
 #define PIDOF_SINGLE	0x01
b1e7a3
 #define PIDOF_OMIT	0x02
b1e7a3
 #define PIDOF_NETFS	0x04
b1e7a3
+#define PIDOF_OMIT_OMIT_MATCHES	0x08
b1e7a3
 
b1e7a3
 /*
b1e7a3
  *	Pidof functionality.
b1e7a3
@@ -861,6 +909,7 @@ int main_pidof(int argc, char **argv)
b1e7a3
 	struct stat	st;
b1e7a3
 	char		tmp[512];
b1e7a3
 
b1e7a3
+	olist = (PROC*)0;
b1e7a3
 	omit = (OMIT*)0;
b1e7a3
 	nlist = (NFS*)0;
b1e7a3
 	opterr = 0;
b1e7a3
@@ -868,7 +917,7 @@ int main_pidof(int argc, char **argv)
b1e7a3
 	if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
b1e7a3
 		flags |= PIDOF_NETFS;
b1e7a3
 
b1e7a3
-	while ((opt = getopt(argc,argv,"hco:sxn")) != EOF) switch (opt) {
b1e7a3
+	while ((opt = getopt(argc,argv,"hcmo:sxn")) != EOF) switch (opt) {
b1e7a3
 		case '?':
b1e7a3
 			nsyslog(LOG_ERR,"invalid options on command line!\n");
b1e7a3
 			closelog();
b1e7a3
@@ -907,6 +956,9 @@ int main_pidof(int argc, char **argv)
b1e7a3
 		case 'x':
b1e7a3
 			scripts_too++;
b1e7a3
 			break;
b1e7a3
+		case 'm':
b1e7a3
+			flags |= PIDOF_OMIT_OMIT_MATCHES;
b1e7a3
+			break;
b1e7a3
 		case 'n':
b1e7a3
 			flags |= PIDOF_NETFS;
b1e7a3
 			break;
b1e7a3
@@ -938,10 +990,13 @@ int main_pidof(int argc, char **argv)
b1e7a3
 			pid_t spid = 0;
b1e7a3
 			while ((p = get_next_from_pid_q(q))) {
b1e7a3
 				if ((flags & PIDOF_OMIT) && omit) {
b1e7a3
-					OMIT * optr;
b1e7a3
-					for (optr = omit; optr; optr = optr->next) {
b1e7a3
+					PROC * optr;
b1e7a3
+					for (optr = olist; optr; optr = optr->next) {
b1e7a3
 						if (optr->pid == p->pid)
b1e7a3
 							break;
b1e7a3
+						if (flags & PIDOF_OMIT_OMIT_MATCHES)
b1e7a3
+							if (matches(optr, p))
b1e7a3
+								break;
b1e7a3
 					}
b1e7a3
 
b1e7a3
 					/*
b1e7a3
@@ -977,6 +1032,7 @@ int main_pidof(int argc, char **argv)
b1e7a3
 	if (!first)
b1e7a3
 		printf("\n");
b1e7a3
 
b1e7a3
+	clear_omit();
b1e7a3
 	clear_mnt();
b1e7a3
 
b1e7a3
 	closelog();