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

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