VirtualBox

Changeset 16178 in vbox for trunk/src/VBox/Main/linux


Ignore:
Timestamp:
Jan 22, 2009 3:39:48 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
41924
Message:

Main/Linux: fixes and clean ups in the DBus code

Location:
trunk/src/VBox/Main/linux
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/linux/HostHardwareLinux.cpp

    r16137 r16178  
    7979extern void VBoxHalShutdown (DBusConnection *pConnection);
    8080extern void VBoxHalShutdownPrivate (DBusConnection *pConnection);
     81extern void VBoxDBusConnectionUnref(DBusConnection *pConnection);
     82extern void VBoxDBusConnectionCloseAndUnref(DBusConnection *pConnection);
     83extern void VBoxDBusMessageUnref(DBusMessage *pMessage);
    8184
    8285static int halInit(RTMemAutoPtr <DBusConnection, VBoxHalShutdown> *pConnection);
     
    109112#if defined(RT_OS_LINUX)
    110113#ifdef VBOX_WITH_DBUS
    111         if (RT_SUCCESS (rc) && VBoxDBusCheckPresence() && (!success || testing()))
     114        if (RT_SUCCESS (rc) && RT_SUCCESS(VBoxLoadDBusLib()) && (!success || testing()))
    112115            rc = getDriveInfoFromHal(&mDVDList, true /* isDVD */, &success);
    113116#endif /* VBOX_WITH_DBUS defined */
     
    154157#if defined(RT_OS_LINUX)
    155158#ifdef VBOX_WITH_DBUS
    156         if (RT_SUCCESS (rc) && VBoxDBusCheckPresence() && (!success || testing()))
     159        if (   RT_SUCCESS (rc)
     160            && RT_SUCCESS(VBoxLoadDBusLib())
     161            && (!success || testing()))
    157162            rc = getDriveInfoFromHal(&mFloppyList, false /* isDVD */, &success);
    158163#endif /* VBOX_WITH_DBUS defined */
     
    197202#if defined(RT_OS_LINUX)
    198203#ifdef VBOX_WITH_DBUS
    199         if (RT_SUCCESS (rc) && VBoxDBusCheckPresence() && (!success || testing()))
     204        if (   RT_SUCCESS (rc)
     205            && RT_SUCCESS(VBoxLoadDBusLib())
     206            && (!success || testing()))
    200207            rc = getUSBDeviceInfoFromHal(&mDeviceList, &halSuccess);
    201208        /* Try the old API if the new one *succeeded* as only one of them will
     
    226233    /** A flag to say that we wish to interrupt the current wait. */
    227234    bool mInterrupt;
     235    /** Constructor */
     236    Context() : mTriggered(false), mInterrupt(false) {}
    228237#endif  /* defined RT_OS_LINUX && defined VBOX_WITH_DBUS */
    229238};
     
    238247    int rc = VINF_SUCCESS;
    239248
    240     if (VBoxDBusCheckPresence())
    241     {
    242         mContext = new Context;
     249    mContext = new Context;
     250    if (RT_SUCCESS(VBoxLoadDBusLib()))
     251    {
    243252        for (unsigned i = 0; RT_SUCCESS(rc) && i < 5 && !mContext->mConnection; ++i)
    244253        {
     
    617626    autoDBusError dbusError;
    618627
    619     RTMemAutoPtr <DBusConnection, VBoxDBusConnectionUnref> dbusConnection;
     628    RTMemAutoPtr <DBusConnection, VBoxDBusConnectionCloseAndUnref> dbusConnection;
    620629    dbusConnection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &dbusError.get());
    621630    if (!dbusConnection)
     
    650659 * @param   pConnection  the connection handle
    651660 */
    652 /* static */
     661/* extern */
    653662void VBoxHalShutdown (DBusConnection *pConnection)
    654663{
     
    672681 * @param   pConnection  the connection handle
    673682 */
    674 /* static */
     683/* extern */
    675684void VBoxHalShutdownPrivate (DBusConnection *pConnection)
    676685{
     
    689698    LogFlowFunc(("returning\n"));
    690699    dbusError.FlowLog();
     700}
     701
     702/** Wrapper around dbus_connection_unref.  We need this to use it as a real
     703 * function in auto pointers, as a function pointer won't wash here. */
     704/* extern */
     705void VBoxDBusConnectionUnref(DBusConnection *pConnection)
     706{
     707    dbus_connection_unref(pConnection);
     708}
     709
     710/**
     711 * This function closes and unrefs a private connection to dbus.  It should
     712 * only be called once no-one else is referencing the connection.
     713 */
     714/* extern */
     715void VBoxDBusConnectionCloseAndUnref(DBusConnection *pConnection)
     716{
     717    dbus_connection_close(pConnection);
     718    dbus_connection_unref(pConnection);
     719}
     720
     721/** Wrapper around dbus_message_unref.  We need this to use it as a real
     722 * function in auto pointers, as a function pointer won't wash here. */
     723/* extern */
     724void VBoxDBusMessageUnref(DBusMessage *pMessage)
     725{
     726    dbus_message_unref(pMessage);
    691727}
    692728
  • trunk/src/VBox/Main/linux/vbox-dbus.cpp

    r16006 r16178  
    2323#include "vbox-dbus.h"
    2424
    25 #include <iprt/err.h>
     25#define LOG_GROUP LOG_GROUP_MAIN
     26#include <VBox/log.h>
     27#include <VBox/err.h>
     28
    2629#include <iprt/ldr.h>
    27 
    28 /**
    29  * Whether we have tried to load libdbus-1 yet.  This flag should only be set
    30  * to "true" after we have either loaded the library and all symbols which we
    31  * need, or failed to load something and unloaded and set back to zero
    32  * pLibDBus.
    33  */
    34 static bool gCheckedForLibDBus = false;
    35 /**
    36  * Pointer to the libdbus-1 shared object.  This should only be set once all
    37  * needed libraries and symbols have been successfully loaded.
    38  */
    39 static RTLDRMOD ghLibDBus = NULL;
     30#include <iprt/assert.h>
     31#include <iprt/once.h>
    4032
    4133/** The following are the symbols which we need from libdbus. */
    42 void (*vbox_dbus_error_init)(DBusError *);
    43 DBusConnection *(*vbox_dbus_bus_get)(DBusBusType, DBusError *);
    44 DBusConnection *(*vbox_dbus_bus_get_private)(DBusBusType, DBusError *);
    45 void (*vbox_dbus_error_free)(DBusError *);
    46 void (*vbox_dbus_connection_unref)(DBusConnection *);
    47 void (*vbox_dbus_connection_close)(DBusConnection *);
    48 void (*vbox_dbus_connection_set_exit_on_disconnect)(DBusConnection *, dbus_bool_t);
    49 dbus_bool_t (*vbox_dbus_bus_name_has_owner)(DBusConnection *, const char *,
    50                                             DBusError *);
    51 void (*vbox_dbus_bus_add_match)(DBusConnection *, const char *, DBusError *);
    52 void (*vbox_dbus_bus_remove_match)(DBusConnection *, const char *, DBusError *);
    53 void (*vbox_dbus_message_unref)(DBusMessage *);
    54 DBusMessage* (*vbox_dbus_message_new_method_call)(const char *, const char *,
    55                                                   const char *, const char *);
    56 void (*vbox_dbus_message_iter_init_append)(DBusMessage *, DBusMessageIter *);
    57 dbus_bool_t (*vbox_dbus_message_iter_append_basic)(DBusMessageIter *, int,
    58                                                    const void *);
    59 DBusMessage * (*vbox_dbus_connection_send_with_reply_and_block)(DBusConnection *,
    60                                                                 DBusMessage *, int,
    61                                                                 DBusError *error);
    62 dbus_bool_t (*vbox_dbus_message_iter_init) (DBusMessage *, DBusMessageIter *);
    63 int (*vbox_dbus_message_iter_get_arg_type) (DBusMessageIter *);
    64 int (*vbox_dbus_message_iter_get_element_type) (DBusMessageIter *);
    65 void (*vbox_dbus_message_iter_recurse) (DBusMessageIter *, DBusMessageIter *);
    66 void (*vbox_dbus_message_iter_get_basic) (DBusMessageIter *, void *);
    67 dbus_bool_t (*vbox_dbus_message_iter_next) (DBusMessageIter *);
    68 dbus_bool_t (*vbox_dbus_connection_add_filter) (DBusConnection *, DBusHandleMessageFunction,
    69                                                 void *, DBusFreeFunction);
    70 void (*vbox_dbus_connection_remove_filter) (DBusConnection *, DBusHandleMessageFunction,
    71                                             void *);
    72 dbus_bool_t (*vbox_dbus_connection_read_write_dispatch) (DBusConnection *, int);
    73 dbus_bool_t (*vbox_dbus_message_is_signal) (DBusMessage *, const char *, const char *);
    74 DBusMessage *(*vbox_dbus_connection_pop_message)(DBusConnection *);
     34#define VBOX_PROXY_STUB(function, rettype, signature, shortsig) \
     35void (*function ## _fn)(void); \
     36rettype function signature \
     37{ return ( (rettype (*) signature) function ## _fn ) shortsig; }
    7538
    76 bool VBoxDBusCheckPresence(void)
     39#include "vbox-dbus-internal.h"
     40
     41static int32_t loadDBusLibOnce(void *, void *);
     42
     43#undef VBOX_PROXY_STUB
     44
     45/* Now comes a table of functions to be loaded from libdbus-1 */
     46typedef struct
    7747{
    78     RTLDRMOD hLibDBus;
     48    const char *name;
     49    void (**fn)(void);
     50} SHARED_FUNC;
    7951
    80     if (ghLibDBus != 0 && gCheckedForLibDBus == true)
    81         return true;
    82     if (gCheckedForLibDBus == true)
    83         return false;
    84     if (   !RT_SUCCESS(RTLdrLoad(LIB_DBUS_1_3, &hLibDBus))
    85         && !RT_SUCCESS(RTLdrLoad(LIB_DBUS_1_2, &hLibDBus)))
    86     {
    87         return false;
    88     }
    89     if (   RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_init",
    90                                      (void **) &vbox_dbus_error_init))
    91         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_get",
    92                                      (void **) &vbox_dbus_bus_get))
    93         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_get_private",
    94                                      (void **) &vbox_dbus_bus_get_private))
    95         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_free",
    96                                      (void **) &vbox_dbus_error_free))
    97         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_unref",
    98                                      (void **) &vbox_dbus_connection_unref))
    99         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_close",
    100                                      (void **) &vbox_dbus_connection_close))
    101         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_set_exit_on_disconnect",
    102                                      (void **) &vbox_dbus_connection_set_exit_on_disconnect))
    103         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_name_has_owner",
    104                                      (void **) &vbox_dbus_bus_name_has_owner))
    105         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_add_match",
    106                                      (void **) &vbox_dbus_bus_add_match))
    107         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_remove_match",
    108                                      (void **) &vbox_dbus_bus_remove_match))
    109         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_unref",
    110                                      (void **) &vbox_dbus_message_unref))
    111         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_new_method_call",
    112                                      (void **) &vbox_dbus_message_new_method_call))
    113         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_init_append",
    114                                      (void **) &vbox_dbus_message_iter_init_append))
    115         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_append_basic",
    116                                      (void **) &vbox_dbus_message_iter_append_basic))
    117         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_send_with_reply_and_block",
    118                                      (void **) &vbox_dbus_connection_send_with_reply_and_block))
    119         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_init",
    120                                      (void **) &vbox_dbus_message_iter_init))
    121         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_get_arg_type",
    122                                      (void **) &vbox_dbus_message_iter_get_arg_type))
    123         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_get_element_type",
    124                                      (void **) &vbox_dbus_message_iter_get_element_type))
    125         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_recurse",
    126                                      (void **) &vbox_dbus_message_iter_recurse))
    127         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_get_basic",
    128                                      (void **) &vbox_dbus_message_iter_get_basic))
    129         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_iter_next",
    130                                      (void **) &vbox_dbus_message_iter_next))
    131         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_add_filter",
    132                                      (void **) &vbox_dbus_connection_add_filter))
    133         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_remove_filter",
    134                                      (void **) &vbox_dbus_connection_remove_filter))
    135         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_read_write_dispatch",
    136                                      (void **) &vbox_dbus_connection_read_write_dispatch))
    137         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_message_is_signal",
    138                                      (void **) &vbox_dbus_message_is_signal))
    139         && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_pop_message",
    140                                      (void **) &vbox_dbus_connection_pop_message))
    141        )
    142     {
    143         ghLibDBus = hLibDBus;
    144         gCheckedForLibDBus = true;
    145         return true;
    146     }
    147     else
    148     {
    149         RTLdrClose(hLibDBus);
    150         gCheckedForLibDBus = true;
    151         return false;
    152     }
     52#define VBOX_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } ,
     53static SHARED_FUNC SharedFuncs[] =
     54{
     55#include "vbox-dbus-internal.h"
     56    { NULL, NULL }
     57};
     58#undef VBOX_PROXY_STUB
     59
     60/* extern */
     61int VBoxLoadDBusLib(void)
     62{
     63    static RTONCE sOnce = RTONCE_INITIALIZER;
     64
     65    LogFlowFunc(("\n"));
     66    int rc = RTOnce (&sOnce, loadDBusLibOnce, NULL, NULL);
     67    LogFlowFunc(("rc = %Rrc\n", rc));
     68    return rc;
    15369}
    15470
    155 void VBoxDBusConnectionUnref(DBusConnection *pConnection)
     71/* The function which does the actual work for VBoxLoadDBusLib, serialised for
     72 * thread safety. */
     73static int32_t loadDBusLibOnce(void *, void *)
    15674{
    157     dbus_connection_unref(pConnection);
     75    int rc = VINF_SUCCESS;
     76    RTLDRMOD hLib;
     77
     78    LogFlowFunc(("\n"));
     79    rc = RTLdrLoad(VBOX_DBUS_1_2_LIB, &hLib);
     80    if (RT_FAILURE(rc))
     81        rc = RTLdrLoad(VBOX_DBUS_1_3_LIB, &hLib);
     82    if (RT_FAILURE(rc))
     83        LogRelFunc(("Failed to load library %s or %s\n",
     84                    VBOX_DBUS_1_2_LIB, VBOX_DBUS_1_3_LIB));
     85    for (unsigned i = 0; RT_SUCCESS(rc) && SharedFuncs[i].name != NULL; ++i)
     86        rc = RTLdrGetSymbol(hLib, SharedFuncs[i].name, (void**)SharedFuncs[i].fn);
     87    LogFlowFunc(("rc = %Rrc\n", rc));
     88    return rc;
    15889}
    15990
    160 void VBoxDBusMessageUnref(DBusMessage *pMessage)
    161 {
    162     dbus_message_unref(pMessage);
    163 }
    164 
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