2003-03-24 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  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 #ifndef DBUS_SYSDEPS_H
25 #define DBUS_SYSDEPS_H
26
27 #include <dbus/dbus-string.h>
28 #include <dbus/dbus-errors.h>
29
30 /* this is perhaps bogus, but strcmp() etc. are faster if we use the
31  * stuff straight out of string.h, so have this here for now.
32  */
33 #include <string.h>
34
35 /* and it would just be annoying to abstract this */
36 #include <errno.h>
37
38 DBUS_BEGIN_DECLS;
39
40 /* The idea of this file is to encapsulate everywhere that we're
41  * relying on external libc features, for ease of security
42  * auditing. The idea is from vsftpd. This also gives us a chance to
43  * make things more convenient to use, e.g.  by reading into a
44  * DBusString. Operating system headers aren't intended to be used
45  * outside of this file and a limited number of others (such as
46  * dbus-memory.c)
47  */
48
49 void _dbus_abort (void);
50
51 const char* _dbus_getenv (const char *varname);
52 dbus_bool_t _dbus_setenv (const char *varname,
53                           const char *value);
54
55 int _dbus_read      (int               fd,
56                      DBusString       *buffer,
57                      int               count);
58 int _dbus_write     (int               fd,
59                      const DBusString *buffer,
60                      int               start,
61                      int               len);
62 int _dbus_write_two (int               fd,
63                      const DBusString *buffer1,
64                      int               start1,
65                      int               len1,
66                      const DBusString *buffer2,
67                      int               start2,
68                      int               len2);
69
70 typedef struct
71 {
72   /* -1 if not available */
73   int pid;
74   int uid;
75   int gid;
76 } DBusCredentials;
77
78 int _dbus_connect_unix_socket (const char     *path,
79                                DBusError      *error);
80 int _dbus_listen_unix_socket  (const char     *path,
81                                DBusError      *error);
82 int _dbus_connect_tcp_socket  (const char     *host,
83                                dbus_uint32_t   port,
84                                DBusError      *error);
85 int _dbus_listen_tcp_socket   (const char     *host,
86                                dbus_uint32_t   port,
87                                DBusError      *error);
88 int _dbus_accept              (int             listen_fd);
89
90 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
91                                                 DBusCredentials *credentials,
92                                                 DBusError       *error);
93 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
94                                                 DBusError       *error);
95
96
97 dbus_bool_t _dbus_credentials_from_username        (const DBusString      *username,
98                                                     DBusCredentials       *credentials);
99 dbus_bool_t _dbus_credentials_from_user_id         (unsigned long          user_id,
100                                                     DBusCredentials       *credentials);
101 dbus_bool_t _dbus_credentials_from_uid_string      (const DBusString      *uid_str,
102                                                     DBusCredentials       *credentials);
103 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
104 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
105                                                     const DBusCredentials *provided_credentials);
106
107 dbus_bool_t _dbus_string_append_our_uid (DBusString *str);
108
109 dbus_bool_t _dbus_homedir_from_username          (const DBusString       *username,
110                                                   DBusString             *homedir);
111 dbus_bool_t _dbus_user_info_from_current_process (const DBusString      **username,
112                                                   const DBusString      **homedir,
113                                                   const DBusCredentials **credentials);
114
115 dbus_bool_t _dbus_get_group_id (const DBusString  *group_name,
116                                 unsigned long     *gid);
117 dbus_bool_t _dbus_get_groups   (unsigned long      uid,
118                                 unsigned long    **group_ids,
119                                 int               *n_group_ids);
120
121 typedef int dbus_atomic_t;
122
123 dbus_atomic_t _dbus_atomic_inc (dbus_atomic_t *atomic);
124 dbus_atomic_t _dbus_atomic_dec (dbus_atomic_t *atomic);
125
126 #define _DBUS_POLLIN      0x0001    /* There is data to read */
127 #define _DBUS_POLLPRI     0x0002    /* There is urgent data to read */
128 #define _DBUS_POLLOUT     0x0004    /* Writing now will not block */
129 #define _DBUS_POLLERR     0x0008    /* Error condition */
130 #define _DBUS_POLLHUP     0x0010    /* Hung up */
131 #define _DBUS_POLLNVAL    0x0020    /* Invalid request: fd not open */
132
133 typedef struct
134 {
135   int fd;
136   short events;
137   short revents;
138 } DBusPollFD;
139
140 int _dbus_poll (DBusPollFD *fds,
141                 int         n_fds,
142                 int         timeout_milliseconds);
143
144 void _dbus_sleep_milliseconds (int milliseconds);
145
146 void _dbus_get_current_time (long *tv_sec,
147                              long *tv_usec);
148
149
150 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
151                                        const DBusString *filename,
152                                        DBusError        *error);
153 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
154                                        const DBusString *filename,
155                                        DBusError        *error);
156
157 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
158                                               DBusError        *error);
159 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
160                                               DBusError        *error);
161 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
162                                               DBusError        *error);
163
164 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
165                                        const DBusString *next_component);
166
167 typedef struct DBusDirIter DBusDirIter;
168
169 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
170                                             DBusError        *error);
171 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
172                                             DBusString       *filename,
173                                             DBusError        *error);
174 void         _dbus_directory_close         (DBusDirIter      *iter);
175
176
177 dbus_bool_t _dbus_generate_random_bytes (DBusString *str,
178                                          int         n_bytes);
179
180 const char *_dbus_errno_to_string  (int errnum);
181 const char* _dbus_error_from_errno (int error_number);
182
183 typedef void (* DBusSpawnChildSetupFunc) (void *user_data);
184
185 dbus_bool_t _dbus_spawn_async (char                    **argv,
186                                DBusSpawnChildSetupFunc   child_setup,
187                                void                     *user_data,
188                                DBusError                *error);
189
190
191 void _dbus_disable_sigpipe (void);
192
193 void _dbus_fd_set_close_on_exec (int fd);
194
195 void _dbus_exit (int code);
196
197 typedef struct
198 {
199   unsigned long mode;
200   unsigned long nlink;
201   unsigned long uid;
202   unsigned long gid;
203   unsigned long size;
204   unsigned long atime;
205   unsigned long mtime;
206   unsigned long ctime;
207 } DBusStat;
208
209 dbus_bool_t _dbus_stat             (const DBusString *filename,
210                                     DBusStat         *statbuf,
211                                     DBusError        *error);
212 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
213                                     int              *fd2,
214                                     DBusError        *error);
215 dbus_bool_t _dbus_close            (int               fd,
216                                     DBusError        *error);
217
218 void        _dbus_print_backtrace  (void);
219
220 DBUS_END_DECLS;
221
222 #endif /* DBUS_SYSDEPS_H */