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"
28 #include <sys/types.h>
34 * @defgroup DBusInternals D-BUS internal implementation details
35 * @brief Documentation useful when developing or debugging D-BUS itself.
40 * @defgroup DBusInternalsUtils Utilities and portability
41 * @ingroup DBusInternals
42 * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
49 * Aborts with an error message if the condition is false.
51 * @param condition condition which must be true.
55 * @def _dbus_assert_not_reached
57 * Aborts with an error message if called.
58 * The given explanation will be printed.
60 * @param explanation explanation of what happened if the code was reached.
64 * @def _DBUS_N_ELEMENTS
66 * Computes the number of elements in a fixed-size array using
69 * @param array the array to count elements in.
73 * @def _DBUS_POINTER_TO_INT
75 * Safely casts a void* to an integer; should only be used on void*
76 * that actually contain integers, for example one created with
77 * _DBUS_INT_TO_POINTER. Only guaranteed to preserve 32 bits.
78 * (i.e. it's used to store 32-bit ints in pointers, but
79 * can't be used to store 64-bit pointers in ints.)
81 * @param pointer pointer to extract an integer from.
84 * @def _DBUS_INT_TO_POINTER
86 * Safely stuffs an integer into a pointer, to be extracted later with
87 * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
89 * @param integer the integer to stuff into a pointer.
94 * Sets all bits in an object to zero.
96 * @param object the object to be zeroed.
101 * Minimum value of type "int"
106 * Maximum value of type "int"
109 * @def _DBUS_MAX_SUN_PATH_LENGTH
111 * Maximum length of the path to a UNIX domain socket,
112 * sockaddr_un::sun_path member. POSIX requires that all systems
113 * support at least 100 bytes here, including the nul termination.
114 * We use 99 for the max value to allow for the nul.
116 * We could probably also do sizeof (addr.sun_path)
117 * but this way we are the same on all platforms
118 * which is probably a good idea.
122 * @typedef DBusForeachFunction
124 * Used to iterate over each item in a collection, such as
129 * Prints a warning message to stderr.
131 * @param format printf-style format string.
134 _dbus_warn (const char *format,
137 /* FIXME not portable enough? */
140 va_start (args, format);
141 vfprintf (stderr, format, args);
146 * Prints a warning message to stderr
147 * if the user has enabled verbose mode.
149 * @param format printf-style format string.
152 _dbus_verbose (const char *format,
156 static dbus_bool_t verbose = TRUE;
157 static dbus_bool_t initted = FALSE;
159 /* things are written a bit oddly here so that
160 * in the non-verbose case we just have the one
161 * conditional and return immediately.
168 verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
174 va_start (args, format);
175 vfprintf (stderr, format, args);
180 * A wrapper around strerror() because some platforms
181 * may be lame and not have strerror().
183 * @param error_number errno.
184 * @returns error description.
187 _dbus_strerror (int error_number)
189 return strerror (error_number);
193 * Converts a UNIX errno into a DBusResultCode.
195 * @param error_number the errno.
196 * @returns the result code.
199 _dbus_result_from_errno (int error_number)
201 switch (error_number)
204 return DBUS_RESULT_SUCCESS;
206 #ifdef EPROTONOSUPPORT
207 case EPROTONOSUPPORT:
208 return DBUS_RESULT_NOT_SUPPORTED;
212 return DBUS_RESULT_NOT_SUPPORTED;
216 return DBUS_RESULT_LIMITS_EXCEEDED; /* kernel out of memory */
220 return DBUS_RESULT_LIMITS_EXCEEDED;
224 return DBUS_RESULT_ACCESS_DENIED;
228 return DBUS_RESULT_ACCESS_DENIED;
232 return DBUS_RESULT_NO_MEMORY;
236 return DBUS_RESULT_NO_MEMORY;
240 return DBUS_RESULT_FAILED;
244 return DBUS_RESULT_FAILED;
248 return DBUS_RESULT_FAILED;
252 return DBUS_RESULT_FAILED;
256 return DBUS_RESULT_FAILED;
260 return DBUS_RESULT_NO_SERVER;
264 return DBUS_RESULT_TIMEOUT;
268 return DBUS_RESULT_NO_NETWORK;
272 return DBUS_RESULT_ADDRESS_IN_USE;
276 return DBUS_RESULT_FAILED;
280 * Duplicates a string. Result must be freed with
281 * dbus_free(). Returns #NULL if memory allocation fails.
282 * If the string to be duplicated is #NULL, returns #NULL.
284 * @param str string to duplicate.
285 * @returns newly-allocated copy.
288 _dbus_strdup (const char *str)
298 copy = dbus_malloc (len + 1);
302 memcpy (copy, str, len + 1);
308 * Sets a file descriptor to be nonblocking.
310 * @param fd the file descriptor.
311 * @param result address of result code.
312 * @returns #TRUE on success.
315 _dbus_set_fd_nonblocking (int fd,
316 DBusResultCode *result)
320 val = fcntl (fd, F_GETFL, 0);
323 dbus_set_result (result, _dbus_result_from_errno (errno));
324 _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
325 _dbus_strerror (errno));
329 if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
331 dbus_set_result (result, _dbus_result_from_errno (errno));
332 _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
333 fd, _dbus_strerror (errno));
342 * Returns a string describing the given type.
344 * @param type the type to describe
345 * @returns a constant string describing the type
348 _dbus_type_to_string (int type)
352 case DBUS_TYPE_INVALID:
354 case DBUS_TYPE_INT32:
356 case DBUS_TYPE_UINT32:
358 case DBUS_TYPE_DOUBLE:
360 case DBUS_TYPE_STRING:
362 case DBUS_TYPE_BYTE_ARRAY: