Blame SOURCES/fapolicyd-fgets-update-thread.patch

eb2dbd
diff -up ./src/cli/fapolicyd-cli.c.upgrade-thread ./src/cli/fapolicyd-cli.c
eb2dbd
--- ./src/cli/fapolicyd-cli.c.upgrade-thread	2022-08-03 18:00:02.374999369 +0200
eb2dbd
+++ ./src/cli/fapolicyd-cli.c	2022-08-03 18:00:09.802830497 +0200
eb2dbd
@@ -482,7 +482,7 @@ static int do_update(void)
eb2dbd
 		}
eb2dbd
 	}
eb2dbd
 
eb2dbd
-	ssize_t ret = write(fd, "1", 2);
eb2dbd
+	ssize_t ret = write(fd, "1\n", 3);
eb2dbd
 
eb2dbd
 	if (ret == -1) {
eb2dbd
 		fprintf(stderr, "Write: %s -> %s\n", _pipe, strerror(errno));
eb2dbd
diff -up ./src/library/database.c.upgrade-thread ./src/library/database.c
eb2dbd
--- ./src/library/database.c.upgrade-thread	2022-06-21 16:55:47.000000000 +0200
eb2dbd
+++ ./src/library/database.c	2022-08-03 17:58:04.034689808 +0200
eb2dbd
@@ -34,6 +34,7 @@
eb2dbd
 #include <errno.h>
eb2dbd
 #include <unistd.h>
eb2dbd
 #include <fcntl.h>
eb2dbd
+#include <ctype.h>
eb2dbd
 #include <gcrypt.h>
eb2dbd
 #include <signal.h>
eb2dbd
 #include <sys/stat.h>
eb2dbd
@@ -43,6 +44,7 @@
eb2dbd
 #include "message.h"
eb2dbd
 #include "llist.h"
eb2dbd
 #include "file.h"
eb2dbd
+#include "fd-fgets.h"
eb2dbd
 
eb2dbd
 #include "fapolicyd-backend.h"
eb2dbd
 #include "backend-manager.h"
eb2dbd
@@ -1181,6 +1183,7 @@ static void *update_thread_main(void *ar
eb2dbd
 			return NULL;
eb2dbd
 	}
eb2dbd
 
eb2dbd
+	fcntl(ffd[0].fd, F_SETFL, O_NONBLOCK);
eb2dbd
 	ffd[0].events = POLLIN;
eb2dbd
 
eb2dbd
 	while (!stop) {
eb2dbd
@@ -1200,97 +1203,102 @@ static void *update_thread_main(void *ar
eb2dbd
 			} else {
eb2dbd
 				msg(LOG_ERR, "Update poll error (%s)",
eb2dbd
 				    strerror_r(errno, err_buff, BUFFER_SIZE));
eb2dbd
-				goto err_out;
eb2dbd
+				goto finalize;
eb2dbd
 			}
eb2dbd
 		} else if (rc == 0) {
eb2dbd
 #ifdef DEBUG
eb2dbd
 			msg(LOG_DEBUG, "Update poll timeout expired");
eb2dbd
 #endif
eb2dbd
-			if (db_operation != DB_NO_OP)
eb2dbd
-				goto handle_db_ops;
eb2dbd
 			continue;
eb2dbd
 		} else {
eb2dbd
 			if (ffd[0].revents & POLLIN) {
eb2dbd
-				ssize_t count = read(ffd[0].fd, buff,
eb2dbd
-						     BUFFER_SIZE-1);
eb2dbd
 
eb2dbd
-				if (count == -1) {
eb2dbd
-					msg(LOG_ERR,
eb2dbd
-					   "Failed to read from a pipe %s (%s)",
eb2dbd
-					   fifo_path,
eb2dbd
-					   strerror_r(errno, err_buff,
eb2dbd
-						      BUFFER_SIZE));
eb2dbd
-					goto err_out;
eb2dbd
-				}
eb2dbd
+				do {
eb2dbd
+					fd_fgets_rewind();
eb2dbd
+					int res = fd_fgets(buff, sizeof(buff), ffd[0].fd);
eb2dbd
 
eb2dbd
-				if (count == 0) {
eb2dbd
-#ifdef DEBUG
eb2dbd
-					msg(LOG_DEBUG,
eb2dbd
-					    "Buffer contains zero bytes!");
eb2dbd
-#endif
eb2dbd
-					continue;
eb2dbd
-				} else // Manually terminate buff
eb2dbd
-					buff[count] = 0;
eb2dbd
-#ifdef DEBUG
eb2dbd
-				msg(LOG_DEBUG, "Buffer contains: \"%s\"", buff);
eb2dbd
-#endif
eb2dbd
-				for (int i = 0 ; i < count ; i++) {
eb2dbd
-					// assume file name
eb2dbd
-					// operation = 0
eb2dbd
-					if (buff[i] == '/') {
eb2dbd
-						db_operation = ONE_FILE;
eb2dbd
+					// nothing to read
eb2dbd
+					if (res == -1)
eb2dbd
 						break;
eb2dbd
-					}
eb2dbd
+					else if (res > 0) {
eb2dbd
+						char* end  = strchr(buff, '\n');
eb2dbd
 
eb2dbd
-					if (buff[i] == '1') {
eb2dbd
-						db_operation = RELOAD_DB;
eb2dbd
-						break;
eb2dbd
+						if (end == NULL) {
eb2dbd
+							msg(LOG_ERR, "Too long line?");
eb2dbd
+							continue;
eb2dbd
+						}
eb2dbd
+
eb2dbd
+						int count = end - buff;
eb2dbd
+
eb2dbd
+						*end = '\0';
eb2dbd
+
eb2dbd
+						for (int i = 0 ; i < count ; i++) {
eb2dbd
+							// assume file name
eb2dbd
+							// operation = 0
eb2dbd
+							if (buff[i] == '/') {
eb2dbd
+								db_operation = ONE_FILE;
eb2dbd
+								break;
eb2dbd
+							}
eb2dbd
+
eb2dbd
+							if (buff[i] == '1') {
eb2dbd
+								db_operation = RELOAD_DB;
eb2dbd
+								break;
eb2dbd
+							}
eb2dbd
+
eb2dbd
+							if (buff[i] == '2') {
eb2dbd
+								db_operation = FLUSH_CACHE;
eb2dbd
+								break;
eb2dbd
+							}
eb2dbd
+
eb2dbd
+							if (isspace(buff[i]))
eb2dbd
+								continue;
eb2dbd
+
eb2dbd
+							msg(LOG_ERR, "Cannot handle data \"%s\" from pipe", buff);
eb2dbd
+							break;
eb2dbd
+						}
eb2dbd
+
eb2dbd
+						*end = '\n';
eb2dbd
+
eb2dbd
+						// got "1" -> reload db
eb2dbd
+						if (db_operation == RELOAD_DB) {
eb2dbd
+							db_operation = DB_NO_OP;
eb2dbd
+							msg(LOG_INFO,
eb2dbd
+								"It looks like there was an update of the system... Syncing DB.");
eb2dbd
+
eb2dbd
+							backend_close();
eb2dbd
+							backend_init(config);
eb2dbd
+							backend_load(config);
eb2dbd
+
eb2dbd
+							if ((rc = update_database(config))) {
eb2dbd
+								msg(LOG_ERR,
eb2dbd
+									"Cannot update trust database!");
eb2dbd
+								close(ffd[0].fd);
eb2dbd
+								backend_close();
eb2dbd
+								unlink_fifo();
eb2dbd
+								exit(rc);
eb2dbd
+							}
eb2dbd
+
eb2dbd
+							msg(LOG_INFO, "Updated");
eb2dbd
+
eb2dbd
+							// Conserve memory
eb2dbd
+							backend_close();
eb2dbd
+							// got "2" -> flush cache
eb2dbd
+						} else if (db_operation == FLUSH_CACHE) {
eb2dbd
+							db_operation = DB_NO_OP;
eb2dbd
+							needs_flush = true;
eb2dbd
+						} else if (db_operation == ONE_FILE) {
eb2dbd
+							db_operation = DB_NO_OP;
eb2dbd
+							if (handle_record(buff))
eb2dbd
+								continue;
eb2dbd
+						}
eb2dbd
 					}
eb2dbd
 
eb2dbd
-					if (buff[i] == '2') {
eb2dbd
-						db_operation = FLUSH_CACHE;
eb2dbd
-						break;
eb2dbd
-					}
eb2dbd
-				}
eb2dbd
-
eb2dbd
-handle_db_ops:
eb2dbd
-				// got "1" -> reload db
eb2dbd
-				if (db_operation == RELOAD_DB) {
eb2dbd
-					db_operation = DB_NO_OP;
eb2dbd
-					msg(LOG_INFO,
eb2dbd
-	    "It looks like there was an update of the system... Syncing DB.");
eb2dbd
-
eb2dbd
-					backend_close();
eb2dbd
-					backend_init(config);
eb2dbd
-					backend_load(config);
eb2dbd
-
eb2dbd
-					if ((rc = update_database(config))) {
eb2dbd
-						msg(LOG_ERR,
eb2dbd
-						   "Cannot update trust database!");
eb2dbd
-						close(ffd[0].fd);
eb2dbd
-						backend_close();
eb2dbd
-						unlink_fifo();
eb2dbd
-						exit(rc);
eb2dbd
-					} else
eb2dbd
-						msg(LOG_INFO, "Updated");
eb2dbd
-
eb2dbd
-					// Conserve memory
eb2dbd
-					backend_close();
eb2dbd
-				// got "2" -> flush cache
eb2dbd
-				} else if (db_operation == FLUSH_CACHE) {
eb2dbd
-					db_operation = DB_NO_OP;
eb2dbd
-					needs_flush = true;
eb2dbd
-				} else if (db_operation == ONE_FILE) {
eb2dbd
-					db_operation = DB_NO_OP;
eb2dbd
-					if (handle_record(buff))
eb2dbd
-						continue;
eb2dbd
-				}
eb2dbd
+				} while(!fd_fgets_eof());
eb2dbd
 			}
eb2dbd
 		}
eb2dbd
-
eb2dbd
 	}
eb2dbd
 
eb2dbd
-err_out:
eb2dbd
+finalize:
eb2dbd
 	close(ffd[0].fd);
eb2dbd
 	unlink_fifo();
eb2dbd