Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_daemonize.c revision 1.1
      1 /*	$NetBSD: rumpuser_daemonize.c,v 1.1 2010/11/30 14:23:24 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 <sys/cdefs.h>
     29 #if !defined(lint)
     30 __RCSID("$NetBSD: rumpuser_daemonize.c,v 1.1 2010/11/30 14:23:24 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <sys/types.h>
     34 #include <sys/socket.h>
     35 
     36 #include <errno.h>
     37 #include <fcntl.h>
     38 #include <paths.h>
     39 #include <stdio.h>
     40 #include <unistd.h>
     41 
     42 static int isdaemonizing;
     43 static int daemonpipe[2];
     44 
     45 #include <rump/rumpuser.h>
     46 
     47 int
     48 rumpuser_daemonize_begin(void)
     49 {
     50 	ssize_t n;
     51 	int error;
     52 
     53 	if (isdaemonizing)
     54 		return EINPROGRESS;
     55 	isdaemonizing = 1;
     56 
     57 	/*
     58 	 * For daemons we need to fork.  However, since we can't fork
     59 	 * after rump_init (which creates threads), do it now.  Add
     60 	 * a little pipe trickery to make sure we don't exit until the
     61 	 * service is fully inited (i.e. interlocked daemonization).
     62 	 * Actually, use sucketpair since that allows to easily steer
     63 	 * clear of the dreaded sigpipe.
     64 	 *
     65 	 * Note: We do *NOT* host chdir("/").  It's up to the caller to
     66 	 * take care of that or not.
     67 	 */
     68 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, daemonpipe) == -1) {
     69 		return errno;
     70 	}
     71 
     72 	switch (fork()) {
     73 	case 0:
     74 		if (setsid() == -1) {
     75 			rumpuser_daemonize_done(errno);
     76 		}
     77 		return 0;
     78 	case -1:
     79 		return errno;
     80 	default:
     81 		close(daemonpipe[1]);
     82 		n = recv(daemonpipe[0], &error, sizeof(error), MSG_NOSIGNAL);
     83 		if (n == -1)
     84 			error = errno;
     85 		else if (n != sizeof(error))
     86 			error = ESRCH;
     87 		_exit(error);
     88 	}
     89 }
     90 
     91 int
     92 rumpuser_daemonize_done(int error)
     93 {
     94 	ssize_t n;
     95 	int fd;
     96 
     97 	if (!isdaemonizing)
     98 		return ENOENT;
     99 
    100 	if (error == 0) {
    101 		fd = open(_PATH_DEVNULL, O_RDWR);
    102 		if (fd == -1) {
    103 			error = errno;
    104 			goto out;
    105 		}
    106 		dup2(fd, STDIN_FILENO);
    107 		dup2(fd, STDOUT_FILENO);
    108 		dup2(fd, STDERR_FILENO);
    109 		if (fd > STDERR_FILENO)
    110 			close(fd);
    111 	}
    112 
    113  out:
    114 	n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
    115 	if (n != sizeof(error))
    116 		return EPIPE;
    117 	else if (n == -1)
    118 		return errno;
    119 	close(daemonpipe[0]);
    120 	close(daemonpipe[1]);
    121 
    122 	return 0;
    123 }
    124