Merge branch 'atomic'
[platform/upstream/dbus.git] / dbus / dbus-sysdeps.h
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps.h Wrappers around system/libc features (internal to D-Bus implementation)
3  * 
4  * Copyright (C) 2002, 2003  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 #ifndef DBUS_SYSDEPS_H
26 #define DBUS_SYSDEPS_H
27
28 #include <config.h>
29
30 #include <dbus/dbus-errors.h>
31
32 /* this is perhaps bogus, but strcmp() etc. are faster if we use the
33  * stuff straight out of string.h, so have this here for now.
34  */
35 #include <string.h>
36 #include <stdarg.h>
37
38 DBUS_BEGIN_DECLS
39
40 #ifdef DBUS_WIN
41 #define _DBUS_PATH_SEPARATOR ";"
42 #else
43 #define _DBUS_PATH_SEPARATOR ":"
44 #endif
45
46 /* Forward declarations */
47
48 /** An opaque string type */
49 typedef struct DBusString DBusString;
50
51 /** An opaque list type */
52 typedef struct DBusList DBusList;
53
54 /** Object that contains a list of credentials such as UNIX or Windows user ID */
55 typedef struct DBusCredentials DBusCredentials;
56
57 /**
58  * @addtogroup DBusSysdeps
59  *
60  * @{
61  */
62
63 /* The idea of this file is to encapsulate everywhere that we're
64  * relying on external libc features, for ease of security
65  * auditing. The idea is from vsftpd. This also gives us a chance to
66  * make things more convenient to use, e.g.  by reading into a
67  * DBusString. Operating system headers aren't intended to be used
68  * outside of this file and a limited number of others (such as
69  * dbus-memory.c)
70  */
71
72 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
73 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
74   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
75 #define _DBUS_GNUC_NORETURN                         \
76   __attribute__((__noreturn__))
77 #else   /* !__GNUC__ */
78 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
79 #define _DBUS_GNUC_NORETURN
80 #endif  /* !__GNUC__ */
81
82 /** @def _DBUS_GNUC_PRINTF
83  * used to tell gcc about printf format strings
84  */
85 /** @def _DBUS_GNUC_NORETURN
86  * used to tell gcc about functions that never return, such as _dbus_abort()
87  */
88
89 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
90
91 const char* _dbus_getenv (const char *varname);
92 dbus_bool_t _dbus_setenv (const char *varname,
93                           const char *value);
94 dbus_bool_t _dbus_clearenv (void);
95
96 /** A process ID */
97 typedef unsigned long dbus_pid_t;
98 /** A user ID */
99 typedef unsigned long dbus_uid_t;
100 /** A group ID */
101 typedef unsigned long dbus_gid_t;
102
103 /** an invalid PID used to represent an uninitialized dbus_pid_t field */
104 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
105 /** an invalid UID used to represent an uninitialized dbus_uid_t field */
106 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
107 /** an invalid GID used to represent an uninitialized dbus_gid_t field */
108 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
109
110 /** an appropriate printf format for dbus_pid_t */
111 #define DBUS_PID_FORMAT "%lu"
112 /** an appropriate printf format for dbus_uid_t */
113 #define DBUS_UID_FORMAT "%lu"
114 /** an appropriate printf format for dbus_gid_t */
115 #define DBUS_GID_FORMAT "%lu"
116
117
118 /**
119  * Socket interface
120  *
121  *  @todo Use for the file descriptors a struct
122  *           - struct DBusSocket{ int d; }; -
123  *        instead of int to get type-safety which 
124  *        will be checked by the compiler.
125  * 
126  */
127
128 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
129                                     DBusError        *error);
130 dbus_bool_t _dbus_close_socket     (int               fd,
131                                     DBusError        *error);
132 int         _dbus_read_socket      (int               fd,
133                                     DBusString       *buffer,
134                                     int               count);
135 int         _dbus_write_socket     (int               fd,
136                                     const DBusString *buffer,
137                                     int               start,
138                                     int               len);
139 int         _dbus_write_socket_two (int               fd,
140                                     const DBusString *buffer1,
141                                     int               start1,
142                                     int               len1,
143                                     const DBusString *buffer2,
144                                     int               start2,
145                                     int               len2);
146 int _dbus_connect_tcp_socket  (const char     *host,
147                                const char     *port,
148                                const char     *family,
149                                DBusError      *error);
150 int _dbus_listen_tcp_socket   (const char     *host,
151                                const char     *port,
152                                const char     *family,
153                                DBusString     *retport,
154                                int           **fds_p,
155                                DBusError      *error);
156 int _dbus_accept              (int             listen_fd);
157
158
159 dbus_bool_t _dbus_read_credentials_socket (int               client_fd,
160                                            DBusCredentials  *credentials,
161                                            DBusError        *error);
162 dbus_bool_t _dbus_send_credentials_socket (int              server_fd,
163                                            DBusError       *error);
164
165 dbus_bool_t _dbus_credentials_add_from_user            (DBusCredentials  *credentials,
166                                                         const DBusString *username);
167 dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials  *credentials);
168 dbus_bool_t _dbus_append_user_from_current_process     (DBusString        *str);
169
170 dbus_bool_t _dbus_parse_unix_user_from_config   (const DBusString  *username,
171                                                  dbus_uid_t        *uid_p);
172 dbus_bool_t _dbus_parse_unix_group_from_config  (const DBusString  *groupname,
173                                                  dbus_gid_t        *gid_p);
174 dbus_bool_t _dbus_unix_groups_from_uid          (dbus_uid_t         uid,
175                                                  dbus_gid_t       **group_ids,
176                                                  int               *n_group_ids);
177 dbus_bool_t _dbus_unix_user_is_at_console       (dbus_uid_t         uid,
178                                                  DBusError         *error);
179 dbus_bool_t _dbus_unix_user_is_process_owner    (dbus_uid_t         uid);
180 dbus_bool_t _dbus_windows_user_is_process_owner (const char        *windows_sid);
181
182 dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
183                                                             DBusCredentials *credentials);
184
185 /** Opaque type representing an atomically-modifiable integer
186  * that can be used from multiple threads.
187  */
188 typedef struct DBusAtomic DBusAtomic;
189
190 /**
191  * An atomic integer safe to increment or decrement from multiple threads.
192  */
193 struct DBusAtomic
194 {
195 #ifdef DBUS_WIN
196   volatile long value; /**< Value of the atomic integer. */
197 #else
198   volatile dbus_int32_t value; /**< Value of the atomic integer. */
199 #endif
200 };
201
202 /* The value we get from autofoo is in the form of a cpp expression;
203  * convert that to a conventional defined/undef switch. (We can't get
204  * the conventional defined/undef because of multiarch builds only running
205  * ./configure once, on Darwin.) */
206 #if DBUS_HAVE_ATOMIC_INT_COND
207 #   define DBUS_HAVE_ATOMIC_INT 1
208 #else
209 #   undef DBUS_HAVE_ATOMIC_INT
210 #endif
211
212 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
213 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
214
215 /** There is data to read */
216 #define _DBUS_POLLIN      0x0001
217 /** There is urgent data to read */
218 #define _DBUS_POLLPRI     0x0002
219 /** Writing now will not block */
220 #define _DBUS_POLLOUT     0x0004
221 /** Error condition */
222 #define _DBUS_POLLERR     0x0008
223 /** Hung up */
224 #define _DBUS_POLLHUP     0x0010
225 /** Invalid request: fd not open */
226 #define _DBUS_POLLNVAL    0x0020
227
228 /**
229  * A portable struct pollfd wrapper. 
230  */
231 typedef struct
232 {
233   int fd;            /**< File descriptor */
234   short events;      /**< Events to poll for */
235   short revents;     /**< Events that occurred */
236 } DBusPollFD;
237
238 int _dbus_poll (DBusPollFD *fds,
239                 int         n_fds,
240                 int         timeout_milliseconds);
241
242 void _dbus_sleep_milliseconds (int milliseconds);
243
244 void _dbus_get_current_time (long *tv_sec,
245                              long *tv_usec);
246
247 /**
248  * File/directory interface
249  */
250 dbus_bool_t _dbus_file_exists         (const char       *file);
251 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
252                                        const DBusString *filename,
253                                        DBusError        *error);
254 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
255                                        const DBusString *filename,
256                                        DBusError        *error);
257
258 dbus_bool_t _dbus_make_file_world_readable   (const DBusString *filename,
259                                               DBusError *error);
260
261 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
262                                               DBusError        *error);
263 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
264                                               DBusError        *error);
265 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
266                                               DBusError        *error);
267 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
268                                               DBusError        *error);
269
270 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
271                                        const DBusString *next_component);
272 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
273                                        DBusString       *dirname);
274 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
275
276 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
277 dbus_bool_t _dbus_get_standard_system_servicedirs (DBusList **dirs);
278
279 dbus_bool_t _dbus_append_system_config_file  (DBusString *str);
280 dbus_bool_t _dbus_append_session_config_file (DBusString *str);
281
282 typedef struct {
283   int fd_or_handle;
284 } DBusPipe;
285
286 void        _dbus_pipe_init                (DBusPipe         *pipe,
287                                             int               fd);
288 void        _dbus_pipe_init_stdout         (DBusPipe         *pipe);
289 int         _dbus_pipe_write               (DBusPipe         *pipe,
290                                             const DBusString *buffer,
291                                             int               start,
292                                             int               len,
293                                             DBusError        *error);
294 int         _dbus_pipe_close               (DBusPipe         *pipe,
295                                             DBusError        *error);
296 dbus_bool_t _dbus_pipe_is_valid            (DBusPipe         *pipe);
297 void        _dbus_pipe_invalidate          (DBusPipe         *pipe);
298 dbus_bool_t _dbus_pipe_is_stdout_or_stderr (DBusPipe         *pipe);
299
300
301 /** Opaque type for reading a directory listing */
302 typedef struct DBusDirIter DBusDirIter;
303
304 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
305                                             DBusError        *error);
306 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
307                                             DBusString       *filename,
308                                             DBusError        *error);
309 void         _dbus_directory_close         (DBusDirIter      *iter);
310
311 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
312                                                     DBusError *error);
313
314 void _dbus_fd_set_close_on_exec (int fd);
315
316 const char* _dbus_get_tmpdir      (void);
317
318 /**
319  * Random numbers 
320  */
321 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
322                                                       int   n_bytes);
323 void        _dbus_generate_random_bytes_buffer (char       *buffer,
324                                                 int         n_bytes);
325 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
326                                                 int         n_bytes);
327 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
328                                                 int         n_bytes);
329
330 const char* _dbus_error_from_errno (int error_number);
331
332 void        _dbus_set_errno_to_zero                  (void);
333 dbus_bool_t _dbus_get_is_errno_nonzero               (void);
334 dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock (void);
335 dbus_bool_t _dbus_get_is_errno_enomem                (void);
336 dbus_bool_t _dbus_get_is_errno_eintr                 (void);
337 const char* _dbus_strerror_from_errno                (void);
338
339 void _dbus_disable_sigpipe (void);
340
341
342 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
343
344 int _dbus_printf_string_upper_bound (const char *format,
345                                      va_list args);
346
347
348 /**
349  * Portable struct with stat() results
350  */
351 typedef struct
352 {
353   unsigned long mode;  /**< File mode */
354   unsigned long nlink; /**< Number of hard links */
355   dbus_uid_t    uid;   /**< User owning file */
356   dbus_gid_t    gid;   /**< Group owning file */
357   unsigned long size;  /**< Size of file */
358   unsigned long atime; /**< Access time */
359   unsigned long mtime; /**< Modify time */
360   unsigned long ctime; /**< Creation time */
361 } DBusStat;
362
363 dbus_bool_t _dbus_stat             (const DBusString *filename,
364                                     DBusStat         *statbuf,
365                                     DBusError        *error);
366 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
367                                     int              *fd2,
368                                     dbus_bool_t       blocking,
369                                     DBusError        *error);
370
371 void        _dbus_print_backtrace  (void);
372
373 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
374                                    DBusPipe         *print_pid_pipe,
375                                    DBusError        *error);
376
377 dbus_bool_t _dbus_verify_daemon_user    (const char *user);
378 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
379                                          DBusError  *error);
380
381 dbus_bool_t _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
382                                               DBusPipe         *print_pid_pipe,
383                                               dbus_pid_t        pid_to_write,
384                                               DBusError        *error);
385
386 /** A UNIX signal handler */
387 typedef void (* DBusSignalHandler) (int sig);
388
389 void _dbus_set_signal_handler (int               sig,
390                                DBusSignalHandler handler);
391
392 dbus_bool_t _dbus_user_at_console (const char *username,
393                                    DBusError  *error);
394
395 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
396  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
397  */
398 #if !defined (DBUS_VA_COPY)
399 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
400 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
401 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
402 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
403 #  else /* va_list is a pointer */
404 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
405 #  endif /* va_list is a pointer */
406 #endif /* !DBUS_VA_COPY */
407
408
409 /**
410  * Casts a primitive C type to a byte array and then indexes
411  * a particular byte of the array.
412  */
413 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
414     (((const char*)&(p))[(i)])
415 /** On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
416  * or b in an 80-bit register, thus failing to compare the two 64-bit
417  * doubles for bitwise equality. So this macro compares the two doubles
418  * bitwise.
419  */
420 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
421      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
422       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
423       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
424       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
425       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
426       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
427       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
428       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
429
430 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
431                                           DBusError *error);
432
433 /** Type representing a universally unique ID
434  * @todo rename to UUID instead of GUID
435  */
436 typedef union DBusGUID DBusGUID;
437
438 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
439                                              dbus_bool_t       create_if_not_found,
440                                              DBusError        *error);
441
442 /**
443  * Initialize threads as in dbus_threads_init_default(), appropriately
444  * for the platform.
445  * @returns #FALSE if no memory
446  */
447 dbus_bool_t _dbus_threads_init_platform_specific (void);
448
449 dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs, 
450                                           const char *suffix, 
451                                           DBusList **dir_list);
452
453 unsigned long _dbus_pid_for_log (void);
454
455 /* FIXME move back to dbus-sysdeps-unix.h probably -
456  * the PID file handling just needs a little more abstraction
457  * in the bus daemon first.
458  */
459 dbus_pid_t    _dbus_getpid (void);
460
461 void _dbus_flush_caches (void);
462
463 /** @} */
464
465 DBUS_END_DECLS
466
467 #endif /* DBUS_SYSDEPS_H */