No subject


Thu Jun 25 05:52:11 EDT 2020


special character file can't be created anymore, all clients keep
overwriting single message in regular file.

To avoid this we check if file we're going to write to is really
character special and ensure it doesn't go away while we check.

Signed-off-by: Sergiy Kibrik <sakib at meta.ua>
---
 ulog.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/ulog.c b/ulog.c
index 776a0c4..10d61da 100644
--- a/ulog.c
+++ b/ulog.c
@@ -23,6 +23,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 static int _ulog_channels = -1;
 static int _ulog_facility = -1;
@@ -89,8 +92,18 @@ static void ulog_defaults(void)
 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 {
 	FILE *kmsg;
+	int fd;
+	struct stat s;
+
+	/* extra checks not to accidentally create regular file */
+	fd = open("/dev/kmsg", O_WRONLY);
+	if (fd < 0)
+		return;
+
+	if (fstat(fd, &s) == 0 &&
+	    s.st_mode & S_IFCHR &&
+	    (kmsg = fdopen(fd, "w")) != NULL) {
 
-	if ((kmsg = fopen("/dev/kmsg", "w")) != NULL) {
 		fprintf(kmsg, "<%u>", priority);
 
 		if (_ulog_ident)
@@ -99,6 +112,8 @@ static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 		vfprintf(kmsg, fmt, ap);
 		fclose(kmsg);
 	}
+
+	close(fd);
 }
 
 static void ulog_stdio(int priority, const char *fmt, va_list ap)
-- 
1.9.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list