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