Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_daemonize.c revision 1.2.4.3
      1  1.2.4.3   yamt /*	$NetBSD: rumpuser_daemonize.c,v 1.2.4.3 2014/05/22 11:37:00 yamt 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.2.4.1   yamt #include "rumpuser_port.h"
     29  1.2.4.1   yamt 
     30      1.1  pooka #if !defined(lint)
     31  1.2.4.3   yamt __RCSID("$NetBSD: rumpuser_daemonize.c,v 1.2.4.3 2014/05/22 11:37:00 yamt 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.2.4.3   yamt #include <stdint.h>
     40      1.1  pooka #include <stdio.h>
     41      1.1  pooka #include <unistd.h>
     42      1.1  pooka 
     43  1.2.4.3   yamt #include "rumpuser_int.h"
     44  1.2.4.3   yamt 
     45  1.2.4.2   yamt #ifdef __sun__
     46  1.2.4.2   yamt #define _PATH_DEVNULL "/dev/null"
     47  1.2.4.2   yamt #else
     48  1.2.4.2   yamt #include <paths.h>
     49  1.2.4.2   yamt #endif
     50  1.2.4.2   yamt 
     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.1  pooka int
     57      1.1  pooka rumpuser_daemonize_begin(void)
     58      1.1  pooka {
     59      1.1  pooka 	ssize_t n;
     60      1.1  pooka 	int error;
     61  1.2.4.3   yamt 	int rv;
     62      1.1  pooka 
     63  1.2.4.3   yamt 	if (isdaemonizing) {
     64  1.2.4.3   yamt 		rv = EINPROGRESS;
     65  1.2.4.3   yamt 		goto out;
     66  1.2.4.3   yamt 	}
     67      1.1  pooka 	isdaemonizing = 1;
     68      1.1  pooka 
     69      1.1  pooka 	/*
     70      1.1  pooka 	 * For daemons we need to fork.  However, since we can't fork
     71      1.1  pooka 	 * after rump_init (which creates threads), do it now.  Add
     72      1.1  pooka 	 * a little pipe trickery to make sure we don't exit until the
     73      1.1  pooka 	 * service is fully inited (i.e. interlocked daemonization).
     74      1.1  pooka 	 * Actually, use sucketpair since that allows to easily steer
     75      1.1  pooka 	 * clear of the dreaded sigpipe.
     76      1.1  pooka 	 *
     77      1.1  pooka 	 * Note: We do *NOT* host chdir("/").  It's up to the caller to
     78      1.1  pooka 	 * take care of that or not.
     79      1.1  pooka 	 */
     80      1.1  pooka 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, daemonpipe) == -1) {
     81  1.2.4.3   yamt 		rv = errno;
     82  1.2.4.3   yamt 		goto out;
     83      1.1  pooka 	}
     84      1.1  pooka 
     85      1.1  pooka 	switch (fork()) {
     86      1.1  pooka 	case 0:
     87      1.1  pooka 		if (setsid() == -1) {
     88      1.1  pooka 			rumpuser_daemonize_done(errno);
     89      1.1  pooka 		}
     90  1.2.4.3   yamt 		rv = 0;
     91  1.2.4.3   yamt 		break;
     92      1.1  pooka 	case -1:
     93  1.2.4.3   yamt 		rv = errno;
     94  1.2.4.3   yamt 		break;
     95      1.1  pooka 	default:
     96      1.1  pooka 		close(daemonpipe[1]);
     97      1.1  pooka 		n = recv(daemonpipe[0], &error, sizeof(error), MSG_NOSIGNAL);
     98      1.1  pooka 		if (n == -1)
     99      1.1  pooka 			error = errno;
    100      1.1  pooka 		else if (n != sizeof(error))
    101      1.1  pooka 			error = ESRCH;
    102      1.1  pooka 		_exit(error);
    103      1.2  pooka 		/*NOTREACHED*/
    104      1.1  pooka 	}
    105  1.2.4.3   yamt 
    106  1.2.4.3   yamt  out:
    107  1.2.4.3   yamt 	ET(rv);
    108      1.1  pooka }
    109      1.1  pooka 
    110      1.1  pooka int
    111      1.1  pooka rumpuser_daemonize_done(int error)
    112      1.1  pooka {
    113      1.1  pooka 	ssize_t n;
    114  1.2.4.3   yamt 	int fd, rv = 0;
    115      1.1  pooka 
    116  1.2.4.3   yamt 	if (!isdaemonizing) {
    117  1.2.4.3   yamt 		rv = ENOENT;
    118  1.2.4.3   yamt 		goto outout;
    119  1.2.4.3   yamt 	}
    120      1.1  pooka 
    121      1.1  pooka 	if (error == 0) {
    122      1.1  pooka 		fd = open(_PATH_DEVNULL, O_RDWR);
    123      1.1  pooka 		if (fd == -1) {
    124      1.1  pooka 			error = errno;
    125      1.1  pooka 			goto out;
    126      1.1  pooka 		}
    127      1.1  pooka 		dup2(fd, STDIN_FILENO);
    128      1.1  pooka 		dup2(fd, STDOUT_FILENO);
    129      1.1  pooka 		dup2(fd, STDERR_FILENO);
    130      1.1  pooka 		if (fd > STDERR_FILENO)
    131      1.1  pooka 			close(fd);
    132      1.1  pooka 	}
    133      1.1  pooka 
    134      1.1  pooka  out:
    135      1.1  pooka 	n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
    136  1.2.4.3   yamt 	if (n != sizeof(error)) {
    137  1.2.4.3   yamt 		rv = EPIPE;
    138  1.2.4.3   yamt 	} else if (n == -1) {
    139  1.2.4.3   yamt 		rv = errno;
    140  1.2.4.3   yamt 	} else {
    141  1.2.4.3   yamt 		close(daemonpipe[0]);
    142  1.2.4.3   yamt 		close(daemonpipe[1]);
    143  1.2.4.3   yamt 	}
    144      1.1  pooka 
    145  1.2.4.3   yamt  outout:
    146  1.2.4.3   yamt 	ET(rv);
    147      1.1  pooka }
    148