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

Generated on Thu Jan 29 16:46:36 2009 for D-BUS by  doxygen 1.4.6