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

Generated on Sun Mar 21 03:52:06 2004 for D-BUS by doxygen 1.3.6-20040222