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