aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2024-05-22 19:50:58 +0200
committerStefano Brivio <sbrivio@redhat.com>2024-05-23 16:43:13 +0200
commitba23b05545e1e316235fad7a66f3cfd643c22146 (patch)
treecd590216a2fab87eea2f109ed28eadb16a551a9e /util.c
parent57d8aa8ffefcc226c5c3179e4a8d346cbe61e865 (diff)
downloadpasst-ba23b05545e1e316235fad7a66f3cfd643c22146.tar
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.tar.gz
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.tar.bz2
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.tar.lz
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.tar.xz
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.tar.zst
passt-ba23b05545e1e316235fad7a66f3cfd643c22146.zip
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 <sbrivio@redhat.com> Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/util.c b/util.c
index 8a2ffaf..cc1c73b 100644
--- a/util.c
+++ b/util.c
@@ -403,6 +403,28 @@ void pidfile_write(int fd, pid_t pid)
}
/**
+ * 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
* @devnull_fd: Open file descriptor for /dev/null