Main Page | Modules | Data Structures | Directories | File List | Data Fields | Related Pages

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 
00102 #define DBUS_CONSOLE_DIR "/var/run/console/"
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 
00259 dbus_bool_t _dbus_generate_random_bytes (DBusString *str,
00260                                          int         n_bytes);
00261 dbus_bool_t _dbus_generate_random_ascii (DBusString *str,
00262                                          int         n_bytes);
00263 
00264 const char *_dbus_errno_to_string  (int errnum);
00265 const char* _dbus_error_from_errno (int error_number);
00266 
00267 void _dbus_disable_sigpipe (void);
00268 
00269 void _dbus_fd_set_close_on_exec (int fd);
00270 
00271 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00272 
00276 typedef struct
00277 {
00278   unsigned long mode;  
00279   unsigned long nlink; 
00280   dbus_uid_t    uid;   
00281   dbus_gid_t    gid;   
00282   unsigned long size;  
00283   unsigned long atime; 
00284   unsigned long mtime; 
00285   unsigned long ctime; 
00286 } DBusStat;
00287 
00288 dbus_bool_t _dbus_stat             (const DBusString *filename,
00289                                     DBusStat         *statbuf,
00290                                     DBusError        *error);
00291 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00292                                     int              *fd2,
00293                                     dbus_bool_t       blocking,
00294                                     DBusError        *error);
00295 dbus_bool_t _dbus_close            (int               fd,
00296                                     DBusError        *error);
00297 
00298 void        _dbus_print_backtrace  (void);
00299 
00300 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00301                                    int               print_pid_fd,
00302                                    DBusError        *error);
00303 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00304                                    unsigned long     pid,
00305                                    DBusError        *error);
00306 dbus_bool_t _dbus_change_identity (unsigned long     uid,
00307                                    unsigned long     gid,
00308                                    DBusError        *error);
00309 
00310 typedef void (* DBusSignalHandler) (int sig);
00311 
00312 void _dbus_set_signal_handler (int               sig,
00313                                DBusSignalHandler handler);
00314 
00315 dbus_bool_t _dbus_file_exists     (const char *file);
00316 dbus_bool_t _dbus_user_at_console (const char *username,
00317                                    DBusError  *error);
00318 
00319 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00320  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00321  */
00322 #if !defined (DBUS_VA_COPY)
00323 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00324 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00325 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00326 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00327 #  else /* va_list is a pointer */
00328 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00329 #  endif /* va_list is a pointer */
00330 #endif /* !DBUS_VA_COPY */
00331 
00332 /* On x86 there is an 80-bit FPU, and if you do "a == b" it may have a
00333  * or b in an 80-bit register, thus failing to compare the two 64-bit
00334  * doubles for bitwise equality.
00335  */
00336 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00337     (((const char*)&(p))[(i)])
00338 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00339      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00340       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00341       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00342       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00343       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00344       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00345       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00346       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00347 
00348 DBUS_END_DECLS
00349 
00350 #endif /* DBUS_SYSDEPS_H */

Generated on Wed Mar 30 21:15:22 2005 for D-BUS by  doxygen 1.4.1