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 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "vbox-libhal.h"
|
---|
24 |
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/ldr.h>
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Whether we have tried to load libdbus and libhal yet. This flag should only be set
|
---|
30 | * to "true" after we have either loaded both libraries and all symbols which we need,
|
---|
31 | * or failed to load something and unloaded and set back to zero pLibDBus and pLibHal.
|
---|
32 | */
|
---|
33 | static bool gCheckedForLibHal = false;
|
---|
34 | /**
|
---|
35 | * Pointer to the libdbus shared object. This should only be set once all needed libraries
|
---|
36 | * and symbols have been successfully loaded.
|
---|
37 | */
|
---|
38 | static RTLDRMOD ghLibDBus = 0;
|
---|
39 | /**
|
---|
40 | * Pointer to the libhal shared object. This should only be set once all needed libraries
|
---|
41 | * and symbols have been successfully loaded.
|
---|
42 | */
|
---|
43 | static RTLDRMOD ghLibHal = 0;
|
---|
44 |
|
---|
45 | /** The following are the symbols which we need from libdbus and libhal. */
|
---|
46 | void (*gDBusErrorInit)(DBusError *);
|
---|
47 | DBusConnection *(*gDBusBusGet)(DBusBusType, DBusError *);
|
---|
48 | void (*gDBusErrorFree)(DBusError *);
|
---|
49 | void (*gDBusConnectionUnref)(DBusConnection *);
|
---|
50 | LibHalContext *(*gLibHalCtxNew)(void);
|
---|
51 | dbus_bool_t (*gLibHalCtxSetDBusConnection)(LibHalContext *, DBusConnection *);
|
---|
52 | dbus_bool_t (*gLibHalCtxInit)(LibHalContext *, DBusError *);
|
---|
53 | char **(*gLibHalFindDeviceByCapability)(LibHalContext *, const char *, int *, DBusError *);
|
---|
54 | char *(*gLibHalDeviceGetPropertyString)(LibHalContext *, const char *, const char *, DBusError *);
|
---|
55 | void (*gLibHalFreeString)(char *);
|
---|
56 | void (*gLibHalFreeStringArray)(char **);
|
---|
57 | dbus_bool_t (*gLibHalCtxShutdown)(LibHalContext *, DBusError *);
|
---|
58 | dbus_bool_t (*gLibHalCtxFree)(LibHalContext *);
|
---|
59 |
|
---|
60 | bool gLibHalCheckPresence(void)
|
---|
61 | {
|
---|
62 | RTLDRMOD hLibDBus;
|
---|
63 | RTLDRMOD hLibHal;
|
---|
64 |
|
---|
65 | if (ghLibDBus != 0 && ghLibHal != 0 && gCheckedForLibHal == true)
|
---|
66 | return true;
|
---|
67 | if (gCheckedForLibHal == true)
|
---|
68 | return false;
|
---|
69 | if (!RT_SUCCESS(RTLdrLoad(LIB_DBUS, &hLibDBus)))
|
---|
70 | return false;
|
---|
71 | if (!RT_SUCCESS(RTLdrLoad(LIB_HAL, &hLibHal)))
|
---|
72 | {
|
---|
73 | RTLdrClose(hLibDBus);
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 | if ( RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_init", (void **) &gDBusErrorInit))
|
---|
77 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_bus_get", (void **) &gDBusBusGet))
|
---|
78 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_error_free", (void **) &gDBusErrorFree))
|
---|
79 | && RT_SUCCESS(RTLdrGetSymbol(hLibDBus, "dbus_connection_unref",
|
---|
80 | (void **) &gDBusConnectionUnref))
|
---|
81 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_new", (void **) &gLibHalCtxNew))
|
---|
82 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_set_dbus_connection",
|
---|
83 | (void **) &gLibHalCtxSetDBusConnection))
|
---|
84 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_init", (void **) &gLibHalCtxInit))
|
---|
85 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_find_device_by_capability",
|
---|
86 | (void **) &gLibHalFindDeviceByCapability))
|
---|
87 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_device_get_property_string",
|
---|
88 | (void **) &gLibHalDeviceGetPropertyString))
|
---|
89 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_free_string", (void **) &gLibHalFreeString))
|
---|
90 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_free_string_array",
|
---|
91 | (void **) &gLibHalFreeStringArray))
|
---|
92 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_shutdown", (void **) &gLibHalCtxShutdown))
|
---|
93 | && RT_SUCCESS(RTLdrGetSymbol(hLibHal, "libhal_ctx_free", (void **) &gLibHalCtxFree))
|
---|
94 | )
|
---|
95 | {
|
---|
96 | ghLibDBus = hLibDBus;
|
---|
97 | ghLibHal = hLibHal;
|
---|
98 | gCheckedForLibHal = true;
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | RTLdrClose(hLibDBus);
|
---|
104 | RTLdrClose(hLibHal);
|
---|
105 | gCheckedForLibHal = true;
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | }
|
---|