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 #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 
00096 typedef unsigned long dbus_pid_t;
00098 typedef unsigned long dbus_uid_t;
00100 typedef unsigned long dbus_gid_t;
00101 
00103 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00104 
00105 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00106 
00107 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00108 
00110 #define DBUS_PID_FORMAT "%lu"
00111 
00112 #define DBUS_UID_FORMAT "%lu"
00113 
00114 #define DBUS_GID_FORMAT "%lu"
00115 
00116 
00127 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
00128                                     DBusError        *error);
00129 dbus_bool_t _dbus_close_socket     (int               fd,
00130                                     DBusError        *error);
00131 int         _dbus_read_socket      (int               fd,
00132                                     DBusString       *buffer,
00133                                     int               count);
00134 int         _dbus_write_socket     (int               fd,
00135                                     const DBusString *buffer,
00136                                     int               start,
00137                                     int               len);
00138 int         _dbus_write_socket_two (int               fd,
00139                                     const DBusString *buffer1,
00140                                     int               start1,
00141                                     int               len1,
00142                                     const DBusString *buffer2,
00143                                     int               start2,
00144                                     int               len2);
00145 int _dbus_connect_tcp_socket  (const char     *host,
00146                                dbus_uint32_t   port,
00147                                DBusError      *error);
00148 int _dbus_listen_tcp_socket   (const char     *host,
00149                                dbus_uint32_t  *port,
00150                                dbus_bool_t     inaddr_any,
00151                                DBusError      *error);
00152 int _dbus_accept              (int             listen_fd);
00153 
00154 
00155 dbus_bool_t _dbus_read_credentials_socket (int               client_fd,
00156                                            DBusCredentials  *credentials,
00157                                            DBusError        *error);
00158 dbus_bool_t _dbus_send_credentials_socket (int              server_fd,
00159                                            DBusError       *error);
00160 
00161 dbus_bool_t _dbus_credentials_add_from_user            (DBusCredentials  *credentials,
00162                                                         const DBusString *username);
00163 dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials  *credentials);
00164 dbus_bool_t _dbus_append_user_from_current_process     (DBusString        *str);
00165 
00166 dbus_bool_t _dbus_parse_unix_user_from_config   (const DBusString  *username,
00167                                                  dbus_uid_t        *uid_p);
00168 dbus_bool_t _dbus_parse_unix_group_from_config  (const DBusString  *groupname,
00169                                                  dbus_gid_t        *gid_p);
00170 dbus_bool_t _dbus_unix_groups_from_uid          (dbus_uid_t         uid,
00171                                                  dbus_gid_t       **group_ids,
00172                                                  int               *n_group_ids);
00173 dbus_bool_t _dbus_unix_user_is_at_console       (dbus_uid_t         uid,
00174                                                  DBusError         *error);
00175 dbus_bool_t _dbus_unix_user_is_process_owner    (dbus_uid_t         uid);
00176 dbus_bool_t _dbus_windows_user_is_process_owner (const char        *windows_sid);
00177 
00178 dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
00179                                                             DBusCredentials *credentials);
00180 
00184 typedef struct DBusAtomic DBusAtomic;
00185 
00189 struct DBusAtomic
00190 {
00191 #ifdef DBUS_WIN
00192   volatile long value; 
00193 #else
00194   volatile dbus_int32_t value; 
00195 #endif
00196 };
00197 
00198 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00199 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00200 
00202 #define _DBUS_POLLIN      0x0001
00203 
00204 #define _DBUS_POLLPRI     0x0002
00205 
00206 #define _DBUS_POLLOUT     0x0004
00207 
00208 #define _DBUS_POLLERR     0x0008
00209 
00210 #define _DBUS_POLLHUP     0x0010
00211 
00212 #define _DBUS_POLLNVAL    0x0020
00213 
00217 typedef struct
00218 {
00219   int fd;            
00220   short events;      
00221   short revents;     
00222 } DBusPollFD;
00223 
00224 int _dbus_poll (DBusPollFD *fds,
00225                 int         n_fds,
00226                 int         timeout_milliseconds);
00227 
00228 void _dbus_sleep_milliseconds (int milliseconds);
00229 
00230 void _dbus_get_current_time (long *tv_sec,
00231                              long *tv_usec);
00232 
00236 dbus_bool_t _dbus_file_exists         (const char       *file);
00237 dbus_bool_t _dbus_file_get_contents   (DBusString       *str,
00238                                        const DBusString *filename,
00239                                        DBusError        *error);
00240 dbus_bool_t _dbus_string_save_to_file (const DBusString *str,
00241                                        const DBusString *filename,
00242                                        DBusError        *error);
00243 
00244 dbus_bool_t _dbus_make_file_world_readable   (const DBusString *filename,
00245                                               DBusError *error);
00246 
00247 dbus_bool_t    _dbus_create_file_exclusively (const DBusString *filename,
00248                                               DBusError        *error);
00249 dbus_bool_t    _dbus_delete_file             (const DBusString *filename,
00250                                               DBusError        *error);
00251 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00252                                               DBusError        *error);
00253 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00254                                               DBusError        *error);
00255 
00256 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00257                                        const DBusString *next_component);
00258 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00259                                        DBusString       *dirname);
00260 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00261 
00262 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
00263 
00264 dbus_bool_t _dbus_append_system_config_file  (DBusString *str);
00265 dbus_bool_t _dbus_append_session_config_file (DBusString *str);
00266 
00267 typedef struct {
00268   int fd_or_handle;
00269 } DBusPipe;
00270 
00271 void        _dbus_pipe_init                (DBusPipe         *pipe,
00272                                             int               fd);
00273 void        _dbus_pipe_init_stdout         (DBusPipe         *pipe);
00274 int         _dbus_pipe_write               (DBusPipe         *pipe,
00275                                             const DBusString *buffer,
00276                                             int               start,
00277                                             int               len,
00278                                             DBusError        *error);
00279 int         _dbus_pipe_close               (DBusPipe         *pipe,
00280                                             DBusError        *error);
00281 dbus_bool_t _dbus_pipe_is_valid            (DBusPipe         *pipe);
00282 void        _dbus_pipe_invalidate          (DBusPipe         *pipe);
00283 dbus_bool_t _dbus_pipe_is_stdout_or_stderr (DBusPipe         *pipe);
00284 
00285 
00287 typedef struct DBusDirIter DBusDirIter;
00288 
00289 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00290                                             DBusError        *error);
00291 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00292                                             DBusString       *filename,
00293                                             DBusError        *error);
00294 void         _dbus_directory_close         (DBusDirIter      *iter);
00295 
00296 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00297                                                     DBusError *error);
00298 
00299 void _dbus_fd_set_close_on_exec (int fd);
00300 
00301 const char* _dbus_get_tmpdir      (void);
00302 
00306 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00307                                                       int   n_bytes);
00308 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00309                                                 int         n_bytes);
00310 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00311                                                 int         n_bytes);
00312 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00313                                                 int         n_bytes);
00314 
00315 const char* _dbus_error_from_errno (int error_number);
00316 
00317 void        _dbus_set_errno_to_zero                  (void);
00318 dbus_bool_t _dbus_get_is_errno_nonzero               (void);
00319 dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock (void);
00320 dbus_bool_t _dbus_get_is_errno_enomem                (void);
00321 dbus_bool_t _dbus_get_is_errno_eintr                 (void);
00322 const char* _dbus_strerror_from_errno                (void);
00323 
00324 void _dbus_disable_sigpipe (void);
00325 
00326 
00327 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00328 
00329 int _dbus_printf_string_upper_bound (const char *format,
00330                                      va_list args);
00331 
00332 
00336 typedef struct
00337 {
00338   unsigned long mode;  
00339   unsigned long nlink; 
00340   dbus_uid_t    uid;   
00341   dbus_gid_t    gid;   
00342   unsigned long size;  
00343   unsigned long atime; 
00344   unsigned long mtime; 
00345   unsigned long ctime; 
00346 } DBusStat;
00347 
00348 dbus_bool_t _dbus_stat             (const DBusString *filename,
00349                                     DBusStat         *statbuf,
00350                                     DBusError        *error);
00351 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00352                                     int              *fd2,
00353                                     dbus_bool_t       blocking,
00354                                     DBusError        *error);
00355 
00356 void        _dbus_print_backtrace  (void);
00357 
00358 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00359                                    DBusPipe         *print_pid_pipe,
00360                                    DBusError        *error);
00361 dbus_bool_t _dbus_write_pid_file  (const DBusString *filename,
00362                                    unsigned long     pid,
00363                                    DBusError        *error);
00364 dbus_bool_t _dbus_verify_daemon_user    (const char *user);
00365 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
00366                                          DBusError  *error);
00367 
00369 typedef void (* DBusSignalHandler) (int sig);
00370 
00371 void _dbus_set_signal_handler (int               sig,
00372                                DBusSignalHandler handler);
00373 
00374 dbus_bool_t _dbus_user_at_console (const char *username,
00375                                    DBusError  *error);
00376 
00377 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00378  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00379  */
00380 #if !defined (DBUS_VA_COPY)
00381 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00382 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00383 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00384 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00385 #  else /* va_list is a pointer */
00386 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00387 #  endif /* va_list is a pointer */
00388 #endif /* !DBUS_VA_COPY */
00389 
00390 
00395 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00396     (((const char*)&(p))[(i)])
00397 
00402 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00403      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00404       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00405       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00406       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00407       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00408       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00409       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00410       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00411 
00412 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
00413                                           DBusError *error);
00414 
00418 typedef union DBusGUID DBusGUID;
00419 
00420 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00421                                              dbus_bool_t       create_if_not_found,
00422                                              DBusError        *error);
00423 
00429 dbus_bool_t _dbus_threads_init_platform_specific (void);
00430 
00431 dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs, 
00432                                           const char *suffix, 
00433                                           DBusList **dir_list);
00434 
00435 unsigned long _dbus_pid_for_log (void);
00436 
00437 /* FIXME move back to dbus-sysdeps-unix.h probably -
00438  * the PID file handling just needs a little more abstraction
00439  * in the bus daemon first.
00440  */
00441 dbus_pid_t    _dbus_getpid (void);
00442 
00443 void _dbus_flush_caches (void);
00444 
00447 DBUS_END_DECLS
00448 
00449 #endif /* DBUS_SYSDEPS_H */

Generated on Tue Oct 14 03:28:54 2008 for D-Bus by  doxygen 1.5.3