Home | History | Annotate | Line # | Download | only in roken
detach.c revision 1.1.1.1
      1 /*	$NetBSD: detach.c,v 1.1.1.1 2017/01/28 20:46:53 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2015
      5  *	Cryptonector LLC.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Cryptonector LLC may not be used to endorse or promote products
     16  *    derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <config.h>
     33 #include <errno.h>
     34 #include <fcntl.h>
     35 #ifdef WIN32
     36 #include <io.h>
     37 #include <stdlib.h>
     38 #else
     39 #include <unistd.h>
     40 #endif
     41 #include <krb5/roken.h>
     42 
     43 #ifdef WIN32
     44 #define dup2 _dup2
     45 #endif
     46 
     47 static int pipefds[2] = {-1, -1};
     48 
     49 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
     50 roken_detach_prep(int argc, char **argv, char *special_arg)
     51 {
     52     pid_t child;
     53     char buf[1];
     54     ssize_t bytes;
     55     int status;
     56 
     57     pipefds[0] = -1;
     58     pipefds[1] = -1;
     59 
     60 #ifdef WIN32
     61     if (_pipe(pipefds, 4, O_BINARY) == -1)
     62         err(1, "failed to setup to detach daemon (_pipe failed)");
     63 #else
     64     if (pipe(pipefds) == -1)
     65         err(1, "failed to setup to detach daemon (pipe failed)");
     66 #endif
     67 
     68 #ifndef WIN32
     69     fflush(stdout);
     70     child = fork();
     71 #else
     72     {
     73         intptr_t child_handle;
     74 	int write_side;
     75         size_t i;
     76 	char *fildes;
     77         char **new_argv;
     78 
     79         new_argv = calloc(argc + 2, sizeof(*new_argv));
     80         if (new_argv == NULL)
     81             err(1, "Out of memory");
     82 
     83 	write_side = _dup(pipefds[1]); /* The new fd will be inherited */
     84 	if (write_side == -1)
     85             err(1, "Out of memory");
     86 
     87 	if (asprintf(&fildes, "%d", write_side) == -1 ||
     88 	    fildes == NULL)
     89             err(1, "failed to setup to detach daemon (_dup failed)");
     90 
     91         new_argv[0] = argv[0];
     92         new_argv[1] = special_arg;
     93         new_argv[2] = fildes;
     94         for (i = 1; argv[i] != NULL; i++)
     95             new_argv[i + 1] = argv[i];
     96 	new_argv[argc + 2] = NULL;
     97 
     98 	_flushall();
     99 	child_handle = spawnvp(_P_NOWAIT, argv[0], new_argv);
    100 	if (child_handle == -1)
    101 	  child = (pid_t)-1;
    102 	else
    103 	  child = GetProcessId((HANDLE)child_handle);
    104     }
    105 #endif
    106     if (child == (pid_t)-1)
    107         err(1, "failed to setup to fork daemon (fork failed)");
    108 
    109 #ifndef WIN32
    110     if (child == 0) {
    111         int fd;
    112 
    113         (void) close(pipefds[0]);
    114         pipefds[0] = -1;
    115         /*
    116          * Keep stdout/stderr for now so output and errors prior to
    117          * detach_finish() can be seen by the user.
    118          */
    119         fd = open(_PATH_DEVNULL, O_RDWR, 0);
    120         if (fd == -1)
    121             err(1, "failed to open /dev/null");
    122         (void) dup2(fd, STDIN_FILENO);
    123         if (fd > STDERR_FILENO)
    124             (void) close(fd);
    125         return;
    126     }
    127 #endif
    128 
    129     (void) close(pipefds[1]);
    130     pipefds[1] = -1;
    131     do {
    132         bytes = read(pipefds[0], buf, sizeof(buf));
    133     } while (bytes == -1 && errno == EINTR);
    134     (void) close(pipefds[0]);
    135     pipefds[0] = -1;
    136     if (bytes == -1) {
    137         /*
    138          * No need to wait for the process.  We've killed it.  If it
    139          * doesn't want to exit, we'd have to wait potentially forever,
    140          * but we want to indicate failure to the user as soon as
    141          * possible.  A wait with timeout would end the same way
    142          * (attempting to kill the process).
    143          */
    144         err(1, "failed to setup daemon child (read from child pipe)");
    145     }
    146     if (bytes == 0) {
    147         warnx("daemon child preparation failed, waiting for child");
    148         status = wait_for_process(child);
    149         if (SE_IS_ERROR(status) || SE_PROCSTATUS(status) != 0)
    150             errx(SE_PROCSTATUS(status),
    151                  "daemon child preparation failed (child exited)");
    152     }
    153     _exit(0);
    154 }
    155 
    156 #ifdef WIN32
    157 #ifdef dup2
    158 #undef dup2
    159 #endif
    160 #define dup2 _dup2
    161 #endif
    162 
    163 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
    164 roken_detach_finish(const char *dir, int daemon_child_fd)
    165 {
    166     char buf[1] = "";
    167     ssize_t bytes;
    168     int fd;
    169 
    170     rk_pidfile(NULL);
    171     if (pipefds[1] == -1 && daemon_child_fd != -1)
    172         pipefds[1] = daemon_child_fd;
    173     if (pipefds[0] != -1)
    174 	(void) close(pipefds[0]);
    175     if (pipefds[1] == -1)
    176         return;
    177 
    178 #ifdef HAVE_SETSID
    179     if (setsid() == -1)
    180         err(1, "failed to detach from tty");
    181 #endif
    182 
    183 #ifndef WIN32
    184     /*
    185      * Hopefully we've written any pidfiles by now, if they had to be in
    186      * the current directory...
    187      *
    188      * The daemons do re-open logs and so on, therefore this chdir()
    189      * call needs to be optional for testing.
    190      */
    191     if (dir != NULL && chdir(dir) == -1)
    192         err(1, "failed to chdir to /");
    193 #endif
    194 
    195     do {
    196         bytes = write(pipefds[1], buf, sizeof(buf));
    197     } while (bytes == -1 && errno == EINTR);
    198     if (bytes == -1)
    199         err(1, "failed to signal parent while detaching");
    200     (void) close(pipefds[1]);
    201     if (bytes != sizeof(buf))
    202         errx(1, "failed to signal parent while detaching");
    203 
    204     fd = open(_PATH_DEVNULL, O_RDWR, 0);
    205     if (fd == -1)
    206         err(1, "failed to open /dev/null");
    207     /*
    208      * Maybe we should check that our output got written, if redirected
    209      * to a file.  File utils normally do this.
    210      */
    211     (void) dup2(fd, STDOUT_FILENO);
    212     (void) dup2(fd, STDERR_FILENO);
    213     if (fd > 2)
    214         (void) close(fd);
    215 }
    216