D-Bus
1.4.10
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 */ 00023 00024 #include <config.h> 00025 #include "dbus-internals.h" 00026 #include "dbus-connection-internal.h" 00027 #include "dbus-transport-unix.h" 00028 #include "dbus-transport-socket.h" 00029 #include "dbus-transport-protected.h" 00030 #include "dbus-watch.h" 00031 #include "dbus-sysdeps-unix.h" 00032 00053 DBusTransport* 00054 _dbus_transport_new_for_domain_socket (const char *path, 00055 dbus_bool_t abstract, 00056 DBusError *error) 00057 { 00058 int fd; 00059 DBusTransport *transport; 00060 DBusString address; 00061 00062 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00063 00064 if (!_dbus_string_init (&address)) 00065 { 00066 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00067 return NULL; 00068 } 00069 00070 fd = -1; 00071 00072 if ((abstract && 00073 !_dbus_string_append (&address, "unix:abstract=")) || 00074 (!abstract && 00075 !_dbus_string_append (&address, "unix:path=")) || 00076 !_dbus_string_append (&address, path)) 00077 { 00078 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00079 goto failed_0; 00080 } 00081 00082 fd = _dbus_connect_unix_socket (path, abstract, error); 00083 if (fd < 0) 00084 { 00085 _DBUS_ASSERT_ERROR_IS_SET (error); 00086 goto failed_0; 00087 } 00088 00089 _dbus_verbose ("Successfully connected to unix socket %s\n", 00090 path); 00091 00092 transport = _dbus_transport_new_for_socket (fd, NULL, &address); 00093 if (transport == NULL) 00094 { 00095 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00096 goto failed_1; 00097 } 00098 00099 _dbus_string_free (&address); 00100 00101 return transport; 00102 00103 failed_1: 00104 _dbus_close_socket (fd, NULL); 00105 failed_0: 00106 _dbus_string_free (&address); 00107 return NULL; 00108 } 00109 00118 DBusTransportOpenResult 00119 _dbus_transport_open_platform_specific (DBusAddressEntry *entry, 00120 DBusTransport **transport_p, 00121 DBusError *error) 00122 { 00123 const char *method; 00124 00125 method = dbus_address_entry_get_method (entry); 00126 _dbus_assert (method != NULL); 00127 00128 if (strcmp (method, "unix") == 0) 00129 { 00130 const char *path = dbus_address_entry_get_value (entry, "path"); 00131 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir"); 00132 const char *abstract = dbus_address_entry_get_value (entry, "abstract"); 00133 00134 if (tmpdir != NULL) 00135 { 00136 _dbus_set_bad_address (error, NULL, NULL, 00137 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on"); 00138 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; 00139 } 00140 00141 if (path == NULL && abstract == NULL) 00142 { 00143 _dbus_set_bad_address (error, "unix", 00144 "path or abstract", 00145 NULL); 00146 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; 00147 } 00148 00149 if (path != NULL && abstract != NULL) 00150 { 00151 _dbus_set_bad_address (error, NULL, NULL, 00152 "can't specify both \"path\" and \"abstract\" options in an address"); 00153 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; 00154 } 00155 00156 if (path) 00157 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE, 00158 error); 00159 else 00160 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE, 00161 error); 00162 if (*transport_p == NULL) 00163 { 00164 _DBUS_ASSERT_ERROR_IS_SET (error); 00165 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; 00166 } 00167 else 00168 { 00169 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00170 return DBUS_TRANSPORT_OPEN_OK; 00171 } 00172 } 00173 #ifdef DBUS_ENABLE_LAUNCHD 00174 else if (strcmp (method, "launchd") == 0) 00175 { 00176 DBusError tmp_error = DBUS_ERROR_INIT; 00177 const char *launchd_env_var = dbus_address_entry_get_value (entry, "env"); 00178 const char *launchd_socket; 00179 DBusString socket_path; 00180 dbus_bool_t valid_socket; 00181 00182 if (!_dbus_string_init (&socket_path)) 00183 { 00184 _DBUS_SET_OOM (error); 00185 return FALSE; 00186 } 00187 00188 if (launchd_env_var == NULL) 00189 { 00190 _dbus_set_bad_address (error, "launchd", "env", NULL); 00191 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; 00192 } 00193 00194 valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error); 00195 00196 if (dbus_error_is_set(error)) 00197 { 00198 _dbus_string_free(&socket_path); 00199 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; 00200 } 00201 00202 if (!valid_socket) 00203 { 00204 dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS, 00205 "launchd's env var %s does not exist", launchd_env_var); 00206 dbus_error_free(error); 00207 dbus_move_error(&tmp_error, error); 00208 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; 00209 } 00210 00211 launchd_socket = _dbus_string_get_const_data(&socket_path); 00212 *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error); 00213 00214 if (*transport_p == NULL) 00215 { 00216 _DBUS_ASSERT_ERROR_IS_SET (error); 00217 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; 00218 } 00219 else 00220 { 00221 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00222 return DBUS_TRANSPORT_OPEN_OK; 00223 } 00224 } 00225 #endif 00226 else 00227 { 00228 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00229 return DBUS_TRANSPORT_OPEN_NOT_HANDLED; 00230 } 00231 } 00232