VirtualBox

Changeset 5866 in vbox


Ignore:
Timestamp:
Nov 28, 2007 6:31:32 PM (17 years ago)
Author:
vboxsync
Message:

Guest additions (Linux userspace client): hopefully fixed daemonising

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/linux/xclient/main.cpp

    r5823 r5866  
    2929
    3030#include <sys/types.h>
     31#include <sys/stat.h>     /* For umask */
     32#include <fcntl.h>        /* For open */
    3133#include <unistd.h>
    3234#include <getopt.h>
    3335
     36#include <sys/time.h>     /* For getrlimit */
     37#include <sys/resource.h> /* For getrlimit */
     38
    3439#include <X11/Xlib.h>
    3540#include <X11/Intrinsic.h>
     
    4449
    4550/**
    46  * Become a daemon process
     51 * Go through the long Un*x ritual required to become a daemon process.
    4752 */
    4853void vboxDaemonise(void)
    4954{
    50     /* First fork and exit the parent process, so that we are sure we are not session leader. */
     55    /** rlimit structure for finding out how many open files we may have. */
     56    struct rlimit rlim;
     57
     58    /* To make sure that we are not currently a session leader, we must first fork and let
     59       the parent process exit, as a newly created child is never session leader.  This will
     60       allow us to call setsid() later. */
    5161    if (fork() != 0)
    5262    {
    5363        exit(0);
    5464    }
    55     /* Detach from the controlling terminal by creating our own session. */
     65    /* Find the maximum number of files we can have open and close them all. */
     66    if (0 != getrlimit(RLIMIT_NOFILE, &rlim))
     67    {
     68        /* For some reason the call failed.  In that case we will just close the three
     69           standard files and hope. */
     70        rlim.rlim_cur = 3;
     71    }
     72    for (int i = 0; i < rlim.rlim_cur; ++i)
     73    {
     74        close(i);
     75    }
     76    /* Change to the root directory to avoid keeping the one we were started in open. */
     77    chdir("/");
     78    /* Set our umask to zero. */
     79    umask(0);
     80    /* And open /dev/null on stdin/out/err. */
     81    open("/dev/null", O_RDONLY);
     82    open("/dev/null", O_WRONLY);
     83    dup(1);
     84    /* Detach from the controlling terminal by creating our own session, to avoid receiving
     85       signals from the old session. */
    5686    setsid();
    57     /* And change to the root directory to avoid holding the one we were started in open. */
    58     chdir("/");
    59     /* Close the standard files. */
    60     close(0);
    61     close(1);
    62     close(2);
     87    /* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */
     88    if (fork() != 0)
     89    {
     90        exit(0);
     91    }
    6392}
    6493
     
    131160        }
    132161    }
     162    if (gbDaemonise)
     163    {
     164        vboxDaemonise();
     165    }
    133166    /* Initialise our runtime before all else. */
    134167    RTR3Init(false);
     
    175208    }
    176209#endif /* SEAMLESS_LINUX defined */
    177     if (gbDaemonise)
    178     {
    179         vboxDaemonise();
    180     }
    181210    vboxClipboardMain();
    182211    vboxClipboardDisconnect();
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette