2006-10-21 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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
37 /* and it would just be annoying to abstract this */
38 #include <errno.h>
39
40 #include <stdarg.h>
41
42 DBUS_BEGIN_DECLS
43
44 /**
45  * @addtogroup DBusSysdeps
46  *
47  * @{
48  */
49
50 /* The idea of this file is to encapsulate everywhere that we're
51  * relying on external libc features, for ease of security
52  * auditing. The idea is from vsftpd. This also gives us a chance to
53  * make things more convenient to use, e.g.  by reading into a
54  * DBusString. Operating system headers aren't intended to be used
55  * outside of this file and a limited number of others (such as
56  * dbus-memory.c)
57  */
58
59 /** An opaque string type */
60 typedef struct DBusString DBusString;
61
62 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
63 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
64   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
65 #define _DBUS_GNUC_NORETURN                         \
66   __attribute__((__noreturn__))
67 #else   /* !__GNUC__ */
68 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
69 #define _DBUS_GNUC_NORETURN
70 #endif  /* !__GNUC__ */
71
72 /** @def _DBUS_GNUC_PRINTF
73  * used to tell gcc about printf format strings
74  */
75 /** @def _DBUS_GNUC_NORETURN
76  * used to tell gcc about functions that never return, such as _dbus_abort()
77  */
78
79 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
80
81 const char* _dbus_getenv (const char *varname);
82 dbus_bool_t _dbus_setenv (const char *varname,
83                           const char *value);
84
85 /** A process ID */
86 typedef unsigned long dbus_pid_t;
87 /** A user ID */
88 typedef unsigned long dbus_uid_t;
89 /** A group ID */
90 typedef unsigned long dbus_gid_t;
91
92 /** an invalid PID used to represent an uninitialized dbus_pid_t field */
93 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
94 /** an invalid UID used to represent an uninitialized dbus_uid_t field */
95 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
96 /** an invalid GID used to represent an uninitialized dbus_gid_t field */
97 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
98
99 /** an appropriate printf format for dbus_pid_t */
100 #define DBUS_PID_FORMAT "%lu"
101 /** an appropriate printf format for dbus_uid_t */
102 #define DBUS_UID_FORMAT "%lu"
103 /** an appropriate printf format for dbus_gid_t */
104 #define DBUS_GID_FORMAT "%lu"
105
106
107 /**
108  * Socket interface
109  *
110  *  @todo Use for the file descriptors a struct
111  *           - struct DBusSocket{ int d; }; -
112  *        instead of int to get type-safety which 
113  *        will be checked by the compiler.
114  * 
115  */
116
117 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
118                                     DBusError        *error);
119 dbus_bool_t _dbus_close_socket     (int               fd,
120                                     DBusError        *error);
121 int         _dbus_read_socket      (int               fd,
122                                     DBusString       *buffer,
123                                     int               count);
124 int         _dbus_write_socket     (int               fd,
125                                     const DBusString *buffer,
126                                     int               start,
127                                     int               len);
128 int         _dbus_write_socket_two (int               fd,
129                                     const DBusString *buffer1,
130                                     int               start1,
131                                     int               len1,
132                                     const DBusString *buffer2,
133                                     int               start2,
134                                     int               len2);
135 int _dbus_connect_tcp_socket  (const char     *host,
136                                dbus_uint32_t   port,
137                                DBusError      *error);
138 int _dbus_listen_tcp_socket   (const char     *host,
139                                dbus_uint32_t   port,
140                                DBusError      *error);
141 int _dbus_accept              (int             listen_fd);
142
143 /**
144  * Struct representing socket credentials
145  */
146 typedef struct
147 {
148   dbus_pid_t pid; /**< process ID or DBUS_PID_UNSET */
149   dbus_uid_t uid; /**< user ID or DBUS_UID_UNSET */
150   dbus_gid_t gid; /**< group ID or DBUS_GID_UNSET */
151 } DBusCredentials;
152
153 /* FIXME these read/send credentials should get moved to sysdeps-unix.h,
154  * or renamed to reflect what they mean cross-platform
155  */
156 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
157                                                 DBusCredentials *credentials,
158                                                 DBusError       *error);
159 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
160                                                 DBusError       *error);
161
162
163 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
164 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
165 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
166                                                     const DBusCredentials *provided_credentials);
167
168
169 /** Information about a UNIX user */
170 typedef struct DBusUserInfo  DBusUserInfo;
171 /** Information about a UNIX group */
172 typedef struct DBusGroupInfo DBusGroupInfo;
173
174 /**
175  * Information about a UNIX user
176  */
177 struct DBusUserInfo
178 {
179   dbus_uid_t  uid;            /**< UID */
180   dbus_gid_t  primary_gid;    /**< GID */
181   dbus_gid_t *group_ids;      /**< Groups IDs, *including* above primary group */
182   int         n_group_ids;    /**< Size of group IDs array */
183   char       *username;       /**< Username */
184   char       *homedir;        /**< Home directory */
185 };
186
187 /**
188  * Information about a UNIX group
189  */
190 struct DBusGroupInfo
191 {
192   dbus_gid_t  gid;            /**< GID */
193   char       *groupname;      /**< Group name */
194 };
195
196 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
197                                       const DBusString *username,
198                                       DBusError        *error);
199 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
200                                       dbus_uid_t        uid,
201                                       DBusError        *error);
202 void        _dbus_user_info_free     (DBusUserInfo     *info);
203
204 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
205                                        const DBusString *groupname,
206                                        DBusError        *error);
207 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
208                                        dbus_gid_t        gid,
209                                        DBusError        *error);
210 void        _dbus_group_info_free     (DBusGroupInfo    *info);
211
212
213 unsigned long _dbus_getpid (void);
214 dbus_uid_t    _dbus_getuid (void);
215 dbus_gid_t    _dbus_getgid (void);
216
217 /** Opaque type representing an atomically-modifiable integer
218  * that can be used from multiple threads.
219  */
220 typedef struct DBusAtomic DBusAtomic;
221
222 /**
223  * An atomic integer safe to increment or decrement from multiple threads.
224  */
225 struct DBusAtomic
226 {
227   volatile dbus_int32_t value; /**< Value of the atomic integer. */
228 };
229
230 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
231 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
232
233 /** There is data to read */
234 #define _DBUS_POLLIN      0x0001
235 /** There is urgent data to read */
236 #define _DBUS_POLLPRI     0x0002
237 /** Writing now will not block */
238 #define _DBUS_POLLOUT     0x0004
239 /** Error condition */
240 #define _DBUS_POLLERR     0x0008
241 /** Hung up */
242 #define _DBUS_POLLHUP     0x0010
243 /** Invalid request: fd not open */
244 #define _DBUS_POLLNVAL    0x0020
245
246 /**
247  * A portable struct pollfd wrapper. 
248  */
249 typedef struct
250 {
251   int fd;            /**< File descriptor */
252   short events;      /**< Events to poll for */
253   short revents;     /**< Events that occurred */
254 } DBusPollFD;
255
256 int _dbus_poll (DBusPollFD *fds,
257                 int         n_fds,
258                 int         timeout_milliseconds);
259
260 void _dbus_sleep_milliseconds (int milliseconds);
261
262 void _dbus_get_current_time (long *tv_sec,
263                              long *tv_usec);
264
265 /**
266  * File/directory interface
267  */
268 dbus_bool_t _dbus_file_exists         (const char       *file);
269 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
270                                        const DBusString *filename,
271                                        DBusError        *error);
272 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
273                                        const DBusString *filename,
274                                        DBusError        *error);
275
276 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
277                                               DBusError        *error);
278 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
279                                               DBusError        *error);
280 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
281                                               DBusError        *error);
282 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
283                                               DBusError        *error);
284
285 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
286                                        const DBusString *next_component);
287 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
288                                        DBusString       *dirname);
289 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
290
291 /** Opaque type for reading a directory listing */
292 typedef struct DBusDirIter DBusDirIter;
293
294 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
295                                             DBusError        *error);
296 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
297                                             DBusString       *filename,
298                                             DBusError        *error);
299 void         _dbus_directory_close         (DBusDirIter      *iter);
300
301 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
302                                                     DBusError *error);
303
304 void _dbus_fd_set_close_on_exec (int fd);
305
306 const char* _dbus_get_tmpdir      (void);
307
308 /**
309  * Random numbers 
310  */
311 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
312                                                       int   n_bytes);
313 void        _dbus_generate_random_bytes_buffer (char       *buffer,
314                                                 int         n_bytes);
315 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
316                                                 int         n_bytes);
317 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
318                                                 int         n_bytes);
319
320 const char* _dbus_error_from_errno (int error_number);
321
322 void _dbus_disable_sigpipe (void);
323
324
325 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
326
327 int _dbus_printf_string_upper_bound (const char *format,
328                                      va_list args);
329
330
331 /**
332  * Portable struct with stat() results
333  */
334 typedef struct
335 {
336   unsigned long mode;  /**< File mode */
337   unsigned long nlink; /**< Number of hard links */
338   dbus_uid_t    uid;   /**< User owning file */
339   dbus_gid_t    gid;   /**< Group owning file */
340   unsigned long size;  /**< Size of file */
341   unsigned long atime; /**< Access time */
342   unsigned long mtime; /**< Modify time */
343   unsigned long ctime; /**< Creation time */
344 } DBusStat;
345
346 dbus_bool_t _dbus_stat             (const DBusString *filename,
347                                     DBusStat         *statbuf,
348                                     DBusError        *error);
349 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
350                                     int              *fd2,
351                                     dbus_bool_t       blocking,
352                                     DBusError        *error);
353
354 void        _dbus_print_backtrace  (void);
355
356 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
357                                    int               print_pid_fd,
358                                    DBusError        *error);
359 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
360                                    unsigned long     pid,
361                                    DBusError        *error);
362 dbus_bool_t _dbus_change_identity (unsigned long     uid,
363                                    unsigned long     gid,
364                                    DBusError        *error);
365
366 /** A UNIX signal handler */
367 typedef void (* DBusSignalHandler) (int sig);
368
369 void _dbus_set_signal_handler (int               sig,
370                                DBusSignalHandler handler);
371
372 dbus_bool_t _dbus_user_at_console (const char *username,
373                                    DBusError  *error);
374
375 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
376  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
377  */
378 #if !defined (DBUS_VA_COPY)
379 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
380 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
381 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
382 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
383 #  else /* va_list is a pointer */
384 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
385 #  endif /* va_list is a pointer */
386 #endif /* !DBUS_VA_COPY */
387
388
389 /**
390  * Casts a primitive C type to a byte array and then indexes
391  * a particular byte of the array.
392  */
393 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
394     (((const char*)&(p))[(i)])
395 /** On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
396  * or b in an 80-bit register, thus failing to compare the two 64-bit
397  * doubles for bitwise equality. So this macro compares the two doubles
398  * bitwise.
399  */
400 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
401      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
402       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
403       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
404       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
405       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
406       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
407       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
408       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
409
410 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
411                              dbus_uid_t        *uid);
412
413 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
414                                           DBusError *error);
415
416 /** Type representing a universally unique ID
417  * @todo rename to UUID instead of GUID
418  */
419 typedef union DBusGUID DBusGUID;
420
421 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
422                                              dbus_bool_t       create_if_not_found,
423                                              DBusError        *error);
424
425
426 /** @} */
427
428 DBUS_END_DECLS
429
430 #endif /* DBUS_SYSDEPS_H */