Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_daemonize.c revision 1.7.26.1
      1  1.7.26.1  martin /*	$NetBSD: rumpuser_daemonize.c,v 1.7.26.1 2024/09/11 16:09:32 martin Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*
      4       1.1   pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5       1.1   pooka  *
      6       1.1   pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1   pooka  * modification, are permitted provided that the following conditions
      8       1.1   pooka  * are met:
      9       1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1   pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1   pooka  *
     15       1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1   pooka  * SUCH DAMAGE.
     26       1.1   pooka  */
     27       1.1   pooka 
     28       1.3   pooka #include "rumpuser_port.h"
     29       1.3   pooka 
     30       1.1   pooka #if !defined(lint)
     31  1.7.26.1  martin __RCSID("$NetBSD: rumpuser_daemonize.c,v 1.7.26.1 2024/09/11 16:09:32 martin Exp $");
     32       1.1   pooka #endif /* !lint */
     33       1.1   pooka 
     34       1.1   pooka #include <sys/types.h>
     35       1.1   pooka #include <sys/socket.h>
     36       1.1   pooka 
     37       1.1   pooka #include <errno.h>
     38       1.1   pooka #include <fcntl.h>
     39       1.6   pooka #include <stdint.h>
     40       1.1   pooka #include <stdio.h>
     41       1.1   pooka #include <unistd.h>
     42       1.1   pooka 
     43       1.5   pooka #include "rumpuser_int.h"
     44       1.5   pooka 
     45       1.7   pooka #if defined(HAVE_PATHS_H)
     46       1.7   pooka #include <paths.h>
     47       1.7   pooka #else
     48       1.4   pooka #define _PATH_DEVNULL "/dev/null"
     49       1.4   pooka #endif
     50       1.4   pooka 
     51       1.1   pooka static int isdaemonizing;
     52       1.1   pooka static int daemonpipe[2];
     53       1.1   pooka 
     54       1.1   pooka #include <rump/rumpuser.h>
     55       1.1   pooka 
     56  1.7.26.1  martin static int
     57  1.7.26.1  martin openstdoutstderr(void)
     58  1.7.26.1  martin {
     59  1.7.26.1  martin 	char path[PATH_MAX];
     60  1.7.26.1  martin 	int fd;
     61  1.7.26.1  martin 
     62  1.7.26.1  martin 	if (getenv_r("RUMP_STDOUT", path, sizeof(path)) == 0) {
     63  1.7.26.1  martin 		if ((fd = open(path, O_WRONLY|O_CREAT)) == -1)
     64  1.7.26.1  martin 			return -1;
     65  1.7.26.1  martin 		dup2(fd, STDOUT_FILENO);
     66  1.7.26.1  martin 		(void)close(fd);
     67  1.7.26.1  martin 	}
     68  1.7.26.1  martin 	if (getenv_r("RUMP_STDERR", path, sizeof(path)) == 0) {
     69  1.7.26.1  martin 		if ((fd = open(path, O_WRONLY|O_CREAT)) == -1)
     70  1.7.26.1  martin 			return -1;
     71  1.7.26.1  martin 		dup2(fd, STDERR_FILENO);
     72  1.7.26.1  martin 		(void)close(fd);
     73  1.7.26.1  martin 	}
     74  1.7.26.1  martin 	return 0;
     75  1.7.26.1  martin }
     76  1.7.26.1  martin 
     77       1.1   pooka int
     78       1.1   pooka rumpuser_daemonize_begin(void)
     79       1.1   pooka {
     80       1.1   pooka 	ssize_t n;
     81       1.1   pooka 	int error;
     82       1.5   pooka 	int rv;
     83       1.1   pooka 
     84       1.5   pooka 	if (isdaemonizing) {
     85       1.5   pooka 		rv = EINPROGRESS;
     86       1.5   pooka 		goto out;
     87       1.5   pooka 	}
     88       1.1   pooka 	isdaemonizing = 1;
     89       1.1   pooka 
     90       1.1   pooka 	/*
     91       1.1   pooka 	 * For daemons we need to fork.  However, since we can't fork
     92       1.1   pooka 	 * after rump_init (which creates threads), do it now.  Add
     93       1.1   pooka 	 * a little pipe trickery to make sure we don't exit until the
     94       1.1   pooka 	 * service is fully inited (i.e. interlocked daemonization).
     95       1.1   pooka 	 * Actually, use sucketpair since that allows to easily steer
     96       1.1   pooka 	 * clear of the dreaded sigpipe.
     97       1.1   pooka 	 *
     98       1.1   pooka 	 * Note: We do *NOT* host chdir("/").  It's up to the caller to
     99       1.1   pooka 	 * take care of that or not.
    100       1.1   pooka 	 */
    101       1.1   pooka 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, daemonpipe) == -1) {
    102       1.5   pooka 		rv = errno;
    103       1.5   pooka 		goto out;
    104       1.1   pooka 	}
    105       1.1   pooka 
    106  1.7.26.1  martin 	if (openstdoutstderr() == -1) {
    107  1.7.26.1  martin 		rv = errno;
    108  1.7.26.1  martin 		(void)close(daemonpipe[0]);
    109  1.7.26.1  martin 		(void)close(daemonpipe[1]);
    110  1.7.26.1  martin 		goto out;
    111  1.7.26.1  martin 	}
    112  1.7.26.1  martin 
    113       1.1   pooka 	switch (fork()) {
    114       1.1   pooka 	case 0:
    115       1.1   pooka 		if (setsid() == -1) {
    116       1.1   pooka 			rumpuser_daemonize_done(errno);
    117       1.1   pooka 		}
    118       1.5   pooka 		rv = 0;
    119       1.5   pooka 		break;
    120       1.1   pooka 	case -1:
    121       1.5   pooka 		rv = errno;
    122       1.5   pooka 		break;
    123       1.1   pooka 	default:
    124       1.1   pooka 		close(daemonpipe[1]);
    125       1.1   pooka 		n = recv(daemonpipe[0], &error, sizeof(error), MSG_NOSIGNAL);
    126       1.1   pooka 		if (n == -1)
    127       1.1   pooka 			error = errno;
    128       1.1   pooka 		else if (n != sizeof(error))
    129       1.1   pooka 			error = ESRCH;
    130       1.1   pooka 		_exit(error);
    131       1.2   pooka 		/*NOTREACHED*/
    132       1.1   pooka 	}
    133       1.5   pooka 
    134       1.5   pooka  out:
    135       1.5   pooka 	ET(rv);
    136       1.1   pooka }
    137       1.1   pooka 
    138       1.1   pooka int
    139       1.1   pooka rumpuser_daemonize_done(int error)
    140       1.1   pooka {
    141       1.1   pooka 	ssize_t n;
    142       1.5   pooka 	int fd, rv = 0;
    143       1.1   pooka 
    144       1.5   pooka 	if (!isdaemonizing) {
    145       1.5   pooka 		rv = ENOENT;
    146       1.5   pooka 		goto outout;
    147       1.5   pooka 	}
    148       1.1   pooka 
    149       1.1   pooka 	if (error == 0) {
    150       1.1   pooka 		fd = open(_PATH_DEVNULL, O_RDWR);
    151       1.1   pooka 		if (fd == -1) {
    152       1.1   pooka 			error = errno;
    153       1.1   pooka 			goto out;
    154       1.1   pooka 		}
    155       1.1   pooka 		dup2(fd, STDIN_FILENO);
    156  1.7.26.1  martin 		if (getenv("RUMP_STDOUT") == NULL)
    157  1.7.26.1  martin 			dup2(fd, STDOUT_FILENO);
    158  1.7.26.1  martin 		if (getenv("RUMP_STDERR") == NULL)
    159  1.7.26.1  martin 			dup2(fd, STDERR_FILENO);
    160       1.1   pooka 		if (fd > STDERR_FILENO)
    161       1.1   pooka 			close(fd);
    162       1.1   pooka 	}
    163       1.1   pooka 
    164  1.7.26.1  martin 	fflush(stdout);
    165  1.7.26.1  martin 	fflush(stderr);
    166  1.7.26.1  martin 
    167       1.1   pooka  out:
    168       1.1   pooka 	n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
    169       1.5   pooka 	if (n != sizeof(error)) {
    170       1.5   pooka 		rv = EPIPE;
    171       1.5   pooka 	} else if (n == -1) {
    172       1.5   pooka 		rv = errno;
    173       1.5   pooka 	} else {
    174       1.5   pooka 		close(daemonpipe[0]);
    175       1.5   pooka 		close(daemonpipe[1]);
    176       1.5   pooka 	}
    177       1.1   pooka 
    178       1.5   pooka  outout:
    179       1.5   pooka 	ET(rv);
    180       1.1   pooka }
    181