D-Bus  1.8.20
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #ifndef DBUS_SYSDEPS_H
00026 #define DBUS_SYSDEPS_H
00027 
00028 #include "config.h"
00029 
00030 #ifdef HAVE_STDINT_H
00031 #include <stdint.h>
00032 #endif
00033 
00034 #ifdef HAVE_INTTYPES_H
00035 #include <inttypes.h>
00036 #endif
00037 
00038 #include <dbus/dbus-errors.h>
00039 #include <dbus/dbus-file.h>
00040 #include <dbus/dbus-string.h>
00041 
00042 /* this is perhaps bogus, but strcmp() etc. are faster if we use the
00043  * stuff straight out of string.h, so have this here for now.
00044  */
00045 #include <string.h>
00046 #include <stdarg.h>
00047 
00048 /* AIX sys/poll.h does #define events reqevents, and other
00049  * wonderousness, so must include sys/poll before declaring
00050  * DBusPollFD
00051  */ 
00052 #ifdef HAVE_POLL
00053 #include <sys/poll.h>
00054 #endif
00055 
00056 #ifdef DBUS_WINCE
00057 /* Windows CE lacks some system functions (such as errno and clock).
00058    We bring them in here.  */
00059 #include "dbus-sysdeps-wince-glue.h"
00060 #endif
00061 
00062 DBUS_BEGIN_DECLS
00063 
00064 #ifdef DBUS_WIN
00065 #define _DBUS_PATH_SEPARATOR ";"
00066 #else
00067 #define _DBUS_PATH_SEPARATOR ":"
00068 #endif
00069 
00070 /* Forward declarations */
00071 
00072 
00074 typedef struct DBusList DBusList;
00075 
00077 typedef struct DBusCredentials DBusCredentials;
00078 
00080 typedef struct DBusPipe DBusPipe;
00081 
00088 void _dbus_abort (void) _DBUS_GNUC_NORETURN;
00089 
00090 const char* _dbus_getenv (const char *varname);
00091 dbus_bool_t _dbus_clearenv (void);
00092 char **     _dbus_get_environment (void);
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_close_socket     (int               fd,
00127                                     DBusError        *error);
00128 int         _dbus_read_socket      (int               fd,
00129                                     DBusString       *buffer,
00130                                     int               count);
00131 int         _dbus_write_socket     (int               fd,
00132                                     const DBusString *buffer,
00133                                     int               start,
00134                                     int               len);
00135 int         _dbus_write_socket_two (int               fd,
00136                                     const DBusString *buffer1,
00137                                     int               start1,
00138                                     int               len1,
00139                                     const DBusString *buffer2,
00140                                     int               start2,
00141                                     int               len2);
00142 
00143 int _dbus_read_socket_with_unix_fds      (int               fd,
00144                                           DBusString       *buffer,
00145                                           int               count,
00146                                           int              *fds,
00147                                           int              *n_fds);
00148 int _dbus_write_socket_with_unix_fds     (int               fd,
00149                                           const DBusString *buffer,
00150                                           int               start,
00151                                           int               len,
00152                                           const int        *fds,
00153                                           int               n_fds);
00154 int _dbus_write_socket_with_unix_fds_two (int               fd,
00155                                           const DBusString *buffer1,
00156                                           int               start1,
00157                                           int               len1,
00158                                           const DBusString *buffer2,
00159                                           int               start2,
00160                                           int               len2,
00161                                           const int        *fds,
00162                                           int               n_fds);
00163 
00164 dbus_bool_t _dbus_socket_is_invalid (int              fd);
00165 
00166 int _dbus_connect_tcp_socket  (const char     *host,
00167                                const char     *port,
00168                                const char     *family,
00169                                DBusError      *error);
00170 int _dbus_connect_tcp_socket_with_nonce  (const char     *host,
00171                                           const char     *port,
00172                                           const char     *family,
00173                                           const char     *noncefile,
00174                                           DBusError      *error);
00175 int _dbus_listen_tcp_socket   (const char     *host,
00176                                const char     *port,
00177                                const char     *family,
00178                                DBusString     *retport,
00179                                int           **fds_p,
00180                                DBusError      *error);
00181 int _dbus_accept              (int             listen_fd);
00182 
00183 
00184 dbus_bool_t _dbus_read_credentials_socket (int               client_fd,
00185                                            DBusCredentials  *credentials,
00186                                            DBusError        *error);
00187 dbus_bool_t _dbus_send_credentials_socket (int              server_fd,
00188                                            DBusError       *error);
00189 
00190 dbus_bool_t _dbus_credentials_add_from_user            (DBusCredentials  *credentials,
00191                                                         const DBusString *username);
00192 dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials  *credentials);
00193 dbus_bool_t _dbus_append_user_from_current_process     (DBusString        *str);
00194 
00195 dbus_bool_t _dbus_parse_unix_user_from_config   (const DBusString  *username,
00196                                                  dbus_uid_t        *uid_p);
00197 dbus_bool_t _dbus_parse_unix_group_from_config  (const DBusString  *groupname,
00198                                                  dbus_gid_t        *gid_p);
00199 dbus_bool_t _dbus_unix_groups_from_uid          (dbus_uid_t         uid,
00200                                                  dbus_gid_t       **group_ids,
00201                                                  int               *n_group_ids);
00202 dbus_bool_t _dbus_unix_user_is_at_console       (dbus_uid_t         uid,
00203                                                  DBusError         *error);
00204 dbus_bool_t _dbus_unix_user_is_process_owner    (dbus_uid_t         uid);
00205 dbus_bool_t _dbus_windows_user_is_process_owner (const char        *windows_sid);
00206 
00207 dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
00208                                                             DBusCredentials *credentials);
00209 
00210 dbus_bool_t _dbus_daemon_is_session_bus_address_published (const char *scope);
00211 
00212 dbus_bool_t _dbus_daemon_publish_session_bus_address (const char* address, const char* shm_name);
00213 
00214 void _dbus_daemon_unpublish_session_bus_address (void);
00215 
00216 dbus_bool_t _dbus_socket_can_pass_unix_fd(int fd);
00217 
00221 typedef struct DBusAtomic DBusAtomic;
00222 
00226 struct DBusAtomic
00227 {
00228 #ifdef DBUS_WIN
00229   volatile long value; 
00230 #else
00231   volatile dbus_int32_t value; 
00232 #endif
00233 };
00234 
00235 /* The value we get from autofoo is in the form of a cpp expression;
00236  * convert that to a conventional defined/undef switch. (We can't get
00237  * the conventional defined/undef because of multiarch builds only running
00238  * ./configure once, on Darwin.) */
00239 #if DBUS_HAVE_ATOMIC_INT_COND
00240 #   define DBUS_HAVE_ATOMIC_INT 1
00241 #else
00242 #   undef DBUS_HAVE_ATOMIC_INT
00243 #endif
00244 
00245 dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic);
00246 dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic);
00247 dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic);
00248 
00249 
00250 /* AIX uses different values for poll */
00251 
00252 #ifdef _AIX
00253 
00254 #define _DBUS_POLLIN      0x0001
00255 
00256 #define _DBUS_POLLPRI     0x0004
00257 
00258 #define _DBUS_POLLOUT     0x0002
00259 
00260 #define _DBUS_POLLERR     0x4000
00261 
00262 #define _DBUS_POLLHUP     0x2000
00263 
00264 #define _DBUS_POLLNVAL    0x8000
00265 #elif defined(__HAIKU__)
00266 
00267 #define _DBUS_POLLIN      0x0001
00268 
00269 #define _DBUS_POLLOUT     0x0002
00270 
00271 #define _DBUS_POLLERR     0x0004
00272 
00273 #define _DBUS_POLLPRI     0x0020
00274 
00275 #define _DBUS_POLLHUP     0x0080
00276 
00277 #define _DBUS_POLLNVAL    0x1000
00278 #elif defined(__QNX__)
00279 
00280 #define _DBUS_POLLOUT     0x0002
00281 
00282 #define _DBUS_POLLIN      0x0005
00283 
00284 #define _DBUS_POLLPRI     0x0008
00285 
00286 #define _DBUS_POLLERR     0x0020
00287 
00288 #define _DBUS_POLLHUP     0x0040
00289 
00290 #define _DBUS_POLLNVAL    0x1000
00291 #else
00292 
00293 #define _DBUS_POLLIN      0x0001
00294 
00295 #define _DBUS_POLLPRI     0x0002
00296 
00297 #define _DBUS_POLLOUT     0x0004
00298 
00299 #define _DBUS_POLLERR     0x0008
00300 
00301 #define _DBUS_POLLHUP     0x0010
00302 
00303 #define _DBUS_POLLNVAL    0x0020
00304 #endif
00305 
00309 typedef struct
00310 {
00311   int fd;            
00312   short events;      
00313   short revents;     
00314 } DBusPollFD;
00315 
00316 int _dbus_poll (DBusPollFD *fds,
00317                 int         n_fds,
00318                 int         timeout_milliseconds);
00319 
00320 void _dbus_sleep_milliseconds (int milliseconds);
00321 
00322 void _dbus_get_monotonic_time (long *tv_sec,
00323                                long *tv_usec);
00324 
00325 void _dbus_get_real_time (long *tv_sec,
00326                           long *tv_usec);
00327 
00331 dbus_bool_t    _dbus_create_directory        (const DBusString *filename,
00332                                               DBusError        *error);
00333 dbus_bool_t    _dbus_delete_directory        (const DBusString *filename,
00334                                               DBusError        *error);
00335 
00336 dbus_bool_t _dbus_concat_dir_and_file (DBusString       *dir,
00337                                        const DBusString *next_component);
00338 dbus_bool_t _dbus_string_get_dirname  (const DBusString *filename,
00339                                        DBusString       *dirname);
00340 dbus_bool_t _dbus_path_is_absolute    (const DBusString *filename);
00341 
00342 dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
00343 dbus_bool_t _dbus_get_standard_system_servicedirs (DBusList **dirs);
00344 
00345 dbus_bool_t _dbus_append_system_config_file  (DBusString *str);
00346 dbus_bool_t _dbus_append_session_config_file (DBusString *str);
00347 
00349 typedef struct DBusDirIter DBusDirIter;
00350 
00351 DBusDirIter* _dbus_directory_open          (const DBusString *filename,
00352                                             DBusError        *error);
00353 dbus_bool_t  _dbus_directory_get_next_file (DBusDirIter      *iter,
00354                                             DBusString       *filename,
00355                                             DBusError        *error);
00356 void         _dbus_directory_close         (DBusDirIter      *iter);
00357 
00358 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
00359                                                     DBusError *error);
00360 
00361 void _dbus_fd_set_close_on_exec (intptr_t fd);
00362 
00363 const char* _dbus_get_tmpdir      (void);
00364 
00368 void        _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
00369                                                       int   n_bytes);
00370 void        _dbus_generate_random_bytes_buffer (char       *buffer,
00371                                                 int         n_bytes);
00372 dbus_bool_t _dbus_generate_random_bytes        (DBusString *str,
00373                                                 int         n_bytes);
00374 dbus_bool_t _dbus_generate_random_ascii        (DBusString *str,
00375                                                 int         n_bytes);
00376 
00377 const char* _dbus_error_from_errno (int error_number);
00378 const char* _dbus_error_from_system_errno (void);
00379 
00380 void        _dbus_set_errno_to_zero                  (void);
00381 dbus_bool_t _dbus_get_is_errno_nonzero               (void);
00382 dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock (void);
00383 dbus_bool_t _dbus_get_is_errno_enomem                (void);
00384 dbus_bool_t _dbus_get_is_errno_eintr                 (void);
00385 dbus_bool_t _dbus_get_is_errno_epipe                 (void);
00386 dbus_bool_t _dbus_get_is_errno_etoomanyrefs           (void);
00387 const char* _dbus_strerror_from_errno                (void);
00388 
00389 void _dbus_disable_sigpipe (void);
00390 
00391 
00392 void _dbus_exit (int code) _DBUS_GNUC_NORETURN;
00393 
00394 int _dbus_printf_string_upper_bound (const char *format,
00395                                      va_list args);
00396 
00397 
00401 typedef struct
00402 {
00403   unsigned long mode;  
00404   unsigned long nlink; 
00405   dbus_uid_t    uid;   
00406   dbus_gid_t    gid;   
00407   unsigned long size;  
00408   unsigned long atime; 
00409   unsigned long mtime; 
00410   unsigned long ctime; 
00411 } DBusStat;
00412 
00413 dbus_bool_t _dbus_stat             (const DBusString *filename,
00414                                     DBusStat         *statbuf,
00415                                     DBusError        *error);
00416 dbus_bool_t _dbus_full_duplex_pipe (int              *fd1,
00417                                     int              *fd2,
00418                                     dbus_bool_t       blocking,
00419                                     DBusError        *error);
00420 
00421 void        _dbus_print_backtrace  (void);
00422 
00423 dbus_bool_t _dbus_become_daemon   (const DBusString *pidfile,
00424                                    DBusPipe         *print_pid_pipe,
00425                                    DBusError        *error,
00426                                    dbus_bool_t       keep_umask);
00427 
00428 dbus_bool_t _dbus_verify_daemon_user    (const char *user);
00429 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
00430                                          DBusError  *error);
00431 
00432 dbus_bool_t _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00433                                               DBusPipe         *print_pid_pipe,
00434                                               dbus_pid_t        pid_to_write,
00435                                               DBusError        *error);
00436 
00437 dbus_bool_t _dbus_command_for_pid (unsigned long  pid,
00438                                    DBusString    *str,
00439                                    int            max_len,
00440                                    DBusError     *error);
00441 
00443 typedef void (* DBusSignalHandler) (int sig);
00444 
00445 void _dbus_set_signal_handler (int               sig,
00446                                DBusSignalHandler handler);
00447 
00448 dbus_bool_t _dbus_user_at_console (const char *username,
00449                                    DBusError  *error);
00450 
00451 void _dbus_init_system_log (dbus_bool_t is_daemon);
00452 
00453 typedef enum {
00454   DBUS_SYSTEM_LOG_INFO,
00455   DBUS_SYSTEM_LOG_SECURITY,
00456   DBUS_SYSTEM_LOG_FATAL
00457 } DBusSystemLogSeverity;
00458 
00459 void _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (2, 3);
00460 void _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args);
00461 
00462 /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables.
00463  * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy.
00464  */
00465 #if !defined (DBUS_VA_COPY)
00466 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00467 #    define DBUS_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00468 #  elif defined (DBUS_VA_COPY_AS_ARRAY)
00469 #    define DBUS_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
00470 #  else /* va_list is a pointer */
00471 #    define DBUS_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00472 #  endif /* va_list is a pointer */
00473 #endif /* !DBUS_VA_COPY */
00474 
00475 
00480 #define _DBUS_BYTE_OF_PRIMITIVE(p, i) \
00481     (((const char*)&(p))[(i)])
00482 
00487 #define _DBUS_DOUBLES_BITWISE_EQUAL(a, b)                                       \
00488      (_DBUS_BYTE_OF_PRIMITIVE (a, 0) == _DBUS_BYTE_OF_PRIMITIVE (b, 0) &&       \
00489       _DBUS_BYTE_OF_PRIMITIVE (a, 1) == _DBUS_BYTE_OF_PRIMITIVE (b, 1) &&       \
00490       _DBUS_BYTE_OF_PRIMITIVE (a, 2) == _DBUS_BYTE_OF_PRIMITIVE (b, 2) &&       \
00491       _DBUS_BYTE_OF_PRIMITIVE (a, 3) == _DBUS_BYTE_OF_PRIMITIVE (b, 3) &&       \
00492       _DBUS_BYTE_OF_PRIMITIVE (a, 4) == _DBUS_BYTE_OF_PRIMITIVE (b, 4) &&       \
00493       _DBUS_BYTE_OF_PRIMITIVE (a, 5) == _DBUS_BYTE_OF_PRIMITIVE (b, 5) &&       \
00494       _DBUS_BYTE_OF_PRIMITIVE (a, 6) == _DBUS_BYTE_OF_PRIMITIVE (b, 6) &&       \
00495       _DBUS_BYTE_OF_PRIMITIVE (a, 7) == _DBUS_BYTE_OF_PRIMITIVE (b, 7))
00496 
00497 dbus_bool_t _dbus_get_autolaunch_address (const char *scope,
00498                                           DBusString *address,
00499                                                               DBusError  *error);
00500 
00501 dbus_bool_t _dbus_lookup_session_address (dbus_bool_t *supported,
00502                                           DBusString  *address,
00503                                           DBusError   *error);
00504 
00508 typedef union DBusGUID DBusGUID;
00509 
00510 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
00511                                              dbus_bool_t       create_if_not_found,
00512                                              DBusError        *error);
00513 
00519 dbus_bool_t _dbus_threads_init_platform_specific (void);
00520 
00524 void _dbus_threads_lock_platform_specific (void);
00525 
00529 void _dbus_threads_unlock_platform_specific (void);
00530 
00531 dbus_bool_t _dbus_split_paths_and_append (DBusString *dirs, 
00532                                           const char *suffix, 
00533                                           DBusList **dir_list);
00534 
00535 unsigned long _dbus_pid_for_log (void);
00536 
00537 /* FIXME move back to dbus-sysdeps-unix.h probably -
00538  * the PID file handling just needs a little more abstraction
00539  * in the bus daemon first.
00540  */
00541 dbus_pid_t    _dbus_getpid (void);
00542 
00543 dbus_bool_t _dbus_change_to_daemon_user (const char *user,
00544                                          DBusError  *error);
00545 
00546 void _dbus_flush_caches (void);
00547 
00548 /*
00549  * replaces the term DBUS_PREFIX in configure_time_path by the
00550  * current dbus installation directory. On unix this function is a noop
00551  *
00552  * @param configure_time_path
00553  * @return real path
00554  */
00555 const char *
00556 _dbus_replace_install_prefix (const char *configure_time_path);
00557 
00558 /* Do not set this too high: it is a denial-of-service risk.
00559  * See <https://bugs.freedesktop.org/show_bug.cgi?id=82820>
00560  *
00561  * (This needs to be in the non-Unix-specific header so that
00562  * the config-parser can use it.)
00563  */
00564 #define DBUS_DEFAULT_MESSAGE_UNIX_FDS 16
00565 
00566 typedef struct DBusRLimit DBusRLimit;
00567 
00568 DBusRLimit     *_dbus_rlimit_save_fd_limit                 (DBusError    *error);
00569 dbus_bool_t     _dbus_rlimit_raise_fd_limit_if_privileged  (unsigned int  desired,
00570                                                             DBusError    *error);
00571 dbus_bool_t     _dbus_rlimit_restore_fd_limit              (DBusRLimit   *saved,
00572                                                             DBusError    *error);
00573 void            _dbus_rlimit_free                          (DBusRLimit   *lim);
00574 
00577 DBUS_END_DECLS
00578 
00579 
00580 #ifdef DBUS_WIN
00581 #include "dbus-sysdeps-win.h"
00582 #endif
00583 
00584 #endif /* DBUS_SYSDEPS_H */