1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains D-Bus service helpers
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <string.h> /* strerror() */
32 #include "../common/safety.h"
33 #include "../common/debug.h"
37 service_new(const char *service_name
,
42 if (!quit
|| num_paths
< 1) {
43 lash_debug("Invalid arguments");
47 lash_debug("Creating D-Bus service");
53 object_path_t
**path_pptr
, *path_ptr
;
55 service
= lash_calloc(1, sizeof(service_t
));
56 service
->object_paths
= lash_calloc(num_paths
+ 1,
57 sizeof(object_path_t
*));
59 dbus_error_init(&err
);
61 service
->connection
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
62 if (dbus_error_is_set(&err
)) {
63 lash_error("Failed to get bus: %s", err
.message
);
67 if (!(service
->unique_name
= dbus_bus_get_unique_name(service
->connection
))) {
68 lash_error("Failed to read unique bus name");
73 r
= dbus_bus_request_name(service
->connection
, service_name
,
74 DBUS_NAME_FLAG_DO_NOT_QUEUE
,
77 lash_error("Failed to acquire bus name: %s", err
.message
);
79 } else if (r
== DBUS_REQUEST_NAME_REPLY_EXISTS
) {
80 lash_error("Requested bus name already exists");
84 service
->name
= lash_strdup(service_name
);
86 service
->name
= lash_strdup("");
89 /* Populate the array and register object paths */
90 va_start(argp
, num_paths
);
91 for (path_pptr
= service
->object_paths
;
92 (*path_pptr
= va_arg(argp
, object_path_t
*));
94 if (!object_path_register(service
->connection
,
96 lash_error("Failed to register object path");
103 /* Set the keepalive pointer */
104 service
->quit
= quit
;
109 dbus_error_free(&err
);
112 lash_free(&service
->object_paths
);
113 service_destroy(service
);
115 /* Always free the object paths which were passed to us so that
116 way we can guarantee that the caller need not worry. */
117 va_start(argp
, num_paths
);
118 while ((path_ptr
= va_arg(argp
, object_path_t
*)))
126 service_destroy(service_t
*service
)
128 lash_debug("Destroying D-Bus service");
131 /* cut the bus connection */
132 if (service
->connection
)
134 /* reap the object path(s) */
135 if (service
->object_paths
)
137 object_path_t
**path_pptr
;
139 for (path_pptr
= service
->object_paths
; *path_pptr
!= NULL
; ++path_pptr
)
141 object_path_destroy(service
->connection
, *path_pptr
);
145 free(service
->object_paths
);
146 service
->object_paths
= NULL
;
149 dbus_connection_unref(service
->connection
);
150 service
->connection
= NULL
;
156 service
->name
= NULL
;
158 service
->unique_name
= NULL
;
159 service
->quit
= NULL
;
167 lash_debug("Nothing to destroy");