Fix more warnings
[lash.git] / dbus / service.c
blobaa251ccddef9ad28bb5614564b545f7a4b988a52
1 /*
2 * LASH
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.
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h> /* strerror() */
26 #include "common/safety.h"
27 #include "common/debug.h"
28 #include "dbus/service.h"
30 service_t *
31 service_new(const char *service_name,
32 bool *quit,
33 int num_paths,
34 ...)
36 if (!quit || num_paths < 1) {
37 lash_debug("Invalid arguments");
38 return NULL;
41 lash_debug("Creating D-Bus service");
43 service_t *service;
44 DBusError err;
45 int r;
46 va_list argp;
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);
58 goto fail_free_err;
61 if (!(service->unique_name = dbus_bus_get_unique_name(service->connection))) {
62 lash_error("Failed to read unique bus name");
63 goto fail_free_err;
66 if (service_name) {
67 r = dbus_bus_request_name(service->connection, service_name,
68 DBUS_NAME_FLAG_DO_NOT_QUEUE,
69 &err);
70 if (r == -1) {
71 lash_error("Failed to acquire bus name: %s", err.message);
72 goto fail_free_err;
73 } else if (r == DBUS_REQUEST_NAME_REPLY_EXISTS) {
74 lash_error("Requested bus name already exists");
75 goto fail_free_err;
78 service->name = lash_strdup(service_name);
79 } else {
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 *));
87 ++path_pptr) {
88 if (!object_path_register(service->connection,
89 *path_pptr)) {
90 lash_error("Failed to register object path");
91 va_end(argp);
92 goto fail;
95 va_end(argp);
97 /* Set the keepalive pointer */
98 service->quit = quit;
100 return service;
102 fail_free_err:
103 dbus_error_free(&err);
105 fail:
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 *)))
113 free(path_ptr);
114 va_end(argp);
116 return NULL;
119 void
120 service_destroy(service_t *service)
122 lash_debug("Destroying D-Bus service");
124 if (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);
137 *path_pptr = NULL;
139 free(service->object_paths);
140 service->object_paths = NULL;
143 /* other stuff */
144 if (service->name) {
145 free(service->name);
146 service->name = NULL;
148 service->unique_name = NULL;
149 service->quit = NULL;
151 /* finalize */
152 free(service);
153 service = NULL;
155 #ifdef LASH_DEBUG
156 else
157 lash_debug("Nothing to destroy");
158 #endif
161 /* EOF */