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 dbus_bool_t _dbus_check_setuid (void);
00087 const char* _dbus_getenv (const char *varname);
00088 dbus_bool_t _dbus_setenv (const char *varname,
00089                           const char *value);
00090 
00092 typedef unsigned long dbus_pid_t;
00094 typedef unsigned long dbus_uid_t;
00096 typedef unsigned long dbus_gid_t;
00097 
00099 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00100 
00101 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00102 
00103 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00104 
00106 #define DBUS_PID_FORMAT "%lu"
00107 
00108 #define DBUS_UID_FORMAT "%lu"
00109 
00110 #define DBUS_GID_FORMAT "%lu"
00111 
00112 
00123 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
00124                                     DBusError        *error);
00125 dbus_bool_t _dbus_close_socket     (int               fd,
00126                                     DBusError        *error);
00127 int         _dbus_read_socket      (int               fd,
00128                                     DBusString       *buffer,
00129                                     int               count);
00130 int         _dbus_write_socket     (int               fd,
00131                                     const DBusString *buffer,
00132                                     int               start,
00133                                     int               len);
00134 int         _dbus_write_socket_two (int               fd,
00135                                     const DBusString *buffer1,
00136                                     int               start1,
00137                                     int               len1,
00138                                     const DBusString *buffer2,
00139                                     int               start2,
00140                                     int               len2);
00141 int _dbus_connect_tcp_socket  (const char     *host,
00142                                dbus_uint32_t   port,
00143                                DBusError      *error);
00144 int _dbus_listen_tcp_socket   (const char     *host,
00145                                dbus_uint32_t   port,
00146                                DBusError      *error);
00147 int _dbus_accept              (int             listen_fd);
00148 
00152 typedef struct
00153 {
00154   dbus_pid_t pid; 
00155   dbus_uid_t uid; 
00156   dbus_gid_t gid; 
00157 } DBusCredentials;
00158 
00159 /* FIXME these read/send credentials should get moved to sysdeps-unix.h,
00160  * or renamed to reflect what they mean cross-platform
00161  */
00162 dbus_bool_t _dbus_read_credentials_unix_socket (int              client_fd,
00163                                                 DBusCredentials *credentials,
00164                                                 DBusError       *error);
00165 dbus_bool_t _dbus_send_credentials_unix_socket (int              server_fd,
00166                                                 DBusError       *error);
00167 
00168 
00169 void        _dbus_credentials_clear                (DBusCredentials       *credentials);
00170 void        _dbus_credentials_from_current_process (DBusCredentials       *credentials);
00171 dbus_bool_t _dbus_credentials_match                (const DBusCredentials *expected_credentials,
00172                                                     const DBusCredentials *provided_credentials);
00173 
00174 
00176 typedef struct DBusUserInfo  DBusUserInfo;
00178 typedef struct DBusGroupInfo DBusGroupInfo;
00179 
00183 struct DBusUserInfo
00184 {
00185   dbus_uid_t  uid;            
00186   dbus_gid_t  primary_gid;    
00187   dbus_gid_t *group_ids;      
00188   int         n_group_ids;    
00189   char       *username;       
00190   char       *homedir;        
00191 };
00192 
00196 struct DBusGroupInfo
00197 {
00198   dbus_gid_t  gid;            
00199   char       *groupname;      
00200 };
00201 
00202 dbus_bool_t _dbus_user_info_fill     (DBusUserInfo     *info,
00203                                       const DBusString *username,
00204                                       DBusError        *error);
00205 dbus_bool_t _dbus_user_info_fill_uid (DBusUserInfo     *info,
00206                                       dbus_uid_t        uid,
00207                                       DBusError        *error);
00208 void        _dbus_user_info_free     (DBusUserInfo     *info);
00209 
00210 dbus_bool_t _dbus_group_info_fill     (DBusGroupInfo    *info,
00211                                        const DBusString *groupname,
00212                                        DBusError        *error);
00213 dbus_bool_t _dbus_group_info_fill_gid (DBusGroupInfo    *info,
00214                                        dbus_gid_t        gid,
00215                                        DBusError        *error);
00216 void        _dbus_group_info_free     (DBusGroupInfo    *info);
00217 
00218 
00219 unsigned long _dbus_getpid (void);
00220 dbus_uid_t    _dbus_getuid (void);
00221 dbus_gid_t    _dbus_getgid (void);
00222 
00226 typedef struct DBusAtomic DBusAtomic;
00227 
00231 struct DBusAtomic
00232 {
00233   volatile dbus_int32_t value; 
00234 };
00235 
00236 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00237 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00238 
00240 #define _DBUS_POLLIN      0x0001
00241 
00242 #define _DBUS_POLLPRI     0x0002
00243 
00244 #define _DBUS_POLLOUT     0x0004
00245 
00246 #define _DBUS_POLLERR     0x0008
00247 
00248 #define _DBUS_POLLHUP     0x0010
00249 
00250 #define _DBUS_POLLNVAL    0x0020
00251 
00255 typedef struct
00256 {
00257   int fd;            
00258   short events;      
00259   short revents;     
00260 } DBusPollFD;
00261 
00262 int _dbus_poll (DBusPollFD *fds,
00263                 int         n_fds,
00264                 int         timeout_milliseconds);
00265 
00266 void _dbus_sleep_milliseconds (int milliseconds);
00267 
00268 void _dbus_get_current_time (long *tv_sec,
00269                              long *tv_usec);
00270 
00274 dbus_bool_t _dbus_file_exists         (const char       *file);
00275 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00276                                        const DBusString *filename,
00277                                        DBusError        *error);
00278 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00279                                        const DBusString *filename,
00280                                        DBusError        *error);
00281 
00282 dbus_bool_t _dbus_make_file_world_readable   (const DBusString *filename,
00283                                               DBusError *error);
00284 
00285 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00286                                               DBusError        *error);
00287 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00288                                               DBusError        *error);
00289 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00290                                               DBusError        *error);
00291 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00292                                               DBusError        *error);
00293 
00294 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00295                                        const DBusString *next_component);
00296 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00297                                        DBusString       *dirname);
00298 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00299 
00300 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
00301 
00303 typedef struct DBusDirIter DBusDirIter;
00304 
00305 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00306                                             DBusError        *error);
00307 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00308                                             DBusString       *filename,
00309                                             DBusError        *error);
00310 void         _dbus_directory_close         (DBusDirIter      *iter);
00311 
00312 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00313                                                     DBusError *error);
00314 
00315 void _dbus_fd_set_close_on_exec (int fd);
00316 
00317 const char* _dbus_get_tmpdir      (void);
00318 
00322 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00323                                                       int   n_bytes);
00324 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00325                                                 int         n_bytes);
00326 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00327                                                 int         n_bytes);
00328 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00329                                                 int         n_bytes);
00330 
00331 const char* _dbus_error_from_errno (int error_number);
00332 
00333 void _dbus_disable_sigpipe (void);
00334 
00335 
00336 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00337 
00338 int _dbus_printf_string_upper_bound (const char *format,
00339                                      va_list args);
00340 
00341 
00345 typedef struct
00346 {
00347   unsigned long mode;  
00348   unsigned long nlink; 
00349   dbus_uid_t    uid;   
00350   dbus_gid_t    gid;   
00351   unsigned long size;  
00352   unsigned long atime; 
00353   unsigned long mtime; 
00354   unsigned long ctime; 
00355 } DBusStat;
00356 
00357 dbus_bool_t _dbus_stat             (const DBusString *filename,
00358                                     DBusStat         *statbuf,
00359                                     DBusError        *error);
00360 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00361                                     int              *fd2,
00362                                     dbus_bool_t       blocking,
00363                                     DBusError        *error);
00364 
00365 void        _dbus_print_backtrace  (void);
00366 
00367 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00368                                    int               print_pid_fd,
00369                                    DBusError        *error);
00370 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00371                                    unsigned long     pid,
00372                                    DBusError        *error);
00373 dbus_bool_t _dbus_change_identity (unsigned long     uid,
00374                                    unsigned long     gid,
00375                                    DBusError        *error);
00376 
00378 typedef void (* DBusSignalHandler) (int sig);
00379 
00380 void _dbus_set_signal_handler (int               sig,
00381                                DBusSignalHandler handler);
00382 
00383 dbus_bool_t _dbus_user_at_console (const char *username,
00384                                    DBusError  *error);
00385 
00386 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00387  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00388  */
00389 #if !defined (DBUS_VA_COPY)
00390 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00391 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00392 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00393 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00394 #  else /* va_list is a pointer */
00395 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00396 #  endif /* va_list is a pointer */
00397 #endif /* !DBUS_VA_COPY */
00398 
00399 
00404 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00405     (((const char*)&(p))[(i)])
00406 
00411 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00412      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00413       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00414       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00415       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00416       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00417       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00418       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00419       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00420 
00421 dbus_bool_t _dbus_parse_uid (const DBusString  *uid_str,
00422                              dbus_uid_t        *uid);
00423 
00424 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
00425                                           DBusError *error);
00426 
00430 typedef union DBusGUID DBusGUID;
00431 
00432 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00433                                              dbus_bool_t       create_if_not_found,
00434                                              DBusError        *error);
00435 
00441 dbus_bool_t _dbus_threads_init_platform_specific (void);
00442 
00445 DBUS_END_DECLS
00446 
00447 #endif /* DBUS_SYSDEPS_H */

Generated on Wed Nov 7 15:11:42 2012 for D-Bus by  doxygen 1.5.1