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"
27 #include <sys/types.h>
33 * @defgroup DBusInternals D-BUS internal implementation details
34 * @brief Documentation useful when developing or debugging D-BUS itself.
39 * @defgroup DBusInternalsUtils Utilities and portability
40 * @ingroup DBusInternals
41 * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
48 * Aborts with an error message if the condition is false.
50 * @param condition condition which must be true.
54 * @def _dbus_assert_not_reached
56 * Aborts with an error message if called.
57 * The given explanation will be printed.
59 * @param explanation explanation of what happened if the code was reached.
63 * @def _DBUS_N_ELEMENTS
65 * Computes the number of elements in a fixed-size array using
68 * @param array the array to count elements in.
72 * @def _DBUS_POINTER_TO_INT
74 * Safely casts a void* to an integer; should only be used on void*
75 * that actually contain integers, for example one created with
76 * _DBUS_INT_TO_POINTER. Only guaranteed to preserve 32 bits.
77 * (i.e. it's used to store 32-bit ints in pointers, but
78 * can't be used to store 64-bit pointers in ints.)
80 * @param pointer pointer to extract an integer from.
83 * @def _DBUS_INT_TO_POINTER
85 * Safely stuffs an integer into a pointer, to be extracted later with
86 * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
88 * @param integer the integer to stuff into a pointer.
93 * Sets all bits in an object to zero.
95 * @param object the object to be zeroed.
100 * Minimum value of type "int"
105 * Maximum value of type "int"
108 * @def _DBUS_MAX_SUN_PATH_LENGTH
110 * Maximum length of the path to a UNIX domain socket,
111 * sockaddr_un::sun_path member. POSIX requires that all systems
112 * support at least 100 bytes here, including the nul termination.
113 * We use 99 for the max value to allow for the nul.
115 * We could probably also do sizeof (addr.sun_path)
116 * but this way we are the same on all platforms
117 * which is probably a good idea.
121 * @typedef DBusForeachFunction
123 * Used to iterate over each item in a collection, such as
128 * Prints a warning message to stderr.
130 * @param format printf-style format string.
133 _dbus_warn (const char *format,
136 /* FIXME not portable enough? */
139 va_start (args, format);
140 vfprintf (stderr, format, args);
145 * Prints a warning message to stderr
146 * if the user has enabled verbose mode.
148 * @param format printf-style format string.
151 _dbus_verbose (const char *format,
155 static dbus_bool_t verbose = TRUE;
156 static dbus_bool_t initted = FALSE;
163 verbose = getenv ("DBUS_VERBOSE") != NULL;
167 va_start (args, format);
168 vfprintf (stderr, format, args);
173 * A wrapper around strerror() because some platforms
174 * may be lame and not have strerror().
176 * @param error_number errno.
177 * @returns error description.
180 _dbus_strerror (int error_number)
182 return strerror (error_number);
186 * Converts a UNIX errno into a DBusResultCode.
188 * @param error_number the errno.
189 * @returns the result code.
192 _dbus_result_from_errno (int error_number)
194 switch (error_number)
197 return DBUS_RESULT_SUCCESS;
199 #ifdef EPROTONOSUPPORT
200 case EPROTONOSUPPORT:
201 return DBUS_RESULT_NOT_SUPPORTED;
205 return DBUS_RESULT_NOT_SUPPORTED;
209 return DBUS_RESULT_LIMITS_EXCEEDED; /* kernel out of memory */
213 return DBUS_RESULT_LIMITS_EXCEEDED;
217 return DBUS_RESULT_ACCESS_DENIED;
221 return DBUS_RESULT_ACCESS_DENIED;
225 return DBUS_RESULT_NO_MEMORY;
229 return DBUS_RESULT_NO_MEMORY;
233 return DBUS_RESULT_FAILED;
237 return DBUS_RESULT_FAILED;
241 return DBUS_RESULT_FAILED;
245 return DBUS_RESULT_FAILED;
249 return DBUS_RESULT_FAILED;
253 return DBUS_RESULT_NO_SERVER;
257 return DBUS_RESULT_TIMEOUT;
261 return DBUS_RESULT_NO_NETWORK;
265 return DBUS_RESULT_ADDRESS_IN_USE;
269 return DBUS_RESULT_FAILED;
273 * Duplicates a string. Result must be freed with
274 * dbus_free(). Returns #NULL if memory allocation fails.
275 * If the string to be duplicated is #NULL, returns #NULL.
277 * @param str string to duplicate.
278 * @returns newly-allocated copy.
281 _dbus_strdup (const char *str)
291 copy = dbus_malloc (len + 1);
295 memcpy (copy, str, len + 1);
301 * Sets a file descriptor to be nonblocking.
303 * @param fd the file descriptor.
304 * @param result address of result code.
305 * @returns #TRUE on success.
308 _dbus_set_fd_nonblocking (int fd,
309 DBusResultCode *result)
313 val = fcntl (fd, F_GETFL, 0);
316 dbus_set_result (result, _dbus_result_from_errno (errno));
317 _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
318 _dbus_strerror (errno));
322 if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
324 dbus_set_result (result, _dbus_result_from_errno (errno));
325 _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
326 fd, _dbus_strerror (errno));