Merge branch 'my-dbus-1.2'
[platform/upstream/dbus.git] / dbus / dbus-transport-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
3  *
4  * Copyright (C) 2002, 2003, 2004  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
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"
31
32 /**
33  * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
34  * @ingroup  DBusInternals
35  * @brief Implementation details of DBusTransport on UNIX
36  *
37  * @{
38  */
39
40 /**
41  * Creates a new transport for the given Unix domain socket
42  * path. This creates a client-side of a transport.
43  *
44  * @todo once we add a way to escape paths in a dbus
45  * address, this function needs to do escaping.
46  *
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.
51  */
52 DBusTransport*
53 _dbus_transport_new_for_domain_socket (const char     *path,
54                                        dbus_bool_t     abstract,
55                                        DBusError      *error)
56 {
57   int fd;
58   DBusTransport *transport;
59   DBusString address;
60   
61   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
62
63   if (!_dbus_string_init (&address))
64     {
65       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
66       return NULL;
67     }
68
69   fd = -1;
70
71   if ((abstract &&
72        !_dbus_string_append (&address, "unix:abstract=")) ||
73       (!abstract &&
74        !_dbus_string_append (&address, "unix:path=")) ||
75       !_dbus_string_append (&address, path))
76     {
77       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
78       goto failed_0;
79     }
80   
81   fd = _dbus_connect_unix_socket (path, abstract, error);
82   if (fd < 0)
83     {
84       _DBUS_ASSERT_ERROR_IS_SET (error);
85       goto failed_0;
86     }
87
88   _dbus_verbose ("Successfully connected to unix socket %s\n",
89                  path);
90
91   transport = _dbus_transport_new_for_socket (fd, NULL, &address);
92   if (transport == NULL)
93     {
94       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
95       goto failed_1;
96     }
97   
98   _dbus_string_free (&address);
99   
100   return transport;
101
102  failed_1:
103   _dbus_close_socket (fd, NULL);
104  failed_0:
105   _dbus_string_free (&address);
106   return NULL;
107 }
108
109 /**
110  * Opens platform specific transport types.
111  * 
112  * @param entry the address entry to try opening
113  * @param transport_p return location for the opened transport
114  * @param error error to be set
115  * @returns result of the attempt
116  */
117 DBusTransportOpenResult
118 _dbus_transport_open_platform_specific (DBusAddressEntry  *entry,
119                                         DBusTransport    **transport_p,
120                                         DBusError         *error)
121 {
122   const char *method;
123   
124   method = dbus_address_entry_get_method (entry);
125   _dbus_assert (method != NULL);
126
127   if (strcmp (method, "unix") == 0)
128     {
129       const char *path = dbus_address_entry_get_value (entry, "path");
130       const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
131       const char *abstract = dbus_address_entry_get_value (entry, "abstract");
132           
133       if (tmpdir != NULL)
134         {
135           _dbus_set_bad_address (error, NULL, NULL,
136                                  "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
137           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
138         }
139           
140       if (path == NULL && abstract == NULL)
141         {
142           _dbus_set_bad_address (error, "unix",
143                                  "path or abstract",
144                                  NULL);
145           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
146         }
147
148       if (path != NULL && abstract != NULL)
149         {
150           _dbus_set_bad_address (error, NULL, NULL,
151                                  "can't specify both \"path\" and \"abstract\" options in an address");
152           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
153         }
154
155       if (path)
156         *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
157                                                            error);
158       else
159         *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
160                                                            error);
161       if (*transport_p == NULL)
162         {
163           _DBUS_ASSERT_ERROR_IS_SET (error);
164           return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
165         }
166       else
167         {
168           _DBUS_ASSERT_ERROR_IS_CLEAR (error);
169           return DBUS_TRANSPORT_OPEN_OK;
170         }      
171     }
172   else
173     {
174       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
175       return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
176     }
177 }
178
179 /** @} */