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);
122 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
130 * Thin wrapper around the write() system call that writes a part of a
131 * DBusString and handles EINTR for you.
133 * @param fd the file descriptor to write
134 * @param buffer the buffer to write data from
135 * @param start the first byte in the buffer to write
136 * @param len the number of bytes to try to write
137 * @returns the number of bytes written or -1 on error
141 const DBusString *buffer,
148 _dbus_string_get_const_data_len (buffer, &data, start, len);
152 bytes_written = write (fd, data, len);
158 if (bytes_written > 0)
159 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
162 return bytes_written;
166 * Like _dbus_write() but will use writev() if possible
167 * to write both buffers in sequence. The return value
168 * is the number of bytes written in the first buffer,
169 * plus the number written in the second. If the first
170 * buffer is written successfully and an error occurs
171 * writing the second, the number of bytes in the first
172 * is returned (i.e. the error is ignored), on systems that
173 * don't have writev. Handles EINTR for you.
174 * The second buffer may be #NULL.
176 * @param fd the file descriptor
177 * @param buffer1 first buffer
178 * @param start1 first byte to write in first buffer
179 * @param len1 number of bytes to write from first buffer
180 * @param buffer2 second buffer, or #NULL
181 * @param start2 first byte to write in second buffer
182 * @param len2 number of bytes to write in second buffer
183 * @returns total bytes written from both buffers, or -1 on error
186 _dbus_write_two (int fd,
187 const DBusString *buffer1,
190 const DBusString *buffer2,
194 _dbus_assert (buffer1 != NULL);
195 _dbus_assert (start1 >= 0);
196 _dbus_assert (start2 >= 0);
197 _dbus_assert (len1 >= 0);
198 _dbus_assert (len2 >= 0);
202 struct iovec vectors[2];
207 _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
210 _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
218 vectors[0].iov_base = (char*) data1;
219 vectors[0].iov_len = len1;
220 vectors[1].iov_base = (char*) data2;
221 vectors[1].iov_len = len2;
225 bytes_written = writev (fd,
232 return bytes_written;
234 #else /* HAVE_WRITEV */
238 ret1 = _dbus_write (fd, buffer1, start1, len1);
239 if (ret1 == len1 && buffer2 != NULL)
241 ret2 = _dbus_write (fd, buffer2, start2, len2);
243 ret2 = 0; /* we can't report an error as the first write was OK */
250 #endif /* !HAVE_WRITEV */
254 * Creates a socket and connects it to the UNIX domain socket at the
255 * given path. The connection fd is returned, and is set up as
258 * @param path the path to UNIX domain socket
259 * @param result return location for error code
260 * @returns connection file descriptor or -1 on error
263 _dbus_connect_unix_socket (const char *path,
264 DBusResultCode *result)
267 struct sockaddr_un addr;
269 fd = socket (AF_LOCAL, SOCK_STREAM, 0);
273 dbus_set_result (result,
274 _dbus_result_from_errno (errno));
276 _dbus_verbose ("Failed to create socket: %s\n",
277 _dbus_strerror (errno));
283 addr.sun_family = AF_LOCAL;
284 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
285 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
287 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
289 dbus_set_result (result,
290 _dbus_result_from_errno (errno));
292 _dbus_verbose ("Failed to connect to socket %s: %s\n",
293 path, _dbus_strerror (errno));
301 if (!_dbus_set_fd_nonblocking (fd, result))
313 * Creates a socket and binds it to the given path,
314 * then listens on the socket. The socket is
315 * set to be nonblocking.
317 * @param path the socket name
318 * @param result return location for errors
319 * @returns the listening file descriptor or -1 on error
322 _dbus_listen_unix_socket (const char *path,
323 DBusResultCode *result)
326 struct sockaddr_un addr;
328 listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
332 dbus_set_result (result, _dbus_result_from_errno (errno));
333 _dbus_verbose ("Failed to create socket \"%s\": %s\n",
334 path, _dbus_strerror (errno));
339 addr.sun_family = AF_LOCAL;
340 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
341 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
343 if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
345 dbus_set_result (result, _dbus_result_from_errno (errno));
346 _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
347 path, _dbus_strerror (errno));
352 if (listen (listen_fd, 30 /* backlog */) < 0)
354 dbus_set_result (result, _dbus_result_from_errno (errno));
355 _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
356 path, _dbus_strerror (errno));
361 if (!_dbus_set_fd_nonblocking (listen_fd, result))
371 * Accepts a connection on a listening UNIX socket.
372 * Specific to UNIX domain sockets because we might
373 * add extra args to this function later to get client
374 * credentials. Handles EINTR for you.
376 * @param listen_fd the listen file descriptor
377 * @returns the connection fd of the client, or -1 on error
380 _dbus_accept_unix_socket (int listen_fd)
385 client_fd = accept (listen_fd, NULL, NULL);
399 * @addtogroup DBusString
404 * Appends an integer to a DBusString.
406 * @param str the string
407 * @param value the integer value
408 * @returns #FALSE if not enough memory or other failure.
411 _dbus_string_append_int (DBusString *str,
414 /* this calculation is from comp.lang.c faq */
415 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
420 orig_len = _dbus_string_get_length (str);
422 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
425 _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
427 snprintf (buf, MAX_LONG_LEN, "%ld", value);
436 _dbus_string_shorten (str, MAX_LONG_LEN - i);
442 * Appends a double to a DBusString.
444 * @param str the string
445 * @param value the floating point value
446 * @returns #FALSE if not enough memory or other failure.
449 _dbus_string_append_double (DBusString *str,
452 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
457 orig_len = _dbus_string_get_length (str);
459 if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
462 _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
464 snprintf (buf, MAX_LONG_LEN, "%g", value);
473 _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
479 * Parses an integer contained in a DBusString. Either return parameter
480 * may be #NULL if you aren't interested in it. The integer is parsed
481 * and stored in value_return. Return parameters are not initialized
482 * if the function returns #FALSE.
484 * @param str the string
485 * @param start the byte index of the start of the integer
486 * @param value_return return location of the integer value or #NULL
487 * @param end_return return location of the end of the integer, or #NULL
488 * @returns #TRUE on success
491 _dbus_string_parse_int (const DBusString *str,
500 _dbus_string_get_const_data_len (str, &p, start,
501 _dbus_string_get_length (str) - start);
505 v = strtol (p, &end, 0);
506 if (end == NULL || end == p || errno != 0)
512 *end_return = (end - p);
518 * Parses a floating point number contained in a DBusString. Either
519 * return parameter may be #NULL if you aren't interested in it. The
520 * integer is parsed and stored in value_return. Return parameters are
521 * not initialized if the function returns #FALSE.
523 * @todo this function is currently locale-dependent. Should
524 * ask alexl to relicense g_ascii_strtod() code and put that in
525 * here instead, so it's locale-independent.
527 * @param str the string
528 * @param start the byte index of the start of the float
529 * @param value_return return location of the float value or #NULL
530 * @param end_return return location of the end of the float, or #NULL
531 * @returns #TRUE on success
534 _dbus_string_parse_double (const DBusString *str,
536 double *value_return,
543 _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
545 _dbus_string_get_const_data_len (str, &p, start,
546 _dbus_string_get_length (str) - start);
550 v = strtod (p, &end);
551 if (end == NULL || end == p || errno != 0)
557 *end_return = (end - p);
562 /** @} end of DBusString */