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 /* Forward declarations */
00045 
00047 typedef struct DBusString DBusString;
00048 
00050 typedef struct DBusList DBusList;
00051 
00058 /* The idea of this file is to encapsulate everywhere that we're
00059  * relying on external libc features, for ease of security
00060  * auditing. The idea is from vsftpd. This also gives us a chance to
00061  * make things more convenient to use, e.g.  by reading into a
00062  * DBusString. Operating system headers aren't intended to be used
00063  * outside of this file and a limited number of others (such as
00064  * dbus-memory.c)
00065  */
00066 
00067 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00068 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
00069   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00070 #define _DBUS_GNUC_NORETURN                         \
00071   __attribute__((__noreturn__))
00072 #else   /* !__GNUC__ */
00073 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
00074 #define _DBUS_GNUC_NORETURN
00075 #endif  /* !__GNUC__ */
00076 
00084 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00085 
00086 const char* _dbus_getenv (const char *varname);
00087 dbus_bool_t _dbus_setenv (const char *varname,
00088                           const char *value);
00089 
00091 typedef unsigned long dbus_pid_t;
00093 typedef unsigned long dbus_uid_t;
00095 typedef unsigned long dbus_gid_t;
00096 
00098 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00099 
00100 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00101 
00102 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00103 
00105 #define DBUS_PID_FORMAT "%lu"
00106 
00107 #define DBUS_UID_FORMAT "%lu"
00108 
00109 #define DBUS_GID_FORMAT "%lu"
00110 
00111 
00122 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
00123                                     DBusError        *error);
00124 dbus_bool_t _dbus_close_socket     (int               fd,
00125                                     DBusError        *error);
00126 int         _dbus_read_socket      (int               fd,
00127                                     DBusString       *buffer,
00128                                     int               count);
00129 int         _dbus_write_socket     (int               fd,
00130                                     const DBusString *buffer,
00131                                     int               start,
00132                                     int               len);
00133 int         _dbus_write_socket_two (int               fd,
00134                                     const DBusString *buffer1,
00135                                     int               start1,
00136                                     int               len1,
00137                                     const DBusString *buffer2,
00138                                     int               start2,
00139                                     int               len2);
00140 int _dbus_connect_tcp_socket  (const char     *host,
00141                                dbus_uint32_t   port,
00142                                DBusError      *error);
00143 int _dbus_listen_tcp_socket   (const char     *host,
00144                                dbus_uint32_t   port,
00145                                DBusError      *error);
00146 int _dbus_accept              (int             listen_fd);
00147 
00151 typedef struct
00152 {
00153   dbus_pid_t pid; 
00154   dbus_uid_t uid; 
00155   dbus_gid_t gid; 
00156 } DBusCredentials;
00157 
00158 /* FIXME these read/send credentials should get moved to sysdeps-unix.h,
00159  * or renamed to reflect what they mean cross-platform
00160  */
00161 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
00162                                                 DBusCredentials *credentials,
00163                                                 DBusError       *error);
00164 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
00165                                                 DBusError       *error);
00166 
00167 
00168 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
00169 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
00170 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
00171                                                     const DBusCredentials *provided_credentials);
00172 
00173 
00175 typedef struct DBusUserInfo  DBusUserInfo;
00177 typedef struct DBusGroupInfo DBusGroupInfo;
00178 
00182 struct DBusUserInfo
00183 {
00184   dbus_uid_t  uid;            
00185   dbus_gid_t  primary_gid;    
00186   dbus_gid_t *group_ids;      
00187   int         n_group_ids;    
00188   char       *username;       
00189   char       *homedir;        
00190 };
00191 
00195 struct DBusGroupInfo
00196 {
00197   dbus_gid_t  gid;            
00198   char       *groupname;      
00199 };
00200 
00201 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
00202                                       const DBusString *username,
00203                                       DBusError        *error);
00204 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
00205                                       dbus_uid_t        uid,
00206                                       DBusError        *error);
00207 void        _dbus_user_info_free     (DBusUserInfo     *info);
00208 
00209 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
00210                                        const DBusString *groupname,
00211                                        DBusError        *error);
00212 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
00213                                        dbus_gid_t        gid,
00214                                        DBusError        *error);
00215 void        _dbus_group_info_free     (DBusGroupInfo    *info);
00216 
00217 
00218 unsigned long _dbus_getpid (void);
00219 dbus_uid_t    _dbus_getuid (void);
00220 dbus_gid_t    _dbus_getgid (void);
00221 
00225 typedef struct DBusAtomic DBusAtomic;
00226 
00230 struct DBusAtomic
00231 {
00232   volatile dbus_int32_t value; 
00233 };
00234 
00235 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00236 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00237 
00239 #define _DBUS_POLLIN      0x0001
00240 
00241 #define _DBUS_POLLPRI     0x0002
00242 
00243 #define _DBUS_POLLOUT     0x0004
00244 
00245 #define _DBUS_POLLERR     0x0008
00246 
00247 #define _DBUS_POLLHUP     0x0010
00248 
00249 #define _DBUS_POLLNVAL    0x0020
00250 
00254 typedef struct
00255 {
00256   int fd;            
00257   short events;      
00258   short revents;     
00259 } DBusPollFD;
00260 
00261 int _dbus_poll (DBusPollFD *fds,
00262                 int         n_fds,
00263                 int         timeout_milliseconds);
00264 
00265 void _dbus_sleep_milliseconds (int milliseconds);
00266 
00267 void _dbus_get_current_time (long *tv_sec,
00268                              long *tv_usec);
00269 
00273 dbus_bool_t _dbus_file_exists         (const char       *file);
00274 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00275                                        const DBusString *filename,
00276                                        DBusError        *error);
00277 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00278                                        const DBusString *filename,
00279                                        DBusError        *error);
00280 
00281 dbus_bool_t _dbus_make_file_world_readable   (const DBusString *filename,
00282                                               DBusError *error);
00283 
00284 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00285                                               DBusError        *error);
00286 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00287                                               DBusError        *error);
00288 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00289                                               DBusError        *error);
00290 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00291                                               DBusError        *error);
00292 
00293 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00294                                        const DBusString *next_component);
00295 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00296                                        DBusString       *dirname);
00297 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00298 
00299 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
00300 
00302 typedef struct DBusDirIter DBusDirIter;
00303 
00304 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00305                                             DBusError        *error);
00306 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00307                                             DBusString       *filename,
00308                                             DBusError        *error);
00309 void         _dbus_directory_close         (DBusDirIter      *iter);
00310 
00311 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00312                                                     DBusError *error);
00313 
00314 void _dbus_fd_set_close_on_exec (int fd);
00315 
00316 const char* _dbus_get_tmpdir      (void);
00317 
00321 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00322                                                       int   n_bytes);
00323 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00324                                                 int         n_bytes);
00325 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00326                                                 int         n_bytes);
00327 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00328                                                 int         n_bytes);
00329 
00330 const char* _dbus_error_from_errno (int error_number);
00331 
00332 void _dbus_disable_sigpipe (void);
00333 
00334 
00335 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00336 
00337 int _dbus_printf_string_upper_bound (const char *format,
00338                                      va_list args);
00339 
00340 
00344 typedef struct
00345 {
00346   unsigned long mode;  
00347   unsigned long nlink; 
00348   dbus_uid_t    uid;   
00349   dbus_gid_t    gid;   
00350   unsigned long size;  
00351   unsigned long atime; 
00352   unsigned long mtime; 
00353   unsigned long ctime; 
00354 } DBusStat;
00355 
00356 dbus_bool_t _dbus_stat             (const DBusString *filename,
00357                                     DBusStat         *statbuf,
00358                                     DBusError        *error);
00359 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00360                                     int              *fd2,
00361                                     dbus_bool_t       blocking,
00362                                     DBusError        *error);
00363 
00364 void        _dbus_print_backtrace  (void);
00365 
00366 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00367                                    int               print_pid_fd,
00368                                    DBusError        *error);
00369 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00370                                    unsigned long     pid,
00371                                    DBusError        *error);
00372 dbus_bool_t _dbus_change_identity (unsigned long     uid,
00373                                    unsigned long     gid,
00374                                    DBusError        *error);
00375 
00377 typedef void (* DBusSignalHandler) (int sig);
00378 
00379 void _dbus_set_signal_handler (int               sig,
00380                                DBusSignalHandler handler);
00381 
00382 dbus_bool_t _dbus_user_at_console (const char *username,
00383                                    DBusError  *error);
00384 
00385 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00386  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00387  */
00388 #if !defined (DBUS_VA_COPY)
00389 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00390 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00391 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00392 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00393 #  else /* va_list is a pointer */
00394 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00395 #  endif /* va_list is a pointer */
00396 #endif /* !DBUS_VA_COPY */
00397 
00398 
00403 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00404     (((const char*)&(p))[(i)])
00405 
00410 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00411      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00412       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00413       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00414       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00415       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00416       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00417       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00418       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00419 
00420 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
00421                              dbus_uid_t        *uid);
00422 
00423 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
00424                                           DBusError *error);
00425 
00429 typedef union DBusGUID DBusGUID;
00430 
00431 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00432                                              dbus_bool_t       create_if_not_found,
00433                                              DBusError        *error);
00434 
00440 dbus_bool_t _dbus_threads_init_platform_specific (void);
00441 
00444 DBUS_END_DECLS
00445 
00446 #endif /* DBUS_SYSDEPS_H */

Generated on Tue Oct 14 03:25:36 2008 for D-Bus by  doxygen 1.5.1