[make-initrd] ueventd: Add a prefix to the logging functions to avoid name collisions

Alexey Gladkov gladkov.alexey at gmail.com
Sat May 6 22:45:01 MSK 2023


Signed-off-by: Alexey Gladkov <gladkov.alexey at gmail.com>
---
 datasrc/ueventd/logging.c         | 10 +++---
 datasrc/ueventd/memory.c          |  6 ++--
 datasrc/ueventd/path.c            |  4 +--
 datasrc/ueventd/queue-processor.c | 28 +++++++--------
 datasrc/ueventd/ueventd.c         | 60 +++++++++++++++----------------
 datasrc/ueventd/ueventd.h         | 20 +++++------
 6 files changed, 64 insertions(+), 64 deletions(-)

diff --git a/datasrc/ueventd/logging.c b/datasrc/ueventd/logging.c
index 671f6814..3a04a5a9 100644
--- a/datasrc/ueventd/logging.c
+++ b/datasrc/ueventd/logging.c
@@ -12,7 +12,7 @@
 
 int log_priority = LOG_INFO;
 
-int logging_level(const char *name)
+int rd_logging_level(const char *name)
 {
 	if (!strcasecmp(name, "debug"))   return LOG_DEBUG;
 	if (!strcasecmp(name, "info"))    return LOG_INFO;
@@ -21,7 +21,7 @@ int logging_level(const char *name)
 	return log_priority;
 }
 
-void logging_init(int loglevel)
+void rd_logging_init(int loglevel)
 {
 	if (!getenv("UEVENTD_USE_STDERR")) {
 		char *rdlog = getenv("RDLOG");
@@ -32,7 +32,7 @@ void logging_init(int loglevel)
 
 		FILE *cons = fopen(logfile, "w+");
 		if (!cons)
-			fatal("open(%s): %m", logfile);
+			rd_fatal("open(%s): %m", logfile);
 
 		fclose(stderr);
 		stderr = cons;
@@ -41,11 +41,11 @@ void logging_init(int loglevel)
 	log_priority = loglevel;
 }
 
-void logging_close(void)
+void rd_logging_close(void)
 {
 }
 
-void message(int priority, const char *fmt, ...)
+void rd_message(int priority, const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
diff --git a/datasrc/ueventd/memory.c b/datasrc/ueventd/memory.c
index cb911d58..674928c0 100644
--- a/datasrc/ueventd/memory.c
+++ b/datasrc/ueventd/memory.c
@@ -14,8 +14,8 @@ void *xcalloc(size_t nmemb, size_t size)
 {
 	void *r = calloc(nmemb, size);
 	if (!r)
-		fatal("calloc: allocating %lu*%lu bytes: %m",
-		      (unsigned long) nmemb, (unsigned long) size);
+		rd_fatal("calloc: allocating %lu*%lu bytes: %m",
+		         (unsigned long) nmemb, (unsigned long) size);
 	return r;
 }
 
@@ -25,7 +25,7 @@ char *xasprintf(char **ptr, const char *fmt, ...)
 
 	va_start(arg, fmt);
 	if (vasprintf(ptr, fmt, arg) < 0)
-		fatal("vasprintf: %m");
+		rd_fatal("vasprintf: %m");
 	va_end(arg);
 
 	return *ptr;
diff --git a/datasrc/ueventd/path.c b/datasrc/ueventd/path.c
index 7880ad5c..835f4fbb 100644
--- a/datasrc/ueventd/path.c
+++ b/datasrc/ueventd/path.c
@@ -18,7 +18,7 @@ DIR *xopendir(const char *path)
 {
 	DIR *dir = opendir(path);
 	if (!dir)
-		fatal("opendir: %s: %m", path);
+		rd_fatal("opendir: %s: %m", path);
 	return dir;
 }
 
@@ -32,7 +32,7 @@ struct dirent *xreaddir(DIR *d, const char *path)
 	if (!ent) {
 		if (!errno)
 			return NULL;
-		fatal("readdir: %s: %m", path);
+		rd_fatal("readdir: %s: %m", path);
 	}
 	return ent;
 }
diff --git a/datasrc/ueventd/queue-processor.c b/datasrc/ueventd/queue-processor.c
index ab5e03e4..74e6dfc0 100644
--- a/datasrc/ueventd/queue-processor.c
+++ b/datasrc/ueventd/queue-processor.c
@@ -24,20 +24,20 @@ void event_handler(struct watch *queue, char *path)
 	pid_t pid = vfork();
 
 	if (pid < 0) {
-		err("fork: %m");
+		rd_err("fork: %m");
 
 	} else if (!pid) {
 		execve(argv[0], argv, environ);
-		fatal("exec: %s: %m", argv[0]);
+		rd_fatal("exec: %s: %m", argv[0]);
 	} else {
 		int status = 0;
 
 		if (waitpid_retry(pid, &status, 0) != pid || !WIFEXITED(status))
-			info("%s: session=%lu: %s failed",
-			     queue->q_name, session, handler_file);
+			rd_info("%s: session=%lu: %s failed",
+			        queue->q_name, session, handler_file);
 		else
-			info("%s: session=%lu: %s finished with return code %d",
-			     queue->q_name, session, handler_file, WEXITSTATUS(status));
+			rd_info("%s: session=%lu: %s finished with return code %d",
+			        queue->q_name, session, handler_file, WEXITSTATUS(status));
 	}
 }
 
