1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (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
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
31 #include <sys/types.h>
34 #include <sys/socket.h>
42 * @addtogroup DBusInternalsUtils
46 * Aborts the program with SIGABRT (dumping core).
52 _exit (1); /* in case someone manages to ignore SIGABRT */
56 * Wrapper for getenv().
58 * @param varname name of environment variable
59 * @returns value of environment variable or #NULL if unset
62 _dbus_getenv (const char *varname)
64 return getenv (varname);
68 * Thin wrapper around the read() system call that appends
69 * the data it reads to the DBusString buffer. It appends
70 * up to the given count, and returns the same value
71 * and same errno as read(). The only exception is that
72 * _dbus_read() handles EINTR for you.
74 * @param fd the file descriptor to read from
75 * @param buffer the buffer to append data to
76 * @param count the amount of data to read
77 * @returns the number of bytes read or -1
88 _dbus_assert (count >= 0);
90 start = _dbus_string_get_length (buffer);
92 if (!_dbus_string_lengthen (buffer, count))
98 _dbus_string_get_data_len (buffer, &data, start, count);
102 bytes_read = read (fd, data, count);
110 /* put length back (note that this doesn't actually realloc anything) */
111 _dbus_string_set_length (buffer, start);
117 /* put length back (doesn't actually realloc) */
118 _dbus_string_set_length (buffer, start + bytes_read);
124 * Thin wrapper around the write() system call that writes a part of a
125 * DBusString and handles EINTR for you.
127 * @param fd the file descriptor to write
128 * @param buffer the buffer to write data from
129 * @param start the first byte in the buffer to write
130 * @param len the number of bytes to try to write
131 * @returns the number of bytes written or -1 on error
135 const DBusString *buffer,
142 _dbus_string_get_const_data_len (buffer, &data, start, len);
146 bytes_written = write (fd, data, len);
151 return bytes_written;
155 * Like _dbus_write() but will use writev() if possible
156 * to write both buffers in sequence. The return value
157 * is the number of bytes written in the first buffer,
158 * plus the number written in the second. If the first
159 * buffer is written successfully and an error occurs
160 * writing the second, the number of bytes in the first
161 * is returned (i.e. the error is ignored), on systems that
162 * don't have writev. Handles EINTR for you.
163 * The second buffer may be #NULL.
165 * @param fd the file descriptor
166 * @param buffer1 first buffer
167 * @param start1 first byte to write in first buffer
168 * @param len1 number of bytes to write from first buffer
169 * @param buffer2 second buffer, or #NULL
170 * @param start2 first byte to write in second buffer
171 * @param len2 number of bytes to write in second buffer
172 * @returns total bytes written from both buffers, or -1 on error
175 _dbus_write_two (int fd,
176 const DBusString *buffer1,
179 const DBusString *buffer2,
183 _dbus_assert (buffer1 != NULL);
184 _dbus_assert (start1 >= 0);
185 _dbus_assert (start2 >= 0);
186 _dbus_assert (len1 >= 0);
187 _dbus_assert (len2 >= 0);
191 struct iovec vectors[2];
196 _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
199 _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
207 vectors[0].iov_base = (char*) data1;
208 vectors[0].iov_len = len1;
209 vectors[1].iov_base = (char*) data2;
210 vectors[1].iov_len = len2;
214 bytes_written = writev (fd,
221 return bytes_written;
223 #else /* HAVE_WRITEV */
227 ret1 = _dbus_write (fd, buffer1, start1, len1);
228 if (ret1 == len1 && buffer2 != NULL)
230 ret2 = _dbus_write (fd, buffer2, start2, len2);
232 ret2 = 0; /* we can't report an error as the first write was OK */
239 #endif /* !HAVE_WRITEV */
243 * Creates a socket and connects it to the UNIX domain socket at the
244 * given path. The connection fd is returned, and is set up as
247 * @param path the path to UNIX domain socket
248 * @param result return location for error code
249 * @returns connection file descriptor or -1 on error
252 _dbus_connect_unix_socket (const char *path,
253 DBusResultCode *result)
256 struct sockaddr_un addr;
258 fd = socket (AF_LOCAL, SOCK_STREAM, 0);
262 dbus_set_result (result,
263 _dbus_result_from_errno (errno));
265 _dbus_verbose ("Failed to create socket: %s\n",
266 _dbus_strerror (errno));
272 addr.sun_family = AF_LOCAL;
273 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
274 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
276 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
278 dbus_set_result (result,
279 _dbus_result_from_errno (errno));
281 _dbus_verbose ("Failed to connect to socket %s: %s\n",
282 path, _dbus_strerror (errno));
290 if (!_dbus_set_fd_nonblocking (fd, result))
302 * Creates a socket and binds it to the given path,
303 * then listens on the socket. The socket is
304 * set to be nonblocking.
306 * @param path the socket name
307 * @param result return location for errors
308 * @returns the listening file descriptor or -1 on error
311 _dbus_listen_unix_socket (const char *path,
312 DBusResultCode *result)
315 struct sockaddr_un addr;
317 listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
321 dbus_set_result (result, _dbus_result_from_errno (errno));
322 _dbus_verbose ("Failed to create socket \"%s\": %s\n",
323 path, _dbus_strerror (errno));
328 addr.sun_family = AF_LOCAL;
329 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
330 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
332 if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
334 dbus_set_result (result, _dbus_result_from_errno (errno));
335 _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
336 path, _dbus_strerror (errno));
341 if (listen (listen_fd, 30 /* backlog */) < 0)
343 dbus_set_result (result, _dbus_result_from_errno (errno));
344 _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
345 path, _dbus_strerror (errno));
350 if (!_dbus_set_fd_nonblocking (listen_fd, result))
360 * Accepts a connection on a listening UNIX socket.
361 * Specific to UNIX domain sockets because we might
362 * add extra args to this function later to get client
363 * credentials. Handles EINTR for you.
365 * @param listen_fd the listen file descriptor
366 * @returns the connection fd of the client, or -1 on error
369 _dbus_accept_unix_socket (int listen_fd)
374 client_fd = accept (listen_fd, NULL, NULL);
388 * @addtogroup DBusString
393 * Appends an integer to a DBusString.
395 * @param str the string
396 * @param value the integer value
397 * @returns #FALSE if not enough memory or other failure.
400 _dbus_string_append_int (DBusString *str,
403 /* this calculation is from comp.lang.c faq */
404 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
409 orig_len = _dbus_string_get_length (str);
411 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
414 _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
416 snprintf (buf, MAX_LONG_LEN, "%ld", value);
425 _dbus_string_shorten (str, MAX_LONG_LEN - i);
431 * Appends a double to a DBusString.
433 * @param str the string
434 * @param value the floating point value
435 * @returns #FALSE if not enough memory or other failure.
438 _dbus_string_append_double (DBusString *str,
441 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
446 orig_len = _dbus_string_get_length (str);
448 if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
451 _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
453 snprintf (buf, MAX_LONG_LEN, "%g", value);
462 _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
468 * Parses an integer contained in a DBusString. Either return parameter
469 * may be #NULL if you aren't interested in it. The integer is parsed
470 * and stored in value_return. Return parameters are not initialized
471 * if the function returns #FALSE.
473 * @param str the string
474 * @param start the byte index of the start of the integer
475 * @param value_return return location of the integer value or #NULL
476 * @param end_return return location of the end of the integer, or #NULL
477 * @returns #TRUE on success
480 _dbus_string_parse_int (const DBusString *str,
489 _dbus_string_get_const_data_len (str, &p, start,
490 _dbus_string_get_length (str) - start);
494 v = strtol (p, &end, 0);
495 if (end == NULL || end == p || errno != 0)
501 *end_return = (end - p);
507 * Parses a floating point number contained in a DBusString. Either
508 * return parameter may be #NULL if you aren't interested in it. The
509 * integer is parsed and stored in value_return. Return parameters are
510 * not initialized if the function returns #FALSE.
512 * @todo this function is currently locale-dependent. Should
513 * ask alexl to relicense g_ascii_strtod() code and put that in
514 * here instead, so it's locale-independent.
516 * @param str the string
517 * @param start the byte index of the start of the float
518 * @param value_return return location of the float value or #NULL
519 * @param end_return return location of the end of the float, or #NULL
520 * @returns #TRUE on success
523 _dbus_string_parse_double (const DBusString *str,
525 double *value_return,
532 _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
534 _dbus_string_get_const_data_len (str, &p, start,
535 _dbus_string_get_length (str) - start);
539 v = strtod (p, &end);
540 if (end == NULL || end == p || errno != 0)
546 *end_return = (end - p);
551 /** @} end of DBusString */