2 * Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
4 * This file is part of the pam_usb project. pam_usb is free software;
5 * you can redistribute it and/or modify it under the terms of the GNU General
6 * Public License version 2, as published by the Free Software Foundation.
8 * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/types.h>
22 #include <dbus/dbus.h>
23 #include <libhal-storage.h>
26 DBusConnection
*pusb_hal_dbus_connect(void)
28 DBusConnection
*dbus
= NULL
;
31 dbus_error_init(&error
);
32 if (!(dbus
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
)))
34 /* Workaround for https://bugs.freedesktop.org/show_bug.cgi?id=11876 */
38 if (!(euid
= geteuid()) && (ruid
= getuid()))
40 dbus_error_free(&error
);
42 dbus
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
);
47 log_error("Cannot connect to system bus: %s\n",
49 dbus_error_free(&error
);
56 void pusb_hal_dbus_disconnect(DBusConnection
*dbus
)
58 dbus_connection_unref(dbus
);
61 LibHalContext
*pusb_hal_init(DBusConnection
*dbus
)
64 LibHalContext
*ctx
= NULL
;
66 dbus_error_init(&error
);
67 if (!(ctx
= libhal_ctx_new()))
69 log_error("Failed to create a HAL context\n");
72 if (!libhal_ctx_set_dbus_connection(ctx
, dbus
))
74 log_error("Failed to attach dbus connection to hal\n");
78 if (!libhal_ctx_init(ctx
, &error
))
80 log_error("libhal_ctx_init: %s\n", error
.name
, error
.message
);
87 void pusb_hal_destroy(LibHalContext
*ctx
)
92 char *pusb_hal_get_property(LibHalContext
*ctx
,
99 dbus_error_init(&error
);
100 data
= libhal_device_get_property_string(ctx
, udi
,
104 log_debug("%s\n", error
.message
);
105 dbus_error_free(&error
);
111 int pusb_hal_check_property(LibHalContext
*ctx
,
119 data
= pusb_hal_get_property(ctx
, udi
, name
);
122 retval
= (strcmp(data
, value
) == 0);
123 libhal_free_string(data
);
127 char **pusb_hal_find_all_items(LibHalContext
*ctx
,
128 const char *property
,
137 dbus_error_init(&error
);
139 devices
= libhal_manager_find_device_string_match(ctx
,
146 log_error("Unable to find item \"%s\": %s\n", property
,
148 dbus_error_free(&error
);
153 libhal_free_string_array(devices
);
160 char *pusb_hal_find_item(LibHalContext
*ctx
,
161 const char *property
,
171 devices
= pusb_hal_find_all_items(ctx
, property
, value
, &n_devices
);
177 for (i
= 0; i
< n_devices
; ++i
)
183 while ((key
= va_arg(ap
, char *)))
187 value
= va_arg(ap
, char *);
188 if (!value
|| *value
== 0x0)
190 if (!pusb_hal_check_property(ctx
, devices
[i
],
193 log_debug("%s did match, but property %s didn't (expecting \"%s\")\n",
194 property
, key
, value
);
201 udi
= strdup(devices
[i
]);
206 libhal_free_string_array(devices
);