8e7c5019a4990458e858837916d94afcdc7bdde5
[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 /* The idea of this file is to encapsulate everywhere that we're
45  * relying on external libc features, for ease of security
46  * auditing. The idea is from vsftpd. This also gives us a chance to
47  * make things more convenient to use, e.g.  by reading into a
48  * DBusString. Operating system headers aren't intended to be used
49  * outside of this file and a limited number of others (such as
50  * dbus-memory.c)
51  */
52
53 typedef struct DBusString DBusString;
54
55 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
56 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
57   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
58 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )     \
59   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
60 #define _DBUS_GNUC_FORMAT( arg_idx )                \
61   __attribute__((__format_arg__ (arg_idx)))
62 #define _DBUS_GNUC_NORETURN                         \
63   __attribute__((__noreturn__))
64 #else   /* !__GNUC__ */
65 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
66 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )
67 #define _DBUS_GNUC_FORMAT( arg_idx )
68 #define _DBUS_GNUC_NORETURN
69 #endif  /* !__GNUC__ */
70
71 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
72
73 const char* _dbus_getenv (const char *varname);
74 dbus_bool_t _dbus_setenv (const char *varname,
75                           const char *value);
76
77 int _dbus_read      (int               fd,
78                      DBusString       *buffer,
79                      int               count);
80 int _dbus_write     (int               fd,
81                      const DBusString *buffer,
82                      int               start,
83                      int               len);
84 int _dbus_write_two (int               fd,
85                      const DBusString *buffer1,
86                      int               start1,
87                      int               len1,
88                      const DBusString *buffer2,
89                      int               start2,
90                      int               len2);
91
92 typedef unsigned long dbus_pid_t;
93 typedef unsigned long dbus_uid_t;
94 typedef unsigned long dbus_gid_t;
95
96 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
97 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
98 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
99
100 #define DBUS_PID_FORMAT "%lu"
101 #define DBUS_UID_FORMAT "%lu"
102 #define DBUS_GID_FORMAT "%lu"
103
104 /**
105  * Struct representing socket credentials
106  */
107 typedef struct
108 {
109   dbus_pid_t pid; /**< process ID or DBUS_PID_UNSET */
110   dbus_uid_t uid; /**< user ID or DBUS_UID_UNSET */
111   dbus_gid_t gid; /**< group ID or DBUS_GID_UNSET */
112 } DBusCredentials;
113
114 int _dbus_connect_unix_socket (const char     *path,
115                                dbus_bool_t     abstract,
116                                DBusError      *error);
117 int _dbus_listen_unix_socket  (const char     *path,
118                                dbus_bool_t     abstract,
119                                DBusError      *error);
120 int _dbus_connect_tcp_socket  (const char     *host,
121                                dbus_uint32_t   port,
122                                DBusError      *error);
123 int _dbus_listen_tcp_socket   (const char     *host,
124                                dbus_uint32_t   port,
125                                DBusError      *error);
126 int _dbus_accept              (int             listen_fd);
127
128 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
129                                                 DBusCredentials *credentials,
130                                                 DBusError       *error);
131 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
132                                                 DBusError       *error);
133
134
135 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
136 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
137 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
138                                                     const DBusCredentials *provided_credentials);
139
140
141 typedef struct DBusUserInfo  DBusUserInfo;
142 typedef struct DBusGroupInfo DBusGroupInfo;
143
144 /**
145  * Information about a UNIX user
146  */
147 struct DBusUserInfo
148 {
149   dbus_uid_t  uid;            /**< UID */
150   dbus_gid_t  primary_gid;    /**< GID */
151   dbus_gid_t *group_ids;      /**< Groups IDs, *including* above primary group */
152   int         n_group_ids;    /**< Size of group IDs array */
153   char       *username;       /**< Username */
154   char       *homedir;        /**< Home directory */
155 };
156
157 /**
158  * Information about a UNIX group
159  */
160 struct DBusGroupInfo
161 {
162   dbus_gid_t  gid;            /**< GID */
163   char       *groupname;      /**< Group name */
164 };
165
166 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
167                                       const DBusString *username,
168                                       DBusError        *error);
169 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
170                                       dbus_uid_t        uid,
171                                       DBusError        *error);
172 void        _dbus_user_info_free     (DBusUserInfo     *info);
173
174 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
175                                        const DBusString *groupname,
176                                        DBusError        *error);
177 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
178                                        dbus_gid_t        gid,
179                                        DBusError        *error);
180 void        _dbus_group_info_free     (DBusGroupInfo    *info);
181
182
183 unsigned long _dbus_getpid (void);
184 dbus_uid_t    _dbus_getuid (void);
185 dbus_gid_t    _dbus_getgid (void);
186
187 typedef struct DBusAtomic DBusAtomic;
188
189 /**
190  * An atomic integer.
191  */
192 struct DBusAtomic
193 {
194   volatile dbus_int32_t value; /**< Value of the atomic integer. */
195 };
196
197 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
198 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
199
200 #define _DBUS_POLLIN      0x0001    /* There is data to read */
201 #define _DBUS_POLLPRI     0x0002    /* There is urgent data to read */
202 #define _DBUS_POLLOUT     0x0004    /* Writing now will not block */
203 #define _DBUS_POLLERR     0x0008    /* Error condition */
204 #define _DBUS_POLLHUP     0x0010    /* Hung up */
205 #define _DBUS_POLLNVAL    0x0020    /* Invalid request: fd not open */
206
207 /**
208  * A portable struct pollfd wrapper. 
209  */
210 typedef struct
211 {
212   int fd;            /**< File descriptor */
213   short events;      /**< Events to poll for */
214   short revents;     /**< Events that occurred */
215 } DBusPollFD;
216
217 int _dbus_poll (DBusPollFD *fds,
218                 int         n_fds,
219                 int         timeout_milliseconds);
220
221 void _dbus_sleep_milliseconds (int milliseconds);
222
223 void _dbus_get_current_time (long *tv_sec,
224                              long *tv_usec);
225
226
227 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
228                                        const DBusString *filename,
229                                        DBusError        *error);
230 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
231                                        const DBusString *filename,
232                                        DBusError        *error);
233
234 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
235                                               DBusError        *error);
236 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
237                                               DBusError        *error);
238 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
239                                               DBusError        *error);
240 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
241                                               DBusError        *error);
242
243 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
244                                        const DBusString *next_component);
245 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
246                                        DBusString       *dirname);
247 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
248
249 typedef struct DBusDirIter DBusDirIter;
250
251 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
252                                             DBusError        *error);
253 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
254                                             DBusString       *filename,
255                                             DBusError        *error);
256 void         _dbus_directory_close         (DBusDirIter      *iter);
257
258 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
259                                                     DBusError *error);
260
261 void        _dbus_generate_random_bytes_buffer (char       *buffer,
262                                                 int         n_bytes);
263 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
264                                                 int         n_bytes);
265 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
266                                                 int         n_bytes);
267
268 const char *_dbus_errno_to_string  (int errnum);
269 const char* _dbus_error_from_errno (int error_number);
270
271 void _dbus_disable_sigpipe (void);
272
273 void _dbus_fd_set_close_on_exec (int fd);
274
275 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
276
277 int _dbus_printf_string_upper_bound (const char *format,
278                                      va_list args);
279
280
281 /**
282  * Portable struct with stat() results
283  */
284 typedef struct
285 {
286   unsigned long mode;  /**< File mode */
287   unsigned long nlink; /**< Number of hard links */
288   dbus_uid_t    uid;   /**< User owning file */
289   dbus_gid_t    gid;   /**< Group owning file */
290   unsigned long size;  /**< Size of file */
291   unsigned long atime; /**< Access time */
292   unsigned long mtime; /**< Modify time */
293   unsigned long ctime; /**< Creation time */
294 } DBusStat;
295
296 dbus_bool_t _dbus_stat             (const DBusString *filename,
297                                     DBusStat         *statbuf,
298                                     DBusError        *error);
299 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
300                                     int              *fd2,
301                                     dbus_bool_t       blocking,
302                                     DBusError        *error);
303 dbus_bool_t _dbus_close            (int               fd,
304                                     DBusError        *error);
305
306 void        _dbus_print_backtrace  (void);
307
308 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
309                                    int               print_pid_fd,
310                                    DBusError        *error);
311 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
312                                    unsigned long     pid,
313                                    DBusError        *error);
314 dbus_bool_t _dbus_change_identity (unsigned long     uid,
315                                    unsigned long     gid,
316                                    DBusError        *error);
317
318 typedef void (* DBusSignalHandler) (int sig);
319
320 void _dbus_set_signal_handler (int               sig,
321                                DBusSignalHandler handler);
322
323 dbus_bool_t _dbus_file_exists     (const char *file);
324 dbus_bool_t _dbus_user_at_console (const char *username,
325                                    DBusError  *error);
326 char* _dbus_get_tmpdir();
327
328 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
329  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
330  */
331 #if !defined (DBUS_VA_COPY)
332 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
333 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
334 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
335 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
336 #  else /* va_list is a pointer */
337 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
338 #  endif /* va_list is a pointer */
339 #endif /* !DBUS_VA_COPY */
340
341 /* On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
342  * or b in an 80-bit register, thus failing to compare the two 64-bit
343  * doubles for bitwise equality.
344  */
345 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
346     (((const char*)&(p))[(i)])
347 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
348      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
349       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
350       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
351       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
352       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
353       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
354       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
355       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
356
357 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
358                              dbus_uid_t        *uid);
359
360 DBUS_END_DECLS
361
362 #endif /* DBUS_SYSDEPS_H */