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

Generated on Fri Sep 21 18:12:13 2007 for D-Bus by  doxygen 1.5.1