4 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
5 * Copyright (C) 2008 Nedko Arnaudov
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <string.h> /* strerror() */
26 #include "common/safety.h"
27 #include "common/debug.h"
28 #include "dbus/service.h"
31 service_new(const char *service_name
,
36 if (!quit
|| num_paths
< 1) {
37 lash_debug("Invalid arguments");
41 lash_debug("Creating D-Bus service");
47 object_path_t
**path_pptr
, *path_ptr
;
49 service
= lash_calloc(1, sizeof(service_t
));
50 service
->object_paths
= lash_calloc(num_paths
+ 1,
51 sizeof(object_path_t
*));
53 dbus_error_init(&err
);
55 service
->connection
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
56 if (dbus_error_is_set(&err
)) {
57 lash_error("Failed to get bus: %s", err
.message
);
61 if (!(service
->unique_name
= dbus_bus_get_unique_name(service
->connection
))) {
62 lash_error("Failed to read unique bus name");
67 r
= dbus_bus_request_name(service
->connection
, service_name
,
68 DBUS_NAME_FLAG_DO_NOT_QUEUE
,
71 lash_error("Failed to acquire bus name: %s", err
.message
);
73 } else if (r
== DBUS_REQUEST_NAME_REPLY_EXISTS
) {
74 lash_error("Requested bus name already exists");
78 service
->name
= lash_strdup(service_name
);
80 service
->name
= lash_strdup("");
83 /* Populate the array and register object paths */
84 va_start(argp
, num_paths
);
85 for (path_pptr
= service
->object_paths
;
86 (*path_pptr
= va_arg(argp
, object_path_t
*));
88 if (!object_path_register(service
->connection
,
90 lash_error("Failed to register object path");
97 /* Set the keepalive pointer */
103 dbus_error_free(&err
);
106 lash_free(&service
->object_paths
);
107 service_destroy(service
);
109 /* Always free the object paths which were passed to us so that
110 way we can guarantee that the caller need not worry. */
111 va_start(argp
, num_paths
);
112 while ((path_ptr
= va_arg(argp
, object_path_t
*)))
120 service_destroy(service_t
*service
)
122 lash_debug("Destroying D-Bus service");
125 /* cut the bus connection */
126 if (service
->connection
) {
127 dbus_connection_unref(service
->connection
);
128 service
->connection
= NULL
;
131 /* reap the object path(s) */
132 if (service
->object_paths
) {
133 object_path_t
**path_pptr
;
134 for (path_pptr
= service
->object_paths
;
135 *path_pptr
; ++path_pptr
) {
136 object_path_destroy(*path_pptr
);
139 free(service
->object_paths
);
140 service
->object_paths
= NULL
;
146 service
->name
= NULL
;
148 service
->unique_name
= NULL
;
149 service
->quit
= NULL
;
157 lash_debug("Nothing to destroy");