WvStreams
dbus-sysdeps.h
00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-sysdeps.h Wrappers around system/libc features (internal to D-BUS implementation)
00003  * 
00004  * Copyright (C) 2002, 2003  Red Hat, Inc.
00005  * Copyright (C) 2003 CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 
00025 #ifndef DBUS_SYSDEPS_H
00026 #define DBUS_SYSDEPS_H
00027 
00028 #include "config.h"
00029 
00030 #include <dbus/dbus-errors.h>
00031 
00032 /* this is perhaps bogus, but strcmp() etc. are faster if we use the
00033  * stuff straight out of string.h, so have this here for now.
00034  */
00035 #include <string.h>
00036 
00037 /* and it would just be annoying to abstract this */
00038 #include <errno.h>
00039 
00040 DBUS_BEGIN_DECLS
00041 
00042 /* The idea of this file is to encapsulate everywhere that we're
00043  * relying on external libc features, for ease of security
00044  * auditing. The idea is from vsftpd. This also gives us a chance to
00045  * make things more convenient to use, e.g.  by reading into a
00046  * DBusString. Operating system headers aren't intended to be used
00047  * outside of this file and a limited number of others (such as
00048  * dbus-memory.c)
00049  */
00050 
00051 typedef struct DBusString DBusString;
00052 
00053 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00054 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
00055   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00056 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )     \
00057   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
00058 #define _DBUS_GNUC_FORMAT( arg_idx )                \
00059   __attribute__((__format_arg__ (arg_idx)))
00060 #define _DBUS_GNUC_NORETURN                         \
00061   __attribute__((__noreturn__))
00062 #else   /* !__GNUC__ */
00063 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
00064 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )
00065 #define _DBUS_GNUC_FORMAT( arg_idx )
00066 #define _DBUS_GNUC_NORETURN
00067 #endif  /* !__GNUC__ */
00068 
00069 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00070 
00071 const char* _dbus_getenv (const char *varname);
00072 dbus_bool_t _dbus_setenv (const char *varname,
00073                           const char *value);
00074 
00075 int _dbus_read      (int               fd,
00076                      DBusString       *buffer,
00077                      int               count);
00078 int _dbus_write     (int               fd,
00079                      const DBusString *buffer,
00080                      int               start,
00081                      int               len);
00082 int _dbus_write_two (int               fd,
00083                      const DBusString *buffer1,
00084                      int               start1,
00085                      int               len1,
00086                      const DBusString *buffer2,
00087                      int               start2,
00088                      int               len2);
00089 
00090 typedef unsigned long dbus_pid_t;
00091 typedef unsigned long dbus_uid_t;
00092 typedef unsigned long dbus_gid_t;
00093 
00094 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00095 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00096 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00097 
00098 #define DBUS_PID_FORMAT "%lu"
00099 #define DBUS_UID_FORMAT "%lu"
00100 #define DBUS_GID_FORMAT "%lu"
00101 
00105 typedef struct
00106 {
00107   dbus_pid_t pid; 
00108   dbus_uid_t uid; 
00109   dbus_gid_t gid; 
00110 } DBusCredentials;
00111 
00112 int _dbus_connect_unix_socket (const char     *path,
00113                                dbus_bool_t     abstract,
00114                                DBusError      *error);
00115 int _dbus_listen_unix_socket  (const char     *path,
00116                                dbus_bool_t     abstract,
00117                                DBusError      *error);
00118 int _dbus_connect_tcp_socket  (const char     *host,
00119                                dbus_uint32_t   port,
00120                                DBusError      *error);
00121 int _dbus_listen_tcp_socket   (const char     *host,
00122                                dbus_uint32_t   port,
00123                                DBusError      *error);
00124 int _dbus_accept              (int             listen_fd);
00125 
00126 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
00127                                                 DBusCredentials *credentials,
00128                                                 DBusError       *error);
00129 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
00130                                                 DBusError       *error);
00131 
00132 
00133 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
00134 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
00135 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
00136                                                     const DBusCredentials *provided_credentials);
00137 
00138 
00139 typedef struct DBusUserInfo  DBusUserInfo;
00140 typedef struct DBusGroupInfo DBusGroupInfo;
00141 
00145 struct DBusUserInfo
00146 {
00147   dbus_uid_t  uid;            
00148   dbus_gid_t  primary_gid;    
00149   dbus_gid_t *group_ids;      
00150   int         n_group_ids;    
00151   char       *username;       
00152   char       *homedir;        
00153 };
00154 
00158 struct DBusGroupInfo
00159 {
00160   dbus_gid_t  gid;            
00161   char       *groupname;      
00162 };
00163 
00164 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
00165                                       const DBusString *username,
00166                                       DBusError        *error);
00167 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
00168                                       dbus_uid_t        uid,
00169                                       DBusError        *error);
00170 void        _dbus_user_info_free     (DBusUserInfo     *info);
00171 
00172 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
00173                                        const DBusString *groupname,
00174                                        DBusError        *error);
00175 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
00176                                        dbus_gid_t        gid,
00177                                        DBusError        *error);
00178 void        _dbus_group_info_free     (DBusGroupInfo    *info);
00179 
00180 
00181 unsigned long _dbus_getpid (void);
00182 dbus_uid_t    _dbus_getuid (void);
00183 dbus_gid_t    _dbus_getgid (void);
00184 
00185 typedef struct DBusAtomic DBusAtomic;
00186 
00190 struct DBusAtomic
00191 {
00192   volatile dbus_int32_t value; 
00193 };
00194 
00195 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00196 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00197 
00198 #define _DBUS_POLLIN      0x0001    /* There is data to read */
00199 #define _DBUS_POLLPRI     0x0002    /* There is urgent data to read */
00200 #define _DBUS_POLLOUT     0x0004    /* Writing now will not block */
00201 #define _DBUS_POLLERR     0x0008    /* Error condition */
00202 #define _DBUS_POLLHUP     0x0010    /* Hung up */
00203 #define _DBUS_POLLNVAL    0x0020    /* Invalid request: fd not open */
00204 
00208 typedef struct
00209 {
00210   int fd;            
00211   short events;      
00212   short revents;     
00213 } DBusPollFD;
00214 
00215 int _dbus_poll (DBusPollFD *fds,
00216                 int         n_fds,
00217                 int         timeout_milliseconds);
00218 
00219 void _dbus_sleep_milliseconds (int milliseconds);
00220 
00221 void _dbus_get_current_time (long *tv_sec,
00222                              long *tv_usec);
00223 
00224 
00225 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00226                                        const DBusString *filename,
00227                                        DBusError        *error);
00228 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00229                                        const DBusString *filename,
00230                                        DBusError        *error);
00231 
00232 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00233                                               DBusError        *error);
00234 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00235                                               DBusError        *error);
00236 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00237                                               DBusError        *error);
00238 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00239                                               DBusError        *error);
00240 
00241 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00242                                        const DBusString *next_component);
00243 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00244                                        DBusString       *dirname);
00245 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00246 
00247 typedef struct DBusDirIter DBusDirIter;
00248 
00249 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00250                                             DBusError        *error);
00251 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00252                                             DBusString       *filename,
00253                                             DBusError        *error);
00254 void         _dbus_directory_close         (DBusDirIter      *iter);
00255 
00256 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00257                                                     DBusError *error);
00258 
00259 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00260                                                 int         n_bytes);
00261 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00262                                                 int         n_bytes);
00263 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00264                                                 int         n_bytes);
00265 
00266 const char *_dbus_errno_to_string  (int errnum);
00267 const char* _dbus_error_from_errno (int error_number);
00268 
00269 void _dbus_disable_sigpipe (void);
00270 
00271 void _dbus_fd_set_close_on_exec (int fd);
00272 
00273 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00274 
00278 typedef struct
00279 {
00280   unsigned long mode;  
00281   unsigned long nlink; 
00282   dbus_uid_t    uid;   
00283   dbus_gid_t    gid;   
00284   unsigned long size;  
00285   unsigned long atime; 
00286   unsigned long mtime; 
00287   unsigned long ctime; 
00288 } DBusStat;
00289 
00290 dbus_bool_t _dbus_stat             (const DBusString *filename,
00291                                     DBusStat         *statbuf,
00292                                     DBusError        *error);
00293 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00294                                     int              *fd2,
00295                                     dbus_bool_t       blocking,
00296                                     DBusError        *error);
00297 dbus_bool_t _dbus_close            (int               fd,
00298                                     DBusError        *error);
00299 
00300 void        _dbus_print_backtrace  (void);
00301 
00302 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00303                                    int               print_pid_fd,
00304                                    DBusError        *error);
00305 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00306                                    unsigned long     pid,
00307                                    DBusError        *error);
00308 dbus_bool_t _dbus_change_identity (unsigned long     uid,
00309                                    unsigned long     gid,
00310                                    DBusError        *error);
00311 
00312 typedef void (* DBusSignalHandler) (int sig);
00313 
00314 void _dbus_set_signal_handler (int               sig,
00315                                DBusSignalHandler handler);
00316 
00317 dbus_bool_t _dbus_file_exists     (const char *file);
00318 dbus_bool_t _dbus_user_at_console (const char *username,
00319                                    DBusError  *error);
00320 
00321 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00322  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00323  */
00324 #if !defined (DBUS_VA_COPY)
00325 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00326 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00327 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00328 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00329 #  else /* va_list is a pointer */
00330 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00331 #  endif /* va_list is a pointer */
00332 #endif /* !DBUS_VA_COPY */
00333 
00334 /* On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
00335  * or b in an 80-bit register, thus failing to compare the two 64-bit
00336  * doubles for bitwise equality.
00337  */
00338 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00339     (((const char*)&(p))[(i)])
00340 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00341      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00342       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00343       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00344       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00345       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00346       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00347       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00348       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00349 
00350 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
00351                              dbus_uid_t        *uid);
00352 
00353 DBUS_END_DECLS
00354 
00355 #endif /* DBUS_SYSDEPS_H */