Blame SOURCES/fapolicyd-already-started.patch

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