voc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-transport-unix.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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-protected.h"
28 #include "dbus-watch.h"
29 #include "dbus-sysdeps-unix.h"
30
31 /**
32  * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
33  * @ingroup  DBusInternals
34  * @brief Implementation details of DBusTransport on UNIX
35  *
36  * @{
37  */
38
39 /**
40  * Creates a new transport for the given Unix domain socket
41  * path. This creates a client-side of a transport.
42  *
43  * @todo once we add a way to escape paths in a dbus
44  * address, this function needs to do escaping.
45  *
46  * @param path the path to the domain socket.
47  * @param abstract #TRUE to use abstract socket namespace
48  * @param error address where an error can be returned.
49  * @returns a new transport, or #NULL on failure.
50  */
51 DBusTransport*
52 _dbus_transport_new_for_domain_socket (const char     *path,
53                                        dbus_bool_t     abstract,
54                                        DBusError      *error)
55 {
56   int fd;
57   DBusTransport *transport;
58   DBusString address;
59   
60   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
61
62   if (!_dbus_string_init (&address))
63     {
64       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
65       return NULL;
66     }
67
68   fd = -1;
69
70   if ((abstract &&
71        !_dbus_string_append (&address, "unix:abstract=")) ||
72       (!abstract &&
73        !_dbus_string_append (&address, "unix:path=")) ||
74       !_dbus_string_append (&address, path))
75     {
76       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
77       goto failed_0;
78     }
79   
80   fd = _dbus_connect_unix_socket (path, abstract, error);
81   if (fd < 0)
82     {
83       _DBUS_ASSERT_ERROR_IS_SET (error);
84       goto failed_0;
85     }
86
87   _dbus_fd_set_close_on_exec (fd);
88   
89   _dbus_verbose ("Successfully connected to unix socket %s\n",
90                  path);
91
92   transport = _dbus_transport_new_for_socket (fd, NULL, &address);
93   if (transport == NULL)
94     {
95       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
96       goto failed_1;
97     }
98   
99   _dbus_string_free (&address);
100   
101   return transport;
102
103  failed_1:
104   _dbus_close_socket (fd, NULL);
105  failed_0:
106   _dbus_string_free (&address);
107   return NULL;
108 }
109
110 /** @} */