VirtualBox

Changeset 6356 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Jan 15, 2008 1:27:51 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
27270
Message:

Integrated VbglR3Daemonize in GuestR3Lib. Linux vboxclient now uses it. Renamed LINUX_CLIPBOARD to VBOX_X11_CLIPBOARD. clipboard-new has the clipboard implemetation using R3 lib, currently used only by Solaris. Note: this change affects OS2, linux, solaris so someone should check for build breaks on OS2.

Location:
trunk/src/VBox/Additions
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp

    r6280 r6356  
    2424# define INCL_ERRORS
    2525# include <os2.h>
     26
     27# include <iprt/alloca.h>
     28# include <iprt/string.h>
     29#elif defined(RT_OS_LINUX)
     30# include <sys/stat.h>
     31# include <fcntl.h>
     32# include <stdlib.h>
     33# include <unistd.h>
     34# include <sys/time.h>
     35# include <sys/resource.h>
     36#elif defined(RT_OS_SOLARIS)
     37# include <sys/types.h>
     38# include <sys/stat.h>
     39# include <stdio.h>
     40# include <stdlib.h>
     41# include <unistd.h>
     42# include <signal.h>
     43# include <fcntl.h>
    2644#endif
    2745
     
    290308}
    291309
     310
    292311/**
    293312 * Change the IRQ filter mask.
     
    304323}
    305324
     325
     326/**
     327 * Daemonize the process for running in the background.
     328 *
     329 * @returns 0 on success
     330 *
     331 * @param   nochdir     Pass 0 to change working directory to root.
     332 * @param   noclose     Pass 0 to redirect standard file streams to /dev/null.
     333 */
     334VBGLR3DECL(int) VbglR3Daemonize(int nochdir, int noclose)
     335{
     336#if defined(RT_OS_LINUX)
     337    /** rlimit structure for finding out how many open files we may have. */
     338    struct rlimit rlim;
     339
     340    /* To make sure that we are not currently a session leader, we must first fork and let
     341       the parent process exit, as a newly created child is never session leader.  This will
     342       allow us to call setsid() later. */
     343    if (fork() != 0)
     344    {
     345        exit(0);
     346    }
     347    /* Find the maximum number of files we can have open and close them all. */
     348    if (0 != getrlimit(RLIMIT_NOFILE, &rlim))
     349    {
     350        /* For some reason the call failed.  In that case we will just close the three
     351           standard files and hope. */
     352        rlim.rlim_cur = 3;
     353    }
     354    for (unsigned int i = 0; i < rlim.rlim_cur; ++i)
     355    {
     356        close(i);
     357    }
     358    /* Change to the root directory to avoid keeping the one we were started in open. */
     359    chdir("/");
     360    /* Set our umask to zero. */
     361    umask(0);
     362    /* And open /dev/null on stdin/out/err. */
     363    open("/dev/null", O_RDONLY);
     364    open("/dev/null", O_WRONLY);
     365    dup(1);
     366    /* Detach from the controlling terminal by creating our own session, to avoid receiving
     367       signals from the old session. */
     368    setsid();
     369    /* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */
     370    if (fork() != 0)
     371    {
     372        exit(0);
     373    }
     374    NOREF(nochdir);
     375    NOREF(noclose);
     376
     377    return 0;
     378
     379#elif defined(RT_OS_OS2)
     380    PPIB pPib;
     381    PTIB pTib;
     382    DosGetInfoBlocks(&pTib, &pPib);
     383
     384    /* Get the full path to the executable. */
     385    char szExe[CCHMAXPATH];
     386    APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe);
     387    if (rc)
     388    {
     389        errno = EDOOFUS;
     390        return -1;
     391    }
     392
     393    /* calc the length of the command line. */
     394    char *pch = pPib->pib_pchcmd;
     395    size_t cch0 = strlen(pch);
     396    pch += cch0 + 1;
     397    size_t cch1 = strlen(pch);
     398    pch += cch1 + 1;
     399    char *pchArgs;
     400    if (cch1 && *pch)
     401    {
     402        do  pch = strchr(pch, '\0') + 1;
     403        while (*pch);
     404
     405        size_t cchTotal = pch - pPib->pib_pchcmd;
     406        pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0"));
     407        memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1);
     408        memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0"));
     409    }
     410    else
     411    {
     412        size_t cchTotal = pch - pPib->pib_pchcmd + 1;
     413        pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized "));
     414        memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1);
     415        pch = pchArgs + cch0 + 1;
     416        memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1);
     417        pch += sizeof(" --daemonized ") - 1;
     418        if (cch1)
     419            memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2);
     420        else
     421            pch[0] = pch[1] = '\0';
     422    }
     423
     424    /* spawn a detach process  */
     425    char szObj[128];
     426    RESULTCODES ResCodes = { 0, 0 };
     427    szObj[0] = '\0';
     428    rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe);
     429    if (rc)
     430    {
     431        /** @todo Change this to some standard log/print error?? */
     432        /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */
     433        errno = EDOOFUS;
     434        return -1;
     435    }
     436    DosExit(EXIT_PROCESS, 0);
     437    return -1;
     438
     439#elif defined(RT_OS_SOLARIS)
     440    if (getppid() == 1) /* We already belong to init process */
     441        return -1;
     442
     443    pid_t pid = fork();
     444    if (pid < 0)         /* The fork() failed. Bad. */
     445        return -1;
     446
     447    if (pid > 0)         /* Quit parent process */
     448        exit(0);
     449
     450    /*
     451     * The orphaned child becomes a daemon after attaching to init. We need to get
     452     * rid of signals, file descriptors & other stuff we inherited from the parent.
     453     */
     454    pid_t newpgid = setsid();
     455    if (newpgid < 0)     /* Failed to create new sesion */
     456        return -1;
     457
     458    /* BSD daemon style. */
     459    if (!noclose)
     460    {
     461        /* Open stdin(0), stdout(1) and stderr(2) to /dev/null */
     462        int fd = open("/dev/null", O_RDWR);
     463        dup2(fd, STDIN_FILENO);
     464        dup2(fd, STDOUT_FILENO);
     465        dup2(fd, STDERR_FILENO);
     466        if (fd > 2)
     467            close(fd);
     468    }
     469
     470    /* Switch our current directory to root */
     471    if (!nochdir)
     472        chdir("/");     /* @todo Check if switching to '/' is the convention for Solaris daemons. */
     473
     474    /* Set file permission to something secure, as we need to run as root on Solaris */
     475    umask(027);
     476    return 0;
     477#endif
     478}
     479
  • trunk/src/VBox/Additions/common/VBoxService/Makefile.kmk

    r6139 r6356  
    3131        VBoxServiceTimeSync.cpp
    3232VBoxService_SOURCES.os2   = \
    33         VBoxService-os2.cpp \
    3433        VBoxService-os2.def \
    3534        VBoxServiceClipboard-os2.cpp
    36 VBoxService_SOURCES.solaris = \
    37     VBoxService-solaris.cpp
    3835VBoxService_LIBS          = \
    3936        $(VBOX_LIB_VBGL_R3) \
    4037        $(VBOX_LIB_IPRT_GUEST_R3)
    41 #VBoxService_LIBS.solaris  = daemon
    4238
    4339include $(PATH_KBUILD)/subfooter.kmk
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r6285 r6356  
    371371        VBoxServiceVerbose(1, "Daemonizing...\n");
    372372        errno = 0;
    373         if (daemon(0, 0) != 0)
     373        if (VbglR3Daemonize(0, 0) != 0)
    374374            return VBoxServiceError("daemon failed: %s\n", strerror(errno));
    375375        /* in-child */
  • trunk/src/VBox/Additions/x11/Makefile.kmk

    r6301 r6356  
    2121# Include sub-dirs.
    2222SUBDIRS =
    23 if1of ($(BUILD_TARGET),linux)
     23if1of ($(BUILD_TARGET),linux solaris)
    2424SUBDIRS += \
    2525        xclient \
     
    2828endif
    2929
    30 ifeq ($(BUILD_TARGET),solaris)
    31 SUBDIRS += \
    32         xmouse \
    33         xgraphics
    34 endif
    35 
    3630include $(PATH_KBUILD)/subfooter.kmk
    3731
  • trunk/src/VBox/Additions/x11/xclient/Makefile.kmk

    r6290 r6356  
    1919include $(PATH_KBUILD)/header.kmk
    2020
     21if1of ($(BUILD_TARGET),linux l4)
    2122ifneq ($(USERNAME),bird) # crap
    2223 PROGRAMS = vboxadd-xclient
     
    2728vboxadd-xclient_TEMPLATE = VBOXLNX32GUESTR3EXE
    2829vboxadd-xclient_SOURCES  = clipboard.cpp main.cpp
    29 vboxadd-xclient_DEFS    += CLIPBOARD_LINUX
     30vboxadd-xclient_DEFS    += VBOX_X11_CLIPBOARD
    3031
    3132ifdef LINUX_SEAMLESS_GUEST
     
    7879 endif
    7980endif
     81endif # target linux l4
     82
     83ifeq ($(BUILD_TARGET),solaris)
     84PROGRAMS = VBoxClient
     85
     86VBoxClient_TEMPLATE = VBOXGUESTR3EXE
     87VBoxClient_DEFS    += VBOX_X11_CLIPBOARD VBOX_HGCM
     88VBoxClient_SOURCES  = \
     89        clipboard-new.cpp \
     90        main.cpp
     91VBoxClient_LIBS = \
     92        $(VBOX_LIB_VBGL_R3) \
     93        $(VBOX_LIB_IPRT_GUEST_R3) \
     94        X11 \
     95        Xt
     96endif # target solaris
    8097
    8198include $(PATH_KBUILD)/footer.kmk
  • trunk/src/VBox/Additions/x11/xclient/main.cpp

    r6290 r6356  
    2626
    2727#include <sys/types.h>
    28 #include <sys/stat.h>     /* For umask */
    29 #include <fcntl.h>        /* For open */
    3028#include <stdlib.h>       /* For exit */
    3129#include <unistd.h>
    3230#include <getopt.h>
    33 
    34 #include <sys/time.h>     /* For getrlimit */
    35 #include <sys/resource.h> /* For getrlimit */
    3631
    3732#include <X11/Xlib.h>
     
    4540
    4641static bool gbDaemonise = true;
    47 
    48 /**
    49  * Go through the long Un*x ritual required to become a daemon process.
    50  */
    51 void vboxDaemonise(void)
    52 {
    53     /** rlimit structure for finding out how many open files we may have. */
    54     struct rlimit rlim;
    55 
    56     /* To make sure that we are not currently a session leader, we must first fork and let
    57        the parent process exit, as a newly created child is never session leader.  This will
    58        allow us to call setsid() later. */
    59     if (fork() != 0)
    60     {
    61         exit(0);
    62     }
    63     /* Find the maximum number of files we can have open and close them all. */
    64     if (0 != getrlimit(RLIMIT_NOFILE, &rlim))
    65     {
    66         /* For some reason the call failed.  In that case we will just close the three
    67            standard files and hope. */
    68         rlim.rlim_cur = 3;
    69     }
    70     for (unsigned int i = 0; i < rlim.rlim_cur; ++i)
    71     {
    72         close(i);
    73     }
    74     /* Change to the root directory to avoid keeping the one we were started in open. */
    75     chdir("/");
    76     /* Set our umask to zero. */
    77     umask(0);
    78     /* And open /dev/null on stdin/out/err. */
    79     open("/dev/null", O_RDONLY);
    80     open("/dev/null", O_WRONLY);
    81     dup(1);
    82     /* Detach from the controlling terminal by creating our own session, to avoid receiving
    83        signals from the old session. */
    84     setsid();
    85     /* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */
    86     if (fork() != 0)
    87     {
    88         exit(0);
    89     }
    90 }
    9142
    9243/**
     
    11061        return 0;
    11162    }
    112 #ifdef CLIPBOARD_LINUX
     63#ifdef VBOX_X11_CLIPBOARD
    11364    vboxClipboardDisconnect();
    11465#endif
     
    157108        }
    158109    }
     110    gbDaemonise = false; // ram
    159111    if (gbDaemonise)
    160112    {
    161         vboxDaemonise();
     113        if (VbglR3Daemonize(0, 0) != 0)
     114        {
     115            LogRel(("VBoxService: failed to daemonize. exiting."));
     116            return 1;
     117        }
    162118    }
    163119    /* Initialise our runtime before all else. */
     
    178134    /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
    179135    XSetErrorHandler(vboxClientXLibErrorHandler);
    180 #ifdef CLIPBOARD_LINUX
     136#ifdef VBOX_X11_CLIPBOARD
    181137    /* Connect to the host clipboard. */
    182138    LogRel(("VBoxService: starting clipboard Guest Additions...\n"));
     
    186142        LogRel(("VBoxService: vboxClipboardConnect failed with rc = %Rrc\n", rc));
    187143    }
    188 #endif  /* CLIPBOARD_LINUX defined */
     144#endif  /* VBOX_X11_CLIPBOARD defined */
    189145#ifdef SEAMLESS_LINUX
    190146    try
     
    208164    }
    209165#endif /* SEAMLESS_LINUX defined */
    210 #ifdef CLIPBOARD_LINUX
     166#ifdef VBOX_X11_CLIPBOARD
    211167    LogRel(("VBoxService: connecting to the shared clipboard service.\n"));
    212168    vboxClipboardMain();
    213169    vboxClipboardDisconnect();
    214 #else  /* CLIPBOARD_LINUX not defined */
     170#else  /* VBOX_X11_CLIPBOARD not defined */
    215171    LogRel(("VBoxService: sleeping...\n"));
    216172    pause();
    217173    LogRel(("VBoxService: exiting...\n"));
    218 #endif  /* CLIPBOARD_LINUX not defined */
     174#endif  /* VBOX_X11_CLIPBOARD not defined */
    219175#ifdef SEAMLESS_LINUX
    220176    try
Note: See TracChangeset for help on using the changeset viewer.

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