dbus-errors.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-errors.c Error reporting
*
* Copyright (C) 2002, 2004 Red Hat Inc.
* Copyright (C) 2003 CodeFactory AB
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-errors.h"
#include "dbus-internals.h"
#include "dbus-string.h"
#include "dbus-protocol.h"
#include <stdarg.h>
#include <string.h>
typedef struct
{
char *name;
char *message;
unsigned int const_message : 1;
unsigned int dummy2 : 1;
unsigned int dummy3 : 1;
unsigned int dummy4 : 1;
unsigned int dummy5 : 1;
void *padding1;
} DBusRealError;
_DBUS_STATIC_ASSERT (sizeof (DBusRealError) == sizeof (DBusError));
static const char*
message_from_error (const char *error)
{
if (strcmp (error, DBUS_ERROR_FAILED) == 0)
return "Unknown error";
else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
return "Not enough memory available";
else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
return "Error reading or writing data";
else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
return "Could not parse address";
else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
return "Feature not supported";
else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
return "Resource limits exceeded";
else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
return "Permission denied";
else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
return "Could not authenticate to server";
else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
return "No server available at address";
else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
return "Connection timed out";
else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
return "Network unavailable";
else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
return "Address already in use";
else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
return "Disconnected.";
else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
return "Invalid arguments.";
else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
return "Did not get a reply message.";
else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
return "File doesn't exist.";
else if (strcmp (error, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0)
return "Object path already in use";
else
return error;
}
/* End of internals */
void
dbus_error_init (DBusError *error)
{
DBusRealError *real;
_DBUS_STATIC_ASSERT (sizeof (DBusError) == sizeof (DBusRealError));
_dbus_return_if_fail (error != NULL);
real = (DBusRealError *)error;
real->name = NULL;
real->message = NULL;
real->const_message = TRUE;
}
void
dbus_error_free (DBusError *error)
{
DBusRealError *real;
_dbus_return_if_fail (error != NULL);
real = (DBusRealError *)error;
if (!real->const_message)
{
dbus_free (real->name);
dbus_free (real->message);
}
dbus_error_init (error);
}
void
dbus_set_error_const (DBusError *error,
const char *name,
const char *message)
{
DBusRealError *real;
_dbus_return_if_error_is_set (error);
_dbus_return_if_fail (name != NULL);
if (error == NULL)
return;
_dbus_assert (error->name == NULL);
_dbus_assert (error->message == NULL);
if (message == NULL)
message = message_from_error (name);
real = (DBusRealError *)error;
real->name = (char*) name;
real->message = (char *)message;
real->const_message = TRUE;
}
void
dbus_move_error (DBusError *src,
DBusError *dest)
{
_dbus_return_if_error_is_set (dest);
if (dest)
{
dbus_error_free (dest);
*dest = *src;
dbus_error_init (src);
}
else
dbus_error_free (src);
}
dbus_bool_t
dbus_error_has_name (const DBusError *error,
const char *name)
{
_dbus_return_val_if_fail (error != NULL, FALSE);
_dbus_return_val_if_fail (name != NULL, FALSE);
_dbus_assert ((error->name != NULL && error->message != NULL) ||
(error->name == NULL && error->message == NULL));
if (error->name != NULL)
{
DBusString str1, str2;
_dbus_string_init_const (&str1, error->name);
_dbus_string_init_const (&str2, name);
return _dbus_string_equal (&str1, &str2);
}
else
return FALSE;
}
dbus_bool_t
dbus_error_is_set (const DBusError *error)
{
_dbus_return_val_if_fail (error != NULL, FALSE);
_dbus_assert ((error->name != NULL && error->message != NULL) ||
(error->name == NULL && error->message == NULL));
return error->name != NULL;
}
void
dbus_set_error (DBusError *error,
const char *name,
const char *format,
...)
{
va_list args;
if (error == NULL)
return;
/* it's a bug to pile up errors */
_dbus_return_if_error_is_set (error);
_dbus_return_if_fail (name != NULL);
va_start (args, format);
_dbus_set_error_valist (error, name, format, args);
va_end (args);
}
void
_dbus_set_error_valist (DBusError *error,
const char *name,
const char *format,
va_list args)
{
DBusRealError *real;
DBusString str;
_dbus_assert (name != NULL);
if (error == NULL)
return;
_dbus_assert (error->name == NULL);
_dbus_assert (error->message == NULL);
if (!_dbus_string_init (&str))
goto nomem;
if (format == NULL)
{
if (!_dbus_string_append (&str,
message_from_error (name)))
{
_dbus_string_free (&str);
goto nomem;
}
}
else
{
if (!_dbus_string_append_printf_valist (&str, format, args))
{
_dbus_string_free (&str);
goto nomem;
}
}
real = (DBusRealError *)error;
if (!_dbus_string_steal_data (&str, &real->message))
{
_dbus_string_free (&str);
goto nomem;
}
_dbus_string_free (&str);
real->name = _dbus_strdup (name);
if (real->name == NULL)
{
dbus_free (real->message);
real->message = NULL;
goto nomem;
}
real->const_message = FALSE;
return;
nomem:
_DBUS_SET_OOM (error);
}
/* End public API */
dbus_move_error
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition dbus-errors.c:281
dbus_set_error_const
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:245
dbus_error_has_name
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition dbus-errors.c:304
dbus_error_init
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition dbus-errors.c:190
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_free
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition dbus-errors.c:213
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition dbus-errors.c:331
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition dbus-internals.c:621
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
DBUS_ERROR_TIMEOUT
#define DBUS_ERROR_TIMEOUT
Certain timeout errors, possibly ETIMEDOUT on a socket.
Definition dbus-protocol.h:389
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition dbus-protocol.h:375
DBUS_ERROR_ADDRESS_IN_USE
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
Definition dbus-protocol.h:393
DBUS_ERROR_BAD_ADDRESS
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
Definition dbus-protocol.h:373
DBUS_ERROR_IO_ERROR
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
Definition dbus-protocol.h:371
DBUS_ERROR_AUTH_FAILED
#define DBUS_ERROR_AUTH_FAILED
Authentication didn't work.
Definition dbus-protocol.h:381
DBUS_ERROR_OBJECT_PATH_IN_USE
#define DBUS_ERROR_OBJECT_PATH_IN_USE
There's already an object with the requested object path.
Definition dbus-protocol.h:458
DBUS_ERROR_ACCESS_DENIED
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don't allow doing what you're trying to do.
Definition dbus-protocol.h:379
DBUS_ERROR_NO_SERVER
#define DBUS_ERROR_NO_SERVER
Unable to connect to server (probably caused by ECONNREFUSED on a socket).
Definition dbus-protocol.h:383
DBUS_ERROR_LIMITS_EXCEEDED
#define DBUS_ERROR_LIMITS_EXCEEDED
Some limited resource is exhausted.
Definition dbus-protocol.h:377
DBUS_ERROR_NO_NETWORK
#define DBUS_ERROR_NO_NETWORK
No network access (probably ENETUNREACH on a socket).
Definition dbus-protocol.h:391
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:361
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
DBUS_ERROR_INVALID_ARGS
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
Definition dbus-protocol.h:397
DBUS_ERROR_NO_REPLY
#define DBUS_ERROR_NO_REPLY
No reply to a message expecting one, usually means a timeout occurred.
Definition dbus-protocol.h:369
DBUS_ERROR_DISCONNECTED
#define DBUS_ERROR_DISCONNECTED
The connection is disconnected and you're trying to use it.
Definition dbus-protocol.h:395
DBUS_ERROR_FILE_NOT_FOUND
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
Definition dbus-protocol.h:399
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition dbus-string.c:980
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition dbus-string.c:686
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_append_printf_valist
dbus_bool_t _dbus_string_append_printf_valist(DBusString *str, const char *format, va_list args)
Appends a printf-style formatted string to the DBusString.
Definition dbus-string.c:1105
_dbus_string_equal
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition dbus-string.c:2075
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusError::name
const char * name
public error name field
Definition dbus-errors.h:52
DBusError::message
const char * message
public error message field
Definition dbus-errors.h:53
DBusRealError
Internals of DBusError.
Definition dbus-errors.c:68
DBusRealError::dummy2
unsigned int dummy2
placeholder
Definition dbus-errors.c:74
DBusRealError::name
char * name
error name
Definition dbus-errors.c:69
DBusRealError::const_message
unsigned int const_message
Message is not owned by DBusError.
Definition dbus-errors.c:72
DBusRealError::dummy5
unsigned int dummy5
placeholder
Definition dbus-errors.c:77
DBusRealError::dummy4
unsigned int dummy4
placeholder
Definition dbus-errors.c:76
DBusRealError::dummy3
unsigned int dummy3
placeholder
Definition dbus-errors.c:75
DBusRealError::padding1
void * padding1
placeholder
Definition dbus-errors.c:79
DBusRealError::message
char * message
error message
Definition dbus-errors.c:70
DBusString
Definition dbus-string.h:47