From 54a19002df18d2f19fc455c9b1ef9f88b96b7bd1 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Thu, 14 Oct 2021 12:17:47 +0200 Subject: conf: Add -P, --pid, to specify a file where own PID is written to Signed-off-by: Stefano Brivio --- passt.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'passt.c') diff --git a/passt.c b/passt.c index 48c2649..2217dd7 100644 --- a/passt.c +++ b/passt.c @@ -227,6 +227,27 @@ static void drop_caps(void) } } +/** + * pid_file() - Write own PID to file, if configured + * @c: Execution context + */ +static void pid_file(struct ctx *c) { + char pid_buf[12]; + int pid_fd, n; + + if (!c->pid_file) + return; + + pid_fd = open(c->pid_file, O_CREAT | O_WRONLY); + if (pid_fd < 0) + return; + + n = snprintf(pid_buf, sizeof(pid_buf), "%i\n", getpid()); + + write(pid_fd, pid_buf, n); + close(pid_fd); +} + /** * main() - Entry point and main loop * @argc: Argument count @@ -237,7 +258,7 @@ static void drop_caps(void) * #syscalls read write open close fork dup2 exit chdir ioctl writev syslog * #syscalls prlimit64 epoll_ctl epoll_create1 epoll_wait accept4 accept listen * #syscalls socket bind connect getsockopt setsockopt recvfrom sendto shutdown - * #syscalls openat fstat fcntl lseek clone setsid exit_group + * #syscalls openat fstat fcntl lseek clone setsid exit_group getpid * #syscalls:pasta rt_sigreturn */ int main(int argc, char **argv) @@ -327,6 +348,8 @@ int main(int argc, char **argv) if (isatty(fileno(stdout)) && !c.foreground) daemon(0, 0); + pid_file(&c); + timer_init(&c, &now); loop: nfds = epoll_wait(c.epollfd, events, EPOLL_EVENTS, TIMER_INTERVAL); -- cgit v1.2.3