index.html
Main Page
|
modules.html
Modules
|
namespaces.html
Namespace List
|
hierarchy.html
Class Hierarchy
|
annotated.html
Data Structures
|
dirs.html
Directories
|
files.html
File List
|
namespacemembers.html
Namespace Members
|
functions.html
Data Fields
|
pages.html
Related Pages
dir_000004.html
dbus
dbus-server-unix.c
00001
/* -*- mode: C; c-file-style: "gnu" -*- */
00002
/* dbus-server-unix.c Server implementation for Unix network protocols.
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-server-unix.h"
00026
#include "dbus-transport-unix.h"
00027
#include "dbus-connection-internal.h"
00028
#include "dbus-string.h"
00029
#include <sys/types.h>
00030
#include <unistd.h>
00031
group__DBusServerUnix.html#ga0
00043
typedef
struct
structDBusServerUnix.html
DBusServerUnix
DBusServerUnix;
00044
structDBusServerUnix.html
00049
struct
DBusServerUnix
00050 {
structDBusServerUnix.html#o0
00051
structDBusServer.html
DBusServer
structDBusServerUnix.html#o0
base
;
structDBusServerUnix.html#o1
00052
int
structDBusServerUnix.html#o1
fd
;
structDBusServerUnix.html#o2
00053
structDBusWatch.html
DBusWatch
*
structDBusServerUnix.html#o2
watch
;
structDBusServerUnix.html#o3
00054
char
*
structDBusServerUnix.html#o3
socket_name
;
00055 };
00056
00057
static
void
00058 unix_finalize (
structDBusServer.html
DBusServer
*server)
00059 {
00060   DBusServerUnix *unix_server = (DBusServerUnix*) server;
00061
00062
group__DBusServerInternals.html#ga9
_dbus_server_finalize_base
(server);
00063
00064
if
(unix_server->
structDBusServerUnix.html#o2
watch
)
00065     {
00066
group__DBusWatchInternals.html#ga2
_dbus_watch_unref
(unix_server->
structDBusServerUnix.html#o2
watch
);
00067       unix_server->
structDBusServerUnix.html#o2
watch
=
group__DBusMacros.html#ga4
NULL
;
00068     }
00069
00070
group__DBusMemory.html#ga3
dbus_free
(unix_server->
structDBusServerUnix.html#o3
socket_name
);
00071
group__DBusMemory.html#ga3
dbus_free
(server);
00072 }
00073
00082
/* Return value is just for memory, not other failures. */
00083
static
group__DBusTypes.html#ga2
dbus_bool_t
00084 handle_new_client_fd_and_unlock (
structDBusServer.html
DBusServer
*server,
00085
int
client_fd)
00086 {
00087
structDBusConnection.html
DBusConnection
*connection;
00088
structDBusTransport.html
DBusTransport
*transport;
00089   DBusNewConnectionFunction new_connection_function;
00090
void
*new_connection_data;
00091
00092   _dbus_verbose (
"Creating new client connection with fd %d\n"
, client_fd);
00093
00094   HAVE_LOCK_CHECK (server);
00095
00096
if
(!
group__DBusInternalsUtils.html#ga102
_dbus_set_fd_nonblocking
(client_fd,
group__DBusMacros.html#ga4
NULL
))
00097     {
00098       SERVER_UNLOCK (server);
00099
return
group__DBusMacros.html#ga2
TRUE
;
00100     }
00101
00102   transport =
group__DBusTransportUnix.html#ga19
_dbus_transport_new_for_fd
(client_fd, &server->
structDBusServer.html#o4
guid_hex
,
group__DBusMacros.html#ga4
NULL
);
00103
if
(transport ==
group__DBusMacros.html#ga4
NULL
)
00104     {
00105       close (client_fd);
00106       SERVER_UNLOCK (server);
00107
return
group__DBusMacros.html#ga3
FALSE
;
00108     }
00109
00110
if
(!
group__DBusTransport.html#ga24
_dbus_transport_set_auth_mechanisms
(transport,
00111                                             (
const
char
**) server->
structDBusServer.html#o13
auth_mechanisms
))
00112     {
00113
group__DBusTransport.html#ga5
_dbus_transport_unref
(transport);
00114       SERVER_UNLOCK (server);
00115
return
group__DBusMacros.html#ga3
FALSE
;
00116     }
00117
00118
/* note that client_fd is now owned by the transport, and will be
00119
* closed on transport disconnection/finalization
00120
*/
00121
00122   connection =
group__DBusConnectionInternals.html#ga42
_dbus_connection_new_for_transport
(transport);
00123
group__DBusTransport.html#ga5
_dbus_transport_unref
(transport);
00124   transport =
group__DBusMacros.html#ga4
NULL
;
/* now under the connection lock */
00125
00126
if
(connection ==
group__DBusMacros.html#ga4
NULL
)
00127     {
00128       SERVER_UNLOCK (server);
00129
return
group__DBusMacros.html#ga3
FALSE
;
00130     }
00131
00132
/* See if someone wants to handle this new connection, self-referencing
00133
* for paranoia.
00134
*/
00135   new_connection_function = server->
structDBusServer.html#o10
new_connection_function
;
00136   new_connection_data = server->
structDBusServer.html#o11
new_connection_data
;
00137
00138
group__DBusServer.html#ga4
_dbus_server_ref_unlocked
(server);
00139   SERVER_UNLOCK (server);
00140
00141
if
(new_connection_function)
00142     {
00143       (* new_connection_function) (server, connection,
00144                                    new_connection_data);
00145
group__DBusServer.html#ga3
dbus_server_unref
(server);
00146     }
00147
00148
/* If no one grabbed a reference, the connection will die. */
00149
group__DBusConnection.html#ga5
dbus_connection_unref
(connection);
00150
00151
return
group__DBusMacros.html#ga2
TRUE
;
00152 }
00153
00154
static
group__DBusTypes.html#ga2
dbus_bool_t
00155 unix_handle_watch (
structDBusWatch.html
DBusWatch
*watch,
00156
unsigned
int
flags,
00157
void
*data)
00158 {
00159
structDBusServer.html
DBusServer
*server = data;
00160   DBusServerUnix *unix_server = data;
00161
00162   SERVER_LOCK (server);
00163
00164
group__DBusInternalsUtils.html#ga130
_dbus_assert
(watch == unix_server->
structDBusServerUnix.html#o2
watch
);
00165
00166   _dbus_verbose (
"Handling client connection, flags 0x%x\n"
, flags);
00167
00168
if
(flags & DBUS_WATCH_READABLE)
00169     {
00170
int
client_fd;
00171
int
listen_fd;
00172
00173       listen_fd =
group__DBusWatch.html#ga0
dbus_watch_get_fd
(watch);
00174
00175       client_fd =
group__DBusInternalsUtils.html#ga68
_dbus_accept
(listen_fd);
00176
00177
if
(client_fd < 0)
00178         {
00179
/* EINTR handled for us */
00180
00181
if
(errno == EAGAIN || errno == EWOULDBLOCK)
00182             _dbus_verbose (
"No client available to accept after all\n"
);
00183
else
00184             _dbus_verbose (
"Failed to accept a client connection: %s\n"
,
00185
group__DBusInternalsUtils.html#ga96
_dbus_strerror
(errno));
00186
00187           SERVER_UNLOCK (server);
00188         }
00189
else
00190         {
00191
group__DBusInternalsUtils.html#ga98
_dbus_fd_set_close_on_exec
(client_fd);
00192
00193
if
(!handle_new_client_fd_and_unlock (server, client_fd))
00194             _dbus_verbose (
"Rejected client connection due to lack of memory\n"
);
00195         }
00196     }
00197
00198
if
(flags & DBUS_WATCH_ERROR)
00199     _dbus_verbose (
"Error on server listening socket\n"
);
00200
00201
if
(flags & DBUS_WATCH_HANGUP)
00202     _dbus_verbose (
"Hangup on server listening socket\n"
);
00203
00204
return
group__DBusMacros.html#ga2
TRUE
;
00205 }
00206
00207
static
void
00208 unix_disconnect (
structDBusServer.html
DBusServer
*server)
00209 {
00210   DBusServerUnix *unix_server = (DBusServerUnix*) server;
00211
00212   HAVE_LOCK_CHECK (server);
00213
00214
if
(unix_server->
structDBusServerUnix.html#o2
watch
)
00215     {
00216
group__DBusServerInternals.html#ga12
_dbus_server_remove_watch
(server,
00217                                  unix_server->
structDBusServerUnix.html#o2
watch
);
00218
group__DBusWatchInternals.html#ga2
_dbus_watch_unref
(unix_server->
structDBusServerUnix.html#o2
watch
);
00219       unix_server->
structDBusServerUnix.html#o2
watch
=
group__DBusMacros.html#ga4
NULL
;
00220     }
00221
00222   close (unix_server->
structDBusServerUnix.html#o1
fd
);
00223   unix_server->
structDBusServerUnix.html#o1
fd
= -1;
00224
00225
if
(unix_server->
structDBusServerUnix.html#o3
socket_name
!=
group__DBusMacros.html#ga4
NULL
)
00226     {
00227
structDBusString.html
DBusString
tmp;
00228
group__DBusString.html#ga7
_dbus_string_init_const
(&tmp, unix_server->
structDBusServerUnix.html#o3
socket_name
);
00229
group__DBusInternalsUtils.html#ga88
_dbus_delete_file
(&tmp,
group__DBusMacros.html#ga4
NULL
);
00230     }
00231
00232   HAVE_LOCK_CHECK (server);
00233 }
00234
00235
static
structDBusServerVTable.html
DBusServerVTable
unix_vtable = {
00236   unix_finalize,
00237   unix_disconnect
00238 };
00239
00253
structDBusServer.html
DBusServer
*
group__DBusServerUnix.html#ga6
00254
group__DBusServerUnix.html#ga6
_dbus_server_new_for_fd
(
int
fd,
00255
const
structDBusString.html
DBusString
*address)
00256 {
00257   DBusServerUnix *unix_server;
00258
structDBusServer.html
DBusServer
*server;
00259
structDBusWatch.html
DBusWatch
*watch;
00260
00261   unix_server =
group__DBusMemory.html#ga7
dbus_new0
(DBusServerUnix, 1);
00262
if
(unix_server ==
group__DBusMacros.html#ga4
NULL
)
00263
return
group__DBusMacros.html#ga4
NULL
;
00264
00265   watch =
group__DBusWatchInternals.html#ga0
_dbus_watch_new
(fd,
00266                            DBUS_WATCH_READABLE,
00267
group__DBusMacros.html#ga2
TRUE
,
00268                            unix_handle_watch, unix_server,
00269
group__DBusMacros.html#ga4
NULL
);
00270
if
(watch ==
group__DBusMacros.html#ga4
NULL
)
00271     {
00272
group__DBusMemory.html#ga3
dbus_free
(unix_server);
00273
return
group__DBusMacros.html#ga4
NULL
;
00274     }
00275
00276
if
(!
group__DBusServerInternals.html#ga8
_dbus_server_init_base
(&unix_server->
structDBusServerUnix.html#o0
base
,
00277                                &unix_vtable, address))
00278     {
00279
group__DBusWatchInternals.html#ga2
_dbus_watch_unref
(watch);
00280
group__DBusMemory.html#ga3
dbus_free
(unix_server);
00281
return
group__DBusMacros.html#ga4
NULL
;
00282     }
00283
00284   server = (
structDBusServer.html
DBusServer
*) unix_server;
00285
00286   SERVER_LOCK (server);
00287
00288
if
(!
group__DBusServerInternals.html#ga11
_dbus_server_add_watch
(&unix_server->
structDBusServerUnix.html#o0
base
,
00289                                watch))
00290     {
00291       SERVER_UNLOCK (server);
00292
group__DBusServerInternals.html#ga9
_dbus_server_finalize_base
(&unix_server->
structDBusServerUnix.html#o0
base
);
00293
group__DBusWatchInternals.html#ga2
_dbus_watch_unref
(watch);
00294
group__DBusMemory.html#ga3
dbus_free
(unix_server);
00295
return
group__DBusMacros.html#ga4
NULL
;
00296     }
00297
00298   unix_server->
structDBusServerUnix.html#o1
fd
= fd;
00299   unix_server->
structDBusServerUnix.html#o2
watch
= watch;
00300
00301   SERVER_UNLOCK (server);
00302
00303
return
(
structDBusServer.html
DBusServer
*) unix_server;
00304 }
00305
00314
structDBusServer.html
DBusServer
*
group__DBusServerUnix.html#ga7
00315
group__DBusServerUnix.html#ga7
_dbus_server_new_for_domain_socket
(
const
char
*path,
00316
group__DBusTypes.html#ga2
dbus_bool_t
abstract
,
00317
structDBusError.html
DBusError
*error)
00318 {
00319
structDBusServer.html
DBusServer
*server;
00320   DBusServerUnix *unix_server;
00321
int
listen_fd;
00322
structDBusString.html
DBusString
address;
00323
char
*path_copy;
00324
structDBusString.html
DBusString
path_str;
00325
00326   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00327
00328
if
(!
group__DBusString.html#ga6
_dbus_string_init
(&address))
00329     {
00330
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00331
return
group__DBusMacros.html#ga4
NULL
;
00332     }
00333
00334
group__DBusString.html#ga7
_dbus_string_init_const
(&path_str, path);
00335
if
((
abstract
&&
00336        !
group__DBusString.html#ga27
_dbus_string_append
(&address,
"unix:abstract="
)) ||
00337       (!
abstract
&&
00338        !
group__DBusString.html#ga27
_dbus_string_append
(&address,
"unix:path="
)) ||
00339       !
group__DBusAddress.html#ga4
_dbus_address_append_escaped
(&address, &path_str))
00340     {
00341
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00342
goto
failed_0;
00343     }
00344
00345   path_copy =
group__DBusInternalsUtils.html#ga8
_dbus_strdup
(path);
00346
if
(path_copy ==
group__DBusMacros.html#ga4
NULL
)
00347     {
00348
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00349
goto
failed_0;
00350     }
00351
00352   listen_fd =
group__DBusInternalsUtils.html#ga62
_dbus_listen_unix_socket
(path,
abstract
, error);
00353
group__DBusInternalsUtils.html#ga98
_dbus_fd_set_close_on_exec
(listen_fd);
00354
00355
if
(listen_fd < 0)
00356     {
00357       _DBUS_ASSERT_ERROR_IS_SET (error);
00358
goto
failed_1;
00359     }
00360
00361   server =
group__DBusServerUnix.html#ga6
_dbus_server_new_for_fd
(listen_fd, &address);
00362
if
(server ==
group__DBusMacros.html#ga4
NULL
)
00363     {
00364
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00365
goto
failed_2;
00366     }
00367
00368   unix_server = (DBusServerUnix*) server;
00369   unix_server->
structDBusServerUnix.html#o3
socket_name
= path_copy;
00370
00371
group__DBusString.html#ga9
_dbus_string_free
(&address);
00372
00373
return
server;
00374
00375  failed_2:
00376
group__DBusInternalsUtils.html#ga101
_dbus_close
(listen_fd,
group__DBusMacros.html#ga4
NULL
);
00377  failed_1:
00378
group__DBusMemory.html#ga3
dbus_free
(path_copy);
00379  failed_0:
00380
group__DBusString.html#ga9
_dbus_string_free
(&address);
00381
00382
return
group__DBusMacros.html#ga4
NULL
;
00383 }
00384
00394
structDBusServer.html
DBusServer
*
group__DBusServerUnix.html#ga8
00395
group__DBusServerUnix.html#ga8
_dbus_server_new_for_tcp_socket
(
const
char
*host,
00396
group__DBusTypes.html#ga3
dbus_uint32_t
port,
00397
structDBusError.html
DBusError
*error)
00398 {
00399
structDBusServer.html
DBusServer
*server;
00400
int
listen_fd;
00401
structDBusString.html
DBusString
address;
00402
structDBusString.html
DBusString
host_str;
00403
00404   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00405
00406
if
(!
group__DBusString.html#ga6
_dbus_string_init
(&address))
00407     {
00408
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00409
return
group__DBusMacros.html#ga4
NULL
;
00410     }
00411
00412
if
(host ==
group__DBusMacros.html#ga4
NULL
)
00413     host =
"localhost"
;
00414
00415
group__DBusString.html#ga7
_dbus_string_init_const
(&host_str, host);
00416
if
(!
group__DBusString.html#ga27
_dbus_string_append
(&address,
"tcp:host="
) ||
00417       !
group__DBusAddress.html#ga4
_dbus_address_append_escaped
(&address, &host_str) ||
00418       !
group__DBusString.html#ga27
_dbus_string_append
(&address,
",port="
) ||
00419       !
group__DBusString.html#ga60
_dbus_string_append_int
(&address, port))
00420     {
00421
group__DBusString.html#ga9
_dbus_string_free
(&address);
00422
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00423
return
group__DBusMacros.html#ga4
NULL
;
00424     }
00425
00426   listen_fd =
group__DBusInternalsUtils.html#ga64
_dbus_listen_tcp_socket
(host, port, error);
00427
group__DBusInternalsUtils.html#ga98
_dbus_fd_set_close_on_exec
(listen_fd);
00428
00429
if
(listen_fd < 0)
00430     {
00431
group__DBusString.html#ga9
_dbus_string_free
(&address);
00432
return
group__DBusMacros.html#ga4
NULL
;
00433     }
00434
00435   server =
group__DBusServerUnix.html#ga6
_dbus_server_new_for_fd
(listen_fd, &address);
00436
if
(server ==
group__DBusMacros.html#ga4
NULL
)
00437     {
00438
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00439       close (listen_fd);
00440
group__DBusString.html#ga9
_dbus_string_free
(&address);
00441
return
group__DBusMacros.html#ga4
NULL
;
00442     }
00443
00444
group__DBusString.html#ga9
_dbus_string_free
(&address);
00445
00446
return
server;
00447
00448
00449 }
00450
Generated on Tue Sep 13 01:28:07 2005 for D-BUS by
http://www.doxygen.org/index.html
doxygen
1.4.4
