dbus-transport-unix.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
*
* Copyright (C) 2002, 2003, 2004 Red Hat Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include <stdio.h>
#include "dbus-internals.h"
#include "dbus-connection-internal.h"
#include "dbus-transport-unix.h"
#include "dbus-transport-socket.h"
#include "dbus-transport-protected.h"
#include "dbus-watch.h"
#include "dbus-sysdeps-unix.h"
#include "dbus-test.h"
static DBusTransport*
_dbus_transport_new_for_exec (const char *path,
char *const argv[],
DBusError *error)
{
DBusSocket fd = DBUS_SOCKET_INIT;
DBusTransport *transport;
DBusString address;
unsigned i;
char *escaped;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (!_dbus_string_init (&address))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
escaped = dbus_address_escape_value (path);
if (!escaped)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto failed;
}
if (!_dbus_string_append (&address, "unixexec:path=") ||
!_dbus_string_append (&address, escaped))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
dbus_free (escaped);
goto failed;
}
dbus_free (escaped);
if (argv)
{
for (i = 0; argv[i]; i++)
{
dbus_bool_t success;
escaped = dbus_address_escape_value (argv[i]);
if (!escaped)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto failed;
}
success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
dbus_free (escaped);
if (!success)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto failed;
}
}
}
fd = _dbus_connect_exec (path, argv, error);
if (!_dbus_socket_is_valid (fd))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto failed;
}
_dbus_verbose ("Successfully connected to process %s\n",
path);
transport = _dbus_transport_new_for_socket (fd, NULL, &address);
if (transport == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto failed;
}
_dbus_string_free (&address);
return transport;
failed:
if (_dbus_socket_is_valid (fd))
_dbus_close_socket (&fd, NULL);
_dbus_string_free (&address);
return NULL;
}
DBusTransportOpenResult
_dbus_transport_open_unixexec (DBusAddressEntry *entry,
DBusTransport **transport_p,
DBusError *error)
{
const char *method;
method = dbus_address_entry_get_method (entry);
_dbus_assert (method != NULL);
if (strcmp (method, "unixexec") == 0)
{
const char *path;
unsigned i;
char **argv;
path = dbus_address_entry_get_value (entry, "path");
if (path == NULL)
{
_dbus_set_bad_address (error, NULL, NULL,
"No process path specified");
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
}
/* First count argv arguments */
for (i = 1; ; i++)
{
char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
snprintf (t, sizeof(t), "argv%u", i);
if (!dbus_address_entry_get_value (entry, t))
break;
}
/* Allocate string array */
argv = dbus_new0 (char*, i+1);
if (!argv)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
/* Fill in string array */
for (i = 0; ; i++)
{
char t[4+20+1];
const char *p;
snprintf (t, sizeof(t), "argv%u", i);
p = dbus_address_entry_get_value (entry, t);
if (!p)
{
if (i == 0)
/* If argv0 isn't specified, fill in the path instead */
p = path;
else
break;
}
argv[i] = _dbus_strdup (p);
if (!argv[i])
{
dbus_free_string_array (argv);
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
}
*transport_p = _dbus_transport_new_for_exec (path, argv, error);
dbus_free_string_array (argv);
if (*transport_p == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_OK;
}
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
}
}
DBusTransportOpenResult
_dbus_transport_open_platform_specific (DBusAddressEntry *entry,
DBusTransport **transport_p,
DBusError *error)
{
#ifdef DBUS_ENABLE_LAUNCHD
const char *method;
method = dbus_address_entry_get_method (entry);
_dbus_assert (method != NULL);
if (strcmp (method, "launchd") == 0)
{
DBusError tmp_error = DBUS_ERROR_INIT;
const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
const char *launchd_socket;
DBusString socket_path;
dbus_bool_t valid_socket;
if (!_dbus_string_init (&socket_path))
{
_DBUS_SET_OOM (error);
return FALSE;
}
if (launchd_env_var == NULL)
{
_dbus_set_bad_address (error, "launchd", "env", NULL);
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
}
valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
if (dbus_error_is_set(error))
{
_dbus_string_free(&socket_path);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
if (!valid_socket)
{
dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS,
"launchd's env var %s does not exist", launchd_env_var);
dbus_error_free(error);
dbus_move_error(&tmp_error, error);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
launchd_socket = _dbus_string_get_const_data(&socket_path);
*transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
if (*transport_p == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_OK;
}
}
else
#endif /* DBUS_ENABLE_LAUNCHD */
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
}
}
_dbus_set_bad_address
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition dbus-address.c:70
dbus_address_entry_get_method
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition dbus-address.c:232
dbus_address_escape_value
char * dbus_address_escape_value(const char *value)
Escapes the given string as a value in a key=value pair for a D-Bus address.
Definition dbus-address.c:588
dbus_address_entry_get_value
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition dbus-address.c:249
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition dbus-errors.h:64
dbus_move_error
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition dbus-errors.c:281
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_free
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition dbus-errors.c:213
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition dbus-errors.c:331
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition dbus-internals.c:621
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
dbus_free_string_array
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition dbus-memory.c:758
DBUS_ERROR_BAD_ADDRESS
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
Definition dbus-protocol.h:373
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition dbus-string.c:980
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_append_printf
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition dbus-string.c:1147
_dbus_lookup_launchd_socket
dbus_bool_t _dbus_lookup_launchd_socket(DBusString *socket_path, const char *launchd_env_var, DBusError *error)
quries launchd for a specific env var which holds the socket path.
Definition dbus-sysdeps-unix.c:4486
_dbus_close_socket
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
Definition dbus-sysdeps-unix.c:314
_dbus_connect_exec
DBusSocket _dbus_connect_exec(const char *path, char *const argv[], DBusError *error)
Creates a UNIX domain socket and connects it to the specified process to execute.
Definition dbus-sysdeps-unix.c:1054
_dbus_transport_new_for_socket
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
Definition dbus-transport-socket.c:1295
_dbus_transport_new_for_domain_socket
DBusTransport * _dbus_transport_new_for_domain_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a new transport for the given Unix domain socket path.
Definition dbus-transport-socket.c:1518
_dbus_transport_open_platform_specific
DBusTransportOpenResult _dbus_transport_open_platform_specific(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens platform specific transport types.
Definition dbus-transport-unix.c:248
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusAddressEntry
Internals of DBusAddressEntry.
Definition dbus-address.c:49
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusSocket
Socket interface.
Definition dbus-sysdeps.h:185
DBusString
Definition dbus-string.h:47
DBusTransport
Object representing a transport such as a socket.
Definition dbus-transport-protected.h:83