dbus-transport-unix.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
00003  *
00004  * Copyright (C) 2002, 2003, 2004  Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-internals.h"
00025 #include "dbus-connection-internal.h"
00026 #include "dbus-transport-unix.h"
00027 #include "dbus-transport-socket.h"
00028 #include "dbus-transport-protected.h"
00029 #include "dbus-watch.h"
00030 #include "dbus-sysdeps-unix.h"
00031 
00052 DBusTransport*
00053 _dbus_transport_new_for_domain_socket (const char     *path,
00054                                        dbus_bool_t     abstract,
00055                                        DBusError      *error)
00056 {
00057   int fd;
00058   DBusTransport *transport;
00059   DBusString address;
00060   
00061   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00062 
00063   if (!_dbus_string_init (&address))
00064     {
00065       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00066       return NULL;
00067     }
00068 
00069   fd = -1;
00070 
00071   if ((abstract &&
00072        !_dbus_string_append (&address, "unix:abstract=")) ||
00073       (!abstract &&
00074        !_dbus_string_append (&address, "unix:path=")) ||
00075       !_dbus_string_append (&address, path))
00076     {
00077       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00078       goto failed_0;
00079     }
00080   
00081   fd = _dbus_connect_unix_socket (path, abstract, error);
00082   if (fd < 0)
00083     {
00084       _DBUS_ASSERT_ERROR_IS_SET (error);
00085       goto failed_0;
00086     }
00087 
00088   _dbus_fd_set_close_on_exec (fd);
00089   
00090   _dbus_verbose ("Successfully connected to unix socket %s\n",
00091                  path);
00092 
00093   transport = _dbus_transport_new_for_socket (fd, NULL, &address);
00094   if (transport == NULL)
00095     {
00096       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00097       goto failed_1;
00098     }
00099   
00100   _dbus_string_free (&address);
00101   
00102   return transport;
00103 
00104  failed_1:
00105   _dbus_close_socket (fd, NULL);
00106  failed_0:
00107   _dbus_string_free (&address);
00108   return NULL;
00109 }
00110 
00119 DBusTransportOpenResult
00120 _dbus_transport_open_platform_specific (DBusAddressEntry  *entry,
00121                                         DBusTransport    **transport_p,
00122                                         DBusError         *error)
00123 {
00124   const char *method;
00125   
00126   method = dbus_address_entry_get_method (entry);
00127   _dbus_assert (method != NULL);
00128 
00129   if (strcmp (method, "unix") == 0)
00130     {
00131       const char *path = dbus_address_entry_get_value (entry, "path");
00132       const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
00133       const char *abstract = dbus_address_entry_get_value (entry, "abstract");
00134           
00135       if (tmpdir != NULL)
00136         {
00137           _dbus_set_bad_address (error, NULL, NULL,
00138                                  "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
00139           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
00140         }
00141           
00142       if (path == NULL && abstract == NULL)
00143         {
00144           _dbus_set_bad_address (error, "unix",
00145                                  "path or abstract",
00146                                  NULL);
00147           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
00148         }
00149 
00150       if (path != NULL && abstract != NULL)
00151         {
00152           _dbus_set_bad_address (error, NULL, NULL,
00153                                  "can't specify both \"path\" and \"abstract\" options in an address");
00154           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
00155         }
00156 
00157       if (path)
00158         *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
00159                                                            error);
00160       else
00161         *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
00162                                                            error);
00163       if (*transport_p == NULL)
00164         {
00165           _DBUS_ASSERT_ERROR_IS_SET (error);
00166           return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
00167         }
00168       else
00169         {
00170           _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00171           return DBUS_TRANSPORT_OPEN_OK;
00172         }      
00173     }
00174   else
00175     {
00176       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00177       return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
00178     }
00179 }
00180 

Generated on Fri Sep 21 18:12:13 2007 for D-Bus by  doxygen 1.5.1