@@ -47,27 +47,27 @@ void move_files(char *srcdir, char *dstdir)
 	int srcfd, dstfd;
 
 	if ((srcfd = open(srcdir, O_RDONLY | O_CLOEXEC)) < 0)
-		fatal("open: %s: %m", srcdir);
+		rd_fatal("open: %s: %m", srcdir);
 
 	errno = 0;
 	if ((dstfd = open(dstdir, O_RDONLY | O_CLOEXEC)) < 0) {
 		if (errno == ENOENT) {
 			if (mkdir(dstdir, 0755) < 0)
-				fatal("mkdir: %s: %m", dstdir);
+				rd_fatal("mkdir: %s: %m", dstdir);
 			dstfd = open(dstdir, O_RDONLY | O_CLOEXEC);
 		}
 		if (dstfd < 0)
-			fatal("open: %s: %m", dstdir);
+			rd_fatal("open: %s: %m", dstdir);
 	}
 
 	DIR *d = fdopendir(srcfd);
 	if (!d)
-		fatal("fdopendir: %m");
+		rd_fatal("fdopendir: %m");
 
 	while ((ent = xreaddir(d, srcdir)) != NULL) {
 		if (ent->d_name[0] != '.' && ent->d_type == DT_REG &&
 		    renameat(srcfd, ent->d_name, dstfd, ent->d_name) < 0)
-			fatal("rename `%s/%s' -> `%s/%s': %m", srcdir, ent->d_name, dstdir, ent->d_name);
+			rd_fatal("rename `%s/%s' -> `%s/%s': %m", srcdir, ent->d_name, dstdir, ent->d_name);
 	}
 
 	closedir(d);
@@ -81,14 +81,14 @@ void signal_unhandled_events(struct watch *queue)
 	len = write_loop(queue->q_parentfd,
 	                 (char *) &queue->q_watchfd, sizeof(queue->q_watchfd));
 	if (len < 0)
-		err("write(pipe): %m");
+		rd_err("write(pipe): %m");
 
-	info("%s: session=%lu: retry with the events remaining in the queue", queue->q_name, session);
+	rd_info("%s: session=%lu: retry with the events remaining in the queue", queue->q_name, session);
 }
 
 void process_events(struct watch *queue)
 {
-	info("%s: session=%lu: processing events", queue->q_name, session);
+	rd_info("%s: session=%lu: processing events", queue->q_name, session);
 
 	char *numenv = NULL;
 
diff --git a/datasrc/ueventd/ueventd.c b/datasrc/ueventd/ueventd.c
index f5923367..f4b89cb9 100644
--- a/datasrc/ueventd/ueventd.c
+++ b/datasrc/ueventd/ueventd.c
@@ -85,7 +85,7 @@ int add_queue_dir(int inotifyfd, const char *path, uint32_t mask)
 		if (errno == EEXIST) {
 			return -128;
 		}
-		err("inotify failed to watch: %s: %m", path);
+		rd_err("inotify failed to watch: %s: %m", path);
 	}
 	return ret;
 }
