1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-server-unix.c Server implementation for Unix network protocols.
*
* Copyright (C) 2002 Red Hat Inc.
*
* Licensed under the Academic Free License version 1.2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "dbus-internals.h"
#include "dbus-server-unix.h"
#include "dbus-transport-unix.h"
#include "dbus-connection-internal.h"
#include <sys/types.h>
#include <unistd.h>
/**
* @defgroup DBusServerUnix DBusServer implementations for UNIX
* @ingroup DBusInternals
* @brief Implementation details of DBusServer on UNIX
*
* @{
*/
/**
*
* Opaque object representing a Unix server implementation.
*/
typedef struct DBusServerUnix DBusServerUnix;
/**
* Implementation details of DBusServerUnix. All members
* are private.
*/
struct DBusServerUnix
{
DBusServer base; /**< Parent class members. */
int fd; /**< File descriptor or -1 if disconnected. */
DBusWatch *watch; /**< File descriptor watch. */
};
static void
unix_finalize (DBusServer *server)
{
DBusServerUnix *unix_server = (DBusServerUnix*) server;
_dbus_server_finalize_base (server);
if (unix_server->watch)
_dbus_watch_unref (unix_server->watch);
dbus_free (server);
}
static void
handle_new_client_fd (DBusServer *server,
int client_fd)
{
DBusConnection *connection;
DBusTransport *transport;
_dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
if (!_dbus_set_fd_nonblocking (client_fd, NULL))
return;
transport = _dbus_transport_new_for_fd (client_fd, TRUE);
if (transport == NULL)
{
close (client_fd);
return;
}
/* note that client_fd is now owned by the transport, and will be
* closed on transport disconnection/finalization
*/
connection = _dbus_connection_new_for_transport (transport);
_dbus_transport_unref (transport);
if (connection == NULL)
return;
_dbus_connection_set_connection_counter (connection,
server->connection_counter);
/* See if someone wants to handle this new connection,
* self-referencing for paranoia
*/
if (server->new_connection_function)
{
dbus_server_ref (server);
(* server->new_connection_function) (server, connection,
server->new_connection_data);
dbus_server_unref (server);
}
/* If no one grabbed a reference, the connection will die. */
dbus_connection_unref (connection);
}
static void
unix_handle_watch (DBusServer *server,
DBusWatch *watch,
unsigned int flags)
{
DBusServerUnix *unix_server = (DBusServerUnix*) server;
_dbus_assert (watch == unix_server->watch);
_dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
if (flags & DBUS_WATCH_READABLE)
{
int client_fd;
int listen_fd;
listen_fd = dbus_watch_get_fd (watch);
client_fd = _dbus_accept (listen_fd);
if (client_fd < 0)
{
/* EINTR handled for us */
if (errno == EAGAIN || errno == EWOULDBLOCK)
_dbus_verbose ("No client available to accept after all\n");
else
_dbus_verbose ("Failed to accept a client connection: %s\n",
_dbus_strerror (errno));
}
else
{
_dbus_fd_set_close_on_exec (client_fd);
handle_new_client_fd (server, client_fd);
}
}
if (flags & DBUS_WATCH_ERROR)
_dbus_verbose ("Error on server listening socket\n");
if (flags & DBUS_WATCH_HANGUP)
_dbus_verbose ("Hangup on server listening socket\n");
}
static void
unix_disconnect (DBusServer *server)
{
DBusServerUnix *unix_server = (DBusServerUnix*) server;
if (unix_server->watch)
{
_dbus_server_remove_watch (server,
unix_server->watch);
_dbus_watch_unref (unix_server->watch);
unix_server->watch = NULL;
}
close (unix_server->fd);
unix_server->fd = -1;
}
static DBusServerVTable unix_vtable = {
unix_finalize,
unix_handle_watch,
unix_disconnect
};
/**
* Creates a new server listening on the given file descriptor. The
* file descriptor should be nonblocking (use
* _dbus_set_fd_nonblocking() to make it so). The file descriptor
* should be listening for connections, that is, listen() should have
* been successfully invoked on it. The server will use accept() to
* accept new client connections.
*
* @param fd the file descriptor.
* @returns the new server, or #NULL if no memory.
*
*/
DBusServer*
_dbus_server_new_for_fd (int fd)
{
DBusServerUnix *unix_server;
DBusWatch *watch;
watch = _dbus_watch_new (fd,
DBUS_WATCH_READABLE);
if (watch == NULL)
return NULL;
unix_server = dbus_new0 (DBusServerUnix, 1);
if (unix_server == NULL)
{
_dbus_watch_unref (watch);
return NULL;
}
if (!_dbus_server_init_base (&unix_server->base,
&unix_vtable))
{
_dbus_watch_unref (watch);
dbus_free (unix_server);
return NULL;
}
if (!_dbus_server_add_watch (&unix_server->base,
watch))
{
_dbus_server_finalize_base (&unix_server->base);
_dbus_watch_unref (watch);
dbus_free (unix_server);
return NULL;
}
unix_server->fd = fd;
unix_server->watch = watch;
return (DBusServer*) unix_server;
}
/**
* Creates a new server listening on the given Unix domain socket.
*
* @param path the path for the domain socket.
* @param result location to store reason for failure.
* @returns the new server, or #NULL on failure.
*/
DBusServer*
_dbus_server_new_for_domain_socket (const char *path,
DBusResultCode *result)
{
DBusServer *server;
int listen_fd;
listen_fd = _dbus_listen_unix_socket (path, result);
_dbus_fd_set_close_on_exec (listen_fd);
if (listen_fd < 0)
return NULL;
server = _dbus_server_new_for_fd (listen_fd);
if (server == NULL)
{
dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
close (listen_fd);
return NULL;
}
return server;
}
/** @} */
|