mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2026-06-19 16:38:30 +02:00
dbus: change unix socket functions to return DBusSocket
This will allow to abstract away for Windows Unix socket support. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
parent
81b49af8a2
commit
f8343fa397
4 changed files with 58 additions and 51 deletions
|
|
@ -350,9 +350,9 @@ _dbus_server_new_for_domain_socket (const char *path,
|
|||
}
|
||||
}
|
||||
|
||||
listen_fd.fd = _dbus_listen_unix_socket (path, abstract, error);
|
||||
listen_fd = _dbus_listen_unix_socket (path, abstract, error);
|
||||
|
||||
if (listen_fd.fd < 0)
|
||||
if (!_dbus_socket_is_valid (listen_fd))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
goto failed_1;
|
||||
|
|
|
|||
|
|
@ -964,14 +964,14 @@ _dbus_write_two (int fd,
|
|||
* @param path the path to UNIX domain socket
|
||||
* @param abstract #TRUE to use abstract namespace
|
||||
* @param error return location for error code
|
||||
* @returns connection file descriptor or -1 on error
|
||||
* @returns a valid socket on success or an invalid socket on error
|
||||
*/
|
||||
int
|
||||
DBusSocket
|
||||
_dbus_connect_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error)
|
||||
{
|
||||
int fd;
|
||||
DBusSocket fd = DBUS_SOCKET_INIT;
|
||||
size_t path_len;
|
||||
struct sockaddr_un addr;
|
||||
_DBUS_STATIC_ASSERT (sizeof (addr.sun_path) > _DBUS_MAX_SUN_PATH_LENGTH);
|
||||
|
|
@ -982,10 +982,10 @@ _dbus_connect_unix_socket (const char *path,
|
|||
path, abstract);
|
||||
|
||||
|
||||
if (!_dbus_open_unix_socket (&fd, error))
|
||||
if (!_dbus_open_unix_socket (&fd.fd, error))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET(error);
|
||||
return -1;
|
||||
return fd;
|
||||
}
|
||||
_DBUS_ASSERT_ERROR_IS_CLEAR(error);
|
||||
|
||||
|
|
@ -1003,8 +1003,8 @@ _dbus_connect_unix_socket (const char *path,
|
|||
{
|
||||
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
|
||||
"Abstract socket name too long\n");
|
||||
_dbus_close (fd, NULL);
|
||||
return -1;
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
return fd;
|
||||
}
|
||||
|
||||
strncpy (&addr.sun_path[1], path, sizeof (addr.sun_path) - 2);
|
||||
|
|
@ -1012,8 +1012,8 @@ _dbus_connect_unix_socket (const char *path,
|
|||
#else /* !__linux__ */
|
||||
dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
|
||||
"Operating system does not support abstract socket namespace\n");
|
||||
_dbus_close (fd, NULL);
|
||||
return -1;
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
return fd;
|
||||
#endif /* !__linux__ */
|
||||
}
|
||||
else
|
||||
|
|
@ -1022,30 +1022,30 @@ _dbus_connect_unix_socket (const char *path,
|
|||
{
|
||||
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
|
||||
"Socket name too long\n");
|
||||
_dbus_close (fd, NULL);
|
||||
return -1;
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
return fd;
|
||||
}
|
||||
|
||||
strncpy (addr.sun_path, path, sizeof (addr.sun_path) - 1);
|
||||
}
|
||||
|
||||
if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
|
||||
if (connect (fd.fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
|
||||
{
|
||||
dbus_set_error (error,
|
||||
_dbus_error_from_errno (errno),
|
||||
"Failed to connect to socket %s: %s",
|
||||
path, _dbus_strerror (errno));
|
||||
|
||||
_dbus_close (fd, NULL);
|
||||
return -1;
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
return fd;
|
||||
}
|
||||
|
||||
if (!_dbus_set_fd_nonblocking (fd, error))
|
||||
if (!_dbus_set_fd_nonblocking (fd.fd, error))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
|
||||
_dbus_close (fd, NULL);
|
||||
return -1;
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
return fd;
|
||||
}
|
||||
|
||||
return fd;
|
||||
|
|
@ -1061,13 +1061,14 @@ _dbus_connect_unix_socket (const char *path,
|
|||
* @param argv the argument list for the process to execute.
|
||||
* argv[0] typically is identical to the path of the executable
|
||||
* @param error return location for error code
|
||||
* @returns connection file descriptor or -1 on error
|
||||
* @returns a valid socket on success or an invalid socket on error
|
||||
*/
|
||||
int
|
||||
DBusSocket
|
||||
_dbus_connect_exec (const char *path,
|
||||
char *const argv[],
|
||||
DBusError *error)
|
||||
{
|
||||
DBusSocket s = DBUS_SOCKET_INIT;
|
||||
int fds[2];
|
||||
pid_t pid;
|
||||
int retval;
|
||||
|
|
@ -1093,7 +1094,8 @@ _dbus_connect_exec (const char *path,
|
|||
_dbus_error_from_errno (errno),
|
||||
"Failed to create socket pair: %s",
|
||||
_dbus_strerror (errno));
|
||||
return -1;
|
||||
_dbus_assert (!_dbus_socket_is_valid (s));
|
||||
return s;
|
||||
}
|
||||
|
||||
if (!cloexec_done)
|
||||
|
|
@ -1116,7 +1118,8 @@ _dbus_connect_exec (const char *path,
|
|||
path, _dbus_strerror (errno));
|
||||
close (fds[0]);
|
||||
close (fds[1]);
|
||||
return -1;
|
||||
_dbus_assert (!_dbus_socket_is_valid (s));
|
||||
return s;
|
||||
}
|
||||
|
||||
if (pid == 0)
|
||||
|
|
@ -1151,10 +1154,12 @@ _dbus_connect_exec (const char *path,
|
|||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
|
||||
close (fds[0]);
|
||||
return -1;
|
||||
_dbus_assert (!_dbus_socket_is_valid (s));
|
||||
return s;
|
||||
}
|
||||
|
||||
return fds[0];
|
||||
s.fd = fds[0];
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1172,13 +1177,14 @@ _dbus_connect_exec (const char *path,
|
|||
* @param path the socket name
|
||||
* @param abstract #TRUE to use abstract namespace
|
||||
* @param error return location for errors
|
||||
* @returns the listening file descriptor or -1 on error
|
||||
* @returns a valid socket on success or an invalid socket on error
|
||||
*/
|
||||
int
|
||||
DBusSocket
|
||||
_dbus_listen_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error)
|
||||
{
|
||||
DBusSocket s = DBUS_SOCKET_INIT;
|
||||
int listen_fd;
|
||||
struct sockaddr_un addr;
|
||||
size_t path_len;
|
||||
|
|
@ -1192,7 +1198,7 @@ _dbus_listen_unix_socket (const char *path,
|
|||
if (!_dbus_open_unix_socket (&listen_fd, error))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET(error);
|
||||
return -1;
|
||||
return s;
|
||||
}
|
||||
_DBUS_ASSERT_ERROR_IS_CLEAR(error);
|
||||
|
||||
|
|
@ -1214,7 +1220,7 @@ _dbus_listen_unix_socket (const char *path,
|
|||
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
|
||||
"Abstract socket name too long\n");
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
return s;
|
||||
}
|
||||
|
||||
strncpy (&addr.sun_path[1], path, sizeof (addr.sun_path) - 2);
|
||||
|
|
@ -1223,7 +1229,7 @@ _dbus_listen_unix_socket (const char *path,
|
|||
dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
|
||||
"Operating system does not support abstract socket namespace\n");
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
return s;
|
||||
#endif /* !__linux__ */
|
||||
}
|
||||
else
|
||||
|
|
@ -1251,8 +1257,8 @@ _dbus_listen_unix_socket (const char *path,
|
|||
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
|
||||
"Socket name too long\n");
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
strncpy (addr.sun_path, path, sizeof (addr.sun_path) - 1);
|
||||
}
|
||||
|
|
@ -1263,7 +1269,7 @@ _dbus_listen_unix_socket (const char *path,
|
|||
"Failed to bind socket \"%s\": %s",
|
||||
path, _dbus_strerror (errno));
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
return s;
|
||||
}
|
||||
|
||||
if (listen (listen_fd, SOMAXCONN /* backlog */) < 0)
|
||||
|
|
@ -1272,14 +1278,14 @@ _dbus_listen_unix_socket (const char *path,
|
|||
"Failed to listen on socket \"%s\": %s",
|
||||
path, _dbus_strerror (errno));
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
return s;
|
||||
}
|
||||
|
||||
if (!_dbus_set_fd_nonblocking (listen_fd, error))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
_dbus_close (listen_fd, NULL);
|
||||
return -1;
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Try opening up the permissions, but if we can't, just go ahead
|
||||
|
|
@ -1288,7 +1294,8 @@ _dbus_listen_unix_socket (const char *path,
|
|||
if (!abstract && chmod (path, 0777) < 0)
|
||||
_dbus_warn ("Could not set mode 0777 on socket %s", path);
|
||||
|
||||
return listen_fd;
|
||||
s.fd = listen_fd;
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -66,16 +66,16 @@ _dbus_write_two (int fd,
|
|||
int start2,
|
||||
int len2);
|
||||
|
||||
int _dbus_connect_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error);
|
||||
int _dbus_listen_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error);
|
||||
DBusSocket _dbus_connect_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error);
|
||||
DBusSocket _dbus_listen_unix_socket (const char *path,
|
||||
dbus_bool_t abstract,
|
||||
DBusError *error);
|
||||
|
||||
int _dbus_connect_exec (const char *path,
|
||||
char *const argv[],
|
||||
DBusError *error);
|
||||
DBusSocket _dbus_connect_exec (const char *path,
|
||||
char *const argv[],
|
||||
DBusError *error);
|
||||
|
||||
int _dbus_listen_systemd_sockets (DBusSocket **fd,
|
||||
DBusError *error);
|
||||
|
|
|
|||
|
|
@ -80,9 +80,9 @@ _dbus_transport_new_for_domain_socket (const char *path,
|
|||
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
|
||||
goto failed_0;
|
||||
}
|
||||
|
||||
fd.fd = _dbus_connect_unix_socket (path, abstract, error);
|
||||
if (fd.fd < 0)
|
||||
|
||||
fd = _dbus_connect_unix_socket (path, abstract, error);
|
||||
if (!_dbus_socket_is_valid (fd))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
goto failed_0;
|
||||
|
|
@ -180,8 +180,8 @@ _dbus_transport_new_for_exec (const char *path,
|
|||
}
|
||||
}
|
||||
|
||||
fd.fd = _dbus_connect_exec (path, argv, error);
|
||||
if (fd.fd < 0)
|
||||
fd = _dbus_connect_exec (path, argv, error);
|
||||
if (!_dbus_socket_is_valid (fd))
|
||||
{
|
||||
_DBUS_ASSERT_ERROR_IS_SET (error);
|
||||
goto failed;
|
||||
|
|
@ -202,7 +202,7 @@ _dbus_transport_new_for_exec (const char *path,
|
|||
return transport;
|
||||
|
||||
failed:
|
||||
if (fd.fd >= 0)
|
||||
if (_dbus_socket_is_valid (fd))
|
||||
_dbus_close_socket (&fd, NULL);
|
||||
|
||||
_dbus_string_free (&address);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue