From ba23b05545e1e316235fad7a66f3cfd643c22146 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Wed, 22 May 2024 19:50:58 +0200 Subject: passt, util: Move opening of PID file to its own function We won't call it from main() any longer: move it. Signed-off-by: Stefano Brivio Reviewed-by: Richard W.M. Jones --- util.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 8a2ffaf..cc1c73b 100644 --- a/util.c +++ b/util.c @@ -402,6 +402,28 @@ void pidfile_write(int fd, pid_t pid) close(fd); } +/** + * pidfile_open() - Open PID file if needed + * @path: Path for PID file, empty string if no PID file is requested + * + * Return: descriptor for PID file, -1 if path is NULL, won't return on failure + */ +int pidfile_open(const char *path) +{ + int fd; + + if (!*path) + return -1; + + if ((fd = open(path, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, + S_IRUSR | S_IWUSR)) < 0) { + perror("PID file open"); + exit(EXIT_FAILURE); + } + + return fd; +} + /** * __daemon() - daemon()-like function writing PID file before parent exits * @pidfile_fd: Open PID file descriptor -- cgit v1.2.3