@@ -103,7 +103,7 @@ int watch_path(int inotifyfd, const char *dir, const char *name, uint32_t mask,
 		return (wfd == -128 ? 0 : wfd);
 
 	if (stat(path, &st) < 0) {
-		err("stat: %s: %m", path);
+		rd_err("stat: %s: %m", path);
 		goto fail;
 	}
 
@@ -127,7 +127,7 @@ int watch_path(int inotifyfd, const char *dir, const char *name, uint32_t mask,
 
 	watch_list = new;
 
-	info("watch path: %s", path);
+	rd_info("watch path: %s", path);
 	free(path);
 	return 0;
 fail:
@@ -216,7 +216,7 @@ void watch_queues(int inotifyfd)
 {
 	DIR *dir = opendir(filter_dir);
 	if (!dir)
-		fatal("opendir: %s: %m", filter_dir);
+		rd_fatal("opendir: %s: %m", filter_dir);
 
 	struct dirent *ent;
 
@@ -225,7 +225,7 @@ void watch_queues(int inotifyfd)
 			continue;
 
 		if (watch_path(inotifyfd, filter_dir, ent->d_name, EV_QUEUE_MASK, F_QUEUE_DIR) < 0)
-			fatal("unable to start watching: %s/%s", filter_dir, ent->d_name);
+			rd_fatal("unable to start watching: %s/%s", filter_dir, ent->d_name);
 	}
 	closedir(dir);
 }
@@ -238,7 +238,7 @@ int process_signal_events(int signfd)
 
 	size = read_retry(signfd, &fdsi, sizeof(fdsi));
 	if (size != sizeof(fdsi)) {
-		err("unable to read signal info");
+		rd_err("unable to read signal info");
 		return 0;
 	}
 
@@ -248,7 +248,7 @@ int process_signal_events(int signfd)
 
 			if (pid <= 0) {
 				if (pid < 0 && errno != ECHILD) {
-					err("waitpid: %m");
+					rd_err("waitpid: %m");
 					return -1;
 				}
 				break;
@@ -262,7 +262,7 @@ int process_signal_events(int signfd)
 		return 0;
 	}
 
-	info("got signal %d, exit", fdsi.ssi_signo);
+	rd_info("got signal %d, exit", fdsi.ssi_signo);
 	return -1;
 }
 
@@ -279,10 +279,10 @@ void setup_signal_fd(struct fd_handler *el)
 	sigaddset(&mask, SIGCHLD);
 
 	if ((el->fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC)) < 0)
-		fatal("signalfd: %m");
+		rd_fatal("signalfd: %m");
 
 	if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
-		fatal("sigprocmask: %m");
+		rd_fatal("sigprocmask: %m");
 
 	el->fd_handler = process_signal_events;
 }
@@ -297,7 +297,7 @@ int process_inotify_events(int inotifyfd)
 		if (len < 0) {
 			if (errno == EAGAIN)
 				break;
-			fatal("read: %m");
+			rd_fatal("read: %m");
 		} else if (!len) {
 			break;
 		}
@@ -315,7 +315,7 @@ int process_inotify_events(int inotifyfd)
 				continue;
 
 			if (event->mask & IN_DELETE_SELF) {
-				info("unwatch path: %s", p->q_name);
+				rd_info("unwatch path: %s", p->q_name);
 				unwatch_path(inotifyfd, p->q_ino);
 				continue;
 			}
@@ -328,9 +328,9 @@ int process_inotify_events(int inotifyfd)
 
 			if (p->q_flags & F_PAUSE_DIR) {
 				if (event->mask & IN_CREATE)
-					info("%s: queue paused", event->name);
+					rd_info("%s: queue paused", event->name);
 				else if (event->mask & IN_DELETE)
-					info("%s: queue unpaused", event->name);
+					rd_info("%s: queue unpaused", event->name);
 				apply_pause();
 				continue;
 			}
@@ -345,7 +345,7 @@ int process_inotify_events(int inotifyfd)
 void setup_inotify_fd(struct fd_handler *el)
 {
 	if ((el->fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) < 0)
-		fatal("inotify_init1: %m");
+		rd_fatal("inotify_init1: %m");
 
 	el->fd_handler = process_inotify_events;
 }
@@ -363,7 +363,7 @@ int process_pipefd_events(int readfd)
 		if (len < 0) {
 			if (errno == EAGAIN)
 				break;
-			fatal("read(pipe): %m");
+			rd_fatal("read(pipe): %m");
 		} else if (!len) {
 			break;
 		}
