D-Bus 1.14.10
dbus-server-socket.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-server-socket.c Server implementation for sockets
3 *
4 * Copyright (C) 2002, 2003, 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <config.h>
25#include "dbus-internals.h"
26#include "dbus-server-socket.h"
27#include "dbus-transport-socket.h"
28#include "dbus-connection-internal.h"
29#include "dbus-memory.h"
30#include "dbus-nonce.h"
31#include "dbus-string.h"
32
45
51{
53 int n_fds;
58};
59
60static void
61socket_finalize (DBusServer *server)
62{
63 DBusServerSocket *socket_server = (DBusServerSocket*) server;
64 int i;
65
67
68 for (i = 0 ; i < socket_server->n_fds ; i++)
69 if (socket_server->watch[i])
70 {
71 _dbus_watch_unref (socket_server->watch[i]);
72 socket_server->watch[i] = NULL;
73 }
74
75 dbus_free (socket_server->fds);
76 dbus_free (socket_server->watch);
77 dbus_free (socket_server->socket_name);
78 _dbus_noncefile_delete (&socket_server->noncefile, NULL);
79 dbus_free (server);
80}
81
82/* Return value is just for memory, not other failures. */
83static dbus_bool_t
84handle_new_client_fd_and_unlock (DBusServer *server,
85 DBusSocket client_fd)
86{
87 DBusConnection *connection;
88 DBusTransport *transport;
89 DBusNewConnectionFunction new_connection_function;
90 void *new_connection_data;
91
92 _dbus_verbose ("Creating new client connection with fd %" DBUS_SOCKET_FORMAT "\n",
93 _dbus_socket_printable (client_fd));
94
95 HAVE_LOCK_CHECK (server);
96
97 if (!_dbus_set_socket_nonblocking (client_fd, NULL))
98 {
99 SERVER_UNLOCK (server);
100 return TRUE;
101 }
102
103 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
104 if (transport == NULL)
105 {
106 _dbus_close_socket (client_fd, NULL);
107 SERVER_UNLOCK (server);
108 return FALSE;
109 }
110
112 (const char **) server->auth_mechanisms))
113 {
114 _dbus_transport_unref (transport);
115 SERVER_UNLOCK (server);
116 return FALSE;
117 }
118
119 /* note that client_fd is now owned by the transport, and will be
120 * closed on transport disconnection/finalization
121 */
122
123 connection = _dbus_connection_new_for_transport (transport);
124 _dbus_transport_unref (transport);
125 transport = NULL; /* now under the connection lock */
126
127 if (connection == NULL)
128 {
129 SERVER_UNLOCK (server);
130 return FALSE;
131 }
132
133 /* See if someone wants to handle this new connection, self-referencing
134 * for paranoia.
135 */
136 new_connection_function = server->new_connection_function;
137 new_connection_data = server->new_connection_data;
138
140 SERVER_UNLOCK (server);
141
142 if (new_connection_function)
143 {
144 (* new_connection_function) (server, connection,
145 new_connection_data);
146 }
147 dbus_server_unref (server);
148
149 /* If no one grabbed a reference, the connection will die. */
151 dbus_connection_unref (connection);
152
153 return TRUE;
154}
155
156static dbus_bool_t
157socket_handle_watch (DBusWatch *watch,
158 unsigned int flags,
159 void *data)
160{
161 DBusServer *server = data;
162 DBusServerSocket *socket_server = data;
163
164#ifndef DBUS_DISABLE_ASSERT
165 int i;
166 dbus_bool_t found = FALSE;
167#endif
168
169 SERVER_LOCK (server);
170
171#ifndef DBUS_DISABLE_ASSERT
172 for (i = 0 ; i < socket_server->n_fds ; i++)
173 {
174 if (socket_server->watch[i] == watch)
175 found = TRUE;
176 }
177 _dbus_assert (found);
178#endif
179
180 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
181
182 if (flags & DBUS_WATCH_READABLE)
183 {
184 DBusSocket client_fd;
185 DBusSocket listen_fd;
186 int saved_errno;
187
188 listen_fd = _dbus_watch_get_socket (watch);
189
190 if (socket_server->noncefile)
191 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
192 else
193 client_fd = _dbus_accept (listen_fd);
194
195 saved_errno = _dbus_save_socket_errno ();
196
197 if (!_dbus_socket_is_valid (client_fd))
198 {
199 /* EINTR handled for us */
200
202 _dbus_verbose ("No client available to accept after all\n");
203 else
204 _dbus_verbose ("Failed to accept a client connection: %s\n",
205 _dbus_strerror (saved_errno));
206
207 SERVER_UNLOCK (server);
208 }
209 else
210 {
211 if (!handle_new_client_fd_and_unlock (server, client_fd))
212 _dbus_verbose ("Rejected client connection due to lack of memory\n");
213 }
214 }
215
216 if (flags & DBUS_WATCH_ERROR)
217 _dbus_verbose ("Error on server listening socket\n");
218
219 if (flags & DBUS_WATCH_HANGUP)
220 _dbus_verbose ("Hangup on server listening socket\n");
221
222 return TRUE;
223}
224
225static void
226socket_disconnect (DBusServer *server)
227{
228 DBusServerSocket *socket_server = (DBusServerSocket*) server;
229 int i;
230
231 HAVE_LOCK_CHECK (server);
232
233 for (i = 0 ; i < socket_server->n_fds ; i++)
234 {
235 if (socket_server->watch[i])
236 {
238 socket_server->watch[i]);
239 _dbus_watch_invalidate (socket_server->watch[i]);
240 _dbus_watch_unref (socket_server->watch[i]);
241 socket_server->watch[i] = NULL;
242 }
243
244 if (_dbus_socket_is_valid (socket_server->fds[i]))
245 {
246 _dbus_close_socket (socket_server->fds[i], NULL);
247 _dbus_socket_invalidate (&socket_server->fds[i]);
248 }
249 }
250
251 if (socket_server->socket_name != NULL)
252 {
253 DBusString tmp;
254 _dbus_string_init_const (&tmp, socket_server->socket_name);
255 _dbus_delete_file (&tmp, NULL);
256 }
257
258 if (server->published_address)
259 _dbus_daemon_unpublish_session_bus_address();
260
261 HAVE_LOCK_CHECK (server);
262}
263
264static const DBusServerVTable socket_vtable = {
265 socket_finalize,
266 socket_disconnect
267};
268
287 int n_fds,
288 const DBusString *address,
289 DBusNonceFile *noncefile,
290 DBusError *error)
291{
292 DBusServerSocket *socket_server;
293 DBusServer *server;
294 int i;
295
296 socket_server = dbus_new0 (DBusServerSocket, 1);
297 if (socket_server == NULL)
298 goto failed;
299
300 socket_server->noncefile = noncefile;
301
302 socket_server->fds = dbus_new (DBusSocket, n_fds);
303 if (!socket_server->fds)
304 goto failed;
305
306 socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
307 if (!socket_server->watch)
308 goto failed;
309
310 for (i = 0 ; i < n_fds ; i++)
311 {
312 DBusWatch *watch;
313
314 watch = _dbus_watch_new (_dbus_socket_get_pollable (fds[i]),
316 TRUE,
317 socket_handle_watch, socket_server,
318 NULL);
319 if (watch == NULL)
320 goto failed;
321
322 socket_server->n_fds++;
323 socket_server->fds[i] = fds[i];
324 socket_server->watch[i] = watch;
325 }
326
327 if (!_dbus_server_init_base (&socket_server->base,
328 &socket_vtable, address,
329 error))
330 goto failed;
331
332 server = (DBusServer*)socket_server;
333
334 SERVER_LOCK (server);
335
336 for (i = 0 ; i < n_fds ; i++)
337 {
338 if (!_dbus_server_add_watch (&socket_server->base,
339 socket_server->watch[i]))
340 {
341 int j;
342
343 /* The caller is still responsible for closing the fds until
344 * we return successfully, so don't let socket_disconnect()
345 * close them */
346 for (j = 0; j < n_fds; j++)
347 _dbus_socket_invalidate (&socket_server->fds[j]);
348
349 /* socket_disconnect() will try to remove all the watches;
350 * make sure it doesn't see the ones that weren't even added
351 * yet */
352 for (j = i; j < n_fds; j++)
353 {
354 _dbus_watch_invalidate (socket_server->watch[j]);
355 _dbus_watch_unref (socket_server->watch[j]);
356 socket_server->watch[j] = NULL;
357 }
358
359 _dbus_server_disconnect_unlocked (server);
360 SERVER_UNLOCK (server);
361 _dbus_server_finalize_base (&socket_server->base);
362 goto failed;
363 }
364 }
365
366 SERVER_UNLOCK (server);
367
368 _dbus_server_trace_ref (&socket_server->base, 0, 1, "new_for_socket");
369 return (DBusServer*) socket_server;
370
371failed:
372 if (socket_server != NULL)
373 {
374 if (socket_server->watch != NULL)
375 {
376 for (i = 0; i < n_fds; i++)
377 {
378 if (socket_server->watch[i] != NULL)
379 {
380 _dbus_watch_invalidate (socket_server->watch[i]);
381 _dbus_watch_unref (socket_server->watch[i]);
382 socket_server->watch[i] = NULL;
383 }
384 }
385 }
386
387 dbus_free (socket_server->watch);
388 dbus_free (socket_server->fds);
389 dbus_free (socket_server);
390 }
391
392 if (error != NULL && !dbus_error_is_set (error))
393 _DBUS_SET_OOM (error);
394
395 return NULL;
396}
397
419 const char *bind,
420 const char *port,
421 const char *family,
422 DBusError *error,
423 dbus_bool_t use_nonce)
424{
425 DBusServer *server = NULL;
426 DBusSocket *listen_fds = NULL;
427 int nlisten_fds = 0, i;
428 DBusString address = _DBUS_STRING_INIT_INVALID;
429 DBusString host_str; /* Initialized as const later, not freed */
430 DBusString port_str = _DBUS_STRING_INIT_INVALID;
431 DBusNonceFile *noncefile = NULL;
432 const char *family_used = NULL;
433
434 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
435
436 if (!_dbus_string_init (&address))
437 {
439 goto failed;
440 }
441
442 if (!_dbus_string_init (&port_str))
443 {
445 goto failed;
446 }
447
448 if (host == NULL)
449 host = "localhost";
450
451 if (port == NULL)
452 port = "0";
453
454 if (bind == NULL)
455 bind = host;
456 else if (strcmp (bind, "*") == 0)
457 bind = NULL;
458
459 nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
460 &port_str,
461 &family_used,
462 &listen_fds, error);
463 if (nlisten_fds <= 0)
464 {
465 _DBUS_ASSERT_ERROR_IS_SET(error);
466 goto failed;
467 }
468
469 _dbus_string_init_const (&host_str, host);
470 if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
471 !_dbus_address_append_escaped (&address, &host_str) ||
472 !_dbus_string_append (&address, ",port=") ||
473 !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
474 {
476 goto failed;
477 }
478 if (family_used &&
479 (!_dbus_string_append (&address, ",family=") ||
480 !_dbus_string_append (&address, family_used)))
481 {
483 goto failed;
484 }
485
486 if (use_nonce)
487 {
488 if (!_dbus_noncefile_create (&noncefile, error))
489 goto failed;
490
491 if (!_dbus_string_append (&address, ",noncefile=") ||
492 !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
493 {
495 goto failed;
496 }
497 }
498
499 server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile, error);
500 if (server == NULL)
501 goto failed;
502
503 /* server has taken ownership of noncefile and the fds in listen_fds */
504 _dbus_string_free (&port_str);
505 _dbus_string_free (&address);
506 dbus_free(listen_fds);
507
508 return server;
509
510failed:
511 _dbus_noncefile_delete (&noncefile, NULL);
512
513 if (listen_fds != NULL)
514 {
515 for (i = 0; i < nlisten_fds; i++)
516 _dbus_close_socket (listen_fds[i], NULL);
517 dbus_free (listen_fds);
518 }
519
520 _dbus_string_free (&port_str);
521 _dbus_string_free (&address);
522 return NULL;
523}
524
537DBusServerListenResult
539 DBusServer **server_p,
540 DBusError *error)
541{
542 const char *method;
543
544 *server_p = NULL;
545
546 method = dbus_address_entry_get_method (entry);
547
548 if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
549 {
550 const char *host;
551 const char *port;
552 const char *bind;
553 const char *family;
554
555 host = dbus_address_entry_get_value (entry, "host");
556 bind = dbus_address_entry_get_value (entry, "bind");
557 port = dbus_address_entry_get_value (entry, "port");
558 family = dbus_address_entry_get_value (entry, "family");
559
560 *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
561 family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
562
563 if (*server_p)
564 {
565 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
566 return DBUS_SERVER_LISTEN_OK;
567 }
568 else
569 {
570 _DBUS_ASSERT_ERROR_IS_SET(error);
571 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
572 }
573 }
574 else
575 {
576 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
577 return DBUS_SERVER_LISTEN_NOT_HANDLED;
578 }
579}
580
590void
592 char *filename)
593{
594 DBusServerSocket *socket_server = (DBusServerSocket*) server;
595
596 socket_server->socket_name = filename;
597}
598
599
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition: dbus-address.c:107
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:230
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:247
DBusConnection * _dbus_connection_new_for_transport(DBusTransport *transport)
Creates a new connection for the given transport.
void _dbus_connection_close_if_only_one_ref(DBusConnection *connection)
Used internally to handle the semantics of dbus_server_set_new_connection_function().
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
Definition: dbus-server.c:296
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
Definition: dbus-server.c:312
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address, DBusError *error)
Initializes the members of the DBusServer base class.
Definition: dbus-server.c:112
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
Definition: dbus-server.c:201
DBUS_PRIVATE_EXPORT void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
Definition: dbus-server.c:456
DBusServer * _dbus_server_new_for_tcp_socket(const char *host, const char *bind, const char *port, const char *family, DBusError *error, dbus_bool_t use_nonce)
Creates a new server listening on TCP.
DBusServer * _dbus_server_new_for_socket(DBusSocket *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile, DBusError *error)
Creates a new server listening on the given file descriptor.
DBusServerListenResult _dbus_server_listen_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for various socket-related addresses (well, currently only tcp a...
void _dbus_server_socket_own_filename(DBusServer *server, char *filename)
This is a bad hack since it's really unix domain socket specific.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
Definition: dbus-server.c:733
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
Definition: dbus-server.h:48
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:966
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:197
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(int e)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted)
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
dbus_bool_t _dbus_set_socket_nonblocking(DBusSocket fd, DBusError *error)
Sets a file descriptor to be nonblocking.
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, const char **retfamily, DBusSocket **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening socket.
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
dbus_bool_t _dbus_transport_set_auth_mechanisms(DBusTransport *transport, const char **mechanisms)
Sets the SASL authentication mechanisms supported by this transport.
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
Definition: dbus-watch.c:169
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
Implementation details of DBusConnection.
Object representing an exception.
Definition: dbus-errors.h:49
Implementation details of DBusServerSocket.
int n_fds
Number of active file handles.
DBusSocket * fds
File descriptor or DBUS_SOCKET_INVALID if disconnected.
DBusNonceFile * noncefile
Nonce file used to authenticate clients.
DBusWatch ** watch
File descriptor watch.
DBusServer base
Parent class members.
char * socket_name
Name of domain socket, to unlink if appropriate.
Virtual table to be implemented by all server "subclasses".
Internals of DBusServer object.
dbus_bool_t published_address
flag which indicates that server has published its bus address.
DBusString guid_hex
Hex-encoded version of GUID.
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
void * new_connection_data
Data for new connection callback.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
Socket interface.
Definition: dbus-sysdeps.h:181
Object representing a transport such as a socket.
Implementation of DBusWatch.
Definition: dbus-watch.c:41