2007-06-09 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-unix.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps-unix.c Wrappers around UNIX system/libc features (internal to D-Bus implementation)
3  * 
4  * Copyright (C) 2002, 2003, 2006  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
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.
13  *
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.
18  * 
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
22  *
23  */
24
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>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <signal.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <sys/socket.h>
43 #include <dirent.h>
44 #include <sys/un.h>
45 #include <pwd.h>
46 #include <time.h>
47 #include <locale.h>
48 #include <sys/time.h>
49 #include <sys/stat.h>
50 #include <sys/wait.h>
51 #include <netinet/in.h>
52 #include <netdb.h>
53 #include <grp.h>
54
55 #ifdef HAVE_ERRNO_H
56 #include <errno.h>
57 #endif
58 #ifdef HAVE_WRITEV
59 #include <sys/uio.h>
60 #endif
61 #ifdef HAVE_POLL
62 #include <sys/poll.h>
63 #endif
64 #ifdef HAVE_BACKTRACE
65 #include <execinfo.h>
66 #endif
67 #ifdef HAVE_GETPEERUCRED
68 #include <ucred.h>
69 #endif
70
71 #ifndef O_BINARY
72 #define O_BINARY 0
73 #endif
74
75 #ifndef HAVE_SOCKLEN_T
76 #define socklen_t int
77 #endif
78
79 static dbus_bool_t
80 _dbus_open_socket (int              *fd_p,
81                    int               domain,
82                    int               type,
83                    int               protocol,
84                    DBusError        *error)
85 {
86   *fd_p = socket (domain, type, protocol);
87   if (*fd_p >= 0)
88     {
89       _dbus_verbose ("socket fd %d opened\n", *fd_p);
90       return TRUE;
91     }
92   else
93     {
94       dbus_set_error(error,
95                      _dbus_error_from_errno (errno),
96                      "Failed to open socket: %s",
97                      _dbus_strerror (errno));
98       return FALSE;
99     }
100 }
101
102 dbus_bool_t
103 _dbus_open_tcp_socket (int              *fd,
104                        DBusError        *error)
105 {
106   return _dbus_open_socket(fd, AF_INET, SOCK_STREAM, 0, error);
107 }
108
109 /**
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
115  */
116 dbus_bool_t
117 _dbus_open_unix_socket (int              *fd,
118                         DBusError        *error)
119 {
120   return _dbus_open_socket(fd, PF_UNIX, SOCK_STREAM, 0, error);
121 }
122
123 /**
124  * Closes a socket. Should not be used on non-socket
125  * file descriptors or handles.
126  *
127  * @param fd the socket
128  * @param error return location for an error
129  * @returns #FALSE if error is set
130  */
131 dbus_bool_t 
132 _dbus_close_socket (int               fd,
133                     DBusError        *error)
134 {
135   return _dbus_close (fd, error);
136 }
137
138 /**
139  * Like _dbus_read(), but only works on sockets so is
140  * available on Windows.
141  *
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
146  */
147 int
148 _dbus_read_socket (int               fd,
149                    DBusString       *buffer,
150                    int               count)
151 {
152   return _dbus_read (fd, buffer, count);
153 }
154
155 /**
156  * Like _dbus_write(), but only supports sockets
157  * and is thus available on Windows.
158  *
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
164  */
165 int
166 _dbus_write_socket (int               fd,
167                     const DBusString *buffer,
168                     int               start,
169                     int               len)
170 {
171   return _dbus_write (fd, buffer, start, len);
172 }
173
174 /**
175  * write data to a pipe.
176  *
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
183  */
184 int
185 _dbus_pipe_write (DBusPipe         *pipe,
186                   const DBusString *buffer,
187                   int               start,
188                   int               len,
189                   DBusError        *error)
190 {
191   int written;
192   
193   written = _dbus_write (pipe->fd_or_handle, buffer, start, len);
194   if (written < 0)
195     {
196       dbus_set_error (error, DBUS_ERROR_FAILED,
197                       "Writing to pipe: %s\n",
198                       _dbus_strerror (errno));
199     }
200   return written;
201 }
202
203 /**
204  * close a pipe.
205  *
206  * @param pipe the pipe instance
207  * @param error return location for an error
208  * @returns #FALSE if error is set
209  */
210 int
211 _dbus_pipe_close  (DBusPipe         *pipe,
212                    DBusError        *error)
213 {
214   if (_dbus_close (pipe->fd_or_handle, error) < 0)
215     {
216       return -1;
217     }
218   else
219     {
220       _dbus_pipe_invalidate (pipe);
221       return 0;
222     }
223 }
224
225 /**
226  * Like _dbus_write_two() but only works on sockets and is thus
227  * available on Windows.
228  * 
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
237  */
238 int
239 _dbus_write_socket_two (int               fd,
240                         const DBusString *buffer1,
241                         int               start1,
242                         int               len1,
243                         const DBusString *buffer2,
244                         int               start2,
245                         int               len2)
246 {
247   return _dbus_write_two (fd, buffer1, start1, len1,
248                           buffer2, start2, len2);
249 }
250
251
252 /**
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.
259  *
260  * Unlike _dbus_read_socket(), _dbus_read() is not available
261  * on Windows.
262  * 
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
267  */
268 int
269 _dbus_read (int               fd,
270             DBusString       *buffer,
271             int               count)
272 {
273   int bytes_read;
274   int start;
275   char *data;
276
277   _dbus_assert (count >= 0);
278   
279   start = _dbus_string_get_length (buffer);
280
281   if (!_dbus_string_lengthen (buffer, count))
282     {
283       errno = ENOMEM;
284       return -1;
285     }
286
287   data = _dbus_string_get_data_len (buffer, start, count);
288
289  again:
290   
291   bytes_read = read (fd, data, count);
292
293   if (bytes_read < 0)
294     {
295       if (errno == EINTR)
296         goto again;
297       else
298         {
299           /* put length back (note that this doesn't actually realloc anything) */
300           _dbus_string_set_length (buffer, start);
301           return -1;
302         }
303     }
304   else
305     {
306       /* put length back (doesn't actually realloc) */
307       _dbus_string_set_length (buffer, start + bytes_read);
308
309 #if 0
310       if (bytes_read > 0)
311         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
312 #endif
313       
314       return bytes_read;
315     }
316 }
317
318 /**
319  * Thin wrapper around the write() system call that writes a part of a
320  * DBusString and handles EINTR for you.
321  * 
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
327  */
328 int
329 _dbus_write (int               fd,
330              const DBusString *buffer,
331              int               start,
332              int               len)
333 {
334   const char *data;
335   int bytes_written;
336   
337   data = _dbus_string_get_const_data_len (buffer, start, len);
338   
339  again:
340
341   bytes_written = write (fd, data, len);
342
343   if (bytes_written < 0 && errno == EINTR)
344     goto again;
345
346 #if 0
347   if (bytes_written > 0)
348     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
349 #endif
350   
351   return bytes_written;
352 }
353
354 /**
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.
364  *
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
373  */
374 int
375 _dbus_write_two (int               fd,
376                  const DBusString *buffer1,
377                  int               start1,
378                  int               len1,
379                  const DBusString *buffer2,
380                  int               start2,
381                  int               len2)
382 {
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);
388   
389 #ifdef HAVE_WRITEV
390   {
391     struct iovec vectors[2];
392     const char *data1;
393     const char *data2;
394     int bytes_written;
395
396     data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
397
398     if (buffer2 != NULL)
399       data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
400     else
401       {
402         data2 = NULL;
403         start2 = 0;
404         len2 = 0;
405       }
406    
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;
411
412   again:
413    
414     bytes_written = writev (fd,
415                             vectors,
416                             data2 ? 2 : 1);
417
418     if (bytes_written < 0 && errno == EINTR)
419       goto again;
420    
421     return bytes_written;
422   }
423 #else /* HAVE_WRITEV */
424   {
425     int ret1;
426     
427     ret1 = _dbus_write (fd, buffer1, start1, len1);
428     if (ret1 == len1 && buffer2 != NULL)
429       {
430         ret2 = _dbus_write (fd, buffer2, start2, len2);
431         if (ret2 < 0)
432           ret2 = 0; /* we can't report an error as the first write was OK */
433        
434         return ret1 + ret2;
435       }
436     else
437       return ret1;
438   }
439 #endif /* !HAVE_WRITEV */   
440 }
441
442 #define _DBUS_MAX_SUN_PATH_LENGTH 99
443
444 /**
445  * @def _DBUS_MAX_SUN_PATH_LENGTH
446  *
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.
451  *
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.
455  */
456
457 /**
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
460  * nonblocking.
461  * 
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.
465  *
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
470  */
471 int
472 _dbus_connect_unix_socket (const char     *path,
473                            dbus_bool_t     abstract,
474                            DBusError      *error)
475 {
476   int fd;
477   size_t path_len;
478   struct sockaddr_un addr;  
479
480   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
481
482   _dbus_verbose ("connecting to unix socket %s abstract=%d\n",
483                  path, abstract);
484   
485   
486   if (!_dbus_open_unix_socket (&fd, error))
487     {
488       _DBUS_ASSERT_ERROR_IS_SET(error);
489       return -1;
490     }
491   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
492
493   _DBUS_ZERO (addr);
494   addr.sun_family = AF_UNIX;
495   path_len = strlen (path);
496
497   if (abstract)
498     {
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 */
502
503       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
504         {
505           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
506                       "Abstract socket name too long\n");
507           _dbus_close (fd, NULL);
508           return -1;
509         }
510         
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);
517       return -1;
518 #endif /* ! HAVE_ABSTRACT_SOCKETS */
519     }
520   else
521     {
522       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
523         {
524           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
525                       "Socket name too long\n");
526           _dbus_close (fd, NULL);
527           return -1;
528         }
529
530       strncpy (addr.sun_path, path, path_len);
531     }
532   
533   if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
534     {      
535       dbus_set_error (error,
536                       _dbus_error_from_errno (errno),
537                       "Failed to connect to socket %s: %s",
538                       path, _dbus_strerror (errno));
539
540       _dbus_close (fd, NULL);
541       fd = -1;
542       
543       return -1;
544     }
545
546   if (!_dbus_set_fd_nonblocking (fd, error))
547     {
548       _DBUS_ASSERT_ERROR_IS_SET (error);
549       
550       _dbus_close (fd, NULL);
551       fd = -1;
552
553       return -1;
554     }
555
556   return fd;
557 }
558
559 /**
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.
564  *
565  * @param fd socket on which to change the #LOCAL_CREDS flag.
566  * @param on whether to enable or disable the #LOCAL_CREDS flag.
567  */
568 static dbus_bool_t
569 _dbus_set_local_creds (int fd, dbus_bool_t on)
570 {
571   dbus_bool_t retval = TRUE;
572
573 #if defined(HAVE_CMSGCRED)
574   /* NOOP just to make sure only one codepath is used 
575    *      and to prefer CMSGCRED
576    */
577 #elif defined(LOCAL_CREDS) 
578   int val = on ? 1 : 0;
579   if (setsockopt (fd, 0, LOCAL_CREDS, &val, sizeof (val)) < 0)
580     {
581       _dbus_verbose ("Unable to set LOCAL_CREDS socket option on fd %d\n", fd);
582       retval = FALSE;
583     }
584   else
585     _dbus_verbose ("LOCAL_CREDS %s for further messages on fd %d\n",
586                    on ? "enabled" : "disabled", fd);
587 #endif
588
589   return retval;
590 }
591
592 /**
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.
596  *
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.
601  *
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
606  */
607 int
608 _dbus_listen_unix_socket (const char     *path,
609                           dbus_bool_t     abstract,
610                           DBusError      *error)
611 {
612   int listen_fd;
613   struct sockaddr_un addr;
614   size_t path_len;
615
616   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
617
618   _dbus_verbose ("listening on unix socket %s abstract=%d\n",
619                  path, abstract);
620   
621   if (!_dbus_open_unix_socket (&listen_fd, error))
622     {
623       _DBUS_ASSERT_ERROR_IS_SET(error);
624       return -1;
625     }
626   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
627
628   _DBUS_ZERO (addr);
629   addr.sun_family = AF_UNIX;
630   path_len = strlen (path);
631   
632   if (abstract)
633     {
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.
637        */
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 */
640
641       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
642         {
643           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
644                       "Abstract socket name too long\n");
645           _dbus_close (listen_fd, NULL);
646           return -1;
647         }
648       
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);
655       return -1;
656 #endif /* ! HAVE_ABSTRACT_SOCKETS */
657     }
658   else
659     {
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.
665        *
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.
669        */
670       {
671         struct stat sb;
672
673         if (stat (path, &sb) == 0 &&
674             S_ISSOCK (sb.st_mode))
675           unlink (path);
676       }
677
678       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
679         {
680           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
681                       "Abstract socket name too long\n");
682           _dbus_close (listen_fd, NULL);
683           return -1;
684         }
685         
686       strncpy (addr.sun_path, path, path_len);
687     }
688   
689   if (bind (listen_fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
690     {
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);
695       return -1;
696     }
697
698   if (listen (listen_fd, 30 /* backlog */) < 0)
699     {
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);
704       return -1;
705     }
706
707   if (!_dbus_set_local_creds (listen_fd, TRUE))
708     {
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));
712       close (listen_fd);
713       return -1;
714     }
715
716   if (!_dbus_set_fd_nonblocking (listen_fd, error))
717     {
718       _DBUS_ASSERT_ERROR_IS_SET (error);
719       _dbus_close (listen_fd, NULL);
720       return -1;
721     }
722   
723   /* Try opening up the permissions, but if we can't, just go ahead
724    * and continue, maybe it will be good enough.
725    */
726   if (!abstract && chmod (path, 0777) < 0)
727     _dbus_warn ("Could not set mode 0777 on socket %s\n",
728                 path);
729   
730   return listen_fd;
731 }
732
733 /**
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
736  * nonblocking.
737  *
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
742  */
743 int
744 _dbus_connect_tcp_socket (const char     *host,
745                           dbus_uint32_t   port,
746                           DBusError      *error)
747 {
748   int fd;
749   struct sockaddr_in addr;
750   struct hostent *he;
751   struct in_addr *haddr;
752
753   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
754  
755   
756   if (!_dbus_open_tcp_socket (&fd, error))
757     {
758       _DBUS_ASSERT_ERROR_IS_SET(error);
759       
760       return -1;
761     }
762   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
763       
764   if (host == NULL)
765     host = "localhost";
766
767   he = gethostbyname (host);
768   if (he == NULL) 
769     {
770       dbus_set_error (error,
771                       _dbus_error_from_errno (errno),
772                       "Failed to lookup hostname: %s",
773                       host);
774       _dbus_close (fd, NULL);
775       return -1;
776     }
777   
778   haddr = ((struct in_addr *) (he->h_addr_list)[0]);
779
780   _DBUS_ZERO (addr);
781   memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
782   addr.sin_family = AF_INET;
783   addr.sin_port = htons (port);
784   
785   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
786     {      
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));
791
792       _dbus_close (fd, NULL);
793       fd = -1;
794       
795       return -1;
796     }
797
798   if (!_dbus_set_fd_nonblocking (fd, error))
799     {
800       _dbus_close (fd, NULL);
801       fd = -1;
802
803       return -1;
804     }
805
806   return fd;
807 }
808
809 /**
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. 
815  *
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
820  */
821 int
822 _dbus_listen_tcp_socket (const char     *host,
823                          dbus_uint32_t  *port,
824                          DBusError      *error)
825 {
826   int listen_fd;
827   struct sockaddr_in addr;
828   struct hostent *he;
829   struct in_addr *haddr;
830   socklen_t len = (socklen_t) sizeof (struct sockaddr);
831
832   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
833   
834   
835   if (!_dbus_open_tcp_socket (&listen_fd, error))
836     {
837       _DBUS_ASSERT_ERROR_IS_SET(error);
838       return -1;
839     }
840   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
841
842   he = gethostbyname (host);
843   if (he == NULL) 
844     {
845       dbus_set_error (error,
846                       _dbus_error_from_errno (errno),
847                       "Failed to lookup hostname: %s",
848                       host);
849       _dbus_close (listen_fd, NULL);
850       return -1;
851     }
852   
853   haddr = ((struct in_addr *) (he->h_addr_list)[0]);
854
855   _DBUS_ZERO (addr);
856   memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
857   addr.sin_family = AF_INET;
858   addr.sin_port = htons (*port);
859
860   if (bind (listen_fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
861     {
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);
866       return -1;
867     }
868
869   if (listen (listen_fd, 30 /* backlog */) < 0)
870     {
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);
875       return -1;
876     }
877
878   getsockname(listen_fd, (struct sockaddr*) &addr, &len);
879   *port = (dbus_uint32_t) ntohs(addr.sin_port);
880
881   if (!_dbus_set_fd_nonblocking (listen_fd, error))
882     {
883       _dbus_close (listen_fd, NULL);
884       return -1;
885     }
886   
887   return listen_fd;
888 }
889
890 static dbus_bool_t
891 write_credentials_byte (int             server_fd,
892                         DBusError      *error)
893 {
894   int bytes_written;
895   char buf[1] = { '\0' };
896 #if defined(HAVE_CMSGCRED) 
897   struct {
898           struct cmsghdr hdr;
899           struct cmsgcred cred;
900   } cmsg;
901   struct iovec iov;
902   struct msghdr msg;
903   iov.iov_base = buf;
904   iov.iov_len = 1;
905
906   memset (&msg, 0, sizeof (msg));
907   msg.msg_iov = &iov;
908   msg.msg_iovlen = 1;
909
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;
916 #endif
917
918   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
919   
920  again:
921
922 #if defined(HAVE_CMSGCRED) 
923   bytes_written = sendmsg (server_fd, &msg, 0);
924 #else
925   bytes_written = write (server_fd, buf, 1);
926 #endif
927
928   if (bytes_written < 0 && errno == EINTR)
929     goto again;
930
931   if (bytes_written < 0)
932     {
933       dbus_set_error (error, _dbus_error_from_errno (errno),
934                       "Failed to write credentials byte: %s",
935                      _dbus_strerror (errno));
936       return FALSE;
937     }
938   else if (bytes_written == 0)
939     {
940       dbus_set_error (error, DBUS_ERROR_IO_ERROR,
941                       "wrote zero bytes writing credentials byte");
942       return FALSE;
943     }
944   else
945     {
946       _dbus_assert (bytes_written == 1);
947       _dbus_verbose ("wrote credentials byte\n");
948       return TRUE;
949     }
950 }
951
952 /**
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.
957  *
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.
962  * 
963  * Fails if no byte is available, so you must select() first.
964  *
965  * The point of the byte is that on some systems we have to
966  * use sendmsg()/recvmsg() to transmit credentials.
967  *
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
972  */
973 dbus_bool_t
974 _dbus_read_credentials_socket  (int              client_fd,
975                                 DBusCredentials *credentials,
976                                 DBusError       *error)
977 {
978   struct msghdr msg;
979   struct iovec iov;
980   char buf;
981   dbus_uid_t uid_read;
982   dbus_pid_t pid_read;
983
984   uid_read = DBUS_UID_UNSET;
985   pid_read = DBUS_PID_UNSET;
986   
987 #ifdef HAVE_CMSGCRED 
988   struct {
989           struct cmsghdr hdr;
990           struct cmsgcred cred;
991   } cmsg;
992
993 #elif defined(LOCAL_CREDS)
994   struct {
995           struct cmsghdr hdr;
996           struct sockcred cred;
997   } cmsg;
998 #endif
999
1000   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1001   
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
1005    */
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));
1009
1010   _dbus_credentials_clear (credentials);
1011
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.
1016    */
1017
1018   iov.iov_base = &buf;
1019   iov.iov_len = 1;
1020
1021   memset (&msg, 0, sizeof (msg));
1022   msg.msg_iov = &iov;
1023   msg.msg_iovlen = 1;
1024
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);
1029 #endif
1030
1031  again:
1032   if (recvmsg (client_fd, &msg, 0) < 0)
1033     {
1034       if (errno == EINTR)
1035         goto again;
1036
1037       dbus_set_error (error, _dbus_error_from_errno (errno),
1038                       "Failed to read credentials byte: %s",
1039                       _dbus_strerror (errno));
1040       return FALSE;
1041     }
1042
1043   if (buf != '\0')
1044     {
1045       dbus_set_error (error, DBUS_ERROR_FAILED,
1046                       "Credentials byte was not nul");
1047       return FALSE;
1048     }
1049
1050 #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
1051   if (cmsg.hdr.cmsg_len < sizeof (cmsg) || cmsg.hdr.cmsg_type != SCM_CREDS)
1052     {
1053       dbus_set_error (error, DBUS_ERROR_FAILED,
1054                       "Message from recvmsg() was not SCM_CREDS");
1055       return FALSE;
1056     }
1057 #endif
1058
1059   _dbus_verbose ("read credentials byte\n");
1060
1061   {
1062 #ifdef SO_PEERCRED
1063     struct ucred cr;   
1064     int cr_len = sizeof (cr);
1065    
1066     if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
1067         cr_len == sizeof (cr))
1068       {
1069         pid_read = cr.pid;
1070         uid_read = cr.uid;
1071       }
1072     else
1073       {
1074         _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
1075                        cr_len, (int) sizeof (cr), _dbus_strerror (errno));
1076       }
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)
1087     uid_t euid;
1088     gid_t egid;
1089     if (getpeereid (client_fd, &euid, &egid) == 0)
1090       {
1091         uid_read = euid;
1092       }
1093     else
1094       {
1095         _dbus_verbose ("Failed to getpeereid() credentials: %s\n", _dbus_strerror (errno));
1096       }
1097 #elif defined(HAVE_GETPEERUCRED)
1098     ucred_t * ucred = NULL;
1099     if (getpeerucred (client_fd, &ucred) == 0)
1100       {
1101         pid_read = ucred_getpid (ucred);
1102         uid_read = ucred_geteuid (ucred);
1103       }
1104     else
1105       {
1106         _dbus_verbose ("Failed to getpeerucred() credentials: %s\n", _dbus_strerror (errno));
1107       }
1108     if (ucred != NULL)
1109       ucred_free (ucred);
1110 #else /* !SO_PEERCRED && !HAVE_CMSGCRED && !HAVE_GETPEEREID && !HAVE_GETPEERUCRED */
1111     _dbus_verbose ("Socket credentials not supported on this OS\n");
1112 #endif
1113   }
1114
1115   _dbus_verbose ("Credentials:"
1116                  "  pid "DBUS_PID_FORMAT
1117                  "  uid "DBUS_UID_FORMAT
1118                  "\n",
1119                  pid_read,
1120                  uid_read);
1121
1122   if (pid_read != DBUS_PID_UNSET)
1123     {
1124       if (!_dbus_credentials_add_unix_pid (credentials, pid_read))
1125         {
1126           _DBUS_SET_OOM (error);
1127           return FALSE;
1128         }
1129     }
1130
1131   if (uid_read != DBUS_UID_UNSET)
1132     {
1133       if (!_dbus_credentials_add_unix_uid (credentials, uid_read))
1134         {
1135           _DBUS_SET_OOM (error);
1136           return FALSE;
1137         }
1138     }
1139   
1140   return TRUE;
1141 }
1142
1143 /**
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.
1150  *
1151  * Fails if no byte can be written, so you must select() first.
1152  *
1153  * The point of the byte is that on some systems we have to
1154  * use sendmsg()/recvmsg() to transmit credentials.
1155  *
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
1159  */
1160 dbus_bool_t
1161 _dbus_send_credentials_socket  (int              server_fd,
1162                                 DBusError       *error)
1163 {
1164   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1165   
1166   if (write_credentials_byte (server_fd, error))
1167     return TRUE;
1168   else
1169     return FALSE;
1170 }
1171
1172 /**
1173  * Accepts a connection on a listening socket.
1174  * Handles EINTR for you.
1175  *
1176  * @param listen_fd the listen file descriptor
1177  * @returns the connection fd of the client, or -1 on error
1178  */
1179 int
1180 _dbus_accept  (int listen_fd)
1181 {
1182   int client_fd;
1183   struct sockaddr addr;
1184   socklen_t addrlen;
1185
1186   addrlen = sizeof (addr);
1187   
1188  retry:
1189   client_fd = accept (listen_fd, &addr, &addrlen);
1190   
1191   if (client_fd < 0)
1192     {
1193       if (errno == EINTR)
1194         goto retry;
1195     }
1196
1197   _dbus_verbose ("client fd %d accepted\n", client_fd);
1198   
1199   return client_fd;
1200 }
1201
1202 /**
1203  * Checks to make sure the given directory is 
1204  * private to the user 
1205  *
1206  * @param dir the name of the directory
1207  * @param error error return
1208  * @returns #FALSE on failure
1209  **/
1210 dbus_bool_t
1211 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
1212 {
1213   const char *directory;
1214   struct stat sb;
1215         
1216   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1217     
1218   directory = _dbus_string_get_const_data (dir);
1219         
1220   if (stat (directory, &sb) < 0)
1221     {
1222       dbus_set_error (error, _dbus_error_from_errno (errno),
1223                       "%s", _dbus_strerror (errno));
1224    
1225       return FALSE;
1226     }
1227     
1228   if ((S_IROTH & sb.st_mode) || (S_IWOTH & sb.st_mode) ||
1229       (S_IRGRP & sb.st_mode) || (S_IWGRP & sb.st_mode))
1230     {
1231       dbus_set_error (error, DBUS_ERROR_FAILED,
1232                      "%s directory is not private to the user", directory);
1233       return FALSE;
1234     }
1235     
1236   return TRUE;
1237 }
1238
1239 static dbus_bool_t
1240 fill_user_info_from_passwd (struct passwd *p,
1241                             DBusUserInfo  *info,
1242                             DBusError     *error)
1243 {
1244   _dbus_assert (p->pw_name != NULL);
1245   _dbus_assert (p->pw_dir != NULL);
1246   
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);
1251   
1252   if (info->username == NULL ||
1253       info->homedir == NULL)
1254     {
1255       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1256       return FALSE;
1257     }
1258
1259   return TRUE;
1260 }
1261
1262 static dbus_bool_t
1263 fill_user_info (DBusUserInfo       *info,
1264                 dbus_uid_t          uid,
1265                 const DBusString   *username,
1266                 DBusError          *error)
1267 {
1268   const char *username_c;
1269   
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);
1273
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;
1280   
1281   if (username != NULL)
1282     username_c = _dbus_string_get_const_data (username);
1283   else
1284     username_c = NULL;
1285
1286   /* For now assuming that the getpwnam() and getpwuid() flavors
1287    * are always symmetrical, if not we have to add more configure
1288    * checks
1289    */
1290   
1291 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
1292   {
1293     struct passwd *p;
1294     int result;
1295     char buf[1024];
1296     struct passwd p_str;
1297
1298     p = NULL;
1299 #ifdef HAVE_POSIX_GETPWNAM_R
1300     if (uid != DBUS_UID_UNSET)
1301       result = getpwuid_r (uid, &p_str, buf, sizeof (buf),
1302                            &p);
1303     else
1304       result = getpwnam_r (username_c, &p_str, buf, sizeof (buf),
1305                            &p);
1306 #else
1307     if (uid != DBUS_UID_UNSET)
1308       p = getpwuid_r (uid, &p_str, buf, sizeof (buf));
1309     else
1310       p = getpwnam_r (username_c, &p_str, buf, sizeof (buf));
1311     result = 0;
1312 #endif /* !HAVE_POSIX_GETPWNAM_R */
1313     if (result == 0 && p == &p_str)
1314       {
1315         if (!fill_user_info_from_passwd (p, info, error))
1316           return FALSE;
1317       }
1318     else
1319       {
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 : "???");
1324         return FALSE;
1325       }
1326   }
1327 #else /* ! HAVE_GETPWNAM_R */
1328   {
1329     /* I guess we're screwed on thread safety here */
1330     struct passwd *p;
1331
1332     if (uid != DBUS_UID_UNSET)
1333       p = getpwuid (uid);
1334     else
1335       p = getpwnam (username_c);
1336
1337     if (p != NULL)
1338       {
1339         if (!fill_user_info_from_passwd (p, info, error))
1340           return FALSE;
1341       }
1342     else
1343       {
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 : "???");
1348         return FALSE;
1349       }
1350   }
1351 #endif  /* ! HAVE_GETPWNAM_R */
1352
1353   /* Fill this in so we can use it to get groups */
1354   username_c = info->username;
1355   
1356 #ifdef HAVE_GETGROUPLIST
1357   {
1358     gid_t *buf;
1359     int buf_count;
1360     int i;
1361     
1362     buf_count = 17;
1363     buf = dbus_new (gid_t, buf_count);
1364     if (buf == NULL)
1365       {
1366         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1367         goto failed;
1368       }
1369     
1370     if (getgrouplist (username_c,
1371                       info->primary_gid,
1372                       buf, &buf_count) < 0)
1373       {
1374         gid_t *new = dbus_realloc (buf, buf_count * sizeof (buf[0]));
1375         if (new == NULL)
1376           {
1377             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1378             dbus_free (buf);
1379             goto failed;
1380           }
1381         
1382         buf = new;
1383
1384         errno = 0;
1385         if (getgrouplist (username_c, info->primary_gid, buf, &buf_count) < 0)
1386           {
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));
1393             dbus_free (buf);
1394             goto failed;
1395           }
1396       }
1397
1398     info->group_ids = dbus_new (dbus_gid_t, buf_count);
1399     if (info->group_ids == NULL)
1400       {
1401         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1402         dbus_free (buf);
1403         goto failed;
1404       }
1405     
1406     for (i = 0; i < buf_count; ++i)
1407       info->group_ids[i] = buf[i];
1408
1409     info->n_group_ids = buf_count;
1410     
1411     dbus_free (buf);
1412   }
1413 #else  /* HAVE_GETGROUPLIST */
1414   {
1415     /* We just get the one group ID */
1416     info->group_ids = dbus_new (dbus_gid_t, 1);
1417     if (info->group_ids == NULL)
1418       {
1419         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1420         goto failed;
1421       }
1422
1423     info->n_group_ids = 1;
1424
1425     (info->group_ids)[0] = info->primary_gid;
1426   }
1427 #endif /* HAVE_GETGROUPLIST */
1428
1429   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1430   
1431   return TRUE;
1432   
1433  failed:
1434   _DBUS_ASSERT_ERROR_IS_SET (error);
1435   return FALSE;
1436 }
1437
1438 /**
1439  * Gets user info for the given username.
1440  *
1441  * @param info user info object to initialize
1442  * @param username the username
1443  * @param error error return
1444  * @returns #TRUE on success
1445  */
1446 dbus_bool_t
1447 _dbus_user_info_fill (DBusUserInfo     *info,
1448                       const DBusString *username,
1449                       DBusError        *error)
1450 {
1451   return fill_user_info (info, DBUS_UID_UNSET,
1452                          username, error);
1453 }
1454
1455 /**
1456  * Gets user info for the given user ID.
1457  *
1458  * @param info user info object to initialize
1459  * @param uid the user ID
1460  * @param error error return
1461  * @returns #TRUE on success
1462  */
1463 dbus_bool_t
1464 _dbus_user_info_fill_uid (DBusUserInfo *info,
1465                           dbus_uid_t    uid,
1466                           DBusError    *error)
1467 {
1468   return fill_user_info (info, uid,
1469                          NULL, error);
1470 }
1471
1472 /**
1473  * Adds the credentials of the current process to the
1474  * passed-in credentials object.
1475  *
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
1478  */
1479 dbus_bool_t
1480 _dbus_credentials_add_from_current_process (DBusCredentials *credentials)
1481 {
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
1485    */
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));
1489
1490   if (!_dbus_credentials_add_unix_pid(credentials, _dbus_getpid()))
1491     return FALSE;
1492   if (!_dbus_credentials_add_unix_uid(credentials, _dbus_getuid()))
1493     return FALSE;
1494
1495   return TRUE;
1496 }
1497
1498 /**
1499  * Parses a desired identity provided from a client in the auth protocol.
1500  * On UNIX this means parsing a UID.
1501  *
1502  * @todo this is broken because it treats OOM and parse error
1503  * the same way. Needs a #DBusError.
1504  * 
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
1508  */
1509 dbus_bool_t
1510 _dbus_credentials_parse_and_add_desired (DBusCredentials  *credentials,
1511                                          const DBusString *desired_identity)
1512 {
1513   dbus_uid_t uid;
1514
1515   if (!_dbus_parse_uid (desired_identity, &uid))
1516     return FALSE;
1517
1518   if (!_dbus_credentials_add_unix_uid (credentials, uid))
1519     return FALSE;
1520
1521   return TRUE;
1522 }
1523
1524 /**
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.
1528  * 
1529  * @param str the string to append to
1530  * @returns #FALSE on no memory
1531  */
1532 dbus_bool_t
1533 _dbus_append_desired_identity (DBusString *str)
1534 {
1535   return _dbus_string_append_uint (str,
1536                                    _dbus_getuid ());
1537 }
1538
1539 /**
1540  * Gets our process ID
1541  * @returns process ID
1542  */
1543 dbus_pid_t
1544 _dbus_getpid (void)
1545 {
1546   return getpid ();
1547 }
1548
1549 /** Gets our UID
1550  * @returns process UID
1551  */
1552 dbus_uid_t
1553 _dbus_getuid (void)
1554 {
1555   return getuid ();
1556 }
1557
1558 /**
1559  * The only reason this is separate from _dbus_getpid() is to allow it
1560  * on Windows for logging but not for other purposes.
1561  * 
1562  * @returns process ID to put in log messages
1563  */
1564 unsigned long
1565 _dbus_pid_for_log (void)
1566 {
1567   return getpid ();
1568 }
1569
1570 /**
1571  * Gets a UID from a UID string.
1572  *
1573  * @param uid_str the UID in string form
1574  * @param uid UID to fill in
1575  * @returns #TRUE if successfully filled in UID
1576  */
1577 dbus_bool_t
1578 _dbus_parse_uid (const DBusString      *uid_str,
1579                  dbus_uid_t            *uid)
1580 {
1581   int end;
1582   long val;
1583   
1584   if (_dbus_string_get_length (uid_str) == 0)
1585     {
1586       _dbus_verbose ("UID string was zero length\n");
1587       return FALSE;
1588     }
1589
1590   val = -1;
1591   end = 0;
1592   if (!_dbus_string_parse_int (uid_str, 0, &val,
1593                                &end))
1594     {
1595       _dbus_verbose ("could not parse string as a UID\n");
1596       return FALSE;
1597     }
1598   
1599   if (end != _dbus_string_get_length (uid_str))
1600     {
1601       _dbus_verbose ("string contained trailing stuff after UID\n");
1602       return FALSE;
1603     }
1604
1605   *uid = val;
1606
1607   return TRUE;
1608 }
1609
1610
1611 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
1612
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)
1619 {
1620   register dbus_int32_t result;
1621
1622   __asm__ __volatile__ ("lock; xaddl %0,%1"
1623                         : "=r" (result), "=m" (atomic->value)
1624                         : "0" (val), "m" (atomic->value));
1625   return result;
1626 }
1627 #endif
1628
1629 /**
1630  * Atomically increments an integer
1631  *
1632  * @param atomic pointer to the integer to increment
1633  * @returns the value before incrementing
1634  *
1635  * @todo implement arch-specific faster atomic ops
1636  */
1637 dbus_int32_t
1638 _dbus_atomic_inc (DBusAtomic *atomic)
1639 {
1640 #ifdef DBUS_USE_ATOMIC_INT_486
1641   return atomic_exchange_and_add (atomic, 1);
1642 #else
1643   dbus_int32_t res;
1644   _DBUS_LOCK (atomic);
1645   res = atomic->value;
1646   atomic->value += 1;
1647   _DBUS_UNLOCK (atomic);
1648   return res;
1649 #endif
1650 }
1651
1652 /**
1653  * Atomically decrement an integer
1654  *
1655  * @param atomic pointer to the integer to decrement
1656  * @returns the value before decrementing
1657  *
1658  * @todo implement arch-specific faster atomic ops
1659  */
1660 dbus_int32_t
1661 _dbus_atomic_dec (DBusAtomic *atomic)
1662 {
1663 #ifdef DBUS_USE_ATOMIC_INT_486
1664   return atomic_exchange_and_add (atomic, -1);
1665 #else
1666   dbus_int32_t res;
1667   
1668   _DBUS_LOCK (atomic);
1669   res = atomic->value;
1670   atomic->value -= 1;
1671   _DBUS_UNLOCK (atomic);
1672   return res;
1673 #endif
1674 }
1675
1676 #ifdef DBUS_BUILD_TESTS
1677 /** Gets our GID
1678  * @returns process GID
1679  */
1680 dbus_gid_t
1681 _dbus_getgid (void)
1682 {
1683   return getgid ();
1684 }
1685 #endif
1686
1687 /**
1688  * Wrapper for poll().
1689  *
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
1694  */
1695 int
1696 _dbus_poll (DBusPollFD *fds,
1697             int         n_fds,
1698             int         timeout_milliseconds)
1699 {
1700 #ifdef HAVE_POLL
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
1703    * no cost.
1704    */
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))
1718     {
1719       return poll ((struct pollfd*) fds,
1720                    n_fds, 
1721                    timeout_milliseconds);
1722     }
1723   else
1724     {
1725       /* We have to convert the DBusPollFD to an array of
1726        * struct pollfd, poll, and convert back.
1727        */
1728       _dbus_warn ("didn't implement poll() properly for this system yet\n");
1729       return -1;
1730     }
1731 #else /* ! HAVE_POLL */
1732
1733   fd_set read_set, write_set, err_set;
1734   int max_fd = 0;
1735   int i;
1736   struct timeval tv;
1737   int ready;
1738   
1739   FD_ZERO (&read_set);
1740   FD_ZERO (&write_set);
1741   FD_ZERO (&err_set);
1742
1743   for (i = 0; i < n_fds; i++)
1744     {
1745       DBusPollFD *fdp = &fds[i];
1746
1747       if (fdp->events & _DBUS_POLLIN)
1748         FD_SET (fdp->fd, &read_set);
1749
1750       if (fdp->events & _DBUS_POLLOUT)
1751         FD_SET (fdp->fd, &write_set);
1752
1753       FD_SET (fdp->fd, &err_set);
1754
1755       max_fd = MAX (max_fd, fdp->fd);
1756     }
1757     
1758   tv.tv_sec = timeout_milliseconds / 1000;
1759   tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
1760
1761   ready = select (max_fd + 1, &read_set, &write_set, &err_set,
1762                   timeout_milliseconds < 0 ? NULL : &tv);
1763
1764   if (ready > 0)
1765     {
1766       for (i = 0; i < n_fds; i++)
1767         {
1768           DBusPollFD *fdp = &fds[i];
1769
1770           fdp->revents = 0;
1771
1772           if (FD_ISSET (fdp->fd, &read_set))
1773             fdp->revents |= _DBUS_POLLIN;
1774
1775           if (FD_ISSET (fdp->fd, &write_set))
1776             fdp->revents |= _DBUS_POLLOUT;
1777
1778           if (FD_ISSET (fdp->fd, &err_set))
1779             fdp->revents |= _DBUS_POLLERR;
1780         }
1781     }
1782
1783   return ready;
1784 #endif
1785 }
1786
1787 /**
1788  * Get current time, as in gettimeofday().
1789  *
1790  * @param tv_sec return location for number of seconds
1791  * @param tv_usec return location for number of microseconds (thousandths)
1792  */
1793 void
1794 _dbus_get_current_time (long *tv_sec,
1795                         long *tv_usec)
1796 {
1797   struct timeval t;
1798
1799   gettimeofday (&t, NULL);
1800
1801   if (tv_sec)
1802     *tv_sec = t.tv_sec;
1803   if (tv_usec)
1804     *tv_usec = t.tv_usec;
1805 }
1806
1807 /**
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.
1811  *
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
1816  */
1817 dbus_bool_t
1818 _dbus_file_get_contents (DBusString       *str,
1819                          const DBusString *filename,
1820                          DBusError        *error)
1821 {
1822   int fd;
1823   struct stat sb;
1824   int orig_len;
1825   int total;
1826   const char *filename_c;
1827
1828   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1829   
1830   filename_c = _dbus_string_get_const_data (filename);
1831   
1832   /* O_BINARY useful on Cygwin */
1833   fd = open (filename_c, O_RDONLY | O_BINARY);
1834   if (fd < 0)
1835     {
1836       dbus_set_error (error, _dbus_error_from_errno (errno),
1837                       "Failed to open \"%s\": %s",
1838                       filename_c,
1839                       _dbus_strerror (errno));
1840       return FALSE;
1841     }
1842
1843   _dbus_verbose ("file fd %d opened\n", fd);
1844   
1845   if (fstat (fd, &sb) < 0)
1846     {
1847       dbus_set_error (error, _dbus_error_from_errno (errno),
1848                       "Failed to stat \"%s\": %s",
1849                       filename_c,
1850                       _dbus_strerror (errno));
1851
1852       _dbus_verbose ("fstat() failed: %s",
1853                      _dbus_strerror (errno));
1854       
1855       _dbus_close (fd, NULL);
1856       
1857       return FALSE;
1858     }
1859
1860   if (sb.st_size > _DBUS_ONE_MEGABYTE)
1861     {
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);
1866       return FALSE;
1867     }
1868   
1869   total = 0;
1870   orig_len = _dbus_string_get_length (str);
1871   if (sb.st_size > 0 && S_ISREG (sb.st_mode))
1872     {
1873       int bytes_read;
1874
1875       while (total < (int) sb.st_size)
1876         {
1877           bytes_read = _dbus_read (fd, str,
1878                                    sb.st_size - total);
1879           if (bytes_read <= 0)
1880             {
1881               dbus_set_error (error, _dbus_error_from_errno (errno),
1882                               "Error reading \"%s\": %s",
1883                               filename_c,
1884                               _dbus_strerror (errno));
1885
1886               _dbus_verbose ("read() failed: %s",
1887                              _dbus_strerror (errno));
1888               
1889               _dbus_close (fd, NULL);
1890               _dbus_string_set_length (str, orig_len);
1891               return FALSE;
1892             }
1893           else
1894             total += bytes_read;
1895         }
1896
1897       _dbus_close (fd, NULL);
1898       return TRUE;
1899     }
1900   else if (sb.st_size != 0)
1901     {
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",
1905                       filename_c);
1906       _dbus_close (fd, NULL);
1907       return FALSE;
1908     }
1909   else
1910     {
1911       _dbus_close (fd, NULL);
1912       return TRUE;
1913     }
1914 }
1915
1916 /**
1917  * Writes a string out to a file. If the file exists,
1918  * it will be atomically overwritten by the new data.
1919  *
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
1924  */
1925 dbus_bool_t
1926 _dbus_string_save_to_file (const DBusString *str,
1927                            const DBusString *filename,
1928                            DBusError        *error)
1929 {
1930   int fd;
1931   int bytes_to_write;
1932   const char *filename_c;
1933   DBusString tmp_filename;
1934   const char *tmp_filename_c;
1935   int total;
1936   dbus_bool_t need_unlink;
1937   dbus_bool_t retval;
1938
1939   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1940   
1941   fd = -1;
1942   retval = FALSE;
1943   need_unlink = FALSE;
1944   
1945   if (!_dbus_string_init (&tmp_filename))
1946     {
1947       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1948       return FALSE;
1949     }
1950
1951   if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
1952     {
1953       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1954       _dbus_string_free (&tmp_filename);
1955       return FALSE;
1956     }
1957   
1958   if (!_dbus_string_append (&tmp_filename, "."))
1959     {
1960       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1961       _dbus_string_free (&tmp_filename);
1962       return FALSE;
1963     }
1964
1965 #define N_TMP_FILENAME_RANDOM_BYTES 8
1966   if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
1967     {
1968       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1969       _dbus_string_free (&tmp_filename);
1970       return FALSE;
1971     }
1972     
1973   filename_c = _dbus_string_get_const_data (filename);
1974   tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
1975
1976   fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
1977              0600);
1978   if (fd < 0)
1979     {
1980       dbus_set_error (error, _dbus_error_from_errno (errno),
1981                       "Could not create %s: %s", tmp_filename_c,
1982                       _dbus_strerror (errno));
1983       goto out;
1984     }
1985
1986   _dbus_verbose ("tmp file fd %d opened\n", fd);
1987   
1988   need_unlink = TRUE;
1989   
1990   total = 0;
1991   bytes_to_write = _dbus_string_get_length (str);
1992
1993   while (total < bytes_to_write)
1994     {
1995       int bytes_written;
1996
1997       bytes_written = _dbus_write (fd, str, total,
1998                                    bytes_to_write - total);
1999
2000       if (bytes_written <= 0)
2001         {
2002           dbus_set_error (error, _dbus_error_from_errno (errno),
2003                           "Could not write to %s: %s", tmp_filename_c,
2004                           _dbus_strerror (errno));
2005           
2006           goto out;
2007         }
2008
2009       total += bytes_written;
2010     }
2011
2012   if (!_dbus_close (fd, NULL))
2013     {
2014       dbus_set_error (error, _dbus_error_from_errno (errno),
2015                       "Could not close file %s: %s",
2016                       tmp_filename_c, _dbus_strerror (errno));
2017
2018       goto out;
2019     }
2020
2021   fd = -1;
2022   
2023   if (rename (tmp_filename_c, filename_c) < 0)
2024     {
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));
2029
2030       goto out;
2031     }
2032
2033   need_unlink = FALSE;
2034   
2035   retval = TRUE;
2036   
2037  out:
2038   /* close first, then unlink, to prevent ".nfs34234235" garbage
2039    * files
2040    */
2041
2042   if (fd >= 0)
2043     _dbus_close (fd, NULL);
2044         
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));
2048
2049   _dbus_string_free (&tmp_filename);
2050
2051   if (!retval)
2052     _DBUS_ASSERT_ERROR_IS_SET (error);
2053   
2054   return retval;
2055 }
2056
2057 /** Makes the file readable by every user in the system.
2058  *
2059  * @param filename the filename
2060  * @param error error location
2061  * @returns #TRUE if the file's permissions could be changed.
2062  */
2063 dbus_bool_t
2064 _dbus_make_file_world_readable(const DBusString *filename,
2065                                DBusError *error)
2066 {
2067   const char *filename_c;
2068
2069   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2070
2071   filename_c = _dbus_string_get_const_data (filename);
2072   if (chmod (filename_c, 0644) == -1)
2073     {
2074       dbus_set_error (error,
2075                       DBUS_ERROR_FAILED,
2076                       "Could not change permissions of file %s: %s\n",
2077                       filename_c,
2078                       _dbus_strerror (errno));
2079       return FALSE;
2080     }
2081   return TRUE;
2082 }
2083
2084 /** Creates the given file, failing if the file already exists.
2085  *
2086  * @param filename the filename
2087  * @param error error location
2088  * @returns #TRUE if we created the file and it didn't exist
2089  */
2090 dbus_bool_t
2091 _dbus_create_file_exclusively (const DBusString *filename,
2092                                DBusError        *error)
2093 {
2094   int fd;
2095   const char *filename_c;
2096
2097   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2098   
2099   filename_c = _dbus_string_get_const_data (filename);
2100   
2101   fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
2102              0600);
2103   if (fd < 0)
2104     {
2105       dbus_set_error (error,
2106                       DBUS_ERROR_FAILED,
2107                       "Could not create file %s: %s\n",
2108                       filename_c,
2109                       _dbus_strerror (errno));
2110       return FALSE;
2111     }
2112
2113   _dbus_verbose ("exclusive file fd %d opened\n", fd);
2114   
2115   if (!_dbus_close (fd, NULL))
2116     {
2117       dbus_set_error (error,
2118                       DBUS_ERROR_FAILED,
2119                       "Could not close file %s: %s\n",
2120                       filename_c,
2121                       _dbus_strerror (errno));
2122       return FALSE;
2123     }
2124   
2125   return TRUE;
2126 }
2127
2128 /**
2129  * Deletes the given file.
2130  *
2131  * @param filename the filename
2132  * @param error error location
2133  * 
2134  * @returns #TRUE if unlink() succeeded
2135  */
2136 dbus_bool_t
2137 _dbus_delete_file (const DBusString *filename,
2138                    DBusError        *error)
2139 {
2140   const char *filename_c;
2141
2142   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2143   
2144   filename_c = _dbus_string_get_const_data (filename);
2145
2146   if (unlink (filename_c) < 0)
2147     {
2148       dbus_set_error (error, DBUS_ERROR_FAILED,
2149                       "Failed to delete file %s: %s\n",
2150                       filename_c, _dbus_strerror (errno));
2151       return FALSE;
2152     }
2153   else
2154     return TRUE;
2155 }
2156
2157 /**
2158  * Creates a directory; succeeds if the directory
2159  * is created or already existed.
2160  *
2161  * @param filename directory filename
2162  * @param error initialized error object
2163  * @returns #TRUE on success
2164  */
2165 dbus_bool_t
2166 _dbus_create_directory (const DBusString *filename,
2167                         DBusError        *error)
2168 {
2169   const char *filename_c;
2170
2171   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2172   
2173   filename_c = _dbus_string_get_const_data (filename);
2174
2175   if (mkdir (filename_c, 0700) < 0)
2176     {
2177       if (errno == EEXIST)
2178         return TRUE;
2179       
2180       dbus_set_error (error, DBUS_ERROR_FAILED,
2181                       "Failed to create directory %s: %s\n",
2182                       filename_c, _dbus_strerror (errno));
2183       return FALSE;
2184     }
2185   else
2186     return TRUE;
2187 }
2188
2189 /**
2190  * Appends the given filename to the given directory.
2191  *
2192  * @todo it might be cute to collapse multiple '/' such as "foo//"
2193  * concat "//bar"
2194  *
2195  * @param dir the directory name
2196  * @param next_component the filename
2197  * @returns #TRUE on success
2198  */
2199 dbus_bool_t
2200 _dbus_concat_dir_and_file (DBusString       *dir,
2201                            const DBusString *next_component)
2202 {
2203   dbus_bool_t dir_ends_in_slash;
2204   dbus_bool_t file_starts_with_slash;
2205
2206   if (_dbus_string_get_length (dir) == 0 ||
2207       _dbus_string_get_length (next_component) == 0)
2208     return TRUE;
2209   
2210   dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
2211                                                     _dbus_string_get_length (dir) - 1);
2212
2213   file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
2214
2215   if (dir_ends_in_slash && file_starts_with_slash)
2216     {
2217       _dbus_string_shorten (dir, 1);
2218     }
2219   else if (!(dir_ends_in_slash || file_starts_with_slash))
2220     {
2221       if (!_dbus_string_append_byte (dir, '/'))
2222         return FALSE;
2223     }
2224
2225   return _dbus_string_copy (next_component, 0, dir,
2226                             _dbus_string_get_length (dir));
2227 }
2228
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
2239
2240 /**
2241  * Sleeps the given number of milliseconds.
2242  * @param milliseconds number of milliseconds
2243  */
2244 void
2245 _dbus_sleep_milliseconds (int milliseconds)
2246 {
2247 #ifdef HAVE_NANOSLEEP
2248   struct timespec req;
2249   struct timespec rem;
2250
2251   req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
2252   req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
2253   rem.tv_sec = 0;
2254   rem.tv_nsec = 0;
2255
2256   while (nanosleep (&req, &rem) < 0 && errno == EINTR)
2257     req = rem;
2258 #elif defined (HAVE_USLEEP)
2259   usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
2260 #else /* ! HAVE_USLEEP */
2261   sleep (MAX (milliseconds / 1000, 1));
2262 #endif
2263 }
2264
2265 static dbus_bool_t
2266 _dbus_generate_pseudorandom_bytes (DBusString *str,
2267                                    int         n_bytes)
2268 {
2269   int old_len;
2270   char *p;
2271   
2272   old_len = _dbus_string_get_length (str);
2273
2274   if (!_dbus_string_lengthen (str, n_bytes))
2275     return FALSE;
2276
2277   p = _dbus_string_get_data_len (str, old_len, n_bytes);
2278
2279   _dbus_generate_pseudorandom_bytes_buffer (p, n_bytes);
2280
2281   return TRUE;
2282 }
2283
2284 /**
2285  * Generates the given number of random bytes,
2286  * using the best mechanism we can come up with.
2287  *
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
2291  */
2292 dbus_bool_t
2293 _dbus_generate_random_bytes (DBusString *str,
2294                              int         n_bytes)
2295 {
2296   int old_len;
2297   int fd;
2298
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
2302    * if the I/O fails.
2303    */
2304   
2305   old_len = _dbus_string_get_length (str);
2306   fd = -1;
2307
2308   /* note, urandom on linux will fall back to pseudorandom */
2309   fd = open ("/dev/urandom", O_RDONLY);
2310   if (fd < 0)
2311     return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2312
2313   _dbus_verbose ("/dev/urandom fd %d opened\n", fd);
2314   
2315   if (_dbus_read (fd, str, n_bytes) != n_bytes)
2316     {
2317       _dbus_close (fd, NULL);
2318       _dbus_string_set_length (str, old_len);
2319       return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2320     }
2321
2322   _dbus_verbose ("Read %d bytes from /dev/urandom\n",
2323                  n_bytes);
2324   
2325   _dbus_close (fd, NULL);
2326   
2327   return TRUE;
2328 }
2329
2330 /**
2331  * Exit the process, returning the given value.
2332  *
2333  * @param code the exit code
2334  */
2335 void
2336 _dbus_exit (int code)
2337 {
2338   _exit (code);
2339 }
2340
2341 /**
2342  * A wrapper around strerror() because some platforms
2343  * may be lame and not have strerror().
2344  *
2345  * @param error_number errno.
2346  * @returns error description.
2347  */
2348 const char*
2349 _dbus_strerror (int error_number)
2350 {
2351   const char *msg;
2352   
2353   msg = strerror (error_number);
2354   if (msg == NULL)
2355     msg = "unknown";
2356
2357   return msg;
2358 }
2359
2360 /**
2361  * signal (SIGPIPE, SIG_IGN);
2362  */
2363 void
2364 _dbus_disable_sigpipe (void)
2365 {
2366   signal (SIGPIPE, SIG_IGN);
2367 }
2368
2369 /**
2370  * Sets the file descriptor to be close
2371  * on exec. Should be called for all file
2372  * descriptors in D-Bus code.
2373  *
2374  * @param fd the file descriptor
2375  */
2376 void
2377 _dbus_fd_set_close_on_exec (int fd)
2378 {
2379   int val;
2380   
2381   val = fcntl (fd, F_GETFD, 0);
2382   
2383   if (val < 0)
2384     return;
2385
2386   val |= FD_CLOEXEC;
2387   
2388   fcntl (fd, F_SETFD, val);
2389 }
2390
2391 /**
2392  * Closes a file descriptor.
2393  *
2394  * @param fd the file descriptor
2395  * @param error error object
2396  * @returns #FALSE if error set
2397  */
2398 dbus_bool_t
2399 _dbus_close (int        fd,
2400              DBusError *error)
2401 {
2402   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2403   
2404  again:
2405   if (close (fd) < 0)
2406     {
2407       if (errno == EINTR)
2408         goto again;
2409
2410       dbus_set_error (error, _dbus_error_from_errno (errno),
2411                       "Could not close fd %d", fd);
2412       return FALSE;
2413     }
2414
2415   return TRUE;
2416 }
2417
2418 /**
2419  * Sets a file descriptor to be nonblocking.
2420  *
2421  * @param fd the file descriptor.
2422  * @param error address of error location.
2423  * @returns #TRUE on success.
2424  */
2425 dbus_bool_t
2426 _dbus_set_fd_nonblocking (int             fd,
2427                           DBusError      *error)
2428 {
2429   int val;
2430
2431   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2432   
2433   val = fcntl (fd, F_GETFL, 0);
2434   if (val < 0)
2435     {
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));
2441       return FALSE;
2442     }
2443
2444   if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
2445     {
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));
2451
2452       return FALSE;
2453     }
2454
2455   return TRUE;
2456 }
2457
2458 /**
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.
2462  */
2463 void
2464 _dbus_print_backtrace (void)
2465 {  
2466 #if defined (HAVE_BACKTRACE) && defined (DBUS_BUILT_R_DYNAMIC)
2467   void *bt[500];
2468   int bt_size;
2469   int i;
2470   char **syms;
2471   
2472   bt_size = backtrace (bt, 500);
2473
2474   syms = backtrace_symbols (bt, bt_size);
2475   
2476   i = 0;
2477   while (i < bt_size)
2478     {
2479       /* don't use dbus_warn since it can _dbus_abort() */
2480       fprintf (stderr, "  %s\n", syms[i]);
2481       ++i;
2482     }
2483   fflush (stderr);
2484
2485   free (syms);
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");
2488 #else
2489   fprintf (stderr, "  D-Bus not compiled with backtrace support so unable to print a backtrace\n");
2490 #endif
2491 }
2492
2493 /**
2494  * Creates a full-duplex pipe (as in socketpair()).
2495  * Sets both ends of the pipe nonblocking.
2496  *
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.
2501  * 
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)
2507  */
2508 dbus_bool_t
2509 _dbus_full_duplex_pipe (int        *fd1,
2510                         int        *fd2,
2511                         dbus_bool_t blocking,
2512                         DBusError  *error)
2513 {
2514 #ifdef HAVE_SOCKETPAIR
2515   int fds[2];
2516
2517   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2518   
2519   if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
2520     {
2521       dbus_set_error (error, _dbus_error_from_errno (errno),
2522                       "Could not create full-duplex pipe");
2523       return FALSE;
2524     }
2525
2526   if (!blocking &&
2527       (!_dbus_set_fd_nonblocking (fds[0], NULL) ||
2528        !_dbus_set_fd_nonblocking (fds[1], NULL)))
2529     {
2530       dbus_set_error (error, _dbus_error_from_errno (errno),
2531                       "Could not set full-duplex pipe nonblocking");
2532       
2533       _dbus_close (fds[0], NULL);
2534       _dbus_close (fds[1], NULL);
2535       
2536       return FALSE;
2537     }
2538   
2539   *fd1 = fds[0];
2540   *fd2 = fds[1];
2541
2542   _dbus_verbose ("full-duplex pipe %d <-> %d\n",
2543                  *fd1, *fd2);
2544   
2545   return TRUE;  
2546 #else
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");
2550   return FALSE;
2551 #endif
2552 }
2553
2554
2555 /**
2556  * Measure the length of the given format string and arguments,
2557  * not including the terminating nul.
2558  *
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
2562  */
2563 int
2564 _dbus_printf_string_upper_bound (const char *format,
2565                                  va_list     args)
2566 {
2567   char c;
2568   return vsnprintf (&c, 1, format, args);
2569 }
2570
2571 /**
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
2574  *
2575  * @returns location of temp directory
2576  */
2577 const char*
2578 _dbus_get_tmpdir(void)
2579 {
2580   static const char* tmpdir = NULL;
2581
2582   if (tmpdir == NULL)
2583     {
2584       /* TMPDIR is what glibc uses, then
2585        * glibc falls back to the P_tmpdir macro which
2586        * just expands to "/tmp"
2587        */
2588       if (tmpdir == NULL)
2589         tmpdir = getenv("TMPDIR");
2590
2591       /* These two env variables are probably
2592        * broken, but maybe some OS uses them?
2593        */
2594       if (tmpdir == NULL)
2595         tmpdir = getenv("TMP");
2596       if (tmpdir == NULL)
2597         tmpdir = getenv("TEMP");
2598
2599       /* And this is the sane fallback. */
2600       if (tmpdir == NULL)
2601         tmpdir = "/tmp";
2602     }
2603   
2604   _dbus_assert(tmpdir != NULL);
2605   
2606   return tmpdir;
2607 }
2608
2609 /**
2610  * Determines the address of the session bus by querying a
2611  * platform-specific method.
2612  *
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.
2616  *
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
2620  */
2621 dbus_bool_t
2622 _dbus_get_autolaunch_address (DBusString *address,
2623                               DBusError  *error)
2624 {
2625   static char *argv[6];
2626   int address_pipe[2] = { -1, -1 };
2627   int errors_pipe[2] = { -1, -1 };
2628   pid_t pid;
2629   int ret;
2630   int status;
2631   int orig_len;
2632   int i;
2633   DBusString uuid;
2634   dbus_bool_t retval;
2635   
2636   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2637   retval = FALSE;
2638
2639   _dbus_string_init (&uuid);
2640   
2641   if (!_dbus_get_local_machine_uuid_encoded (&uuid))
2642     {
2643       _DBUS_SET_OOM (error);
2644       goto out;
2645     }
2646   
2647   i = 0;
2648   argv[i] = "dbus-launch";
2649   ++i;
2650   argv[i] = "--autolaunch";
2651   ++i;
2652   argv[i] = _dbus_string_get_data (&uuid);
2653   ++i;
2654   argv[i] = "--binary-syntax";
2655   ++i;
2656   argv[i] = "--close-stderr";
2657   ++i;
2658   argv[i] = NULL;
2659   ++i;
2660
2661   _dbus_assert (i == _DBUS_N_ELEMENTS (argv));
2662   
2663   orig_len = _dbus_string_get_length (address);
2664   
2665 #define READ_END        0
2666 #define WRITE_END       1
2667   if (pipe (address_pipe) < 0)
2668     {
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));
2674       goto out;
2675     }
2676   if (pipe (errors_pipe) < 0)
2677     {
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));
2683       goto out;
2684     }
2685
2686   pid = fork ();
2687   if (pid < 0)
2688     {
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));
2694       goto out;
2695     }
2696
2697   if (pid == 0)
2698     {
2699       /* child process */
2700       int fd = open ("/dev/null", O_RDWR);
2701       if (fd == -1)
2702         /* huh?! can't open /dev/null? */
2703         _exit (1);
2704
2705       _dbus_verbose ("/dev/null fd %d opened\n", fd);
2706       
2707       /* set-up stdXXX */
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 */
2713
2714       if (dup2 (fd, 0) == -1)
2715         _exit (1);
2716       if (dup2 (address_pipe[WRITE_END], 1) == -1)
2717         _exit (1);
2718       if (dup2 (errors_pipe[WRITE_END], 2) == -1)
2719         _exit (1);
2720
2721       close (fd);
2722       close (address_pipe[WRITE_END]);
2723       close (errors_pipe[WRITE_END]);
2724
2725       execv (DBUS_BINDIR "/dbus-launch", argv);
2726
2727       /* failed, try searching PATH */
2728       execvp ("dbus-launch", argv);
2729
2730       /* still nothing, we failed */
2731       _exit (1);
2732     }
2733
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;
2739
2740   ret = 0;
2741   do 
2742     {
2743       ret = _dbus_read (address_pipe[READ_END], address, 1024);
2744     }
2745   while (ret > 0);
2746
2747   /* reap the child process to avoid it lingering as zombie */
2748   do
2749     {
2750       ret = waitpid (pid, &status, 0);
2751     }
2752   while (ret == -1 && errno == EINTR);
2753
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)
2758     {
2759       /* The process ended with error */
2760       DBusString error_message;
2761       _dbus_string_init (&error_message);
2762       ret = 0;
2763       do
2764         {
2765           ret = _dbus_read (errors_pipe[READ_END], &error_message, 1024);
2766         }
2767       while (ret > 0);
2768
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));
2774       else
2775         dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
2776                         "Failed to execute dbus-launch to autolaunch D-Bus session");
2777       goto out;
2778     }
2779
2780   retval = TRUE;
2781   
2782  out:
2783   if (retval)
2784     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2785   else
2786     _DBUS_ASSERT_ERROR_IS_SET (error);
2787
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]);
2796
2797   _dbus_string_free (&uuid);
2798   return retval;
2799 }
2800
2801 /**
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).
2805  *
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.
2813  *
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
2818  */
2819 dbus_bool_t
2820 _dbus_read_local_machine_uuid (DBusGUID   *machine_id,
2821                                dbus_bool_t create_if_not_found,
2822                                DBusError  *error)
2823 {
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);
2827 }
2828
2829 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
2830
2831
2832 /**
2833  * Returns the standard directories for a session bus to look for service 
2834  * activation files 
2835  *
2836  * On UNIX this should be the standard xdg freedesktop.org data directories:
2837  *
2838  * XDG_DATA_HOME=${XDG_DATA_HOME-$HOME/.local/share}
2839  * XDG_DATA_DIRS=${XDG_DATA_DIRS-/usr/local/share:/usr/share}
2840  *
2841  * and
2842  *
2843  * DBUS_DATADIR
2844  *
2845  * @param dirs the directory list we are returning
2846  * @returns #FALSE on OOM 
2847  */
2848
2849 dbus_bool_t 
2850 _dbus_get_standard_session_servicedirs (DBusList **dirs)
2851 {
2852   const char *xdg_data_home;
2853   const char *xdg_data_dirs;
2854   DBusString servicedir_path;
2855
2856   if (!_dbus_string_init (&servicedir_path))
2857     return FALSE;
2858
2859   xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
2860   xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
2861
2862   if (xdg_data_dirs != NULL)
2863     {
2864       if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
2865         goto oom;
2866
2867       if (!_dbus_string_append (&servicedir_path, ":"))
2868         goto oom;
2869     }
2870   else
2871     {
2872       if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
2873         goto oom;
2874     }
2875
2876   /* 
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 
2881    */
2882   if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR":"))
2883         goto oom;
2884
2885   if (xdg_data_home != NULL)
2886     {
2887       if (!_dbus_string_append (&servicedir_path, xdg_data_home))
2888         goto oom;
2889     }
2890   else
2891     {
2892       const DBusString *homedir;
2893       DBusString local_share;
2894
2895       if (!_dbus_homedir_from_current_process (&homedir))
2896         goto oom;
2897        
2898       if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
2899         goto oom;
2900
2901       _dbus_string_init_const (&local_share, "/.local/share");
2902       if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
2903         goto oom;
2904     }
2905
2906   if (!_dbus_split_paths_and_append (&servicedir_path, 
2907                                      DBUS_UNIX_STANDARD_SESSION_SERVICEDIR, 
2908                                      dirs))
2909     goto oom;
2910
2911   _dbus_string_free (&servicedir_path);  
2912   return TRUE;
2913
2914  oom:
2915   _dbus_string_free (&servicedir_path);
2916   return FALSE;
2917 }
2918
2919 /**
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.
2923  *
2924  * 
2925  */
2926 void
2927 _dbus_flush_caches (void)
2928 {
2929   _dbus_user_database_flush_system ();
2930 }
2931
2932 /* tests in dbus-sysdeps-util.c */