@@ -383,7 +383,7 @@ int process_pipefd_events(int readfd)
 void setup_pipe_fd(struct fd_handler *el)
 {
 	if (pipe2(pipefd, O_NONBLOCK | O_CLOEXEC) < 0)
-		fatal("pipe2: %m");
+		rd_fatal("pipe2: %m");
 
 	el->fd = pipefd[PIPE_READ];
 	el->fd_handler = process_pipefd_events;
@@ -395,13 +395,13 @@ int setup_epoll_fd(struct fd_handler list[FD_MAX])
 	struct epoll_event ev = { .events = EPOLLIN };
 
 	if ((epollfd = epoll_create1(EPOLL_CLOEXEC)) < 0)
-		fatal("epoll_create1: %m");
+		rd_fatal("epoll_create1: %m");
 
 	for (int i = 0; i < FD_MAX; i++) {
 		ev.data.fd = list[i].fd;
 
 		if (epoll_ctl(epollfd, EPOLL_CTL_ADD, list[i].fd, &ev) < 0)
-			fatal("epoll_ctl: %m");
+			rd_fatal("epoll_ctl: %m");
 	}
 
 	return epollfd;
@@ -415,14 +415,14 @@ pid_t spawn_worker(int epollfd, struct watch *queue)
 
 	pid = fork();
 	if (pid < 0) {
-		err("fork: %m");
+		rd_err("fork: %m");
 		return 0;
 	} else if (pid > 0) {
 		return pid;
 	}
 
 	if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
-		fatal("prctl(PR_SET_PDEATHSIG): %m");
+		rd_fatal("prctl(PR_SET_PDEATHSIG): %m");
 
 	for (int i = 0; i < FD_MAX; i++) {
 		if (fd_list[i].fd >= 0)
@@ -434,7 +434,7 @@ pid_t spawn_worker(int epollfd, struct watch *queue)
 	sigemptyset(&mask);
 
 	if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
-		fatal("sigprocmask: %m");
+		rd_fatal("sigprocmask: %m");
 
 	process_events(queue);
 
@@ -445,7 +445,7 @@ char *get_param_dir(const char *name)
 {
 	char *value = getenv(name);
 	if (!value)
-		fatal("please set `%s' env variable", name);
+		rd_fatal("please set `%s' env variable", name);
 	return value;
 }
 
@@ -463,7 +463,7 @@ int main(int argc, char **argv)
 				exit(0);
 				break;
 			case 'l':
-				loglevel = logging_level(optarg);
+				loglevel = rd_logging_level(optarg);
 				break;
 			default:
 				exit(1);
@@ -472,11 +472,11 @@ int main(int argc, char **argv)
 	}
 
 	if (optind == argc)
-		fatal("specify handler program");
+		rd_fatal("specify handler program");
 
-	logging_init(loglevel);
+	rd_logging_init(loglevel);
 
-	info("starting server ...");
+	rd_info("starting server ...");
 
 	filter_dir = get_param_dir("filterdir");
 	uevent_dir = get_param_dir("ueventdir");
@@ -507,7 +507,7 @@ int main(int argc, char **argv)
 		if (fdcount < 0) {
 			if (errno == EINTR)
 				continue;
-			fatal("epoll_wait: %m");
+			rd_fatal("epoll_wait: %m");
 		}
 
 		for (i = 0; i < fdcount; i++) {
@@ -540,12 +540,12 @@ done:
 	for (i = 0; i < FD_MAX; i++) {
 		if (fd_list[i].fd >= 0) {
 			if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fd_list[i].fd, NULL) < 0)
-				err("epoll_ctl: %m");
+				rd_err("epoll_ctl: %m");
 			close(fd_list[i].fd);
 		}
 	}
 	close(epollfd);
-	logging_close();
+	rd_logging_close();
 
 	return EXIT_SUCCESS;
 }
diff --git a/datasrc/ueventd/ueventd.h b/datasrc/ueventd/ueventd.h
index a5e50379..e7ee1a8b 100644
--- a/datasrc/ueventd/ueventd.h
+++ b/datasrc/ueventd/ueventd.h
@@ -56,21 +56,21 @@ extern  int is_dot_dir(struct dirent *ent) __attribute__((nonnull(1)));
 #include <syslog.h>
 #include <stdlib.h>
 
-extern void logging_init(int level);
-extern void logging_close(void);
-extern int logging_level(const char *lvl)               __attribute__((nonnull(1)));
-extern void message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
+extern void rd_logging_init(int level);
+extern void rd_logging_close(void);
+extern int rd_logging_level(const char *lvl)               __attribute__((nonnull(1)));
+extern void rd_message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-#define __message(level, format, arg...) message(level, format, ##arg)
+#define __rd_message(level, format, arg...) rd_message(level, format, ##arg)
 
-#define fatal(format, arg...)                       \
+#define rd_fatal(format, arg...)                    \
 	do {                                        \
-		__message(LOG_CRIT, "%s:%d: " format, __FILE__, __LINE__, ##arg); \
+		rd_message(LOG_CRIT, "%s:%d: " format, __FILE__, __LINE__, ##arg); \
 		_exit(EXIT_FAILURE);                \
 	} while (0)
 
-#define err(format, arg...)  __message(LOG_ERR,   format, ##arg)
-#define info(format, arg...) __message(LOG_INFO,  format, ##arg)
-#define dbg(format, arg...)  __message(LOG_DEBUG, format, ##arg)
+#define rd_err(format, arg...)  __rd_message(LOG_ERR,   format, ##arg)
+#define rd_info(format, arg...) __rd_message(LOG_INFO,  format, ##arg)
+#define rd_dbg(format, arg...)  __rd_message(LOG_DEBUG, format, ##arg)
 
 #endif /* __UEVENTD_H__ */
-- 
2.33.7



More information about the Make-initrd mailing list