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 #include <stdarg.h>
00041 
00042 DBUS_BEGIN_DECLS
00043 
00044 /* The idea of this file is to encapsulate everywhere that we're
00045  * relying on external libc features, for ease of security
00046  * auditing. The idea is from vsftpd. This also gives us a chance to
00047  * make things more convenient to use, e.g.  by reading into a
00048  * DBusString. Operating system headers aren't intended to be used
00049  * outside of this file and a limited number of others (such as
00050  * dbus-memory.c)
00051  */
00052 
00053 typedef struct DBusString DBusString;
00054 
00055 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00056 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
00057   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00058 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )     \
00059   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
00060 #define _DBUS_GNUC_FORMAT( arg_idx )                \
00061   __attribute__((__format_arg__ (arg_idx)))
00062 #define _DBUS_GNUC_NORETURN                         \
00063   __attribute__((__noreturn__))
00064 #else   /* !__GNUC__ */
00065 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
00066 #define _DBUS_GNUC_SCANF( format_idx, arg_idx )
00067 #define _DBUS_GNUC_FORMAT( arg_idx )
00068 #define _DBUS_GNUC_NORETURN
00069 #endif  /* !__GNUC__ */
00070 
00071 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00072 
00073 const char* _dbus_getenv (const char *varname);
00074 dbus_bool_t _dbus_setenv (const char *varname,
00075                           const char *value);
00076 
00077 int _dbus_read      (int               fd,
00078                      DBusString       *buffer,
00079                      int               count);
00080 int _dbus_write     (int               fd,
00081                      const DBusString *buffer,
00082                      int               start,
00083                      int               len);
00084 int _dbus_write_two (int               fd,
00085                      const DBusString *buffer1,
00086                      int               start1,
00087                      int               len1,
00088                      const DBusString *buffer2,
00089                      int               start2,
00090                      int               len2);
00091 
00092 typedef unsigned long dbus_pid_t;
00093 typedef unsigned long dbus_uid_t;
00094 typedef unsigned long dbus_gid_t;
00095 
00096 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00097 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00098 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00099 
00100 #define DBUS_PID_FORMAT "%lu"
00101 #define DBUS_UID_FORMAT "%lu"
00102 #define DBUS_GID_FORMAT "%lu"
00103 
00107 typedef struct
00108 {
00109   dbus_pid_t pid; 
00110   dbus_uid_t uid; 
00111   dbus_gid_t gid; 
00112 } DBusCredentials;
00113 
00114 int _dbus_connect_unix_socket (const char     *path,
00115                                dbus_bool_t     abstract,
00116                                DBusError      *error);
00117 int _dbus_listen_unix_socket  (const char     *path,
00118                                dbus_bool_t     abstract,
00119                                DBusError      *error);
00120 int _dbus_connect_tcp_socket  (const char     *host,
00121                                dbus_uint32_t   port,
00122                                DBusError      *error);
00123 int _dbus_listen_tcp_socket   (const char     *host,
00124                                dbus_uint32_t   port,
00125                                DBusError      *error);
00126 int _dbus_accept              (int             listen_fd);
00127 
00128 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
00129                                                 DBusCredentials *credentials,
00130                                                 DBusError       *error);
00131 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
00132                                                 DBusError       *error);
00133 
00134 
00135 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
00136 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
00137 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
00138                                                     const DBusCredentials *provided_credentials);
00139 
00140 
00141 typedef struct DBusUserInfo  DBusUserInfo;
00142 typedef struct DBusGroupInfo DBusGroupInfo;
00143 
00147 struct DBusUserInfo
00148 {
00149   dbus_uid_t  uid;            
00150   dbus_gid_t  primary_gid;    
00151   dbus_gid_t *group_ids;      
00152   int         n_group_ids;    
00153   char       *username;       
00154   char       *homedir;        
00155 };
00156 
00160 struct DBusGroupInfo
00161 {
00162   dbus_gid_t  gid;            
00163   char       *groupname;      
00164 };
00165 
00166 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
00167                                       const DBusString *username,
00168                                       DBusError        *error);
00169 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
00170                                       dbus_uid_t        uid,
00171                                       DBusError        *error);
00172 void        _dbus_user_info_free     (DBusUserInfo     *info);
00173 
00174 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
00175                                        const DBusString *groupname,
00176                                        DBusError        *error);
00177 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
00178                                        dbus_gid_t        gid,
00179                                        DBusError        *error);
00180 void        _dbus_group_info_free     (DBusGroupInfo    *info);
00181 
00182 
00183 unsigned long _dbus_getpid (void);
00184 dbus_uid_t    _dbus_getuid (void);
00185 dbus_gid_t    _dbus_getgid (void);
00186 
00187 typedef struct DBusAtomic DBusAtomic;
00188 
00192 struct DBusAtomic
00193 {
00194   volatile dbus_int32_t value; 
00195 };
00196 
00197 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00198 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00199 
00200 #define _DBUS_POLLIN      0x0001    /* There is data to read */
00201 #define _DBUS_POLLPRI     0x0002    /* There is urgent data to read */
00202 #define _DBUS_POLLOUT     0x0004    /* Writing now will not block */
00203 #define _DBUS_POLLERR     0x0008    /* Error condition */
00204 #define _DBUS_POLLHUP     0x0010    /* Hung up */
00205 #define _DBUS_POLLNVAL    0x0020    /* Invalid request: fd not open */
00206 
00210 typedef struct
00211 {
00212   int fd;            
00213   short events;      
00214   short revents;     
00215 } DBusPollFD;
00216 
00217 int _dbus_poll (DBusPollFD *fds,
00218                 int         n_fds,
00219                 int         timeout_milliseconds);
00220 
00221 void _dbus_sleep_milliseconds (int milliseconds);
00222 
00223 void _dbus_get_current_time (long *tv_sec,
00224                              long *tv_usec);
00225 
00226 
00227 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00228                                        const DBusString *filename,
00229                                        DBusError        *error);
00230 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00231                                        const DBusString *filename,
00232                                        DBusError        *error);
00233 
00234 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00235                                               DBusError        *error);
00236 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00237                                               DBusError        *error);
00238 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00239                                               DBusError        *error);
00240 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00241                                               DBusError        *error);
00242 
00243 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00244                                        const DBusString *next_component);
00245 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00246                                        DBusString       *dirname);
00247 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00248 
00249 typedef struct DBusDirIter DBusDirIter;
00250 
00251 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00252                                             DBusError        *error);
00253 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00254                                             DBusString       *filename,
00255                                             DBusError        *error);
00256 void         _dbus_directory_close         (DBusDirIter      *iter);
00257 
00258 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00259                                                     DBusError *error);
00260 
00261 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00262                                                       int   n_bytes);
00263 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00264                                                 int         n_bytes);
00265 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00266                                                 int         n_bytes);
00267 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00268                                                 int         n_bytes);
00269 
00270 const char *_dbus_errno_to_string  (int errnum);
00271 const char* _dbus_error_from_errno (int error_number);
00272 
00273 void _dbus_disable_sigpipe (void);
00274 
00275 void _dbus_fd_set_close_on_exec (int fd);
00276 
00277 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00278 
00279 int _dbus_printf_string_upper_bound (const char *format,
00280                                      va_list args);
00281 
00282 
00286 typedef struct
00287 {
00288   unsigned long mode;  
00289   unsigned long nlink; 
00290   dbus_uid_t    uid;   
00291   dbus_gid_t    gid;   
00292   unsigned long size;  
00293   unsigned long atime; 
00294   unsigned long mtime; 
00295   unsigned long ctime; 
00296 } DBusStat;
00297 
00298 dbus_bool_t _dbus_stat             (const DBusString *filename,
00299                                     DBusStat         *statbuf,
00300                                     DBusError        *error);
00301 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00302                                     int              *fd2,
00303                                     dbus_bool_t       blocking,
00304                                     DBusError        *error);
00305 dbus_bool_t _dbus_close            (int               fd,
00306                                     DBusError        *error);
00307 
00308 void        _dbus_print_backtrace  (void);
00309 
00310 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00311                                    int               print_pid_fd,
00312                                    DBusError        *error);
00313 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00314                                    unsigned long     pid,
00315                                    DBusError        *error);
00316 dbus_bool_t _dbus_change_identity (unsigned long     uid,
00317                                    unsigned long     gid,
00318                                    DBusError        *error);
00319 
00320 typedef void (* DBusSignalHandler) (int sig);
00321 
00322 void _dbus_set_signal_handler (int               sig,
00323                                DBusSignalHandler handler);
00324 
00325 dbus_bool_t _dbus_file_exists     (const char *file);
00326 dbus_bool_t _dbus_user_at_console (const char *username,
00327                                    DBusError  *error);
00328 const char* _dbus_get_tmpdir      (void);
00329 
00330 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00331  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00332  */
00333 #if !defined (DBUS_VA_COPY)
00334 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00335 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00336 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00337 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00338 #  else /* va_list is a pointer */
00339 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00340 #  endif /* va_list is a pointer */
00341 #endif /* !DBUS_VA_COPY */
00342 
00343 /* On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
00344  * or b in an 80-bit register, thus failing to compare the two 64-bit
00345  * doubles for bitwise equality.
00346  */
00347 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00348     (((const char*)&(p))[(i)])
00349 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00350      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00351       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00352       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00353       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00354       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00355       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00356       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00357       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00358 
00359 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
00360                              dbus_uid_t        *uid);
00361 
00362 DBUS_END_DECLS
00363 
00364 #endif /* DBUS_SYSDEPS_H */

Generated on Wed Jan 3 04:58:29 2007 for D-Bus by  doxygen 1.4.7