1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-internals.c random utility stuff (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-internals.h"
24 #include "dbus-protocol.h"
25 #include "dbus-test.h"
29 #include <sys/types.h>
36 * @defgroup DBusInternals D-BUS internal implementation details
37 * @brief Documentation useful when developing or debugging D-BUS itself.
42 * @defgroup DBusInternalsUtils Utilities and portability
43 * @ingroup DBusInternals
44 * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
51 * Aborts with an error message if the condition is false.
53 * @param condition condition which must be true.
57 * @def _dbus_assert_not_reached
59 * Aborts with an error message if called.
60 * The given explanation will be printed.
62 * @param explanation explanation of what happened if the code was reached.
66 * @def _DBUS_N_ELEMENTS
68 * Computes the number of elements in a fixed-size array using
71 * @param array the array to count elements in.
75 * @def _DBUS_POINTER_TO_INT
77 * Safely casts a void* to an integer; should only be used on void*
78 * that actually contain integers, for example one created with
79 * _DBUS_INT_TO_POINTER. Only guaranteed to preserve 32 bits.
80 * (i.e. it's used to store 32-bit ints in pointers, but
81 * can't be used to store 64-bit pointers in ints.)
83 * @param pointer pointer to extract an integer from.
86 * @def _DBUS_INT_TO_POINTER
88 * Safely stuffs an integer into a pointer, to be extracted later with
89 * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
91 * @param integer the integer to stuff into a pointer.
96 * Sets all bits in an object to zero.
98 * @param object the object to be zeroed.
103 * Minimum value of type "int"
108 * Maximum value of type "int"
111 * @def _DBUS_MAX_SUN_PATH_LENGTH
113 * Maximum length of the path to a UNIX domain socket,
114 * sockaddr_un::sun_path member. POSIX requires that all systems
115 * support at least 100 bytes here, including the nul termination.
116 * We use 99 for the max value to allow for the nul.
118 * We could probably also do sizeof (addr.sun_path)
119 * but this way we are the same on all platforms
120 * which is probably a good idea.
124 * @typedef DBusForeachFunction
126 * Used to iterate over each item in a collection, such as
131 * Prints a warning message to stderr.
133 * @param format printf-style format string.
136 _dbus_warn (const char *format,
139 /* FIXME not portable enough? */
142 va_start (args, format);
143 vfprintf (stderr, format, args);
148 * Prints a warning message to stderr
149 * if the user has enabled verbose mode.
150 * This is the real function implementation,
151 * use _dbus_verbose() macro in code.
153 * @param format printf-style format string.
156 _dbus_verbose_real (const char *format,
160 static dbus_bool_t verbose = TRUE;
161 static dbus_bool_t initted = FALSE;
163 /* things are written a bit oddly here so that
164 * in the non-verbose case we just have the one
165 * conditional and return immediately.
172 verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
178 va_start (args, format);
179 vfprintf (stderr, format, args);
184 * A wrapper around strerror() because some platforms
185 * may be lame and not have strerror().
187 * @param error_number errno.
188 * @returns error description.
191 _dbus_strerror (int error_number)
193 return strerror (error_number);
197 * Converts a UNIX errno into a DBusResultCode.
199 * @todo should cover more errnos, specifically those
202 * @param error_number the errno.
203 * @returns the result code.
206 _dbus_result_from_errno (int error_number)
208 switch (error_number)
211 return DBUS_RESULT_SUCCESS;
213 #ifdef EPROTONOSUPPORT
214 case EPROTONOSUPPORT:
215 return DBUS_RESULT_NOT_SUPPORTED;
219 return DBUS_RESULT_NOT_SUPPORTED;
223 return DBUS_RESULT_LIMITS_EXCEEDED; /* kernel out of memory */
227 return DBUS_RESULT_LIMITS_EXCEEDED;
231 return DBUS_RESULT_ACCESS_DENIED;
235 return DBUS_RESULT_ACCESS_DENIED;
239 return DBUS_RESULT_NO_MEMORY;
243 return DBUS_RESULT_NO_MEMORY;
247 return DBUS_RESULT_FAILED;
251 return DBUS_RESULT_FAILED;
255 return DBUS_RESULT_FAILED;
259 return DBUS_RESULT_FAILED;
263 return DBUS_RESULT_FAILED;
267 return DBUS_RESULT_NO_SERVER;
271 return DBUS_RESULT_TIMEOUT;
275 return DBUS_RESULT_NO_NETWORK;
279 return DBUS_RESULT_ADDRESS_IN_USE;
283 return DBUS_RESULT_FILE_NOT_FOUND;
287 return DBUS_RESULT_FILE_NOT_FOUND;
291 return DBUS_RESULT_FAILED;
295 * Duplicates a string. Result must be freed with
296 * dbus_free(). Returns #NULL if memory allocation fails.
297 * If the string to be duplicated is #NULL, returns #NULL.
299 * @param str string to duplicate.
300 * @returns newly-allocated copy.
303 _dbus_strdup (const char *str)
313 copy = dbus_malloc (len + 1);
317 memcpy (copy, str, len + 1);
323 * Sets a file descriptor to be nonblocking.
325 * @param fd the file descriptor.
326 * @param result address of result code.
327 * @returns #TRUE on success.
330 _dbus_set_fd_nonblocking (int fd,
331 DBusResultCode *result)
335 val = fcntl (fd, F_GETFL, 0);
338 dbus_set_result (result, _dbus_result_from_errno (errno));
339 _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
340 _dbus_strerror (errno));
344 if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
346 dbus_set_result (result, _dbus_result_from_errno (errno));
347 _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
348 fd, _dbus_strerror (errno));
357 * Returns a string describing the given type.
359 * @param type the type to describe
360 * @returns a constant string describing the type
363 _dbus_type_to_string (int type)
367 case DBUS_TYPE_INVALID:
369 case DBUS_TYPE_INT32:
371 case DBUS_TYPE_UINT32:
373 case DBUS_TYPE_DOUBLE:
375 case DBUS_TYPE_STRING:
377 case DBUS_TYPE_BYTE_ARRAY: