aboutgitcodebugslistschat
diff options
context:
space:
mode:
-rw-r--r--passt.c11
-rw-r--r--util.c22
-rw-r--r--util.h1
3 files changed, 25 insertions, 9 deletions
diff --git a/passt.c b/passt.c
index fb9773d..e2446fc 100644
--- a/passt.c
+++ b/passt.c
@@ -199,7 +199,7 @@ void exit_handler(int signal)
*/
int main(int argc, char **argv)
{
- int nfds, i, devnull_fd = -1, pidfile_fd = -1;
+ int nfds, i, devnull_fd = -1, pidfile_fd;
struct epoll_event events[EPOLL_EVENTS];
char *log_name, argv0[PATH_MAX], *name;
struct ctx c = { 0 };
@@ -299,14 +299,7 @@ int main(int argc, char **argv)
}
}
- if (*c.pid_file) {
- if ((pidfile_fd = open(c.pid_file,
- O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
- S_IRUSR | S_IWUSR)) < 0) {
- perror("PID file open");
- exit(EXIT_FAILURE);
- }
- }
+ pidfile_fd = pidfile_open(c.pid_file);
if (isolate_prefork(&c))
die("Failed to sandbox process, exiting");
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
diff --git a/util.h b/util.h
index 6fa8df2..aa6e4b4 100644
--- a/util.h
+++ b/util.h
@@ -156,6 +156,7 @@ char *line_read(char *buf, size_t len, int fd);
void ns_enter(const struct ctx *c);
bool ns_is_init(void);
int open_in_ns(const struct ctx *c, const char *path, int flags);
+int pidfile_open(const char *path);
void pidfile_write(int fd, pid_t pid);
int __daemon(int pidfile_fd, int devnull_fd);
int fls(unsigned long x);