diff options
author | Havoc Pennington <hp@redhat.com> | 2006-09-16 15:34:34 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2006-09-16 15:34:34 +0000 |
commit | 08079a5bdedd6ec91cda413983e4bd3b6ee0252d (patch) | |
tree | cbb7fe4a86f449c2595825c720557c955076a874 /dbus | |
parent | e24dec0a89a5853cb448fb3037f2658f2b682650 (diff) |
2006-09-16 Havoc Pennington <hp@redhat.com>
* dbus/dbus-sysdeps-unix.h: small change to Peter's patch to make
dbus-sysdeps-unix-util.c build, add unix-specific sysdeps header.
* dbus/dbus-sysdeps.h, dbus-sysdeps-unix.c: patch from Peter
Kümmel bug #8249 to make the sysdeps.h read/write/open/close
functions specifically for sockets only, and move generic
read/write/open/close into unix-specific code.
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/Makefile.am | 3 | ||||
-rw-r--r-- | dbus/dbus-server-debug-pipe.c | 6 | ||||
-rw-r--r-- | dbus/dbus-server-unix.c | 8 | ||||
-rw-r--r-- | dbus/dbus-spawn.c | 10 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-unix.c | 64 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-util-unix.c | 3 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.h | 79 | ||||
-rw-r--r-- | dbus/dbus-transport-unix.c | 56 |
8 files changed, 150 insertions, 79 deletions
diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 77ce2a9d..996abe8c 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -104,8 +104,9 @@ DBUS_SHARED_SOURCES= \ dbus-string.h \ dbus-string-private.h \ dbus-sysdeps.c \ - dbus-sysdeps-unix.c \ dbus-sysdeps.h \ + dbus-sysdeps-unix.c \ + dbus-sysdeps-unix.h \ dbus-userdb.c \ dbus-userdb.h diff --git a/dbus/dbus-server-debug-pipe.c b/dbus/dbus-server-debug-pipe.c index 0917c1e0..20c68a8c 100644 --- a/dbus/dbus-server-debug-pipe.c +++ b/dbus/dbus-server-debug-pipe.c @@ -260,8 +260,8 @@ _dbus_transport_debug_pipe_new (const char *server_name, NULL, &address); if (client_transport == NULL) { - _dbus_close (client_fd, NULL); - _dbus_close (server_fd, NULL); + _dbus_close_socket (client_fd, NULL); + _dbus_close_socket (server_fd, NULL); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&address); return NULL; @@ -276,7 +276,7 @@ _dbus_transport_debug_pipe_new (const char *server_name, if (server_transport == NULL) { _dbus_transport_unref (client_transport); - _dbus_close (server_fd, NULL); + _dbus_close_socket (server_fd, NULL); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 64b4598a..377dfb03 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -102,7 +102,7 @@ handle_new_client_fd_and_unlock (DBusServer *server, transport = _dbus_transport_new_for_fd (client_fd, &server->guid_hex, NULL); if (transport == NULL) { - _dbus_close (client_fd, NULL); + _dbus_close_socket (client_fd, NULL); SERVER_UNLOCK (server); return FALSE; } @@ -219,7 +219,7 @@ unix_disconnect (DBusServer *server) unix_server->watch = NULL; } - _dbus_close (unix_server->fd, NULL); + _dbus_close_socket (unix_server->fd, NULL); unix_server->fd = -1; if (unix_server->socket_name != NULL) @@ -373,7 +373,7 @@ _dbus_server_new_for_domain_socket (const char *path, return server; failed_2: - _dbus_close (listen_fd, NULL); + _dbus_close_socket (listen_fd, NULL); failed_1: dbus_free (path_copy); failed_0: @@ -436,7 +436,7 @@ _dbus_server_new_for_tcp_socket (const char *host, if (server == NULL) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - _dbus_close (listen_fd, NULL); + _dbus_close_socket (listen_fd, NULL); _dbus_string_free (&address); return NULL; } diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index f01ada27..b0a4fa2b 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -276,13 +276,13 @@ _dbus_babysitter_unref (DBusBabysitter *sitter) * a hangup and then the babysitter will * quit itself. */ - _dbus_close (sitter->socket_to_babysitter, NULL); + _dbus_close_socket (sitter->socket_to_babysitter, NULL); sitter->socket_to_babysitter = -1; } if (sitter->error_pipe_from_child >= 0) { - _dbus_close (sitter->error_pipe_from_child, NULL); + _dbus_close_socket (sitter->error_pipe_from_child, NULL); sitter->error_pipe_from_child = -1; } @@ -470,7 +470,7 @@ static void close_socket_to_babysitter (DBusBabysitter *sitter) { _dbus_verbose ("Closing babysitter\n"); - _dbus_close (sitter->socket_to_babysitter, NULL); + _dbus_close_socket (sitter->socket_to_babysitter, NULL); sitter->socket_to_babysitter = -1; } @@ -478,7 +478,7 @@ static void close_error_pipe_from_child (DBusBabysitter *sitter) { _dbus_verbose ("Closing child error\n"); - _dbus_close (sitter->error_pipe_from_child, NULL); + _dbus_close_socket (sitter->error_pipe_from_child, NULL); sitter->error_pipe_from_child = -1; } @@ -753,7 +753,7 @@ close_and_invalidate (int *fd) return -1; else { - ret = _dbus_close (*fd, NULL); + ret = _dbus_close_socket (*fd, NULL); *fd = -1; } diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 51898443..1ce928af 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -24,6 +24,7 @@ #include "dbus-internals.h" #include "dbus-sysdeps.h" +#include "dbus-sysdeps-unix.h" #include "dbus-threads.h" #include "dbus-protocol.h" #include "dbus-string.h" @@ -76,6 +77,54 @@ * @{ */ +dbus_bool_t +_dbus_open_socket (int *fd, + int domain, + int type, + int protocol) +{ + *fd = socket (domain, type, protocol); + return fd >= 0; +} + +dbus_bool_t +_dbus_close_socket (int fd, + DBusError *error) +{ + return _dbus_close (fd, error); +} + +int +_dbus_read_socket (int fd, + DBusString *buffer, + int count) +{ + return _dbus_read (fd, buffer, count); +} + +int +_dbus_write_socket (int fd, + const DBusString *buffer, + int start, + int len) +{ + return _dbus_write (fd, buffer, start, len); +} + +int +_dbus_write_socket_two (int fd, + const DBusString *buffer1, + int start1, + int len1, + const DBusString *buffer2, + int start2, + int len2) +{ + return _dbus_write_two (fd, buffer1, start1, len1, + buffer2, start2, len2); +} + + /** * Thin wrapper around the read() system call that appends * the data it reads to the DBusString buffer. It appends @@ -306,9 +355,8 @@ _dbus_connect_unix_socket (const char *path, _dbus_verbose ("connecting to unix socket %s abstract=%d\n", path, abstract); - fd = socket (PF_UNIX, SOCK_STREAM, 0); - if (fd < 0) + if (!_dbus_open_socket (&fd, PF_UNIX, SOCK_STREAM, 0)) { dbus_set_error (error, _dbus_error_from_errno (errno), @@ -442,9 +490,7 @@ _dbus_listen_unix_socket (const char *path, _dbus_verbose ("listening on unix socket %s abstract=%d\n", path, abstract); - listen_fd = socket (PF_UNIX, SOCK_STREAM, 0); - - if (listen_fd < 0) + if (!_dbus_open_socket (&listen_fd, PF_UNIX, SOCK_STREAM, 0)) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to create socket \"%s\": %s", @@ -578,10 +624,9 @@ _dbus_connect_tcp_socket (const char *host, struct in_addr *haddr; _DBUS_ASSERT_ERROR_IS_CLEAR (error); + - fd = socket (AF_INET, SOCK_STREAM, 0); - - if (fd < 0) + if (!_dbus_open_socket (&fd, AF_INET, SOCK_STREAM, 0)) { dbus_set_error (error, _dbus_error_from_errno (errno), @@ -658,9 +703,8 @@ _dbus_listen_tcp_socket (const char *host, _DBUS_ASSERT_ERROR_IS_CLEAR (error); - listen_fd = socket (AF_INET, SOCK_STREAM, 0); - if (listen_fd < 0) + if (!_dbus_open_socket (&listen_fd, AF_INET, SOCK_STREAM, 0)) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to create socket \"%s:%d\": %s", diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index a99d338e..aa2f4ad2 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -22,6 +22,7 @@ * */ #include "dbus-sysdeps.h" +#include "dbus-sysdeps-unix.h" #include "dbus-internals.h" #include "dbus-protocol.h" #include "dbus-string.h" @@ -152,7 +153,7 @@ _dbus_become_daemon (const DBusString *pidfile, } bytes = _dbus_string_get_length (&pid); - if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes) + if (_dbus_write_socket (print_pid_fd, &pid, 0, bytes) != bytes) { dbus_set_error (error, DBUS_ERROR_FAILED, "Printing message bus PID: %s\n", diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 3df40c78..729f14d2 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -74,20 +74,6 @@ const char* _dbus_getenv (const char *varname); dbus_bool_t _dbus_setenv (const char *varname, const char *value); -int _dbus_read (int fd, - DBusString *buffer, - int count); -int _dbus_write (int fd, - const DBusString *buffer, - int start, - int len); -int _dbus_write_two (int fd, - const DBusString *buffer1, - int start1, - int len1, - const DBusString *buffer2, - int start2, - int len2); typedef unsigned long dbus_pid_t; typedef unsigned long dbus_uid_t; @@ -101,15 +87,39 @@ typedef unsigned long dbus_gid_t; #define DBUS_UID_FORMAT "%lu" #define DBUS_GID_FORMAT "%lu" + /** - * Struct representing socket credentials + * Socket interface + * + * @todo Use for the file descriptors a struct + * - struct DBusSocket{ int d; }; - + * instead of int to get type-safety which + * will be checked by the compiler. + * */ -typedef struct -{ - dbus_pid_t pid; /**< process ID or DBUS_PID_UNSET */ - dbus_uid_t uid; /**< user ID or DBUS_UID_UNSET */ - dbus_gid_t gid; /**< group ID or DBUS_GID_UNSET */ -} DBusCredentials; + +dbus_bool_t _dbus_open_socket (int *fd, + int domain, + int type, + int protocol); +dbus_bool_t _dbus_close_socket (int fd, + DBusError *error); +int _dbus_read_socket (int fd, + DBusString *buffer, + int count); +int _dbus_write_socket (int fd, + const DBusString *buffer, + int start, + int len); +int _dbus_write_socket_two (int fd, + const DBusString *buffer1, + int start1, + int len1, + const DBusString *buffer2, + int start2, + int len2); + + int _dbus_connect_unix_socket (const char *path, dbus_bool_t abstract, @@ -125,6 +135,16 @@ int _dbus_listen_tcp_socket (const char *host, DBusError *error); int _dbus_accept (int listen_fd); +/** + * Struct representing socket credentials + */ +typedef struct +{ + dbus_pid_t pid; /**< process ID or DBUS_PID_UNSET */ + dbus_uid_t uid; /**< user ID or DBUS_UID_UNSET */ + dbus_gid_t gid; /**< group ID or DBUS_GID_UNSET */ +} DBusCredentials; + dbus_bool_t _dbus_read_credentials_unix_socket (int client_fd, DBusCredentials *credentials, DBusError *error); @@ -223,7 +243,10 @@ void _dbus_sleep_milliseconds (int milliseconds); void _dbus_get_current_time (long *tv_sec, long *tv_usec); - +/** + * File/directory interface + */ +dbus_bool_t _dbus_file_exists (const char *file); dbus_bool_t _dbus_file_get_contents (DBusString *str, const DBusString *filename, DBusError *error); @@ -258,6 +281,13 @@ void _dbus_directory_close (DBusDirIter *iter); dbus_bool_t _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error); +void _dbus_fd_set_close_on_exec (int fd); + +const char* _dbus_get_tmpdir (void); + +/** + * Random numbers + */ void _dbus_generate_pseudorandom_bytes_buffer (char *buffer, int n_bytes); void _dbus_generate_random_bytes_buffer (char *buffer, @@ -272,7 +302,6 @@ const char* _dbus_error_from_errno (int error_number); void _dbus_disable_sigpipe (void); -void _dbus_fd_set_close_on_exec (int fd); void _dbus_exit (int code) _DBUS_GNUC_NORETURN; @@ -302,8 +331,6 @@ dbus_bool_t _dbus_full_duplex_pipe (int *fd1, int *fd2, dbus_bool_t blocking, DBusError *error); -dbus_bool_t _dbus_close (int fd, - DBusError *error); void _dbus_print_backtrace (void); @@ -322,10 +349,8 @@ typedef void (* DBusSignalHandler) (int sig); void _dbus_set_signal_handler (int sig, DBusSignalHandler handler); -dbus_bool_t _dbus_file_exists (const char *file); dbus_bool_t _dbus_user_at_console (const char *username, DBusError *error); -const char* _dbus_get_tmpdir (void); /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. * config.h may have already defined DBUS_VA_COPY as va_copy or __va_copy. diff --git a/dbus/dbus-transport-unix.c b/dbus/dbus-transport-unix.c index 5536e923..55a89a5f 100644 --- a/dbus/dbus-transport-unix.c +++ b/dbus/dbus-transport-unix.c @@ -250,8 +250,8 @@ read_data_into_auth (DBusTransport *transport, _dbus_auth_get_buffer (transport->auth, &buffer); - bytes_read = _dbus_read (unix_transport->fd, - buffer, unix_transport->max_bytes_read_per_iteration); + bytes_read = _dbus_read_socket (unix_transport->fd, + buffer, unix_transport->max_bytes_read_per_iteration); _dbus_auth_return_buffer (transport->auth, buffer, bytes_read > 0 ? bytes_read : 0); @@ -305,9 +305,9 @@ write_data_from_auth (DBusTransport *transport) &buffer)) return FALSE; - bytes_written = _dbus_write (unix_transport->fd, - buffer, - 0, _dbus_string_get_length (buffer)); + bytes_written = _dbus_write_socket (unix_transport->fd, + buffer, + 0, _dbus_string_get_length (buffer)); if (bytes_written > 0) { @@ -556,10 +556,10 @@ do_writing (DBusTransport *transport) #endif bytes_written = - _dbus_write (unix_transport->fd, - &unix_transport->encoded_outgoing, - unix_transport->message_bytes_written, - total_bytes_to_write - unix_transport->message_bytes_written); + _dbus_write_socket (unix_transport->fd, + &unix_transport->encoded_outgoing, + unix_transport->message_bytes_written, + total_bytes_to_write - unix_transport->message_bytes_written); } else { @@ -573,21 +573,21 @@ do_writing (DBusTransport *transport) if (unix_transport->message_bytes_written < header_len) { bytes_written = - _dbus_write_two (unix_transport->fd, - header, - unix_transport->message_bytes_written, - header_len - unix_transport->message_bytes_written, - body, - 0, body_len); + _dbus_write_socket_two (unix_transport->fd, + header, + unix_transport->message_bytes_written, + header_len - unix_transport->message_bytes_written, + body, + 0, body_len); } else { bytes_written = - _dbus_write (unix_transport->fd, - body, - (unix_transport->message_bytes_written - header_len), - body_len - - (unix_transport->message_bytes_written - header_len)); + _dbus_write_socket (unix_transport->fd, + body, + (unix_transport->message_bytes_written - header_len), + body_len - + (unix_transport->message_bytes_written - header_len)); } } @@ -682,9 +682,9 @@ do_reading (DBusTransport *transport) if (_dbus_string_get_length (&unix_transport->encoded_incoming) > 0) bytes_read = _dbus_string_get_length (&unix_transport->encoded_incoming); else - bytes_read = _dbus_read (unix_transport->fd, - &unix_transport->encoded_incoming, - unix_transport->max_bytes_read_per_iteration); + bytes_read = _dbus_read_socket (unix_transport->fd, + &unix_transport->encoded_incoming, + unix_transport->max_bytes_read_per_iteration); _dbus_assert (_dbus_string_get_length (&unix_transport->encoded_incoming) == bytes_read); @@ -719,8 +719,8 @@ do_reading (DBusTransport *transport) _dbus_message_loader_get_buffer (transport->loader, &buffer); - bytes_read = _dbus_read (unix_transport->fd, - buffer, unix_transport->max_bytes_read_per_iteration); + bytes_read = _dbus_read_socket (unix_transport->fd, + buffer, unix_transport->max_bytes_read_per_iteration); _dbus_message_loader_return_buffer (transport->loader, buffer, @@ -882,7 +882,7 @@ unix_disconnect (DBusTransport *transport) free_watches (transport); - _dbus_close (unix_transport->fd, NULL); + _dbus_close_socket (unix_transport->fd, NULL); unix_transport->fd = -1; } @@ -1247,7 +1247,7 @@ _dbus_transport_new_for_domain_socket (const char *path, return transport; failed_1: - _dbus_close (fd, NULL); + _dbus_close_socket (fd, NULL); failed_0: _dbus_string_free (&address); return NULL; @@ -1307,7 +1307,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, if (transport == NULL) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - _dbus_close (fd, NULL); + _dbus_close_socket (fd, NULL); _dbus_string_free (&address); fd = -1; } |