1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
4 * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-connection-internal.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-transport-socket.h"
28 #include "dbus-transport-protected.h"
29 #include "dbus-watch.h"
30 #include "dbus-sysdeps-unix.h"
33 * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
34 * @ingroup DBusInternals
35 * @brief Implementation details of DBusTransport on UNIX
41 * Creates a new transport for the given Unix domain socket
42 * path. This creates a client-side of a transport.
44 * @todo once we add a way to escape paths in a dbus
45 * address, this function needs to do escaping.
47 * @param path the path to the domain socket.
48 * @param abstract #TRUE to use abstract socket namespace
49 * @param error address where an error can be returned.
50 * @returns a new transport, or #NULL on failure.
53 _dbus_transport_new_for_domain_socket (const char *path,
58 DBusTransport *transport;
61 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
63 if (!_dbus_string_init (&address))
65 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
72 !_dbus_string_append (&address, "unix:abstract=")) ||
74 !_dbus_string_append (&address, "unix:path=")) ||
75 !_dbus_string_append (&address, path))
77 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
81 fd = _dbus_connect_unix_socket (path, abstract, error);
84 _DBUS_ASSERT_ERROR_IS_SET (error);
88 _dbus_fd_set_close_on_exec (fd);
90 _dbus_verbose ("Successfully connected to unix socket %s\n",
93 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
94 if (transport == NULL)
96 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
100 _dbus_string_free (&address);
105 _dbus_close_socket (fd, NULL);
107 _dbus_string_free (&address);
111 DBusTransportOpenResult
112 _dbus_transport_open_platform_specific (DBusAddressEntry *entry,
113 DBusTransport **transport_p,
118 method = dbus_address_entry_get_method (entry);
119 _dbus_assert (method != NULL);
121 if (strcmp (method, "unix") == 0)
123 const char *path = dbus_address_entry_get_value (entry, "path");
124 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
125 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
129 _dbus_set_bad_address (error, NULL, NULL,
130 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
131 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
134 if (path == NULL && abstract == NULL)
136 _dbus_set_bad_address (error, "unix",
139 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
142 if (path != NULL && abstract != NULL)
144 _dbus_set_bad_address (error, NULL, NULL,
145 "can't specify both \"path\" and \"abstract\" options in an address");
146 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
150 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
153 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
155 if (*transport_p == NULL)
157 _DBUS_ASSERT_ERROR_IS_SET (error);
158 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
162 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
163 return DBUS_TRANSPORT_OPEN_OK;
168 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
169 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;