Fix error checks in graph_client module
[ladish.git] / cdbus / method.c
blob1b36d52f561a3e064947eada18d527f69f5dd3bf
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008,2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains D-Bus methods helpers
10 **************************************************************************
12 * Licensed under the Academic Free License version 2.1
14 * LADI Session Handler is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * LADI Session Handler is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
26 * or write to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "../common.h"
31 #include "helpers.h"
32 #include "method.h"
34 void cdbus_error(struct cdbus_method_call * call_ptr, const char * err_name, const char * format, ...)
36 va_list ap;
37 char message[1024];
38 const char *interface_name;
40 va_start(ap, format);
42 vsnprintf(message, sizeof(message), format, ap);
43 message[sizeof(message) - 1] = '\0';
45 va_end(ap);
47 if (call_ptr != NULL)
49 interface_name = (call_ptr->iface && call_ptr->iface->name && call_ptr->iface->name[0]) ? call_ptr->iface->name : "<unknown>";
51 log_error("In method %s.%s: %s", interface_name, call_ptr->method_name, message);
53 call_ptr->reply = dbus_message_new_error(call_ptr->message, err_name, message);
55 else
57 log_error("%s", message);
62 * Construct a void method return.
64 * The operation can only fail due to lack of memory, in which case
65 * there's no sense in trying to construct an error return. Instead,
66 * call_ptr->reply will be set to NULL and handled in send_method_return().
68 void cdbus_method_return_new_void(struct cdbus_method_call * call_ptr)
70 if (!(call_ptr->reply = dbus_message_new_method_return(call_ptr->message))) {
71 log_error("Ran out of memory trying to construct method return");
76 * Construct a method return which holds a single argument or, if
77 * the type parameter is DBUS_TYPE_INVALID, no arguments at all
78 * (a void message).
80 * The operation can only fail due to lack of memory, in which case
81 * there's no sense in trying to construct an error return. Instead,
82 * call_ptr->reply will be set to NULL and handled in send_method_return().
84 void cdbus_method_return_new_single(struct cdbus_method_call * call_ptr, int type, const void * arg)
86 if (!call_ptr || !arg) {
87 log_error("Invalid arguments");
88 return;
91 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
93 if (!call_ptr->reply)
94 goto fail_no_mem;
96 /* Prevent crash on NULL input string. */
97 if (type == DBUS_TYPE_STRING && !(*((const char **) arg)))
98 *((const char **) arg) = "";
100 DBusMessageIter iter;
102 dbus_message_iter_init_append(call_ptr->reply, &iter);
104 if (dbus_message_iter_append_basic(&iter, type, arg))
105 return;
107 dbus_message_unref(call_ptr->reply);
108 call_ptr->reply = NULL;
110 fail_no_mem:
111 log_error("Ran out of memory trying to construct method return");
114 void cdbus_method_return_new_valist(struct cdbus_method_call * call_ptr, int type, ...)
116 if (!call_ptr) {
117 log_error("Call pointer is NULL");
118 return;
121 if (type == DBUS_TYPE_INVALID) {
122 log_error("No argument(s) supplied");
123 return;
126 va_list argp;
128 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
129 if (!call_ptr->reply)
130 goto fail_no_mem;
132 va_start(argp, type);
134 if (dbus_message_append_args_valist(call_ptr->reply, type, argp)) {
135 va_end(argp);
136 return;
139 va_end(argp);
141 dbus_message_unref(call_ptr->reply);
142 call_ptr->reply = NULL;
144 fail_no_mem:
145 log_error("Ran out of memory trying to construct method return");
149 * Send a method return.
151 * If call_ptr->reply is NULL, i.e. a previous attempt to construct
152 * a return has failed, attempt to send a void return.
154 void cdbus_method_return_send(struct cdbus_method_call * call_ptr)
156 if (call_ptr->reply) {
157 retry_send:
158 if (!dbus_connection_send(call_ptr->connection, call_ptr->reply, NULL))
159 log_error("Ran out of memory trying to queue "
160 "method return");
161 else
162 dbus_connection_flush(call_ptr->connection);
164 dbus_message_unref(call_ptr->reply);
165 call_ptr->reply = NULL;
166 } else {
167 log_debug("Message was NULL, trying to construct a void return");
169 if ((call_ptr->reply = dbus_message_new_method_return(call_ptr->message))) {
170 log_debug("Constructed a void return, trying to queue it");
171 goto retry_send;
172 } else {
173 log_error("Failed to construct method return!");
178 bool cdbus_method_return_verify(DBusMessage * msg, const char ** str)
180 if (!msg || dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR)
181 return true;
183 const char *ptr;
185 if (!dbus_message_get_args(msg, &cdbus_g_dbus_error,
186 DBUS_TYPE_STRING, &ptr,
187 DBUS_TYPE_INVALID)) {
188 log_error("Cannot read description from D-Bus error message: %s ",
189 cdbus_g_dbus_error.message);
190 dbus_error_free(&cdbus_g_dbus_error);
191 ptr = NULL;
194 if (str)
195 *str = ptr;
197 return false;