/** $Id$ */ /** @file * VBoxService - Guest Additions Service Skeleton. */ /* * Copyright (C) 2007 innotek GmbH * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include #include #include #include #include #include #ifdef USE_LIBDAEMON #include #include #endif #include #include "VBoxServiceInternal.h" /** * Solaris daemon() call (currently does not use libdaemon). * * @returns 0 on success */ int daemon(int nochdir, int noclose) { #ifdef USE_LIBDAEMON pid_t pid = daemon_fork(); if (pid < 0) { daemon_retval_done(); return -1; } if (pid) exit(0); daemon_close_all(-1); return 0; #else if (getppid() == 1) /* We already belong to init process */ return -1; pid_t pid = fork(); if (pid < 0) /* The fork() failed. Bad. */ return -1; if (pid > 0) /* Quit parent process */ exit(0); /* * The orphaned child becomes a daemon after attaching to init. We need to get * rid of signals, file descriptors & other stuff we inherited from the parent. */ pid_t newpgid = setsid(); if (newpgid < 0) /* Failed to create new sesion */ return -1; /* BSD daemon style. */ if (!noclose) { /* Open stdin(0), stdout(1) and stderr(2) to /dev/null */ int fd = open("/dev/null", O_RDWR); dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if (fd > 2) close(fd); } /* Switch our current directory to root */ if (!nochdir) chdir("/"); /* @todo Check if switching to '/' is the convention for Solaris daemons. */ /* Set file permission to something secure, as we need to run as root on Solaris */ umask(027); return 0; #endif }