a407482878bd72db27f885ef802b382056773578
[platform/upstream/dbus.git] / dbus / dbus-sysdeps.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
3  * 
4  * Copyright (C) 2002  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <pwd.h>
37 #include <time.h>
38 #ifdef HAVE_WRITEV
39 #include <sys/uio.h>
40 #endif
41 #ifdef HAVE_POLL
42 #include <sys/poll.h>
43 #endif
44
45 /**
46  * @addtogroup DBusInternalsUtils
47  * @{
48  */
49 /**
50  * Aborts the program with SIGABRT (dumping core).
51  */
52 void
53 _dbus_abort (void)
54 {
55   abort ();
56   _exit (1); /* in case someone manages to ignore SIGABRT */
57 }
58
59 /**
60  * Wrapper for getenv().
61  *
62  * @param varname name of environment variable
63  * @returns value of environment variable or #NULL if unset
64  */
65 const char*
66 _dbus_getenv (const char *varname)
67 {  
68   return getenv (varname);
69 }
70
71 /**
72  * Thin wrapper around the read() system call that appends
73  * the data it reads to the DBusString buffer. It appends
74  * up to the given count, and returns the same value
75  * and same errno as read(). The only exception is that
76  * _dbus_read() handles EINTR for you.
77  *
78  * @param fd the file descriptor to read from
79  * @param buffer the buffer to append data to
80  * @param count the amount of data to read
81  * @returns the number of bytes read or -1
82  */
83 int
84 _dbus_read (int               fd,
85             DBusString       *buffer,
86             int               count)
87 {
88   int bytes_read;
89   int start;
90   char *data;
91
92   _dbus_assert (count >= 0);
93   
94   start = _dbus_string_get_length (buffer);
95
96   if (!_dbus_string_lengthen (buffer, count))
97     {
98       errno = ENOMEM;
99       return -1;
100     }
101
102   _dbus_string_get_data_len (buffer, &data, start, count);
103
104  again:
105   
106   bytes_read = read (fd, data, count);
107
108   if (bytes_read < 0)
109     {
110       if (errno == EINTR)
111         goto again;
112       else
113         {
114           /* put length back (note that this doesn't actually realloc anything) */
115           _dbus_string_set_length (buffer, start);
116           return -1;
117         }
118     }
119   else
120     {
121       /* put length back (doesn't actually realloc) */
122       _dbus_string_set_length (buffer, start + bytes_read);
123
124 #if 0
125       if (bytes_read > 0)
126         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
127 #endif
128       
129       return bytes_read;
130     }
131 }
132
133 /**
134  * Thin wrapper around the write() system call that writes a part of a
135  * DBusString and handles EINTR for you.
136  * 
137  * @param fd the file descriptor to write
138  * @param buffer the buffer to write data from
139  * @param start the first byte in the buffer to write
140  * @param len the number of bytes to try to write
141  * @returns the number of bytes written or -1 on error
142  */
143 int
144 _dbus_write (int               fd,
145              const DBusString *buffer,
146              int               start,
147              int               len)
148 {
149   const char *data;
150   int bytes_written;
151   
152   _dbus_string_get_const_data_len (buffer, &data, start, len);
153   
154  again:
155
156   bytes_written = write (fd, data, len);
157
158   if (bytes_written < 0 && errno == EINTR)
159     goto again;
160
161 #if 0
162   if (bytes_written > 0)
163     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
164 #endif
165   
166   return bytes_written;
167 }
168
169 /**
170  * Like _dbus_write() but will use writev() if possible
171  * to write both buffers in sequence. The return value
172  * is the number of bytes written in the first buffer,
173  * plus the number written in the second. If the first
174  * buffer is written successfully and an error occurs
175  * writing the second, the number of bytes in the first
176  * is returned (i.e. the error is ignored), on systems that
177  * don't have writev. Handles EINTR for you.
178  * The second buffer may be #NULL.
179  *
180  * @param fd the file descriptor
181  * @param buffer1 first buffer
182  * @param start1 first byte to write in first buffer
183  * @param len1 number of bytes to write from first buffer
184  * @param buffer2 second buffer, or #NULL
185  * @param start2 first byte to write in second buffer
186  * @param len2 number of bytes to write in second buffer
187  * @returns total bytes written from both buffers, or -1 on error
188  */
189 int
190 _dbus_write_two (int               fd,
191                  const DBusString *buffer1,
192                  int               start1,
193                  int               len1,
194                  const DBusString *buffer2,
195                  int               start2,
196                  int               len2)
197 {
198   _dbus_assert (buffer1 != NULL);
199   _dbus_assert (start1 >= 0);
200   _dbus_assert (start2 >= 0);
201   _dbus_assert (len1 >= 0);
202   _dbus_assert (len2 >= 0);
203   
204 #ifdef HAVE_WRITEV
205   {
206     struct iovec vectors[2];
207     const char *data1;
208     const char *data2;
209     int bytes_written;
210
211     _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
212
213     if (buffer2 != NULL)
214       _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
215     else
216       {
217         data2 = NULL;
218         start2 = 0;
219         len2 = 0;
220       }
221    
222     vectors[0].iov_base = (char*) data1;
223     vectors[0].iov_len = len1;
224     vectors[1].iov_base = (char*) data2;
225     vectors[1].iov_len = len2;
226
227   again:
228    
229     bytes_written = writev (fd,
230                             vectors,
231                             data2 ? 2 : 1);
232
233     if (bytes_written < 0 && errno == EINTR)
234       goto again;
235    
236     return bytes_written;
237   }
238 #else /* HAVE_WRITEV */
239   {
240     int ret1;
241     
242     ret1 = _dbus_write (fd, buffer1, start1, len1);
243     if (ret1 == len1 && buffer2 != NULL)
244       {
245         ret2 = _dbus_write (fd, buffer2, start2, len2);
246         if (ret2 < 0)
247           ret2 = 0; /* we can't report an error as the first write was OK */
248        
249         return ret1 + ret2;
250       }
251     else
252       return ret1;
253   }
254 #endif /* !HAVE_WRITEV */   
255 }
256
257 /**
258  * Creates a socket and connects it to the UNIX domain socket at the
259  * given path.  The connection fd is returned, and is set up as
260  * nonblocking.
261  *
262  * @param path the path to UNIX domain socket
263  * @param result return location for error code
264  * @returns connection file descriptor or -1 on error
265  */
266 int
267 _dbus_connect_unix_socket (const char     *path,
268                            DBusResultCode *result)
269 {
270   int fd;
271   struct sockaddr_un addr;  
272   
273   fd = socket (AF_LOCAL, SOCK_STREAM, 0);
274
275   if (fd < 0)
276     {
277       dbus_set_result (result,
278                        _dbus_result_from_errno (errno));
279       
280       _dbus_verbose ("Failed to create socket: %s\n",
281                      _dbus_strerror (errno)); 
282       
283       return -1;
284     }
285
286   _DBUS_ZERO (addr);
287   addr.sun_family = AF_LOCAL;
288   strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
289   addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
290   
291   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
292     {      
293       dbus_set_result (result,
294                        _dbus_result_from_errno (errno));
295
296       _dbus_verbose ("Failed to connect to socket %s: %s\n",
297                      path, _dbus_strerror (errno));
298
299       close (fd);
300       fd = -1;
301       
302       return -1;
303     }
304
305   if (!_dbus_set_fd_nonblocking (fd, result))
306     {
307       close (fd);
308       fd = -1;
309
310       return -1;
311     }
312
313   return fd;
314 }
315
316 /**
317  * Creates a socket and binds it to the given path,
318  * then listens on the socket. The socket is
319  * set to be nonblocking. 
320  *
321  * @param path the socket name
322  * @param result return location for errors
323  * @returns the listening file descriptor or -1 on error
324  */
325 int
326 _dbus_listen_unix_socket (const char     *path,
327                           DBusResultCode *result)
328 {
329   int listen_fd;
330   struct sockaddr_un addr;
331
332   listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
333
334   if (listen_fd < 0)
335     {
336       dbus_set_result (result, _dbus_result_from_errno (errno));
337       _dbus_verbose ("Failed to create socket \"%s\": %s\n",
338                      path, _dbus_strerror (errno));
339       return -1;
340     }
341
342   _DBUS_ZERO (addr);
343   addr.sun_family = AF_LOCAL;
344   strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
345   addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
346   
347   if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
348     {
349       dbus_set_result (result, _dbus_result_from_errno (errno));
350       _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
351                      path, _dbus_strerror (errno));
352       close (listen_fd);
353       return -1;
354     }
355
356   if (listen (listen_fd, 30 /* backlog */) < 0)
357     {
358       dbus_set_result (result, _dbus_result_from_errno (errno));      
359       _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
360                      path, _dbus_strerror (errno));
361       close (listen_fd);
362       return -1;
363     }
364
365   if (!_dbus_set_fd_nonblocking (listen_fd, result))
366     {
367       close (listen_fd);
368       return -1;
369     }
370   
371   return listen_fd;
372 }
373
374 /* try to read a single byte and return #TRUE if we read it
375  * and it's equal to nul.
376  */
377 static dbus_bool_t
378 read_credentials_byte (int             client_fd,
379                        DBusResultCode *result)
380 {
381   char buf[1];
382   int bytes_read;
383
384  again:
385   bytes_read = read (client_fd, buf, 1);
386   if (bytes_read < 0)
387     {
388       if (errno == EINTR)
389         goto again;
390       else
391         {
392           dbus_set_result (result, _dbus_result_from_errno (errno));      
393           _dbus_verbose ("Failed to read credentials byte: %s\n",
394                          _dbus_strerror (errno));
395           return FALSE;
396         }
397     }
398   else if (bytes_read == 0)
399     {
400       dbus_set_result (result, DBUS_RESULT_IO_ERROR);
401       _dbus_verbose ("EOF reading credentials byte\n");
402       return FALSE;
403     }
404   else
405     {
406       _dbus_assert (bytes_read == 1);
407
408       if (buf[0] != '\0')
409         {
410           dbus_set_result (result, DBUS_RESULT_FAILED);
411           _dbus_verbose ("Credentials byte was not nul\n");
412           return FALSE;
413         }
414
415       _dbus_verbose ("read credentials byte\n");
416       
417       return TRUE;
418     }
419 }
420
421 static dbus_bool_t
422 write_credentials_byte (int             server_fd,
423                         DBusResultCode *result)
424 {
425   int bytes_written;
426   char buf[1] = { '\0' };
427   
428  again:
429
430   bytes_written = write (server_fd, buf, 1);
431
432   if (bytes_written < 0 && errno == EINTR)
433     goto again;
434
435   if (bytes_written < 0)
436     {
437       dbus_set_result (result, _dbus_result_from_errno (errno));      
438       _dbus_verbose ("Failed to write credentials byte: %s\n",
439                      _dbus_strerror (errno));
440       return FALSE;
441     }
442   else if (bytes_written == 0)
443     {
444       dbus_set_result (result, DBUS_RESULT_IO_ERROR);
445       _dbus_verbose ("wrote zero bytes writing credentials byte\n");
446       return FALSE;
447     }
448   else
449     {
450       _dbus_assert (bytes_written == 1);
451       _dbus_verbose ("wrote credentials byte\n");
452       return TRUE;
453     }
454 }
455
456 /**
457  * Reads a single byte which must be nul (an error occurs otherwise),
458  * and reads unix credentials if available. Fills in pid/uid/gid with
459  * -1 if no credentials are available. Return value indicates whether
460  * a byte was read, not whether we got valid credentials. On some
461  * systems, such as Linux, reading/writing the byte isn't actually
462  * required, but we do it anyway just to avoid multiple codepaths.
463  * 
464  * Fails if no byte is available, so you must select() first.
465  *
466  * The point of the byte is that on some systems we have to
467  * use sendmsg()/recvmsg() to transmit credentials.
468  *
469  * @param client_fd the client file descriptor
470  * @param credentials struct to fill with credentials of client
471  * @param result location to store result code
472  * @returns #TRUE on success
473  */
474 dbus_bool_t
475 _dbus_read_credentials_unix_socket  (int              client_fd,
476                                      DBusCredentials *credentials,
477                                      DBusResultCode  *result)
478 {
479   credentials->pid = -1;
480   credentials->uid = -1;
481   credentials->gid = -1;
482   
483 #ifdef SO_PEERCRED
484   if (read_credentials_byte (client_fd, result))
485     {
486       struct ucred cr;   
487       int cr_len = sizeof (cr);
488    
489       if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
490           cr_len == sizeof (cr))
491         {
492           credentials->pid = cr.pid;
493           credentials->uid = cr.uid;
494           credentials->gid = cr.gid;
495           _dbus_verbose ("Got credentials pid %d uid %d gid %d\n",
496                          credentials->pid,
497                          credentials->uid,
498                          credentials->gid);
499         }
500       else
501         {
502           _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
503                          cr_len, (int) sizeof (cr), _dbus_strerror (errno));
504         }
505
506       return TRUE;
507     }
508   else
509     return FALSE;
510 #else /* !SO_PEERCRED */
511   _dbus_verbose ("Socket credentials not supported on this OS\n");
512   return TRUE;
513 #endif
514 }
515
516 /**
517  * Sends a single nul byte with our UNIX credentials as ancillary
518  * data.  Returns #TRUE if the data was successfully written.  On
519  * systems that don't support sending credentials, just writes a byte,
520  * doesn't send any credentials.  On some systems, such as Linux,
521  * reading/writing the byte isn't actually required, but we do it
522  * anyway just to avoid multiple codepaths.
523  *
524  * Fails if no byte can be written, so you must select() first.
525  *
526  * The point of the byte is that on some systems we have to
527  * use sendmsg()/recvmsg() to transmit credentials.
528  *
529  * @param server_fd file descriptor for connection to server
530  * @param result return location for error code
531  * @returns #TRUE if the byte was sent
532  */
533 dbus_bool_t
534 _dbus_send_credentials_unix_socket  (int              server_fd,
535                                      DBusResultCode  *result)
536 {
537   if (write_credentials_byte (server_fd, result))
538     return TRUE;
539   else
540     return FALSE;
541 }
542
543 /**
544  * Accepts a connection on a listening socket.
545  * Handles EINTR for you.
546  *
547  * @param listen_fd the listen file descriptor
548  * @returns the connection fd of the client, or -1 on error
549  */
550 int
551 _dbus_accept  (int listen_fd)
552 {
553   int client_fd;
554   
555  retry:
556   client_fd = accept (listen_fd, NULL, NULL);
557   
558   if (client_fd < 0)
559     {
560       if (errno == EINTR)
561         goto retry;
562     }
563   
564   return client_fd;
565 }
566
567 /** @} */
568
569 /**
570  * @addtogroup DBusString
571  *
572  * @{
573  */
574 /**
575  * Appends an integer to a DBusString.
576  * 
577  * @param str the string
578  * @param value the integer value
579  * @returns #FALSE if not enough memory or other failure.
580  */
581 dbus_bool_t
582 _dbus_string_append_int (DBusString *str,
583                          long        value)
584 {
585   /* this calculation is from comp.lang.c faq */
586 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1)  /* +1 for '-' */
587   int orig_len;
588   int i;
589   char *buf;
590   
591   orig_len = _dbus_string_get_length (str);
592
593   if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
594     return FALSE;
595
596   _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
597
598   snprintf (buf, MAX_LONG_LEN, "%ld", value);
599
600   i = 0;
601   while (*buf)
602     {
603       ++buf;
604       ++i;
605     }
606   
607   _dbus_string_shorten (str, MAX_LONG_LEN - i);
608   
609   return TRUE;
610 }
611
612 /**
613  * Appends a double to a DBusString.
614  * 
615  * @param str the string
616  * @param value the floating point value
617  * @returns #FALSE if not enough memory or other failure.
618  */
619 dbus_bool_t
620 _dbus_string_append_double (DBusString *str,
621                             double      value)
622 {
623 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
624   int orig_len;
625   char *buf;
626   int i;
627   
628   orig_len = _dbus_string_get_length (str);
629
630   if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
631     return FALSE;
632
633   _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
634
635   snprintf (buf, MAX_LONG_LEN, "%g", value);
636
637   i = 0;
638   while (*buf)
639     {
640       ++buf;
641       ++i;
642     }
643   
644   _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
645   
646   return TRUE;
647 }
648
649 /**
650  * Parses an integer contained in a DBusString. Either return parameter
651  * may be #NULL if you aren't interested in it. The integer is parsed
652  * and stored in value_return. Return parameters are not initialized
653  * if the function returns #FALSE.
654  *
655  * @param str the string
656  * @param start the byte index of the start of the integer
657  * @param value_return return location of the integer value or #NULL
658  * @param end_return return location of the end of the integer, or #NULL
659  * @returns #TRUE on success
660  */
661 dbus_bool_t
662 _dbus_string_parse_int (const DBusString *str,
663                         int               start,
664                         long             *value_return,
665                         int              *end_return)
666 {
667   long v;
668   const char *p;
669   char *end;
670
671   _dbus_string_get_const_data_len (str, &p, start,
672                                    _dbus_string_get_length (str) - start);
673
674   end = NULL;
675   errno = 0;
676   v = strtol (p, &end, 0);
677   if (end == NULL || end == p || errno != 0)
678     return FALSE;
679
680   if (value_return)
681     *value_return = v;
682   if (end_return)
683     *end_return = (end - p);
684
685   return TRUE;
686 }
687
688 /**
689  * Parses a floating point number contained in a DBusString. Either
690  * return parameter may be #NULL if you aren't interested in it. The
691  * integer is parsed and stored in value_return. Return parameters are
692  * not initialized if the function returns #FALSE.
693  *
694  * @todo this function is currently locale-dependent. Should
695  * ask alexl to relicense g_ascii_strtod() code and put that in
696  * here instead, so it's locale-independent.
697  *
698  * @param str the string
699  * @param start the byte index of the start of the float
700  * @param value_return return location of the float value or #NULL
701  * @param end_return return location of the end of the float, or #NULL
702  * @returns #TRUE on success
703  */
704 dbus_bool_t
705 _dbus_string_parse_double (const DBusString *str,
706                            int               start,
707                            double           *value_return,
708                            int              *end_return)
709 {
710   double v;
711   const char *p;
712   char *end;
713
714   _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
715   
716   _dbus_string_get_const_data_len (str, &p, start,
717                                    _dbus_string_get_length (str) - start);
718
719   end = NULL;
720   errno = 0;
721   v = strtod (p, &end);
722   if (end == NULL || end == p || errno != 0)
723     return FALSE;
724
725   if (value_return)
726     *value_return = v;
727   if (end_return)
728     *end_return = (end - p);
729
730   return TRUE;
731 }
732
733 /**
734  * Gets the credentials corresponding to the given username.
735  *
736  * @param username the username
737  * @param credentials credentials to fill in
738  * @returns #TRUE if the username existed and we got some credentials
739  */
740 dbus_bool_t
741 _dbus_credentials_from_username (const DBusString *username,
742                                  DBusCredentials  *credentials)
743 {
744   const char *username_c_str;
745   
746   credentials->pid = -1;
747   credentials->uid = -1;
748   credentials->gid = -1;
749
750   _dbus_string_get_const_data (username, &username_c_str);
751   
752 #ifdef HAVE_GETPWNAM_R
753   {
754     struct passwd *p;
755     int result;
756     char buf[1024];
757     struct passwd p_str;
758
759     p = NULL;
760     result = getpwnam_r (username_c_str, &p_str, buf, sizeof (buf),
761                          &p);
762
763     if (result == 0 && p == &p_str)
764       {
765         credentials->uid = p->pw_uid;
766         credentials->gid = p->pw_gid;
767
768         _dbus_verbose ("Username %s has uid %d gid %d\n",
769                        username_c_str, credentials->uid, credentials->gid);
770         return TRUE;
771       }
772     else
773       {
774         _dbus_verbose ("User %s unknown\n", username_c_str);
775         return FALSE;
776       }
777   }
778 #else /* ! HAVE_GETPWNAM_R */
779   {
780     /* I guess we're screwed on thread safety here */
781     struct passwd *p;
782
783     p = getpwnam (username_c_str);
784
785     if (p != NULL)
786       {
787         credentials->uid = p->pw_uid;
788         credentials->gid = p->pw_gid;
789
790         _dbus_verbose ("Username %s has uid %d gid %d\n",
791                        username_c_str, credentials->uid, credentials->gid);
792         return TRUE;
793       }
794     else
795       {
796         _dbus_verbose ("User %s unknown\n", username_c_str);
797         return FALSE;
798       }
799   }
800 #endif  
801 }
802
803 /**
804  * Gets credentials from a UID string. (Parses a string to a UID
805  * and converts to a DBusCredentials.)
806  *
807  * @param uid_str the UID in string form
808  * @param credentials credentials to fill in
809  * @returns #TRUE if successfully filled in some credentials
810  */
811 dbus_bool_t
812 _dbus_credentials_from_uid_string (const DBusString      *uid_str,
813                                    DBusCredentials       *credentials)
814 {
815   int end;
816   long uid;
817
818   credentials->pid = -1;
819   credentials->uid = -1;
820   credentials->gid = -1;
821   
822   if (_dbus_string_get_length (uid_str) == 0)
823     {
824       _dbus_verbose ("UID string was zero length\n");
825       return FALSE;
826     }
827
828   uid = -1;
829   end = 0;
830   if (!_dbus_string_parse_int (uid_str, 0, &uid,
831                                &end))
832     {
833       _dbus_verbose ("could not parse string as a UID\n");
834       return FALSE;
835     }
836   
837   if (end != _dbus_string_get_length (uid_str))
838     {
839       _dbus_verbose ("string contained trailing stuff after UID\n");
840       return FALSE;
841     }
842
843   credentials->uid = uid;
844
845   return TRUE;
846 }
847
848 /**
849  * Gets the credentials of the current process.
850  *
851  * @param credentials credentials to fill in.
852  */
853 void
854 _dbus_credentials_from_current_process (DBusCredentials *credentials)
855 {
856   credentials->pid = getpid ();
857   credentials->uid = getuid ();
858   credentials->gid = getgid ();
859 }
860
861 /**
862  * Checks whether the provided_credentials are allowed to log in
863  * as the expected_credentials.
864  *
865  * @param expected_credentials credentials we're trying to log in as
866  * @param provided_credentials credentials we have
867  * @returns #TRUE if we can log in
868  */
869 dbus_bool_t
870 _dbus_credentials_match (const DBusCredentials *expected_credentials,
871                          const DBusCredentials *provided_credentials)
872 {
873   if (provided_credentials->uid < 0)
874     return FALSE;
875   else if (expected_credentials->uid < 0)
876     return FALSE;
877   else if (provided_credentials->uid == 0)
878     return TRUE;
879   else if (provided_credentials->uid == expected_credentials->uid)
880     return TRUE;
881   else
882     return FALSE;
883 }
884
885 /**
886  * Appends the uid of the current process to the given string.
887  *
888  * @param str the string to append to
889  * @returns #TRUE on success
890  */
891 dbus_bool_t
892 _dbus_string_append_our_uid (DBusString *str)
893 {
894   return _dbus_string_append_int (str, getuid ());
895 }
896
897
898 /**
899  * Wrapper for poll().
900  *
901  * @todo need a fallback implementation using select()
902  *
903  * @param fds the file descriptors to poll
904  * @param n_fds number of descriptors in the array
905  * @param timeout_milliseconds timeout or -1 for infinite
906  * @returns numbers of fds with revents, or <0 on error
907  */
908 int
909 _dbus_poll (DBusPollFD *fds,
910             int         n_fds,
911             int         timeout_milliseconds)
912 {
913 #ifdef HAVE_POLL
914   /* This big thing is a constant expression and should get optimized
915    * out of existence. So it's more robust than a configure check at
916    * no cost.
917    */
918   if (_DBUS_POLLIN == POLLIN &&
919       _DBUS_POLLPRI == POLLPRI &&
920       _DBUS_POLLOUT == POLLOUT &&
921       _DBUS_POLLERR == POLLERR &&
922       _DBUS_POLLHUP == POLLHUP &&
923       _DBUS_POLLNVAL == POLLNVAL &&
924       sizeof (DBusPollFD) == sizeof (struct pollfd) &&
925       _DBUS_STRUCT_OFFSET (DBusPollFD, fd) ==
926       _DBUS_STRUCT_OFFSET (struct pollfd, fd) &&
927       _DBUS_STRUCT_OFFSET (DBusPollFD, events) ==
928       _DBUS_STRUCT_OFFSET (struct pollfd, events) &&
929       _DBUS_STRUCT_OFFSET (DBusPollFD, revents) ==
930       _DBUS_STRUCT_OFFSET (struct pollfd, revents))
931     {
932       return poll ((struct pollfd*) fds,
933                    n_fds, 
934                    timeout_milliseconds);
935     }
936   else
937     {
938       /* We have to convert the DBusPollFD to an array of
939        * struct pollfd, poll, and convert back.
940        */
941       _dbus_warn ("didn't implement poll() properly for this system yet\n");
942       return -1;
943     }
944 #else /* ! HAVE_POLL */
945   _dbus_warn ("need to implement select() fallback for systems with no poll()\n");
946   return -1;
947 #endif
948 }
949
950 /** nanoseconds in a second */
951 #define NANOSECONDS_PER_SECOND       1000000000
952 /** microseconds in a second */
953 #define MICROSECONDS_PER_SECOND      1000000
954 /** milliseconds in a second */
955 #define MILLISECONDS_PER_SECOND      1000
956 /** nanoseconds in a millisecond */
957 #define NANOSECONDS_PER_MILLISECOND  1000000
958 /** microseconds in a millisecond */
959 #define MICROSECONDS_PER_MILLISECOND 1000
960
961 /**
962  * Sleeps the given number of milliseconds.
963  * @param milliseconds number of milliseconds
964  */
965 void
966 _dbus_sleep_milliseconds (int milliseconds)
967 {
968 #ifdef HAVE_NANOSLEEP
969   struct timespec req;
970   struct timespec rem;
971
972   req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
973   req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
974   rem.tv_sec = 0;
975   rem.tv_nsec = 0;
976
977   while (nanosleep (&req, &rem) < 0 && errno == EINTR)
978     req = rem;
979 #elif defined (HAVE_USLEEP)
980   usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
981 #else /* ! HAVE_USLEEP */
982   sleep (MAX (milliseconds / 1000, 1));
983 #endif
984 }
985
986 /** @} end of sysdeps */