D-Bus
1.4.16
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-errors.c Error reporting 00003 * 00004 * Copyright (C) 2002, 2004 Red Hat Inc. 00005 * Copyright (C) 2003 CodeFactory AB 00006 * 00007 * Licensed under the Academic Free License version 2.1 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 * 00023 */ 00024 00025 #include <config.h> 00026 #include "dbus-errors.h" 00027 #include "dbus-internals.h" 00028 #include "dbus-string.h" 00029 #include "dbus-protocol.h" 00030 #include <stdarg.h> 00031 #include <string.h> 00032 00065 typedef struct 00066 { 00067 char *name; 00068 char *message; 00070 unsigned int const_message : 1; 00072 unsigned int dummy2 : 1; 00073 unsigned int dummy3 : 1; 00074 unsigned int dummy4 : 1; 00075 unsigned int dummy5 : 1; 00077 void *padding1; 00079 } DBusRealError; 00080 00081 _DBUS_STATIC_ASSERT (sizeof (DBusRealError) == sizeof (DBusError)); 00082 00091 static const char* 00092 message_from_error (const char *error) 00093 { 00094 if (strcmp (error, DBUS_ERROR_FAILED) == 0) 00095 return "Unknown error"; 00096 else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0) 00097 return "Not enough memory available"; 00098 else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0) 00099 return "Error reading or writing data"; 00100 else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0) 00101 return "Could not parse address"; 00102 else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0) 00103 return "Feature not supported"; 00104 else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0) 00105 return "Resource limits exceeded"; 00106 else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0) 00107 return "Permission denied"; 00108 else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0) 00109 return "Could not authenticate to server"; 00110 else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0) 00111 return "No server available at address"; 00112 else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0) 00113 return "Connection timed out"; 00114 else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0) 00115 return "Network unavailable"; 00116 else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0) 00117 return "Address already in use"; 00118 else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0) 00119 return "Disconnected."; 00120 else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0) 00121 return "Invalid arguments."; 00122 else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0) 00123 return "Did not get a reply message."; 00124 else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0) 00125 return "File doesn't exist."; 00126 else if (strcmp (error, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0) 00127 return "Object path already in use"; 00128 else 00129 return error; 00130 } 00131 /* End of internals */ 00133 00187 void 00188 dbus_error_init (DBusError *error) 00189 { 00190 DBusRealError *real; 00191 00192 _dbus_return_if_fail (error != NULL); 00193 00194 _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError)); 00195 00196 real = (DBusRealError *)error; 00197 00198 real->name = NULL; 00199 real->message = NULL; 00200 00201 real->const_message = TRUE; 00202 } 00203 00210 void 00211 dbus_error_free (DBusError *error) 00212 { 00213 DBusRealError *real; 00214 00215 _dbus_return_if_fail (error != NULL); 00216 00217 real = (DBusRealError *)error; 00218 00219 if (!real->const_message) 00220 { 00221 dbus_free (real->name); 00222 dbus_free (real->message); 00223 } 00224 00225 dbus_error_init (error); 00226 } 00227 00242 void 00243 dbus_set_error_const (DBusError *error, 00244 const char *name, 00245 const char *message) 00246 { 00247 DBusRealError *real; 00248 00249 _dbus_return_if_error_is_set (error); 00250 _dbus_return_if_fail (name != NULL); 00251 00252 if (error == NULL) 00253 return; 00254 00255 _dbus_assert (error->name == NULL); 00256 _dbus_assert (error->message == NULL); 00257 00258 if (message == NULL) 00259 message = message_from_error (name); 00260 00261 real = (DBusRealError *)error; 00262 00263 real->name = (char*) name; 00264 real->message = (char *)message; 00265 real->const_message = TRUE; 00266 } 00267 00278 void 00279 dbus_move_error (DBusError *src, 00280 DBusError *dest) 00281 { 00282 _dbus_return_if_error_is_set (dest); 00283 00284 if (dest) 00285 { 00286 dbus_error_free (dest); 00287 *dest = *src; 00288 dbus_error_init (src); 00289 } 00290 else 00291 dbus_error_free (src); 00292 } 00293 00301 dbus_bool_t 00302 dbus_error_has_name (const DBusError *error, 00303 const char *name) 00304 { 00305 _dbus_return_val_if_fail (error != NULL, FALSE); 00306 _dbus_return_val_if_fail (name != NULL, FALSE); 00307 00308 _dbus_assert ((error->name != NULL && error->message != NULL) || 00309 (error->name == NULL && error->message == NULL)); 00310 00311 if (error->name != NULL) 00312 { 00313 DBusString str1, str2; 00314 _dbus_string_init_const (&str1, error->name); 00315 _dbus_string_init_const (&str2, name); 00316 return _dbus_string_equal (&str1, &str2); 00317 } 00318 else 00319 return FALSE; 00320 } 00321 00328 dbus_bool_t 00329 dbus_error_is_set (const DBusError *error) 00330 { 00331 _dbus_return_val_if_fail (error != NULL, FALSE); 00332 _dbus_assert ((error->name != NULL && error->message != NULL) || 00333 (error->name == NULL && error->message == NULL)); 00334 return error->name != NULL; 00335 } 00336 00353 void 00354 dbus_set_error (DBusError *error, 00355 const char *name, 00356 const char *format, 00357 ...) 00358 { 00359 DBusRealError *real; 00360 DBusString str; 00361 va_list args; 00362 00363 if (error == NULL) 00364 return; 00365 00366 /* it's a bug to pile up errors */ 00367 _dbus_return_if_error_is_set (error); 00368 _dbus_return_if_fail (name != NULL); 00369 00370 _dbus_assert (error->name == NULL); 00371 _dbus_assert (error->message == NULL); 00372 00373 if (!_dbus_string_init (&str)) 00374 goto nomem; 00375 00376 if (format == NULL) 00377 { 00378 if (!_dbus_string_append (&str, 00379 message_from_error (name))) 00380 { 00381 _dbus_string_free (&str); 00382 va_end (args); 00383 goto nomem; 00384 } 00385 } 00386 else 00387 { 00388 va_start (args, format); 00389 if (!_dbus_string_append_printf_valist (&str, format, args)) 00390 { 00391 _dbus_string_free (&str); 00392 va_end (args); 00393 goto nomem; 00394 } 00395 va_end (args); 00396 } 00397 00398 real = (DBusRealError *)error; 00399 00400 if (!_dbus_string_steal_data (&str, &real->message)) 00401 { 00402 _dbus_string_free (&str); 00403 goto nomem; 00404 } 00405 _dbus_string_free (&str); 00406 00407 real->name = _dbus_strdup (name); 00408 if (real->name == NULL) 00409 { 00410 dbus_free (real->message); 00411 real->message = NULL; 00412 goto nomem; 00413 } 00414 real->const_message = FALSE; 00415 00416 return; 00417 00418 nomem: 00419 _DBUS_SET_OOM (error); 00420 } 00421 /* End public API */