Blame SOURCES/fapolicyd-already-started.patch

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