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  
00039 /* AIX sys/poll.h does #define events reqevents, and other
00040  * wonderousness, so must include sys/poll before declaring
00041  * DBusPollFD
00042  */ 
00043 #ifdef HAVE_POLL
00044 #include <sys/poll.h>
00045 #endif
00046 
00047 DBUS_BEGIN_DECLS
00048 
00049 #ifdef DBUS_WIN
00050 #define _DBUS_PATH_SEPARATOR ";"
00051 #else
00052 #define _DBUS_PATH_SEPARATOR ":"
00053 #endif
00054 
00055 /* Forward declarations */
00056 
00058 typedef struct DBusString DBusString;
00059 
00061 typedef struct DBusList DBusList;
00062 
00064 typedef struct DBusCredentials DBusCredentials;
00065 
00072 /* The idea of this file is to encapsulate everywhere that we're
00073  * relying on external libc features, for ease of security
00074  * auditing. The idea is from vsftpd. This also gives us a chance to
00075  * make things more convenient to use, e.g.  by reading into a
00076  * DBusString. Operating system headers aren't intended to be used
00077  * outside of this file and a limited number of others (such as
00078  * dbus-memory.c)
00079  */
00080 
00081 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00082 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )    \
00083   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00084 #define _DBUS_GNUC_NORETURN                         \
00085   __attribute__((__noreturn__))
00086 #else   /* !__GNUC__ */
00087 #define _DBUS_GNUC_PRINTF( format_idx, arg_idx )
00088 #define _DBUS_GNUC_NORETURN
00089 #endif  /* !__GNUC__ */
00090 
00098 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00099 
00100 const char* _dbus_getenv (const char *varname);
00101 dbus_bool_t _dbus_setenv (const char *varname,
00102                           const char *value);
00103 dbus_bool_t _dbus_clearenv (void);
00104 
00106 typedef unsigned long dbus_pid_t;
00108 typedef unsigned long dbus_uid_t;
00110 typedef unsigned long dbus_gid_t;
00111 
00113 #define DBUS_PID_UNSET ((dbus_pid_t) -1)
00114 
00115 #define DBUS_UID_UNSET ((dbus_uid_t) -1)
00116 
00117 #define DBUS_GID_UNSET ((dbus_gid_t) -1)
00118 
00120 #define DBUS_PID_FORMAT "%lu"
00121 
00122 #define DBUS_UID_FORMAT "%lu"
00123 
00124 #define DBUS_GID_FORMAT "%lu"
00125 
00126 
00137 dbus_bool_t _dbus_open_tcp_socket  (int              *fd,
00138                                     DBusError        *error);
00139 dbus_bool_t _dbus_close_socket     (int               fd,
00140                                     DBusError        *error);
00141 int         _dbus_read_socket      (int               fd,
00142                                     DBusString       *buffer,
00143                                     int               count);
00144 int         _dbus_write_socket     (int               fd,
00145                                     const DBusString *buffer,
00146                                     int               start,
00147                                     int               len);
00148 int         _dbus_write_socket_two (int               fd,
00149                                     const DBusString *buffer1,
00150                                     int               start1,
00151                                     int               len1,
00152                                     const DBusString *buffer2,
00153                                     int               start2,
00154                                     int               len2);
00155 int _dbus_connect_tcp_socket  (const char     *host,
00156                                const char     *port,
00157                                const char     *family,
00158                                DBusError      *error);
00159 int _dbus_listen_tcp_socket   (const char     *host,
00160                                const char     *port,
00161                                const char     *family,
00162                                DBusString     *retport,
00163                                int           **fds_p,
00164                                DBusError      *error);
00165 int _dbus_accept              (int             listen_fd);
00166 
00167 
00168 dbus_bool_t _dbus_read_credentials_socket (int               client_fd,
00169                                            DBusCredentials  *credentials,
00170                                            DBusError        *error);
00171 dbus_bool_t _dbus_send_credentials_socket (int              server_fd,
00172                                            DBusError       *error);
00173 
00174 dbus_bool_t _dbus_credentials_add_from_user            (DBusCredentials  *credentials,
00175                                                         const DBusString *username);
00176 dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials  *credentials);
00177 dbus_bool_t _dbus_append_user_from_current_process     (DBusString        *str);
00178 
00179 dbus_bool_t _dbus_parse_unix_user_from_config   (const DBusString  *username,
00180                                                  dbus_uid_t        *uid_p);
00181 dbus_bool_t _dbus_parse_unix_group_from_config  (const DBusString  *groupname,
00182                                                  dbus_gid_t        *gid_p);
00183 dbus_bool_t _dbus_unix_groups_from_uid          (dbus_uid_t         uid,
00184                                                  dbus_gid_t       **group_ids,
00185                                                  int               *n_group_ids);
00186 dbus_bool_t _dbus_unix_user_is_at_console       (dbus_uid_t         uid,
00187                                                  DBusError         *error);
00188 dbus_bool_t _dbus_unix_user_is_process_owner    (dbus_uid_t         uid);
00189 dbus_bool_t _dbus_windows_user_is_process_owner (const char        *windows_sid);
00190 
00191 dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
00192                                                             DBusCredentials *credentials);
00193 
00197 typedef struct DBusAtomic DBusAtomic;
00198 
00202 struct DBusAtomic
00203 {
00204 #ifdef DBUS_WIN
00205   volatile long value; 
00206 #else
00207   volatile dbus_int32_t value; 
00208 #endif
00209 };
00210 
00211 /* The value we get from autofoo is in the form of a cpp expression;
00212  * convert that to a conventional defined/undef switch. (We can't get
00213  * the conventional defined/undef because of multiarch builds only running
00214  * ./configure once, on Darwin.) */
00215 #if DBUS_HAVE_ATOMIC_INT_COND
00216 #   define DBUS_HAVE_ATOMIC_INT 1
00217 #else
00218 #   undef DBUS_HAVE_ATOMIC_INT
00219 #endif
00220 
00221 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00222 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00223 
00224 
00225 /* AIX uses different values for poll */
00226 
00227 #ifdef _AIX
00228 
00229 #define _DBUS_POLLIN      0x0001
00230 
00231 #define _DBUS_POLLPRI     0x0004
00232 
00233 #define _DBUS_POLLOUT     0x0002
00234 
00235 #define _DBUS_POLLERR     0x4000
00236 
00237 #define _DBUS_POLLHUP     0x2000
00238 
00239 #define _DBUS_POLLNVAL    0x8000
00240 #else
00241 
00242 #define _DBUS_POLLIN      0x0001
00243 
00244 #define _DBUS_POLLPRI     0x0002
00245 
00246 #define _DBUS_POLLOUT     0x0004
00247 
00248 #define _DBUS_POLLERR     0x0008
00249 
00250 #define _DBUS_POLLHUP     0x0010
00251 
00252 #define _DBUS_POLLNVAL    0x0020
00253 #endif
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 dbus_bool_t _dbus_get_standard_system_servicedirs (DBusList **dirs);
00305 
00306 dbus_bool_t _dbus_append_system_config_file  (DBusString *str);
00307 dbus_bool_t _dbus_append_session_config_file (DBusString *str);
00308 
00309 typedef struct {
00310   int fd_or_handle;
00311 } DBusPipe;
00312 
00313 void        _dbus_pipe_init                (DBusPipe         *pipe,
00314                                             int               fd);
00315 void        _dbus_pipe_init_stdout         (DBusPipe         *pipe);
00316 int         _dbus_pipe_write               (DBusPipe         *pipe,
00317                                             const DBusString *buffer,
00318                                             int               start,
00319                                             int               len,
00320                                             DBusError        *error);
00321 int         _dbus_pipe_close               (DBusPipe         *pipe,
00322                                             DBusError        *error);
00323 dbus_bool_t _dbus_pipe_is_valid            (DBusPipe         *pipe);
00324 void        _dbus_pipe_invalidate          (DBusPipe         *pipe);
00325 dbus_bool_t _dbus_pipe_is_stdout_or_stderr (DBusPipe         *pipe);
00326 
00327 
00329 typedef struct DBusDirIter DBusDirIter;
00330 
00331 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00332                                             DBusError        *error);
00333 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00334                                             DBusString       *filename,
00335                                             DBusError        *error);
00336 void         _dbus_directory_close         (DBusDirIter      *iter);
00337 
00338 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00339                                                     DBusError *error);
00340 
00341 void _dbus_fd_set_close_on_exec (int fd);
00342 
00343 const char* _dbus_get_tmpdir      (void);
00344 
00348 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00349                                                       int   n_bytes);
00350 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00351                                                 int         n_bytes);
00352 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00353                                                 int         n_bytes);
00354 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00355                                                 int         n_bytes);
00356 
00357 const char* _dbus_error_from_errno (int error_number);
00358 
00359 void        _dbus_set_errno_to_zero                  (void);
00360 dbus_bool_t _dbus_get_is_errno_nonzero               (void);
00361 dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock (void);
00362 dbus_bool_t _dbus_get_is_errno_enomem                (void);
00363 dbus_bool_t _dbus_get_is_errno_eintr                 (void);
00364 const char* _dbus_strerror_from_errno                (void);
00365 
00366 void _dbus_disable_sigpipe (void);
00367 
00368 
00369 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00370 
00371 int _dbus_printf_string_upper_bound (const char *format,
00372                                      va_list args);
00373 
00374 
00378 typedef struct
00379 {
00380   unsigned long mode;  
00381   unsigned long nlink; 
00382   dbus_uid_t    uid;   
00383   dbus_gid_t    gid;   
00384   unsigned long size;  
00385   unsigned long atime; 
00386   unsigned long mtime; 
00387   unsigned long ctime; 
00388 } DBusStat;
00389 
00390 dbus_bool_t _dbus_stat             (const DBusString *filename,
00391                                     DBusStat         *statbuf,
00392                                     DBusError        *error);
00393 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00394                                     int              *fd2,
00395                                     dbus_bool_t       blocking,
00396                                     DBusError        *error);
00397 
00398 void        _dbus_print_backtrace  (void);
00399 
00400 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00401                                    DBusPipe         *print_pid_pipe,
00402                                    DBusError        *error);
00403 
00404 dbus_bool_t _dbus_verify_daemon_user    (const char *user);
00405 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
00406                                          DBusError  *error);
00407 
00408 dbus_bool_t _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00409                                               DBusPipe         *print_pid_pipe,
00410                                               dbus_pid_t        pid_to_write,
00411                                               DBusError        *error);
00412 
00414 typedef void (* DBusSignalHandler) (int sig);
00415 
00416 void _dbus_set_signal_handler (int               sig,
00417                                DBusSignalHandler handler);
00418 
00419 dbus_bool_t _dbus_user_at_console (const char *username,
00420                                    DBusError  *error);
00421 
00422 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. 
00423  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. 
00424  */
00425 #if !defined (DBUS_VA_COPY)
00426 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00427 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00428 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00429 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00430 #  else /* va_list is a pointer */
00431 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00432 #  endif /* va_list is a pointer */
00433 #endif /* !DBUS_VA_COPY */
00434 
00435 
00440 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00441     (((const char*)&(p))[(i)])
00442 
00447 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00448      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00449       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00450       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00451       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00452       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00453       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00454       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00455       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00456 
00457 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
00458                                           DBusError *error);
00459 
00463 typedef union DBusGUID DBusGUID;
00464 
00465 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00466                                              dbus_bool_t       create_if_not_found,
00467                                              DBusError        *error);
00468 
00474 dbus_bool_t _dbus_threads_init_platform_specific (void);
00475 
00476 dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs, 
00477                                           const char *suffix, 
00478                                           DBusList **dir_list);
00479 
00480 unsigned long _dbus_pid_for_log (void);
00481 
00482 /* FIXME move back to dbus-sysdeps-unix.h probably -
00483  * the PID file handling just needs a little more abstraction
00484  * in the bus daemon first.
00485  */
00486 dbus_pid_t    _dbus_getpid (void);
00487 
00488 void _dbus_flush_caches (void);
00489 
00492 DBUS_END_DECLS
00493 
00494 #endif /* DBUS_SYSDEPS_H */

Generated on Sat Jun 14 22:43:04 2008 for D-Bus by  doxygen 1.4.6