safe

Password protected secret keeper
git clone git://git.z3bra.org/safe.git
Log | Files | Refs | README | LICENSE

commit 4cf289d713e858ae1ed0dc4e2c744787562aa3b9
parent 91a51b83db319c0421378197ce9998a1d71a6cfc
Author: Willy Goiffon <dev@z3bra.org>
Date:   Mon, 24 Jun 2019 12:33:33 +0200

Print child PID when forking agent

Diffstat:
safe-agent.c | 37++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/safe-agent.c b/safe-agent.c @@ -192,7 +192,7 @@ int main(int argc, char *argv[]) { pid_t pid; - int dflag, timeout; + int timeout, fd, dflag; size_t dirlen; char path[PATH_MAX] = SOCKDIR; @@ -223,14 +223,37 @@ main(int argc, char *argv[]) sockp = path; } - printf("SAFE_PID=%d\n", pid); - printf("SAFE_SOCK=%s\n", sockp); - printf("export SAFE_PID SAFE_SOCK\n"); - fflush(stdout); + if (dflag) { + printf("SAFE_PID=%d; export SAFE_PID\n", pid); + printf("SAFE_SOCK=%s; export SAFE_SOCK\n", sockp); + fflush(stdout); + goto skip; + } + + pid = fork(); + if (pid < 0) + err(1, "fork"); + + if (pid) { + printf("SAFE_PID=%d; export SAFE_PID\n", pid); + printf("SAFE_SOCK=%s; export SAFE_SOCK\n", sockp); + return 0; + } - if (!dflag) - daemon(0, 0); + if (setsid() < 0) + err(1, "setsid"); + chdir("/"); + if ((fd = open("/dev/null", O_RDWR, 0)) != -1) { + (void)dup2(fd, STDIN_FILENO); + (void)dup2(fd, STDOUT_FILENO); + (void)dup2(fd, STDERR_FILENO); + if (fd > 2) + close(fd); + } + +skip: + pid = getpid(); signal(SIGINT, sighandler); signal(SIGTERM, sighandler); signal(SIGUSR1, sighandler);