dbus-sysdeps.h

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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 #include <stdarg.h>
00037 
00038 DBUS_BEGIN_DECLS
00039 
00040 #ifdef DBUS_WIN
00041 #define _DBUS_PATH_SEPARATOR ";"
00042 #else
00043 #define _DBUS_PATH_SEPARATOR ":"
00044 #endif
00045 
00046 /* Forward declarations */
00047 
00049 typedef struct DBusString DBusString;
00050 
00052 typedef struct DBusList DBusList;
00053 
00055 typedef struct DBusCredentials DBusCredentials;
00056 
00063 /* The idea of this file is to encapsulate everywhere that we're
00064  * relying on external libc features, for ease of security
00065  * auditing. The idea is from vsftpd. This also gives us a chance to
00066  * make things more convenient to use, e.g.  by reading into a
00067  * DBusString. Operating system headers aren't intended to be used
00068  * outside of this file and a limited number of others (such as
00069  * dbus-memory.c)
00070  */
00071 
00072 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00073 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
00074   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00075 #define _DBUS_GNUC_NORETURN                         \
00076   __attribute__((__noreturn__))
00077 #else   /* !__GNUC__ */
00078 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
00079 #define _DBUS_GNUC_NORETURN
00080 #endif  /* !__GNUC__ */
00081 
00089 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00090 
00091 const char* _dbus_getenv (const char *varname);
00092 dbus_bool_t _dbus_setenv (const char *varname,
00093                           const char *value);
00094 dbus_bool_t _dbus_clearenv (void);
00095 
00097 typedef unsigned long dbus_pid_t;
00099 typedef unsigned long dbus_uid_t;
00101 typedef unsigned long dbus_gid_t;
00102 
00104 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00105 
00106 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00107 
00108 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00109 
00111 #define DBUS_PID_FORMAT "%lu"
00112 
00113 #define DBUS_UID_FORMAT "%lu"
00114 
00115 #define DBUS_GID_FORMAT "%lu"
00116 
00117 
00128 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
00129                                     DBusError        *error);
00130 dbus_bool_t _dbus_close_socket     (int               fd,
00131                                     DBusError        *error);
00132 int         _dbus_read_socket      (int               fd,
00133                                     DBusString       *buffer,
00134                                     int               count);
00135 int         _dbus_write_socket     (int               fd,
00136                                     const DBusString *buffer,
00137                                     int               start,
00138                                     int               len);
00139 int         _dbus_write_socket_two (int               fd,
00140                                     const DBusString *buffer1,
00141                                     int               start1,
00142                                     int               len1,
00143                                     const DBusString *buffer2,
00144                                     int               start2,
00145                                     int               len2);
00146 int _dbus_connect_tcp_socket  (const char     *host,
00147                                const char     *port,
00148                                const char     *family,
00149                                DBusError      *error);
00150 int _dbus_listen_tcp_socket   (const char     *host,
00151                                const char     *port,
00152                                const char     *family,
00153                                DBusString     *retport,
00154                                int           **fds_p,
00155                                DBusError      *error);
00156 int _dbus_accept              (int             listen_fd);
00157 
00158 
00159 dbus_bool_t _dbus_read_credentials_socket (int               client_fd,
00160                                            DBusCredentials  *credentials,
00161                                            DBusError        *error);
00162 dbus_bool_t _dbus_send_credentials_socket (int              server_fd,
00163                                            DBusError       *error);
00164 
00165 dbus_bool_t _dbus_credentials_add_from_user            (DBusCredentials  *credentials,
00166                                                         const DBusString *username);
00167 dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials  *credentials);
00168 dbus_bool_t _dbus_append_user_from_current_process     (DBusString        *str);
00169 
00170 dbus_bool_t _dbus_parse_unix_user_from_config   (const DBusString  *username,
00171                                                  dbus_uid_t        *uid_p);
00172 dbus_bool_t _dbus_parse_unix_group_from_config  (const DBusString  *groupname,
00173                                                  dbus_gid_t        *gid_p);
00174 dbus_bool_t _dbus_unix_groups_from_uid          (dbus_uid_t         uid,
00175                                                  dbus_gid_t       **group_ids,
00176                                                  int               *n_group_ids);
00177 dbus_bool_t _dbus_unix_user_is_at_console       (dbus_uid_t         uid,
00178                                                  DBusError         *error);
00179 dbus_bool_t _dbus_unix_user_is_process_owner    (dbus_uid_t         uid);
00180 dbus_bool_t _dbus_windows_user_is_process_owner (const char        *windows_sid);
00181 
00182 dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
00183                                                             DBusCredentials *credentials);
00184 
00188 typedef struct DBusAtomic DBusAtomic;
00189 
00193 struct DBusAtomic
00194 {
00195 #ifdef DBUS_WIN
00196   volatile long value; 
00197 #else
00198   volatile dbus_int32_t value; 
00199 #endif
00200 };
00201 
00202 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00203 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00204 
00206 #define _DBUS_POLLIN      0x0001
00207 
00208 #define _DBUS_POLLPRI     0x0002
00209 
00210 #define _DBUS_POLLOUT     0x0004
00211 
00212 #define _DBUS_POLLERR     0x0008
00213 
00214 #define _DBUS_POLLHUP     0x0010
00215 
00216 #define _DBUS_POLLNVAL    0x0020
00217 
00221 typedef struct
00222 {
00223   int fd;            
00224   short events;      
00225   short revents;     
00226 } DBusPollFD;
00227 
00228 int _dbus_poll (DBusPollFD *fds,
00229                 int         n_fds,
00230                 int         timeout_milliseconds);
00231 
00232 void _dbus_sleep_milliseconds (int milliseconds);
00233 
00234 void _dbus_get_current_time (long *tv_sec,
00235                              long *tv_usec);
00236 
00240 dbus_bool_t _dbus_file_exists         (const char       *file);
00241 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00242                                        const DBusString *filename,
00243                                        DBusError        *error);
00244 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00245                                        const DBusString *filename,
00246                                        DBusError        *error);
00247 
00248 dbus_bool_t _dbus_make_file_world_readable   (const DBusString *filename,
00249                                               DBusError *error);
00250 
00251 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00252                                               DBusError        *error);
00253 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00254                                               DBusError        *error);
00255 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00256                                               DBusError        *error);
00257 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00258                                               DBusError        *error);
00259 
00260 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00261                                        const DBusString *next_component);
00262 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00263                                        DBusString       *dirname);
00264 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00265 
00266 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
00267 dbus_bool_t _dbus_get_standard_system_servicedirs (DBusList **dirs);
00268 
00269 dbus_bool_t _dbus_append_system_config_file  (DBusString *str);
00270 dbus_bool_t _dbus_append_session_config_file (DBusString *str);
00271 
00272 typedef struct {
00273   int fd_or_handle;
00274 } DBusPipe;
00275 
00276 void        _dbus_pipe_init                (DBusPipe         *pipe,
00277                                             int               fd);
00278 void        _dbus_pipe_init_stdout         (DBusPipe         *pipe);
00279 int         _dbus_pipe_write               (DBusPipe         *pipe,
00280                                             const DBusString *buffer,
00281                                             int               start,
00282                                             int               len,
00283                                             DBusError        *error);
00284 int         _dbus_pipe_close               (DBusPipe         *pipe,
00285                                             DBusError        *error);
00286 dbus_bool_t _dbus_pipe_is_valid            (DBusPipe         *pipe);
00287 void        _dbus_pipe_invalidate          (DBusPipe         *pipe);
00288 dbus_bool_t _dbus_pipe_is_stdout_or_stderr (DBusPipe         *pipe);
00289 
00290 
00292 typedef struct DBusDirIter DBusDirIter;
00293 
00294 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00295                                             DBusError        *error);
00296 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00297                                             DBusString       *filename,
00298                                             DBusError        *error);
00299 void         _dbus_directory_close         (DBusDirIter      *iter);
00300 
00301 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00302                                                     DBusError *error);
00303 
00304 void _dbus_fd_set_close_on_exec (int fd);
00305 
00306 const char* _dbus_get_tmpdir      (void);
00307 
00311 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00312                                                       int   n_bytes);
00313 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00314                                                 int         n_bytes);
00315 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00316                                                 int         n_bytes);
00317 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00318                                                 int         n_bytes);
00319 
00320 const char* _dbus_error_from_errno (int error_number);
00321 
00322 void        _dbus_set_errno_to_zero                  (void);
00323 dbus_bool_t _dbus_get_is_errno_nonzero               (void);
00324 dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock (void);
00325 dbus_bool_t _dbus_get_is_errno_enomem                (void);
00326 dbus_bool_t _dbus_get_is_errno_eintr                 (void);
00327 const char* _dbus_strerror_from_errno                (void);
00328 
00329 void _dbus_disable_sigpipe (void);
00330 
00331 
00332 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00333 
00334 int _dbus_printf_string_upper_bound (const char *format,
00335                                      va_list args);
00336 
00337 
00341 typedef struct
00342 {
00343   unsigned long mode;  
00344   unsigned long nlink; 
00345   dbus_uid_t    uid;   
00346   dbus_gid_t    gid;   
00347   unsigned long size;  
00348   unsigned long atime; 
00349   unsigned long mtime; 
00350   unsigned long ctime; 
00351 } DBusStat;
00352 
00353 dbus_bool_t _dbus_stat             (const DBusString *filename,
00354                                     DBusStat         *statbuf,
00355                                     DBusError        *error);
00356 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00357                                     int              *fd2,
00358                                     dbus_bool_t       blocking,
00359                                     DBusError        *error);
00360 
00361 void        _dbus_print_backtrace  (void);
00362 
00363 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00364                                    DBusPipe         *print_pid_pipe,
00365                                    DBusError        *error);
00366 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00367                                    unsigned long     pid,
00368                                    DBusError        *error);
00369 dbus_bool_t _dbus_verify_daemon_user    (const char *user);
00370 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
00371                                          DBusError  *error);
00372 
00374 typedef void (* DBusSignalHandler) (int sig);
00375 
00376 void _dbus_set_signal_handler (int               sig,
00377                                DBusSignalHandler handler);
00378 
00379 dbus_bool_t _dbus_user_at_console (const char *username,
00380                                    DBusError  *error);
00381 
00382 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00383  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00384  */
00385 #if !defined (DBUS_VA_COPY)
00386 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00387 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00388 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00389 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00390 #  else /* va_list is a pointer */
00391 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00392 #  endif /* va_list is a pointer */
00393 #endif /* !DBUS_VA_COPY */
00394 
00395 
00400 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00401     (((const char*)&(p))[(i)])
00402 
00407 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00408      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00409       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00410       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00411       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00412       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00413       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00414       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00415       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00416 
00417 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
00418                                           DBusError *error);
00419 
00423 typedef union DBusGUID DBusGUID;
00424 
00425 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00426                                              dbus_bool_t       create_if_not_found,
00427                                              DBusError        *error);
00428 
00434 dbus_bool_t _dbus_threads_init_platform_specific (void);
00435 
00436 dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs, 
00437                                           const char *suffix, 
00438                                           DBusList **dir_list);
00439 
00440 unsigned long _dbus_pid_for_log (void);
00441 
00442 /* FIXME move back to dbus-sysdeps-unix.h probably -
00443  * the PID file handling just needs a little more abstraction
00444  * in the bus daemon first.
00445  */
00446 dbus_pid_t    _dbus_getpid (void);
00447 
00448 void _dbus_flush_caches (void);
00449 
00452 DBUS_END_DECLS
00453 
00454 #endif /* DBUS_SYSDEPS_H */

Generated on Mon Feb 1 19:15:26 2010 for D-Bus by  doxygen 1.4.7