Blame SOURCES/fapolicyd-already-started.patch

32a688
diff -up ./src/daemon/fapolicyd.c.already-started ./src/daemon/fapolicyd.c
32a688
--- ./src/daemon/fapolicyd.c.already-started	2023-01-12 17:40:45.366909652 +0100
32a688
+++ ./src/daemon/fapolicyd.c	2023-01-12 17:46:22.458139519 +0100
32a688
@@ -378,6 +378,58 @@ static void usage(void)
32a688
 }
32a688
 
32a688
 
32a688
+int already_running(void)
32a688
+{
32a688
+	int pidfd = open(pidfile, O_RDONLY);
32a688
+	if (pidfd >= 0) {
32a688
+		char pid_buf[16];
32a688
+
32a688
+		if (fd_fgets(pid_buf, sizeof(pid_buf), pidfd)) {
32a688
+			int pid;
32a688
+			char exe_buf[80], my_path[80];
32a688
+
32a688
+			// Get our path
32a688
+			if (get_program_from_pid(getpid(),
32a688
+					sizeof(exe_buf), my_path) == NULL)
32a688
+				goto err_out; // shouldn't happen, but be safe
32a688
+
32a688
+			// convert pidfile to integer
32a688
+			errno = 0;
32a688
+			pid = strtoul(pid_buf, NULL, 10);
32a688
+			if (errno)
32a688
+				goto err_out; // shouldn't happen, but be safe
32a688
+
32a688
+			// verify it really is fapolicyd
32a688
+			if (get_program_from_pid(pid,
32a688
+					sizeof(exe_buf), exe_buf) == NULL)
32a688
+				goto good; //if pid doesn't exist, we're OK
32a688
+
32a688
+			// If the path doesn't have fapolicyd in it, we're OK
32a688
+			if (strstr(exe_buf, "fapolicyd") == NULL)
32a688
+				goto good;
32a688
+
32a688
+			if (strcmp(exe_buf, my_path) == 0)
32a688
+				goto err_out; // if the same, we need to exit
32a688
+
32a688
+			// one last sanity check in case path is unexpected
32a688
+			// for example: /sbin/fapolicyd & /home/test/fapolicyd
32a688
+			if (pid != getpid())
32a688
+				goto err_out;
32a688
+good:
32a688
+			close(pidfd);
32a688
+			unlink(pidfile);
32a688
+			return 0;
32a688
+		} else
32a688
+		    msg(LOG_ERR, "fapolicyd pid file found but unreadable");
32a688
+err_out: // At this point, we have a pid file, let's just assume it's alive
32a688
+	 // because if 2 are running, it deadlocks the machine
32a688
+		close(pidfd);
32a688
+		return 1;
32a688
+	}
32a688
+	return 0; // pid file doesn't exist, we're good to go
32a688
+}
32a688
+
32a688
+
32a688
 int main(int argc, const char *argv[])
32a688
 {
32a688
 	struct pollfd pfd[2];
32a688
@@ -428,6 +480,11 @@ int main(int argc, const char *argv[])
32a688
 		}
32a688
 	}
32a688
 
32a688
+	if (already_running()) {
32a688
+		msg(LOG_ERR, "fapolicyd is already running");
32a688
+		exit(1);
32a688
+	}
32a688
+
32a688
 	// Set a couple signal handlers
32a688
 	sa.sa_flags = 0;
32a688
 	sigemptyset(&sa.sa_mask);
32a688
@@ -446,9 +503,6 @@ int main(int argc, const char *argv[])
32a688
 	setrlimit(RLIMIT_FSIZE, &limit);
32a688
 	setrlimit(RLIMIT_NOFILE, &limit);
32a688
 
32a688
-	// Set strict umask
32a688
-	(void) umask( 0117 );
32a688
-
32a688
 	// get more time slices because everything is waiting on us
32a688
 	rc = nice(-config.nice_val);
32a688
 	if (rc == -1)
32a688
@@ -473,17 +527,20 @@ int main(int argc, const char *argv[])
32a688
 		exit(1);
32a688
 	}
32a688
 
32a688
-	if (preconstruct_fifo(&config)) {
32a688
-		msg(LOG_ERR, "Cannot contruct a pipe");
32a688
-		exit(1);
32a688
-	}
32a688
-
32a688
 	// Setup filesystem to watch list
32a688
 	init_fs_list(config.watch_fs);
32a688
 
32a688
 	// Write the pid file for the init system
32a688
 	write_pid_file();
32a688
 
32a688
+	// Set strict umask
32a688
+	(void) umask( 0117 );
32a688
+
32a688
+	if (preconstruct_fifo(&config)) {
32a688
+		msg(LOG_ERR, "Cannot contruct a pipe");
32a688
+		exit(1);
32a688
+	}
32a688
+
32a688
 	// If we are not going to be root, then setup necessary capabilities
32a688
 	if (config.uid != 0) {
32a688
 		capng_clear(CAPNG_SELECT_BOTH);