1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps-unix.c Wrappers around UNIX system/libc features (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-sysdeps-unix.h"
28 #include "dbus-threads.h"
29 #include "dbus-protocol.h"
30 #include "dbus-transport.h"
31 #include "dbus-string.h"
32 #include "dbus-userdb.h"
33 #include "dbus-list.h"
34 #include "dbus-credentials.h"
35 #include <sys/types.h>
42 #include <sys/socket.h>
51 #include <netinet/in.h>
67 #ifdef HAVE_GETPEERUCRED
75 #ifndef HAVE_SOCKLEN_T
80 _dbus_open_socket (int *fd_p,
86 *fd_p = socket (domain, type, protocol);
89 _dbus_verbose ("socket fd %d opened\n", *fd_p);
95 _dbus_error_from_errno (errno),
96 "Failed to open socket: %s",
97 _dbus_strerror (errno));
103 _dbus_open_tcp_socket (int *fd,
106 return _dbus_open_socket(fd, AF_INET, SOCK_STREAM, 0, error);
110 * Opens a UNIX domain socket (as in the socket() call).
111 * Does not bind the socket.
112 * @param fd return location for socket descriptor
113 * @param error return location for an error
114 * @returns #FALSE if error is set
117 _dbus_open_unix_socket (int *fd,
120 return _dbus_open_socket(fd, PF_UNIX, SOCK_STREAM, 0, error);
124 * Closes a socket. Should not be used on non-socket
125 * file descriptors or handles.
127 * @param fd the socket
128 * @param error return location for an error
129 * @returns #FALSE if error is set
132 _dbus_close_socket (int fd,
135 return _dbus_close (fd, error);
139 * Like _dbus_read(), but only works on sockets so is
140 * available on Windows.
142 * @param fd the socket
143 * @param buffer string to append data to
144 * @param count max amount of data to read
145 * @returns number of bytes appended to the string
148 _dbus_read_socket (int fd,
152 return _dbus_read (fd, buffer, count);
156 * Like _dbus_write(), but only supports sockets
157 * and is thus available on Windows.
159 * @param fd the file descriptor to write
160 * @param buffer the buffer to write data from
161 * @param start the first byte in the buffer to write
162 * @param len the number of bytes to try to write
163 * @returns the number of bytes written or -1 on error
166 _dbus_write_socket (int fd,
167 const DBusString *buffer,
171 return _dbus_write (fd, buffer, start, len);
175 * write data to a pipe.
177 * @param pipe the pipe instance
178 * @param buffer the buffer to write data from
179 * @param start the first byte in the buffer to write
180 * @param len the number of bytes to try to write
181 * @param error error return
182 * @returns the number of bytes written or -1 on error
185 _dbus_pipe_write (DBusPipe *pipe,
186 const DBusString *buffer,
193 written = _dbus_write (pipe->fd_or_handle, buffer, start, len);
196 dbus_set_error (error, DBUS_ERROR_FAILED,
197 "Writing to pipe: %s\n",
198 _dbus_strerror (errno));
206 * @param pipe the pipe instance
207 * @param error return location for an error
208 * @returns #FALSE if error is set
211 _dbus_pipe_close (DBusPipe *pipe,
214 if (_dbus_close (pipe->fd_or_handle, error) < 0)
220 _dbus_pipe_invalidate (pipe);
226 * Like _dbus_write_two() but only works on sockets and is thus
227 * available on Windows.
229 * @param fd the file descriptor
230 * @param buffer1 first buffer
231 * @param start1 first byte to write in first buffer
232 * @param len1 number of bytes to write from first buffer
233 * @param buffer2 second buffer, or #NULL
234 * @param start2 first byte to write in second buffer
235 * @param len2 number of bytes to write in second buffer
236 * @returns total bytes written from both buffers, or -1 on error
239 _dbus_write_socket_two (int fd,
240 const DBusString *buffer1,
243 const DBusString *buffer2,
247 return _dbus_write_two (fd, buffer1, start1, len1,
248 buffer2, start2, len2);
253 * Thin wrapper around the read() system call that appends
254 * the data it reads to the DBusString buffer. It appends
255 * up to the given count, and returns the same value
256 * and same errno as read(). The only exception is that
257 * _dbus_read() handles EINTR for you. Also, _dbus_read() can
258 * return ENOMEM, even though regular UNIX read doesn't.
260 * Unlike _dbus_read_socket(), _dbus_read() is not available
263 * @param fd the file descriptor to read from
264 * @param buffer the buffer to append data to
265 * @param count the amount of data to read
266 * @returns the number of bytes read or -1
277 _dbus_assert (count >= 0);
279 start = _dbus_string_get_length (buffer);
281 if (!_dbus_string_lengthen (buffer, count))
287 data = _dbus_string_get_data_len (buffer, start, count);
291 bytes_read = read (fd, data, count);
299 /* put length back (note that this doesn't actually realloc anything) */
300 _dbus_string_set_length (buffer, start);
306 /* put length back (doesn't actually realloc) */
307 _dbus_string_set_length (buffer, start + bytes_read);
311 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
319 * Thin wrapper around the write() system call that writes a part of a
320 * DBusString and handles EINTR for you.
322 * @param fd the file descriptor to write
323 * @param buffer the buffer to write data from
324 * @param start the first byte in the buffer to write
325 * @param len the number of bytes to try to write
326 * @returns the number of bytes written or -1 on error
330 const DBusString *buffer,
337 data = _dbus_string_get_const_data_len (buffer, start, len);
341 bytes_written = write (fd, data, len);
343 if (bytes_written < 0 && errno == EINTR)
347 if (bytes_written > 0)
348 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
351 return bytes_written;
355 * Like _dbus_write() but will use writev() if possible
356 * to write both buffers in sequence. The return value
357 * is the number of bytes written in the first buffer,
358 * plus the number written in the second. If the first
359 * buffer is written successfully and an error occurs
360 * writing the second, the number of bytes in the first
361 * is returned (i.e. the error is ignored), on systems that
362 * don't have writev. Handles EINTR for you.
363 * The second buffer may be #NULL.
365 * @param fd the file descriptor
366 * @param buffer1 first buffer
367 * @param start1 first byte to write in first buffer
368 * @param len1 number of bytes to write from first buffer
369 * @param buffer2 second buffer, or #NULL
370 * @param start2 first byte to write in second buffer
371 * @param len2 number of bytes to write in second buffer
372 * @returns total bytes written from both buffers, or -1 on error
375 _dbus_write_two (int fd,
376 const DBusString *buffer1,
379 const DBusString *buffer2,
383 _dbus_assert (buffer1 != NULL);
384 _dbus_assert (start1 >= 0);
385 _dbus_assert (start2 >= 0);
386 _dbus_assert (len1 >= 0);
387 _dbus_assert (len2 >= 0);
391 struct iovec vectors[2];
396 data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
399 data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
407 vectors[0].iov_base = (char*) data1;
408 vectors[0].iov_len = len1;
409 vectors[1].iov_base = (char*) data2;
410 vectors[1].iov_len = len2;
414 bytes_written = writev (fd,
418 if (bytes_written < 0 && errno == EINTR)
421 return bytes_written;
423 #else /* HAVE_WRITEV */
427 ret1 = _dbus_write (fd, buffer1, start1, len1);
428 if (ret1 == len1 && buffer2 != NULL)
430 ret2 = _dbus_write (fd, buffer2, start2, len2);
432 ret2 = 0; /* we can't report an error as the first write was OK */
439 #endif /* !HAVE_WRITEV */
442 #define _DBUS_MAX_SUN_PATH_LENGTH 99
445 * @def _DBUS_MAX_SUN_PATH_LENGTH
447 * Maximum length of the path to a UNIX domain socket,
448 * sockaddr_un::sun_path member. POSIX requires that all systems
449 * support at least 100 bytes here, including the nul termination.
450 * We use 99 for the max value to allow for the nul.
452 * We could probably also do sizeof (addr.sun_path)
453 * but this way we are the same on all platforms
454 * which is probably a good idea.
458 * Creates a socket and connects it to the UNIX domain socket at the
459 * given path. The connection fd is returned, and is set up as
462 * Uses abstract sockets instead of filesystem-linked sockets if
463 * requested (it's possible only on Linux; see "man 7 unix" on Linux).
464 * On non-Linux abstract socket usage always fails.
466 * @param path the path to UNIX domain socket
467 * @param abstract #TRUE to use abstract namespace
468 * @param error return location for error code
469 * @returns connection file descriptor or -1 on error
472 _dbus_connect_unix_socket (const char *path,
473 dbus_bool_t abstract,
478 struct sockaddr_un addr;
480 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
482 _dbus_verbose ("connecting to unix socket %s abstract=%d\n",
486 if (!_dbus_open_unix_socket (&fd, error))
488 _DBUS_ASSERT_ERROR_IS_SET(error);
491 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
494 addr.sun_family = AF_UNIX;
495 path_len = strlen (path);
499 #ifdef HAVE_ABSTRACT_SOCKETS
500 addr.sun_path[0] = '\0'; /* this is what says "use abstract" */
501 path_len++; /* Account for the extra nul byte added to the start of sun_path */
503 if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
505 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
506 "Abstract socket name too long\n");
507 _dbus_close (fd, NULL);
511 strncpy (&addr.sun_path[1], path, path_len);
512 /* _dbus_verbose_bytes (addr.sun_path, sizeof (addr.sun_path)); */
513 #else /* HAVE_ABSTRACT_SOCKETS */
514 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
515 "Operating system does not support abstract socket namespace\n");
516 _dbus_close (fd, NULL);
518 #endif /* ! HAVE_ABSTRACT_SOCKETS */
522 if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
524 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
525 "Socket name too long\n");
526 _dbus_close (fd, NULL);
530 strncpy (addr.sun_path, path, path_len);
533 if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
535 dbus_set_error (error,
536 _dbus_error_from_errno (errno),
537 "Failed to connect to socket %s: %s",
538 path, _dbus_strerror (errno));
540 _dbus_close (fd, NULL);
546 if (!_dbus_set_fd_nonblocking (fd, error))
548 _DBUS_ASSERT_ERROR_IS_SET (error);
550 _dbus_close (fd, NULL);
560 * Enables or disables the reception of credentials on the given socket during
561 * the next message transmission. This is only effective if the #LOCAL_CREDS
562 * system feature exists, in which case the other side of the connection does
563 * not have to do anything special to send the credentials.
565 * @param fd socket on which to change the #LOCAL_CREDS flag.
566 * @param on whether to enable or disable the #LOCAL_CREDS flag.
569 _dbus_set_local_creds (int fd, dbus_bool_t on)
571 dbus_bool_t retval = TRUE;
573 #if defined(HAVE_CMSGCRED)
574 /* NOOP just to make sure only one codepath is used
575 * and to prefer CMSGCRED
577 #elif defined(LOCAL_CREDS)
578 int val = on ? 1 : 0;
579 if (setsockopt (fd, 0, LOCAL_CREDS, &val, sizeof (val)) < 0)
581 _dbus_verbose ("Unable to set LOCAL_CREDS socket option on fd %d\n", fd);
585 _dbus_verbose ("LOCAL_CREDS %s for further messages on fd %d\n",
586 on ? "enabled" : "disabled", fd);
593 * Creates a socket and binds it to the given path,
594 * then listens on the socket. The socket is
595 * set to be nonblocking.
597 * Uses abstract sockets instead of filesystem-linked
598 * sockets if requested (it's possible only on Linux;
599 * see "man 7 unix" on Linux).
600 * On non-Linux abstract socket usage always fails.
602 * @param path the socket name
603 * @param abstract #TRUE to use abstract namespace
604 * @param error return location for errors
605 * @returns the listening file descriptor or -1 on error
608 _dbus_listen_unix_socket (const char *path,
609 dbus_bool_t abstract,
613 struct sockaddr_un addr;
616 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
618 _dbus_verbose ("listening on unix socket %s abstract=%d\n",
621 if (!_dbus_open_unix_socket (&listen_fd, error))
623 _DBUS_ASSERT_ERROR_IS_SET(error);
626 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
629 addr.sun_family = AF_UNIX;
630 path_len = strlen (path);
634 #ifdef HAVE_ABSTRACT_SOCKETS
635 /* remember that abstract names aren't nul-terminated so we rely
636 * on sun_path being filled in with zeroes above.
638 addr.sun_path[0] = '\0'; /* this is what says "use abstract" */
639 path_len++; /* Account for the extra nul byte added to the start of sun_path */
641 if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
643 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
644 "Abstract socket name too long\n");
645 _dbus_close (listen_fd, NULL);
649 strncpy (&addr.sun_path[1], path, path_len);
650 /* _dbus_verbose_bytes (addr.sun_path, sizeof (addr.sun_path)); */
651 #else /* HAVE_ABSTRACT_SOCKETS */
652 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
653 "Operating system does not support abstract socket namespace\n");
654 _dbus_close (listen_fd, NULL);
656 #endif /* ! HAVE_ABSTRACT_SOCKETS */
660 /* Discussed security implications of this with Nalin,
661 * and we couldn't think of where it would kick our ass, but
662 * it still seems a bit sucky. It also has non-security suckage;
663 * really we'd prefer to exit if the socket is already in use.
664 * But there doesn't seem to be a good way to do this.
666 * Just to be extra careful, I threw in the stat() - clearly
667 * the stat() can't *fix* any security issue, but it at least
668 * avoids inadvertent/accidental data loss.
673 if (stat (path, &sb) == 0 &&
674 S_ISSOCK (sb.st_mode))
678 if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
680 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
681 "Abstract socket name too long\n");
682 _dbus_close (listen_fd, NULL);
686 strncpy (addr.sun_path, path, path_len);
689 if (bind (listen_fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
691 dbus_set_error (error, _dbus_error_from_errno (errno),
692 "Failed to bind socket \"%s\": %s",
693 path, _dbus_strerror (errno));
694 _dbus_close (listen_fd, NULL);
698 if (listen (listen_fd, 30 /* backlog */) < 0)
700 dbus_set_error (error, _dbus_error_from_errno (errno),
701 "Failed to listen on socket \"%s\": %s",
702 path, _dbus_strerror (errno));
703 _dbus_close (listen_fd, NULL);
707 if (!_dbus_set_local_creds (listen_fd, TRUE))
709 dbus_set_error (error, _dbus_error_from_errno (errno),
710 "Failed to enable LOCAL_CREDS on socket \"%s\": %s",
711 path, _dbus_strerror (errno));
716 if (!_dbus_set_fd_nonblocking (listen_fd, error))
718 _DBUS_ASSERT_ERROR_IS_SET (error);
719 _dbus_close (listen_fd, NULL);
723 /* Try opening up the permissions, but if we can't, just go ahead
724 * and continue, maybe it will be good enough.
726 if (!abstract && chmod (path, 0777) < 0)
727 _dbus_warn ("Could not set mode 0777 on socket %s\n",
734 * Creates a socket and connects to a socket at the given host
735 * and port. The connection fd is returned, and is set up as
738 * @param host the host name to connect to
739 * @param port the prot to connect to
740 * @param error return location for error code
741 * @returns connection file descriptor or -1 on error
744 _dbus_connect_tcp_socket (const char *host,
749 struct sockaddr_in addr;
751 struct in_addr *haddr;
753 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
756 if (!_dbus_open_tcp_socket (&fd, error))
758 _DBUS_ASSERT_ERROR_IS_SET(error);
762 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
767 he = gethostbyname (host);
770 dbus_set_error (error,
771 _dbus_error_from_errno (errno),
772 "Failed to lookup hostname: %s",
774 _dbus_close (fd, NULL);
778 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
781 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
782 addr.sin_family = AF_INET;
783 addr.sin_port = htons (port);
785 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
787 dbus_set_error (error,
788 _dbus_error_from_errno (errno),
789 "Failed to connect to socket %s:%d %s",
790 host, port, _dbus_strerror (errno));
792 _dbus_close (fd, NULL);
798 if (!_dbus_set_fd_nonblocking (fd, error))
800 _dbus_close (fd, NULL);
810 * Creates a socket and binds it to the given path,
811 * then listens on the socket. The socket is
812 * set to be nonblocking.
813 * In case of port=0 a random free port is used and
814 * returned in the port parameter.
816 * @param host the host name to listen on
817 * @param port the prot to listen on, if zero a free port will be used
818 * @param error return location for errors
819 * @returns the listening file descriptor or -1 on error
822 _dbus_listen_tcp_socket (const char *host,
827 struct sockaddr_in addr;
829 struct in_addr *haddr;
830 socklen_t len = (socklen_t) sizeof (struct sockaddr);
832 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
835 if (!_dbus_open_tcp_socket (&listen_fd, error))
837 _DBUS_ASSERT_ERROR_IS_SET(error);
840 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
842 he = gethostbyname (host);
845 dbus_set_error (error,
846 _dbus_error_from_errno (errno),
847 "Failed to lookup hostname: %s",
849 _dbus_close (listen_fd, NULL);
853 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
856 memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
857 addr.sin_family = AF_INET;
858 addr.sin_port = htons (*port);
860 if (bind (listen_fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
862 dbus_set_error (error, _dbus_error_from_errno (errno),
863 "Failed to bind socket \"%s:%d\": %s",
864 host, *port, _dbus_strerror (errno));
865 _dbus_close (listen_fd, NULL);
869 if (listen (listen_fd, 30 /* backlog */) < 0)
871 dbus_set_error (error, _dbus_error_from_errno (errno),
872 "Failed to listen on socket \"%s:%d\": %s",
873 host, *port, _dbus_strerror (errno));
874 _dbus_close (listen_fd, NULL);
878 getsockname(listen_fd, (struct sockaddr*) &addr, &len);
879 *port = (dbus_uint32_t) ntohs(addr.sin_port);
881 if (!_dbus_set_fd_nonblocking (listen_fd, error))
883 _dbus_close (listen_fd, NULL);
891 write_credentials_byte (int server_fd,
895 char buf[1] = { '\0' };
896 #if defined(HAVE_CMSGCRED)
899 struct cmsgcred cred;
906 memset (&msg, 0, sizeof (msg));
910 msg.msg_control = &cmsg;
911 msg.msg_controllen = sizeof (cmsg);
912 memset (&cmsg, 0, sizeof (cmsg));
913 cmsg.hdr.cmsg_len = sizeof (cmsg);
914 cmsg.hdr.cmsg_level = SOL_SOCKET;
915 cmsg.hdr.cmsg_type = SCM_CREDS;
918 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
922 #if defined(HAVE_CMSGCRED)
923 bytes_written = sendmsg (server_fd, &msg, 0);
925 bytes_written = write (server_fd, buf, 1);
928 if (bytes_written < 0 && errno == EINTR)
931 if (bytes_written < 0)
933 dbus_set_error (error, _dbus_error_from_errno (errno),
934 "Failed to write credentials byte: %s",
935 _dbus_strerror (errno));
938 else if (bytes_written == 0)
940 dbus_set_error (error, DBUS_ERROR_IO_ERROR,
941 "wrote zero bytes writing credentials byte");
946 _dbus_assert (bytes_written == 1);
947 _dbus_verbose ("wrote credentials byte\n");
953 * Reads a single byte which must be nul (an error occurs otherwise),
954 * and reads unix credentials if available. Clears the credentials
955 * object, then adds pid/uid if available, so any previous credentials
956 * stored in the object are lost.
958 * Return value indicates whether a byte was read, not whether
959 * we got valid credentials. On some systems, such as Linux,
960 * reading/writing the byte isn't actually required, but we do it
961 * anyway just to avoid multiple codepaths.
963 * Fails if no byte is available, so you must select() first.
965 * The point of the byte is that on some systems we have to
966 * use sendmsg()/recvmsg() to transmit credentials.
968 * @param client_fd the client file descriptor
969 * @param credentials object to add client credentials to
970 * @param error location to store error code
971 * @returns #TRUE on success
974 _dbus_read_credentials_socket (int client_fd,
975 DBusCredentials *credentials,
984 uid_read = DBUS_UID_UNSET;
985 pid_read = DBUS_PID_UNSET;
990 struct cmsgcred cred;
993 #elif defined(LOCAL_CREDS)
996 struct sockcred cred;
1000 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1002 /* The POSIX spec certainly doesn't promise this, but
1003 * we need these assertions to fail as soon as we're wrong about
1004 * it so we can do the porting fixups
1006 _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t));
1007 _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t));
1008 _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t));
1010 _dbus_credentials_clear (credentials);
1012 /* Systems supporting LOCAL_CREDS are configured to have this feature
1013 * enabled (if it does not conflict with HAVE_CMSGCRED) prior accepting
1014 * the connection. Therefore, the received message must carry the
1015 * credentials information without doing anything special.
1018 iov.iov_base = &buf;
1021 memset (&msg, 0, sizeof (msg));
1025 #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
1026 memset (&cmsg, 0, sizeof (cmsg));
1027 msg.msg_control = &cmsg;
1028 msg.msg_controllen = sizeof (cmsg);
1032 if (recvmsg (client_fd, &msg, 0) < 0)
1037 dbus_set_error (error, _dbus_error_from_errno (errno),
1038 "Failed to read credentials byte: %s",
1039 _dbus_strerror (errno));
1045 dbus_set_error (error, DBUS_ERROR_FAILED,
1046 "Credentials byte was not nul");
1050 #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
1051 if (cmsg.hdr.cmsg_len < sizeof (cmsg) || cmsg.hdr.cmsg_type != SCM_CREDS)
1053 dbus_set_error (error, DBUS_ERROR_FAILED,
1054 "Message from recvmsg() was not SCM_CREDS");
1059 _dbus_verbose ("read credentials byte\n");
1064 int cr_len = sizeof (cr);
1066 if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
1067 cr_len == sizeof (cr))
1074 _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
1075 cr_len, (int) sizeof (cr), _dbus_strerror (errno));
1077 #elif defined(HAVE_CMSGCRED)
1078 pid_read = cmsg.cred.cmcred_pid;
1079 uid_read = cmsg.cred.cmcred_euid;
1080 #elif defined(LOCAL_CREDS)
1081 pid_read = DBUS_PID_UNSET;
1082 uid_read = cmsg.cred.sc_uid;
1083 /* Since we have already got the credentials from this socket, we can
1084 * disable its LOCAL_CREDS flag if it was ever set. */
1085 _dbus_set_local_creds (client_fd, FALSE);
1086 #elif defined(HAVE_GETPEEREID)
1089 if (getpeereid (client_fd, &euid, &egid) == 0)
1095 _dbus_verbose ("Failed to getpeereid() credentials: %s\n", _dbus_strerror (errno));
1097 #elif defined(HAVE_GETPEERUCRED)
1098 ucred_t * ucred = NULL;
1099 if (getpeerucred (client_fd, &ucred) == 0)
1101 pid_read = ucred_getpid (ucred);
1102 uid_read = ucred_geteuid (ucred);
1106 _dbus_verbose ("Failed to getpeerucred() credentials: %s\n", _dbus_strerror (errno));
1110 #else /* !SO_PEERCRED && !HAVE_CMSGCRED && !HAVE_GETPEEREID && !HAVE_GETPEERUCRED */
1111 _dbus_verbose ("Socket credentials not supported on this OS\n");
1115 _dbus_verbose ("Credentials:"
1116 " pid "DBUS_PID_FORMAT
1117 " uid "DBUS_UID_FORMAT
1122 if (pid_read != DBUS_PID_UNSET)
1124 if (!_dbus_credentials_add_unix_pid (credentials, pid_read))
1126 _DBUS_SET_OOM (error);
1131 if (uid_read != DBUS_UID_UNSET)
1133 if (!_dbus_credentials_add_unix_uid (credentials, uid_read))
1135 _DBUS_SET_OOM (error);
1144 * Sends a single nul byte with our UNIX credentials as ancillary
1145 * data. Returns #TRUE if the data was successfully written. On
1146 * systems that don't support sending credentials, just writes a byte,
1147 * doesn't send any credentials. On some systems, such as Linux,
1148 * reading/writing the byte isn't actually required, but we do it
1149 * anyway just to avoid multiple codepaths.
1151 * Fails if no byte can be written, so you must select() first.
1153 * The point of the byte is that on some systems we have to
1154 * use sendmsg()/recvmsg() to transmit credentials.
1156 * @param server_fd file descriptor for connection to server
1157 * @param error return location for error code
1158 * @returns #TRUE if the byte was sent
1161 _dbus_send_credentials_socket (int server_fd,
1164 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1166 if (write_credentials_byte (server_fd, error))
1173 * Accepts a connection on a listening socket.
1174 * Handles EINTR for you.
1176 * @param listen_fd the listen file descriptor
1177 * @returns the connection fd of the client, or -1 on error
1180 _dbus_accept (int listen_fd)
1183 struct sockaddr addr;
1186 addrlen = sizeof (addr);
1189 client_fd = accept (listen_fd, &addr, &addrlen);
1197 _dbus_verbose ("client fd %d accepted\n", client_fd);
1203 * Checks to make sure the given directory is
1204 * private to the user
1206 * @param dir the name of the directory
1207 * @param error error return
1208 * @returns #FALSE on failure
1211 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
1213 const char *directory;
1216 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1218 directory = _dbus_string_get_const_data (dir);
1220 if (stat (directory, &sb) < 0)
1222 dbus_set_error (error, _dbus_error_from_errno (errno),
1223 "%s", _dbus_strerror (errno));
1228 if ((S_IROTH & sb.st_mode) || (S_IWOTH & sb.st_mode) ||
1229 (S_IRGRP & sb.st_mode) || (S_IWGRP & sb.st_mode))
1231 dbus_set_error (error, DBUS_ERROR_FAILED,
1232 "%s directory is not private to the user", directory);
1240 fill_user_info_from_passwd (struct passwd *p,
1244 _dbus_assert (p->pw_name != NULL);
1245 _dbus_assert (p->pw_dir != NULL);
1247 info->uid = p->pw_uid;
1248 info->primary_gid = p->pw_gid;
1249 info->username = _dbus_strdup (p->pw_name);
1250 info->homedir = _dbus_strdup (p->pw_dir);
1252 if (info->username == NULL ||
1253 info->homedir == NULL)
1255 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1263 fill_user_info (DBusUserInfo *info,
1265 const DBusString *username,
1268 const char *username_c;
1270 /* exactly one of username/uid provided */
1271 _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
1272 _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
1274 info->uid = DBUS_UID_UNSET;
1275 info->primary_gid = DBUS_GID_UNSET;
1276 info->group_ids = NULL;
1277 info->n_group_ids = 0;
1278 info->username = NULL;
1279 info->homedir = NULL;
1281 if (username != NULL)
1282 username_c = _dbus_string_get_const_data (username);
1286 /* For now assuming that the getpwnam() and getpwuid() flavors
1287 * are always symmetrical, if not we have to add more configure
1291 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
1296 struct passwd p_str;
1299 #ifdef HAVE_POSIX_GETPWNAM_R
1300 if (uid != DBUS_UID_UNSET)
1301 result = getpwuid_r (uid, &p_str, buf, sizeof (buf),
1304 result = getpwnam_r (username_c, &p_str, buf, sizeof (buf),
1307 if (uid != DBUS_UID_UNSET)
1308 p = getpwuid_r (uid, &p_str, buf, sizeof (buf));
1310 p = getpwnam_r (username_c, &p_str, buf, sizeof (buf));
1312 #endif /* !HAVE_POSIX_GETPWNAM_R */
1313 if (result == 0 && p == &p_str)
1315 if (!fill_user_info_from_passwd (p, info, error))
1320 dbus_set_error (error, _dbus_error_from_errno (errno),
1321 "User \"%s\" unknown or no memory to allocate password entry\n",
1322 username_c ? username_c : "???");
1323 _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
1327 #else /* ! HAVE_GETPWNAM_R */
1329 /* I guess we're screwed on thread safety here */
1332 if (uid != DBUS_UID_UNSET)
1335 p = getpwnam (username_c);
1339 if (!fill_user_info_from_passwd (p, info, error))
1344 dbus_set_error (error, _dbus_error_from_errno (errno),
1345 "User \"%s\" unknown or no memory to allocate password entry\n",
1346 username_c ? username_c : "???");
1347 _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
1351 #endif /* ! HAVE_GETPWNAM_R */
1353 /* Fill this in so we can use it to get groups */
1354 username_c = info->username;
1356 #ifdef HAVE_GETGROUPLIST
1363 buf = dbus_new (gid_t, buf_count);
1366 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1370 if (getgrouplist (username_c,
1372 buf, &buf_count) < 0)
1374 gid_t *new = dbus_realloc (buf, buf_count * sizeof (buf[0]));
1377 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1385 if (getgrouplist (username_c, info->primary_gid, buf, &buf_count) < 0)
1387 dbus_set_error (error,
1388 _dbus_error_from_errno (errno),
1389 "Failed to get groups for username \"%s\" primary GID "
1390 DBUS_GID_FORMAT ": %s\n",
1391 username_c, info->primary_gid,
1392 _dbus_strerror (errno));
1398 info->group_ids = dbus_new (dbus_gid_t, buf_count);
1399 if (info->group_ids == NULL)
1401 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1406 for (i = 0; i < buf_count; ++i)
1407 info->group_ids[i] = buf[i];
1409 info->n_group_ids = buf_count;
1413 #else /* HAVE_GETGROUPLIST */
1415 /* We just get the one group ID */
1416 info->group_ids = dbus_new (dbus_gid_t, 1);
1417 if (info->group_ids == NULL)
1419 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1423 info->n_group_ids = 1;
1425 (info->group_ids)[0] = info->primary_gid;
1427 #endif /* HAVE_GETGROUPLIST */
1429 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1434 _DBUS_ASSERT_ERROR_IS_SET (error);
1439 * Gets user info for the given username.
1441 * @param info user info object to initialize
1442 * @param username the username
1443 * @param error error return
1444 * @returns #TRUE on success
1447 _dbus_user_info_fill (DBusUserInfo *info,
1448 const DBusString *username,
1451 return fill_user_info (info, DBUS_UID_UNSET,
1456 * Gets user info for the given user ID.
1458 * @param info user info object to initialize
1459 * @param uid the user ID
1460 * @param error error return
1461 * @returns #TRUE on success
1464 _dbus_user_info_fill_uid (DBusUserInfo *info,
1468 return fill_user_info (info, uid,
1473 * Adds the credentials of the current process to the
1474 * passed-in credentials object.
1476 * @param credentials credentials to add to
1477 * @returns #FALSE if no memory; does not properly roll back on failure, so only some credentials may have been added
1480 _dbus_credentials_add_from_current_process (DBusCredentials *credentials)
1482 /* The POSIX spec certainly doesn't promise this, but
1483 * we need these assertions to fail as soon as we're wrong about
1484 * it so we can do the porting fixups
1486 _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t));
1487 _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t));
1488 _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t));
1490 if (!_dbus_credentials_add_unix_pid(credentials, _dbus_getpid()))
1492 if (!_dbus_credentials_add_unix_uid(credentials, _dbus_getuid()))
1499 * Parses a desired identity provided from a client in the auth protocol.
1500 * On UNIX this means parsing a UID.
1502 * @todo this is broken because it treats OOM and parse error
1503 * the same way. Needs a #DBusError.
1505 * @param credentials the credentials to add what we parse to
1506 * @param desired_identity the string to parse
1507 * @returns #TRUE if we successfully parsed something
1510 _dbus_credentials_parse_and_add_desired (DBusCredentials *credentials,
1511 const DBusString *desired_identity)
1515 if (!_dbus_parse_uid (desired_identity, &uid))
1518 if (!_dbus_credentials_add_unix_uid (credentials, uid))
1525 * Append to the string the identity we would like to have when we authenticate,
1526 * on UNIX this is the current process UID and on Windows something else.
1527 * No escaping is required, that is done in dbus-auth.c.
1529 * @param str the string to append to
1530 * @returns #FALSE on no memory
1533 _dbus_append_desired_identity (DBusString *str)
1535 return _dbus_string_append_uint (str,
1540 * Gets our process ID
1541 * @returns process ID
1550 * @returns process UID
1559 * The only reason this is separate from _dbus_getpid() is to allow it
1560 * on Windows for logging but not for other purposes.
1562 * @returns process ID to put in log messages
1565 _dbus_pid_for_log (void)
1571 * Gets a UID from a UID string.
1573 * @param uid_str the UID in string form
1574 * @param uid UID to fill in
1575 * @returns #TRUE if successfully filled in UID
1578 _dbus_parse_uid (const DBusString *uid_str,
1584 if (_dbus_string_get_length (uid_str) == 0)
1586 _dbus_verbose ("UID string was zero length\n");
1592 if (!_dbus_string_parse_int (uid_str, 0, &val,
1595 _dbus_verbose ("could not parse string as a UID\n");
1599 if (end != _dbus_string_get_length (uid_str))
1601 _dbus_verbose ("string contained trailing stuff after UID\n");
1611 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
1613 #ifdef DBUS_USE_ATOMIC_INT_486
1614 /* Taken from CVS version 1.7 of glibc's sysdeps/i386/i486/atomicity.h */
1615 /* Since the asm stuff here is gcc-specific we go ahead and use "inline" also */
1616 static inline dbus_int32_t
1617 atomic_exchange_and_add (DBusAtomic *atomic,
1618 volatile dbus_int32_t val)
1620 register dbus_int32_t result;
1622 __asm__ __volatile__ ("lock; xaddl %0,%1"
1623 : "=r" (result), "=m" (atomic->value)
1624 : "0" (val), "m" (atomic->value));
1630 * Atomically increments an integer
1632 * @param atomic pointer to the integer to increment
1633 * @returns the value before incrementing
1635 * @todo implement arch-specific faster atomic ops
1638 _dbus_atomic_inc (DBusAtomic *atomic)
1640 #ifdef DBUS_USE_ATOMIC_INT_486
1641 return atomic_exchange_and_add (atomic, 1);
1644 _DBUS_LOCK (atomic);
1645 res = atomic->value;
1647 _DBUS_UNLOCK (atomic);
1653 * Atomically decrement an integer
1655 * @param atomic pointer to the integer to decrement
1656 * @returns the value before decrementing
1658 * @todo implement arch-specific faster atomic ops
1661 _dbus_atomic_dec (DBusAtomic *atomic)
1663 #ifdef DBUS_USE_ATOMIC_INT_486
1664 return atomic_exchange_and_add (atomic, -1);
1668 _DBUS_LOCK (atomic);
1669 res = atomic->value;
1671 _DBUS_UNLOCK (atomic);
1676 #ifdef DBUS_BUILD_TESTS
1678 * @returns process GID
1688 * Wrapper for poll().
1690 * @param fds the file descriptors to poll
1691 * @param n_fds number of descriptors in the array
1692 * @param timeout_milliseconds timeout or -1 for infinite
1693 * @returns numbers of fds with revents, or <0 on error
1696 _dbus_poll (DBusPollFD *fds,
1698 int timeout_milliseconds)
1701 /* This big thing is a constant expression and should get optimized
1702 * out of existence. So it's more robust than a configure check at
1705 if (_DBUS_POLLIN == POLLIN &&
1706 _DBUS_POLLPRI == POLLPRI &&
1707 _DBUS_POLLOUT == POLLOUT &&
1708 _DBUS_POLLERR == POLLERR &&
1709 _DBUS_POLLHUP == POLLHUP &&
1710 _DBUS_POLLNVAL == POLLNVAL &&
1711 sizeof (DBusPollFD) == sizeof (struct pollfd) &&
1712 _DBUS_STRUCT_OFFSET (DBusPollFD, fd) ==
1713 _DBUS_STRUCT_OFFSET (struct pollfd, fd) &&
1714 _DBUS_STRUCT_OFFSET (DBusPollFD, events) ==
1715 _DBUS_STRUCT_OFFSET (struct pollfd, events) &&
1716 _DBUS_STRUCT_OFFSET (DBusPollFD, revents) ==
1717 _DBUS_STRUCT_OFFSET (struct pollfd, revents))
1719 return poll ((struct pollfd*) fds,
1721 timeout_milliseconds);
1725 /* We have to convert the DBusPollFD to an array of
1726 * struct pollfd, poll, and convert back.
1728 _dbus_warn ("didn't implement poll() properly for this system yet\n");
1731 #else /* ! HAVE_POLL */
1733 fd_set read_set, write_set, err_set;
1739 FD_ZERO (&read_set);
1740 FD_ZERO (&write_set);
1743 for (i = 0; i < n_fds; i++)
1745 DBusPollFD *fdp = &fds[i];
1747 if (fdp->events & _DBUS_POLLIN)
1748 FD_SET (fdp->fd, &read_set);
1750 if (fdp->events & _DBUS_POLLOUT)
1751 FD_SET (fdp->fd, &write_set);
1753 FD_SET (fdp->fd, &err_set);
1755 max_fd = MAX (max_fd, fdp->fd);
1758 tv.tv_sec = timeout_milliseconds / 1000;
1759 tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
1761 ready = select (max_fd + 1, &read_set, &write_set, &err_set,
1762 timeout_milliseconds < 0 ? NULL : &tv);
1766 for (i = 0; i < n_fds; i++)
1768 DBusPollFD *fdp = &fds[i];
1772 if (FD_ISSET (fdp->fd, &read_set))
1773 fdp->revents |= _DBUS_POLLIN;
1775 if (FD_ISSET (fdp->fd, &write_set))
1776 fdp->revents |= _DBUS_POLLOUT;
1778 if (FD_ISSET (fdp->fd, &err_set))
1779 fdp->revents |= _DBUS_POLLERR;
1788 * Get current time, as in gettimeofday().
1790 * @param tv_sec return location for number of seconds
1791 * @param tv_usec return location for number of microseconds (thousandths)
1794 _dbus_get_current_time (long *tv_sec,
1799 gettimeofday (&t, NULL);
1804 *tv_usec = t.tv_usec;
1808 * Appends the contents of the given file to the string,
1809 * returning error code. At the moment, won't open a file
1810 * more than a megabyte in size.
1812 * @param str the string to append to
1813 * @param filename filename to load
1814 * @param error place to set an error
1815 * @returns #FALSE if error was set
1818 _dbus_file_get_contents (DBusString *str,
1819 const DBusString *filename,
1826 const char *filename_c;
1828 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1830 filename_c = _dbus_string_get_const_data (filename);
1832 /* O_BINARY useful on Cygwin */
1833 fd = open (filename_c, O_RDONLY | O_BINARY);
1836 dbus_set_error (error, _dbus_error_from_errno (errno),
1837 "Failed to open \"%s\": %s",
1839 _dbus_strerror (errno));
1843 _dbus_verbose ("file fd %d opened\n", fd);
1845 if (fstat (fd, &sb) < 0)
1847 dbus_set_error (error, _dbus_error_from_errno (errno),
1848 "Failed to stat \"%s\": %s",
1850 _dbus_strerror (errno));
1852 _dbus_verbose ("fstat() failed: %s",
1853 _dbus_strerror (errno));
1855 _dbus_close (fd, NULL);
1860 if (sb.st_size > _DBUS_ONE_MEGABYTE)
1862 dbus_set_error (error, DBUS_ERROR_FAILED,
1863 "File size %lu of \"%s\" is too large.",
1864 (unsigned long) sb.st_size, filename_c);
1865 _dbus_close (fd, NULL);
1870 orig_len = _dbus_string_get_length (str);
1871 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
1875 while (total < (int) sb.st_size)
1877 bytes_read = _dbus_read (fd, str,
1878 sb.st_size - total);
1879 if (bytes_read <= 0)
1881 dbus_set_error (error, _dbus_error_from_errno (errno),
1882 "Error reading \"%s\": %s",
1884 _dbus_strerror (errno));
1886 _dbus_verbose ("read() failed: %s",
1887 _dbus_strerror (errno));
1889 _dbus_close (fd, NULL);
1890 _dbus_string_set_length (str, orig_len);
1894 total += bytes_read;
1897 _dbus_close (fd, NULL);
1900 else if (sb.st_size != 0)
1902 _dbus_verbose ("Can only open regular files at the moment.\n");
1903 dbus_set_error (error, DBUS_ERROR_FAILED,
1904 "\"%s\" is not a regular file",
1906 _dbus_close (fd, NULL);
1911 _dbus_close (fd, NULL);
1917 * Writes a string out to a file. If the file exists,
1918 * it will be atomically overwritten by the new data.
1920 * @param str the string to write out
1921 * @param filename the file to save string to
1922 * @param error error to be filled in on failure
1923 * @returns #FALSE on failure
1926 _dbus_string_save_to_file (const DBusString *str,
1927 const DBusString *filename,
1932 const char *filename_c;
1933 DBusString tmp_filename;
1934 const char *tmp_filename_c;
1936 dbus_bool_t need_unlink;
1939 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1943 need_unlink = FALSE;
1945 if (!_dbus_string_init (&tmp_filename))
1947 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1951 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
1953 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1954 _dbus_string_free (&tmp_filename);
1958 if (!_dbus_string_append (&tmp_filename, "."))
1960 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1961 _dbus_string_free (&tmp_filename);
1965 #define N_TMP_FILENAME_RANDOM_BYTES 8
1966 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
1968 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1969 _dbus_string_free (&tmp_filename);
1973 filename_c = _dbus_string_get_const_data (filename);
1974 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
1976 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
1980 dbus_set_error (error, _dbus_error_from_errno (errno),
1981 "Could not create %s: %s", tmp_filename_c,
1982 _dbus_strerror (errno));
1986 _dbus_verbose ("tmp file fd %d opened\n", fd);
1991 bytes_to_write = _dbus_string_get_length (str);
1993 while (total < bytes_to_write)
1997 bytes_written = _dbus_write (fd, str, total,
1998 bytes_to_write - total);
2000 if (bytes_written <= 0)
2002 dbus_set_error (error, _dbus_error_from_errno (errno),
2003 "Could not write to %s: %s", tmp_filename_c,
2004 _dbus_strerror (errno));
2009 total += bytes_written;
2012 if (!_dbus_close (fd, NULL))
2014 dbus_set_error (error, _dbus_error_from_errno (errno),
2015 "Could not close file %s: %s",
2016 tmp_filename_c, _dbus_strerror (errno));
2023 if (rename (tmp_filename_c, filename_c) < 0)
2025 dbus_set_error (error, _dbus_error_from_errno (errno),
2026 "Could not rename %s to %s: %s",
2027 tmp_filename_c, filename_c,
2028 _dbus_strerror (errno));
2033 need_unlink = FALSE;
2038 /* close first, then unlink, to prevent ".nfs34234235" garbage
2043 _dbus_close (fd, NULL);
2045 if (need_unlink && unlink (tmp_filename_c) < 0)
2046 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
2047 tmp_filename_c, _dbus_strerror (errno));
2049 _dbus_string_free (&tmp_filename);
2052 _DBUS_ASSERT_ERROR_IS_SET (error);
2057 /** Makes the file readable by every user in the system.
2059 * @param filename the filename
2060 * @param error error location
2061 * @returns #TRUE if the file's permissions could be changed.
2064 _dbus_make_file_world_readable(const DBusString *filename,
2067 const char *filename_c;
2069 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2071 filename_c = _dbus_string_get_const_data (filename);
2072 if (chmod (filename_c, 0644) == -1)
2074 dbus_set_error (error,
2076 "Could not change permissions of file %s: %s\n",
2078 _dbus_strerror (errno));
2084 /** Creates the given file, failing if the file already exists.
2086 * @param filename the filename
2087 * @param error error location
2088 * @returns #TRUE if we created the file and it didn't exist
2091 _dbus_create_file_exclusively (const DBusString *filename,
2095 const char *filename_c;
2097 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2099 filename_c = _dbus_string_get_const_data (filename);
2101 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
2105 dbus_set_error (error,
2107 "Could not create file %s: %s\n",
2109 _dbus_strerror (errno));
2113 _dbus_verbose ("exclusive file fd %d opened\n", fd);
2115 if (!_dbus_close (fd, NULL))
2117 dbus_set_error (error,
2119 "Could not close file %s: %s\n",
2121 _dbus_strerror (errno));
2129 * Deletes the given file.
2131 * @param filename the filename
2132 * @param error error location
2134 * @returns #TRUE if unlink() succeeded
2137 _dbus_delete_file (const DBusString *filename,
2140 const char *filename_c;
2142 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2144 filename_c = _dbus_string_get_const_data (filename);
2146 if (unlink (filename_c) < 0)
2148 dbus_set_error (error, DBUS_ERROR_FAILED,
2149 "Failed to delete file %s: %s\n",
2150 filename_c, _dbus_strerror (errno));
2158 * Creates a directory; succeeds if the directory
2159 * is created or already existed.
2161 * @param filename directory filename
2162 * @param error initialized error object
2163 * @returns #TRUE on success
2166 _dbus_create_directory (const DBusString *filename,
2169 const char *filename_c;
2171 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2173 filename_c = _dbus_string_get_const_data (filename);
2175 if (mkdir (filename_c, 0700) < 0)
2177 if (errno == EEXIST)
2180 dbus_set_error (error, DBUS_ERROR_FAILED,
2181 "Failed to create directory %s: %s\n",
2182 filename_c, _dbus_strerror (errno));
2190 * Appends the given filename to the given directory.
2192 * @todo it might be cute to collapse multiple '/' such as "foo//"
2195 * @param dir the directory name
2196 * @param next_component the filename
2197 * @returns #TRUE on success
2200 _dbus_concat_dir_and_file (DBusString *dir,
2201 const DBusString *next_component)
2203 dbus_bool_t dir_ends_in_slash;
2204 dbus_bool_t file_starts_with_slash;
2206 if (_dbus_string_get_length (dir) == 0 ||
2207 _dbus_string_get_length (next_component) == 0)
2210 dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
2211 _dbus_string_get_length (dir) - 1);
2213 file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
2215 if (dir_ends_in_slash && file_starts_with_slash)
2217 _dbus_string_shorten (dir, 1);
2219 else if (!(dir_ends_in_slash || file_starts_with_slash))
2221 if (!_dbus_string_append_byte (dir, '/'))
2225 return _dbus_string_copy (next_component, 0, dir,
2226 _dbus_string_get_length (dir));
2229 /** nanoseconds in a second */
2230 #define NANOSECONDS_PER_SECOND 1000000000
2231 /** microseconds in a second */
2232 #define MICROSECONDS_PER_SECOND 1000000
2233 /** milliseconds in a second */
2234 #define MILLISECONDS_PER_SECOND 1000
2235 /** nanoseconds in a millisecond */
2236 #define NANOSECONDS_PER_MILLISECOND 1000000
2237 /** microseconds in a millisecond */
2238 #define MICROSECONDS_PER_MILLISECOND 1000
2241 * Sleeps the given number of milliseconds.
2242 * @param milliseconds number of milliseconds
2245 _dbus_sleep_milliseconds (int milliseconds)
2247 #ifdef HAVE_NANOSLEEP
2248 struct timespec req;
2249 struct timespec rem;
2251 req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
2252 req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
2256 while (nanosleep (&req, &rem) < 0 && errno == EINTR)
2258 #elif defined (HAVE_USLEEP)
2259 usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
2260 #else /* ! HAVE_USLEEP */
2261 sleep (MAX (milliseconds / 1000, 1));
2266 _dbus_generate_pseudorandom_bytes (DBusString *str,
2272 old_len = _dbus_string_get_length (str);
2274 if (!_dbus_string_lengthen (str, n_bytes))
2277 p = _dbus_string_get_data_len (str, old_len, n_bytes);
2279 _dbus_generate_pseudorandom_bytes_buffer (p, n_bytes);
2285 * Generates the given number of random bytes,
2286 * using the best mechanism we can come up with.
2288 * @param str the string
2289 * @param n_bytes the number of random bytes to append to string
2290 * @returns #TRUE on success, #FALSE if no memory
2293 _dbus_generate_random_bytes (DBusString *str,
2299 /* FALSE return means "no memory", if it could
2300 * mean something else then we'd need to return
2301 * a DBusError. So we always fall back to pseudorandom
2305 old_len = _dbus_string_get_length (str);
2308 /* note, urandom on linux will fall back to pseudorandom */
2309 fd = open ("/dev/urandom", O_RDONLY);
2311 return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2313 _dbus_verbose ("/dev/urandom fd %d opened\n", fd);
2315 if (_dbus_read (fd, str, n_bytes) != n_bytes)
2317 _dbus_close (fd, NULL);
2318 _dbus_string_set_length (str, old_len);
2319 return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2322 _dbus_verbose ("Read %d bytes from /dev/urandom\n",
2325 _dbus_close (fd, NULL);
2331 * Exit the process, returning the given value.
2333 * @param code the exit code
2336 _dbus_exit (int code)
2342 * A wrapper around strerror() because some platforms
2343 * may be lame and not have strerror().
2345 * @param error_number errno.
2346 * @returns error description.
2349 _dbus_strerror (int error_number)
2353 msg = strerror (error_number);
2361 * signal (SIGPIPE, SIG_IGN);
2364 _dbus_disable_sigpipe (void)
2366 signal (SIGPIPE, SIG_IGN);
2370 * Sets the file descriptor to be close
2371 * on exec. Should be called for all file
2372 * descriptors in D-Bus code.
2374 * @param fd the file descriptor
2377 _dbus_fd_set_close_on_exec (int fd)
2381 val = fcntl (fd, F_GETFD, 0);
2388 fcntl (fd, F_SETFD, val);
2392 * Closes a file descriptor.
2394 * @param fd the file descriptor
2395 * @param error error object
2396 * @returns #FALSE if error set
2399 _dbus_close (int fd,
2402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2410 dbus_set_error (error, _dbus_error_from_errno (errno),
2411 "Could not close fd %d", fd);
2419 * Sets a file descriptor to be nonblocking.
2421 * @param fd the file descriptor.
2422 * @param error address of error location.
2423 * @returns #TRUE on success.
2426 _dbus_set_fd_nonblocking (int fd,
2431 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2433 val = fcntl (fd, F_GETFL, 0);
2436 dbus_set_error (error, _dbus_error_from_errno (errno),
2437 "Failed to get flags from file descriptor %d: %s",
2438 fd, _dbus_strerror (errno));
2439 _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
2440 _dbus_strerror (errno));
2444 if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
2446 dbus_set_error (error, _dbus_error_from_errno (errno),
2447 "Failed to set nonblocking flag of file descriptor %d: %s",
2448 fd, _dbus_strerror (errno));
2449 _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
2450 fd, _dbus_strerror (errno));
2459 * On GNU libc systems, print a crude backtrace to stderr. On other
2460 * systems, print "no backtrace support" and block for possible gdb
2461 * attachment if an appropriate environment variable is set.
2464 _dbus_print_backtrace (void)
2466 #if defined (HAVE_BACKTRACE) && defined (DBUS_BUILT_R_DYNAMIC)
2472 bt_size = backtrace (bt, 500);
2474 syms = backtrace_symbols (bt, bt_size);
2479 /* don't use dbus_warn since it can _dbus_abort() */
2480 fprintf (stderr, " %s\n", syms[i]);
2486 #elif defined (HAVE_BACKTRACE) && ! defined (DBUS_BUILT_R_DYNAMIC)
2487 fprintf (stderr, " D-Bus not built with -rdynamic so unable to print a backtrace\n");
2489 fprintf (stderr, " D-Bus not compiled with backtrace support so unable to print a backtrace\n");
2494 * Creates a full-duplex pipe (as in socketpair()).
2495 * Sets both ends of the pipe nonblocking.
2497 * @todo libdbus only uses this for the debug-pipe server, so in
2498 * principle it could be in dbus-sysdeps-util.c, except that
2499 * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
2500 * debug-pipe server is used.
2502 * @param fd1 return location for one end
2503 * @param fd2 return location for the other end
2504 * @param blocking #TRUE if pipe should be blocking
2505 * @param error error return
2506 * @returns #FALSE on failure (if error is set)
2509 _dbus_full_duplex_pipe (int *fd1,
2511 dbus_bool_t blocking,
2514 #ifdef HAVE_SOCKETPAIR
2517 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2519 if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
2521 dbus_set_error (error, _dbus_error_from_errno (errno),
2522 "Could not create full-duplex pipe");
2527 (!_dbus_set_fd_nonblocking (fds[0], NULL) ||
2528 !_dbus_set_fd_nonblocking (fds[1], NULL)))
2530 dbus_set_error (error, _dbus_error_from_errno (errno),
2531 "Could not set full-duplex pipe nonblocking");
2533 _dbus_close (fds[0], NULL);
2534 _dbus_close (fds[1], NULL);
2542 _dbus_verbose ("full-duplex pipe %d <-> %d\n",
2547 _dbus_warn ("_dbus_full_duplex_pipe() not implemented on this OS\n");
2548 dbus_set_error (error, DBUS_ERROR_FAILED,
2549 "_dbus_full_duplex_pipe() not implemented on this OS");
2556 * Measure the length of the given format string and arguments,
2557 * not including the terminating nul.
2559 * @param format a printf-style format string
2560 * @param args arguments for the format string
2561 * @returns length of the given format string and args
2564 _dbus_printf_string_upper_bound (const char *format,
2568 return vsnprintf (&c, 1, format, args);
2572 * Gets the temporary files directory by inspecting the environment variables
2573 * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
2575 * @returns location of temp directory
2578 _dbus_get_tmpdir(void)
2580 static const char* tmpdir = NULL;
2584 /* TMPDIR is what glibc uses, then
2585 * glibc falls back to the P_tmpdir macro which
2586 * just expands to "/tmp"
2589 tmpdir = getenv("TMPDIR");
2591 /* These two env variables are probably
2592 * broken, but maybe some OS uses them?
2595 tmpdir = getenv("TMP");
2597 tmpdir = getenv("TEMP");
2599 /* And this is the sane fallback. */
2604 _dbus_assert(tmpdir != NULL);
2610 * Determines the address of the session bus by querying a
2611 * platform-specific method.
2613 * If successful, returns #TRUE and appends the address to @p
2614 * address. If a failure happens, returns #FALSE and
2615 * sets an error in @p error.
2617 * @param address a DBusString where the address can be stored
2618 * @param error a DBusError to store the error in case of failure
2619 * @returns #TRUE on success, #FALSE if an error happened
2622 _dbus_get_autolaunch_address (DBusString *address,
2625 static char *argv[6];
2626 int address_pipe[2] = { -1, -1 };
2627 int errors_pipe[2] = { -1, -1 };
2636 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2639 _dbus_string_init (&uuid);
2641 if (!_dbus_get_local_machine_uuid_encoded (&uuid))
2643 _DBUS_SET_OOM (error);
2648 argv[i] = "dbus-launch";
2650 argv[i] = "--autolaunch";
2652 argv[i] = _dbus_string_get_data (&uuid);
2654 argv[i] = "--binary-syntax";
2656 argv[i] = "--close-stderr";
2661 _dbus_assert (i == _DBUS_N_ELEMENTS (argv));
2663 orig_len = _dbus_string_get_length (address);
2667 if (pipe (address_pipe) < 0)
2669 dbus_set_error (error, _dbus_error_from_errno (errno),
2670 "Failed to create a pipe: %s",
2671 _dbus_strerror (errno));
2672 _dbus_verbose ("Failed to create a pipe to call dbus-launch: %s\n",
2673 _dbus_strerror (errno));
2676 if (pipe (errors_pipe) < 0)
2678 dbus_set_error (error, _dbus_error_from_errno (errno),
2679 "Failed to create a pipe: %s",
2680 _dbus_strerror (errno));
2681 _dbus_verbose ("Failed to create a pipe to call dbus-launch: %s\n",
2682 _dbus_strerror (errno));
2689 dbus_set_error (error, _dbus_error_from_errno (errno),
2690 "Failed to fork(): %s",
2691 _dbus_strerror (errno));
2692 _dbus_verbose ("Failed to fork() to call dbus-launch: %s\n",
2693 _dbus_strerror (errno));
2700 int fd = open ("/dev/null", O_RDWR);
2702 /* huh?! can't open /dev/null? */
2705 _dbus_verbose ("/dev/null fd %d opened\n", fd);
2708 close (address_pipe[READ_END]);
2709 close (errors_pipe[READ_END]);
2710 close (0); /* close stdin */
2711 close (1); /* close stdout */
2712 close (2); /* close stderr */
2714 if (dup2 (fd, 0) == -1)
2716 if (dup2 (address_pipe[WRITE_END], 1) == -1)
2718 if (dup2 (errors_pipe[WRITE_END], 2) == -1)
2722 close (address_pipe[WRITE_END]);
2723 close (errors_pipe[WRITE_END]);
2725 execv (DBUS_BINDIR "/dbus-launch", argv);
2727 /* failed, try searching PATH */
2728 execvp ("dbus-launch", argv);
2730 /* still nothing, we failed */
2734 /* parent process */
2735 close (address_pipe[WRITE_END]);
2736 close (errors_pipe[WRITE_END]);
2737 address_pipe[WRITE_END] = -1;
2738 errors_pipe[WRITE_END] = -1;
2743 ret = _dbus_read (address_pipe[READ_END], address, 1024);
2747 /* reap the child process to avoid it lingering as zombie */
2750 ret = waitpid (pid, &status, 0);
2752 while (ret == -1 && errno == EINTR);
2754 /* We succeeded if the process exited with status 0 and
2755 anything was read */
2756 if (!WIFEXITED (status) || WEXITSTATUS (status) != 0 ||
2757 _dbus_string_get_length (address) == orig_len)
2759 /* The process ended with error */
2760 DBusString error_message;
2761 _dbus_string_init (&error_message);
2765 ret = _dbus_read (errors_pipe[READ_END], &error_message, 1024);
2769 _dbus_string_set_length (address, orig_len);
2770 if (_dbus_string_get_length (&error_message) > 0)
2771 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
2772 "dbus-launch failed to autolaunch D-Bus session: %s",
2773 _dbus_string_get_data (&error_message));
2775 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
2776 "Failed to execute dbus-launch to autolaunch D-Bus session");
2784 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2786 _DBUS_ASSERT_ERROR_IS_SET (error);
2788 if (address_pipe[0] != -1)
2789 close (address_pipe[0]);
2790 if (address_pipe[1] != -1)
2791 close (address_pipe[1]);
2792 if (errors_pipe[0] != -1)
2793 close (errors_pipe[0]);
2794 if (errors_pipe[1] != -1)
2795 close (errors_pipe[1]);
2797 _dbus_string_free (&uuid);
2802 * Reads the uuid of the machine we're running on from
2803 * the dbus configuration. Optionally try to create it
2804 * (only root can do this usually).
2806 * On UNIX, reads a file that gets created by dbus-uuidgen
2807 * in a post-install script. On Windows, if there's a standard
2808 * machine uuid we could just use that, but I can't find one
2809 * with the right properties (the hardware profile guid can change
2810 * without rebooting I believe). If there's no standard one
2811 * we might want to use the registry instead of a file for
2812 * this, and I'm not sure how we'd ensure the uuid gets created.
2814 * @param machine_id guid to init with the machine's uuid
2815 * @param create_if_not_found try to create the uuid if it doesn't exist
2816 * @param error the error return
2817 * @returns #FALSE if the error is set
2820 _dbus_read_local_machine_uuid (DBusGUID *machine_id,
2821 dbus_bool_t create_if_not_found,
2824 DBusString filename;
2825 _dbus_string_init_const (&filename, DBUS_MACHINE_UUID_FILE);
2826 return _dbus_read_uuid_file (&filename, machine_id, create_if_not_found, error);
2829 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
2833 * Returns the standard directories for a session bus to look for service
2836 * On UNIX this should be the standard xdg freedesktop.org data directories:
2838 * XDG_DATA_HOME=${XDG_DATA_HOME-$HOME/.local/share}
2839 * XDG_DATA_DIRS=${XDG_DATA_DIRS-/usr/local/share:/usr/share}
2845 * @param dirs the directory list we are returning
2846 * @returns #FALSE on OOM
2850 _dbus_get_standard_session_servicedirs (DBusList **dirs)
2852 const char *xdg_data_home;
2853 const char *xdg_data_dirs;
2854 DBusString servicedir_path;
2856 if (!_dbus_string_init (&servicedir_path))
2859 xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
2860 xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
2862 if (xdg_data_dirs != NULL)
2864 if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
2867 if (!_dbus_string_append (&servicedir_path, ":"))
2872 if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
2877 * add configured datadir to defaults
2878 * this may be the same as an xdg dir
2879 * however the config parser should take
2880 * care of duplicates
2882 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR":"))
2885 if (xdg_data_home != NULL)
2887 if (!_dbus_string_append (&servicedir_path, xdg_data_home))
2892 const DBusString *homedir;
2893 DBusString local_share;
2895 if (!_dbus_homedir_from_current_process (&homedir))
2898 if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
2901 _dbus_string_init_const (&local_share, "/.local/share");
2902 if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
2906 if (!_dbus_split_paths_and_append (&servicedir_path,
2907 DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
2911 _dbus_string_free (&servicedir_path);
2915 _dbus_string_free (&servicedir_path);
2920 * Called when the bus daemon is signaled to reload its configuration; any
2921 * caches should be nuked. Of course any caches that need explicit reload
2922 * are probably broken, but c'est la vie.
2927 _dbus_flush_caches (void)
2929 _dbus_user_database_flush_system ();
2932 /* tests in dbus-sysdeps-util.c */