1 | /** @file
|
---|
2 | *
|
---|
3 | * Module to dynamically load libhal and libdbus and load all symbols
|
---|
4 | * which are needed by VirtualBox.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "vbox-libhal.h"
|
---|
20 |
|
---|
21 | #include <iprt/err.h>
|
---|
22 | #include <iprt/ldr.h>
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Whether we have tried to load libdbus and libhal yet. This flag should only be set
|
---|
26 | * to "true" after we have either loaded both libraries and all symbols which we need,
|
---|
27 | * or failed to load something and unloaded and set back to zero pLibDBus and pLibHal.
|
---|
28 | */
|
---|
29 | static bool gCheckedForLibHal = false;
|
---|
30 | /**
|
---|
31 | * Pointer to the libdbus shared object. This should only be set once all needed libraries
|
---|
32 | * and symbols have been successfully loaded.
|
---|
33 | */
|
---|
34 | static RTLDRMOD ghLibDBus = 0;
|
---|
35 | /**
|
---|
36 | * Pointer to the libhal shared object. This should only be set once all needed libraries
|
---|
37 | * and symbols have been successfully loaded.
|
---|
38 | */
|
---|
39 | static RTLDRMOD ghLibHal = 0;
|
---|
40 |
|
---|
41 | /** The following are the symbols which we need from libdbus and libhal. */
|
---|
42 | void (*gDBusErrorInit)(DBusError *);
|
---|
43 | DBusConnection *(*gDBusBusGet)(DBusBusType, DBusError *);
|
---|
44 | void (*gDBusErrorFree)(DBusError *);
|
---|
45 | void (*gDBusConnectionUnref)(DBusConnection *);
|
---|
46 | LibHalContext *(*gLibHalCtxNew)(void);
|
---|
47 | dbus_bool_t (*gLibHalCtxSetDBusConnection)(LibHalContext *, DBusConnection *);
|
---|
48 | dbus_bool_t (*gLibHalCtxInit)(LibHalContext *, DBusError *);
|
---|
49 | char **(*gLibHalFindDeviceStringMatch)(LibHalContext *, const char *, const char *, int *,
|
---|
50 | DBusError *);
|
---|
51 | char *(*gLibHalDeviceGetPropertyString)(LibHalContext *, const char *, const char *, DBusError *);
|
---|
52 | void (*gLibHalFreeString)(char *);
|
---|
53 | void (*gLibHalFreeStringArray)(char **);
|
---|
54 | dbus_bool_t (*gLibHalCtxShutdown)(LibHalContext *, DBusError *);
|
---|
55 | dbus_bool_t (*gLibHalCtxFree)(LibHalContext *);
|
---|
56 |
|
---|
57 | bool gLibHalCheckPresence(void)
|
---|
58 | {
|
---|
59 | RTLDRMOD hLibDBus;
|
---|
60 | RTLDRMOD hLibHal;
|
---|
61 |
|
---|
62 | if (ghLibDBus != 0 && ghLibHal != 0 && gCheckedForLibHal == true)
|
---|
63 | return true;
|
---|
64 | if (gCheckedForLibHal == true)
|
---|
65 | return false;
|
---|
66 | if (!RT_SUCCESS(RTLdrLoad(LIB_DBUS, &hLibDBus)))
|
---|
67 | return false;
|
---|
68 | if (!RT_SUCCESS(RTLdrLoad(LIB_HAL, &hLibHal)))
|
---|
69 | {
|
---|
70 | RTLdrClose(hLibDBus);
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | if ( RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_init", (void **) &gDBusErrorInit))
|
---|
74 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_get", (void **) &gDBusBusGet))
|
---|
75 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_free", (void **) &gDBusErrorFree))
|
---|
76 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_unref",
|
---|
77 | (void **) &gDBusConnectionUnref))
|
---|
78 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_new", (void **) &gLibHalCtxNew))
|
---|
79 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_set_dbus_connection",
|
---|
80 | (void **) &gLibHalCtxSetDBusConnection))
|
---|
81 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_init", (void **) &gLibHalCtxInit))
|
---|
82 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_manager_find_device_string_match",
|
---|
83 | (void **) &gLibHalFindDeviceStringMatch))
|
---|
84 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_device_get_property_string",
|
---|
85 | (void **) &gLibHalDeviceGetPropertyString))
|
---|
86 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_free_string", (void **) &gLibHalFreeString))
|
---|
87 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_free_string_array",
|
---|
88 | (void **) &gLibHalFreeStringArray))
|
---|
89 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_shutdown", (void **) &gLibHalCtxShutdown))
|
---|
90 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_free", (void **) &gLibHalCtxFree))
|
---|
91 | )
|
---|
92 | {
|
---|
93 | ghLibDBus = hLibDBus;
|
---|
94 | ghLibHal = hLibHal;
|
---|
95 | gCheckedForLibHal = true;
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | RTLdrClose(hLibDBus);
|
---|
101 | RTLdrClose(hLibHal);
|
---|
102 | gCheckedForLibHal = true;
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | }
|
---|