D-Bus 1.12.4
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 if (socket_server->noncefile)
79 _dbus_noncefile_delete (socket_server->noncefile, NULL);
80 dbus_free (socket_server->noncefile);
81 dbus_free (server);
82}
83
84/* Return value is just for memory, not other failures. */
85static dbus_bool_t
86handle_new_client_fd_and_unlock (DBusServer *server,
87 DBusSocket client_fd)
88{
89 DBusConnection *connection;
90 DBusTransport *transport;
91 DBusNewConnectionFunction new_connection_function;
92 void *new_connection_data;
93
94 _dbus_verbose ("Creating new client connection with fd %" DBUS_SOCKET_FORMAT "\n",
95 _dbus_socket_printable (client_fd));
96
97 HAVE_LOCK_CHECK (server);
98
99 if (!_dbus_set_socket_nonblocking (client_fd, NULL))
100 {
101 SERVER_UNLOCK (server);
102 return TRUE;
103 }
104
105 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
106 if (transport == NULL)
107 {
108 _dbus_close_socket (client_fd, NULL);
109 SERVER_UNLOCK (server);
110 return FALSE;
111 }
112
114 (const char **) server->auth_mechanisms))
115 {
116 _dbus_transport_unref (transport);
117 SERVER_UNLOCK (server);
118 return FALSE;
119 }
120
121 /* note that client_fd is now owned by the transport, and will be
122 * closed on transport disconnection/finalization
123 */
124
125 connection = _dbus_connection_new_for_transport (transport);
126 _dbus_transport_unref (transport);
127 transport = NULL; /* now under the connection lock */
128
129 if (connection == NULL)
130 {
131 SERVER_UNLOCK (server);
132 return FALSE;
133 }
134
135 /* See if someone wants to handle this new connection, self-referencing
136 * for paranoia.
137 */
138 new_connection_function = server->new_connection_function;
139 new_connection_data = server->new_connection_data;
140
142 SERVER_UNLOCK (server);
143
144 if (new_connection_function)
145 {
146 (* new_connection_function) (server, connection,
147 new_connection_data);
148 }
149 dbus_server_unref (server);
150
151 /* If no one grabbed a reference, the connection will die. */
153 dbus_connection_unref (connection);
154
155 return TRUE;
156}
157
158static dbus_bool_t
159socket_handle_watch (DBusWatch *watch,
160 unsigned int flags,
161 void *data)
162{
163 DBusServer *server = data;
164 DBusServerSocket *socket_server = data;
165
166#ifndef DBUS_DISABLE_ASSERT
167 int i;
168 dbus_bool_t found = FALSE;
169#endif
170
171 SERVER_LOCK (server);
172
173#ifndef DBUS_DISABLE_ASSERT
174 for (i = 0 ; i < socket_server->n_fds ; i++)
175 {
176 if (socket_server->watch[i] == watch)
177 found = TRUE;
178 }
179 _dbus_assert (found);
180#endif
181
182 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
183
184 if (flags & DBUS_WATCH_READABLE)
185 {
186 DBusSocket client_fd;
187 DBusSocket listen_fd;
188 int saved_errno;
189
190 listen_fd = _dbus_watch_get_socket (watch);
191
192 if (socket_server->noncefile)
193 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
194 else
195 client_fd = _dbus_accept (listen_fd);
196
197 saved_errno = _dbus_save_socket_errno ();
198
199 if (!_dbus_socket_is_valid (client_fd))
200 {
201 /* EINTR handled for us */
202
204 _dbus_verbose ("No client available to accept after all\n");
205 else
206 _dbus_verbose ("Failed to accept a client connection: %s\n",
207 _dbus_strerror (saved_errno));
208
209 SERVER_UNLOCK (server);
210 }
211 else
212 {
213 if (!handle_new_client_fd_and_unlock (server, client_fd))
214 _dbus_verbose ("Rejected client connection due to lack of memory\n");
215 }
216 }
217
218 if (flags & DBUS_WATCH_ERROR)
219 _dbus_verbose ("Error on server listening socket\n");
220
221 if (flags & DBUS_WATCH_HANGUP)
222 _dbus_verbose ("Hangup on server listening socket\n");
223
224 return TRUE;
225}
226
227static void
228socket_disconnect (DBusServer *server)
229{
230 DBusServerSocket *socket_server = (DBusServerSocket*) server;
231 int i;
232
233 HAVE_LOCK_CHECK (server);
234
235 for (i = 0 ; i < socket_server->n_fds ; i++)
236 {
237 if (socket_server->watch[i])
238 {
240 socket_server->watch[i]);
241 _dbus_watch_invalidate (socket_server->watch[i]);
242 _dbus_watch_unref (socket_server->watch[i]);
243 socket_server->watch[i] = NULL;
244 }
245
246 if (_dbus_socket_is_valid (socket_server->fds[i]))
247 {
248 _dbus_close_socket (socket_server->fds[i], NULL);
249 _dbus_socket_invalidate (&socket_server->fds[i]);
250 }
251 }
252
253 if (socket_server->socket_name != NULL)
254 {
255 DBusString tmp;
256 _dbus_string_init_const (&tmp, socket_server->socket_name);
257 _dbus_delete_file (&tmp, NULL);
258 }
259
260 if (server->published_address)
261 _dbus_daemon_unpublish_session_bus_address();
262
263 HAVE_LOCK_CHECK (server);
264}
265
266static const DBusServerVTable socket_vtable = {
267 socket_finalize,
268 socket_disconnect
269};
270
289 int n_fds,
290 const DBusString *address,
291 DBusNonceFile *noncefile,
292 DBusError *error)
293{
294 DBusServerSocket *socket_server;
295 DBusServer *server;
296 int i;
297
298 socket_server = dbus_new0 (DBusServerSocket, 1);
299 if (socket_server == NULL)
300 goto failed_0;
301
302 socket_server->noncefile = noncefile;
303
304 socket_server->fds = dbus_new (DBusSocket, n_fds);
305 if (!socket_server->fds)
306 goto failed_0;
307
308 socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
309 if (!socket_server->watch)
310 goto failed_1;
311
312 for (i = 0 ; i < n_fds ; i++)
313 {
314 DBusWatch *watch;
315
316 watch = _dbus_watch_new (_dbus_socket_get_pollable (fds[i]),
318 TRUE,
319 socket_handle_watch, socket_server,
320 NULL);
321 if (watch == NULL)
322 goto failed_2;
323
324 socket_server->n_fds++;
325 socket_server->fds[i] = fds[i];
326 socket_server->watch[i] = watch;
327 }
328
329 if (!_dbus_server_init_base (&socket_server->base,
330 &socket_vtable, address,
331 error))
332 goto failed_2;
333
334 server = (DBusServer*)socket_server;
335
336 SERVER_LOCK (server);
337
338 for (i = 0 ; i < n_fds ; i++)
339 {
340 if (!_dbus_server_add_watch (&socket_server->base,
341 socket_server->watch[i]))
342 {
343 int j;
344
345 /* The caller is still responsible for closing the fds until
346 * we return successfully, so don't let socket_disconnect()
347 * close them */
348 for (j = 0; j < n_fds; j++)
349 _dbus_socket_invalidate (&socket_server->fds[j]);
350
351 /* socket_disconnect() will try to remove all the watches;
352 * make sure it doesn't see the ones that weren't even added
353 * yet */
354 for (j = i; j < n_fds; j++)
355 {
356 _dbus_watch_invalidate (socket_server->watch[j]);
357 _dbus_watch_unref (socket_server->watch[j]);
358 socket_server->watch[j] = NULL;
359 }
360
361 _dbus_server_disconnect_unlocked (server);
362 SERVER_UNLOCK (server);
363 _dbus_server_finalize_base (&socket_server->base);
364 goto failed_2;
365 }
366 }
367
368 SERVER_UNLOCK (server);
369
370 _dbus_server_trace_ref (&socket_server->base, 0, 1, "new_for_socket");
371 return (DBusServer*) socket_server;
372
373 failed_2:
374 for (i = 0 ; i < n_fds ; i++)
375 {
376 if (socket_server->watch[i] != NULL)
377 {
378 _dbus_watch_invalidate (socket_server->watch[i]);
379 _dbus_watch_unref (socket_server->watch[i]);
380 socket_server->watch[i] = NULL;
381 }
382 }
383 dbus_free (socket_server->watch);
384
385 failed_1:
386 dbus_free (socket_server->fds);
387
388 failed_0:
389 dbus_free (socket_server);
390
391 if (error != NULL && !dbus_error_is_set (error))
392 _DBUS_SET_OOM (error);
393
394 return NULL;
395}
396
418 const char *bind,
419 const char *port,
420 const char *family,
421 DBusError *error,
422 dbus_bool_t use_nonce)
423{
424 DBusServer *server;
425 DBusSocket *listen_fds = NULL;
426 int nlisten_fds = 0, i;
427 DBusString address;
428 DBusString host_str;
429 DBusString port_str;
430 DBusNonceFile *noncefile;
431
432 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
433
434 noncefile = NULL;
435
436 if (!_dbus_string_init (&address))
437 {
439 return NULL;
440 }
441
442 if (!_dbus_string_init (&port_str))
443 {
445 goto failed_0;
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 &listen_fds, error);
462 if (nlisten_fds <= 0)
463 {
464 _DBUS_ASSERT_ERROR_IS_SET(error);
465 goto failed_1;
466 }
467
468 _dbus_string_init_const (&host_str, host);
469 if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
470 !_dbus_address_append_escaped (&address, &host_str) ||
471 !_dbus_string_append (&address, ",port=") ||
472 !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
473 {
475 goto failed_2;
476 }
477 if (family &&
478 (!_dbus_string_append (&address, ",family=") ||
479 !_dbus_string_append (&address, family)))
480 {
482 goto failed_2;
483 }
484
485 if (use_nonce)
486 {
487 noncefile = dbus_new0 (DBusNonceFile, 1);
488 if (noncefile == NULL)
489 {
491 goto failed_2;
492 }
493
494 if (!_dbus_noncefile_create (noncefile, error))
495 goto failed_3;
496
497 if (!_dbus_string_append (&address, ",noncefile=") ||
498 !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
499 {
501 goto failed_4;
502 }
503
504 }
505
506 server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile, error);
507 if (server == NULL)
508 {
509 if (noncefile != NULL)
510 goto failed_4;
511 else
512 goto failed_2;
513 }
514
515 _dbus_string_free (&port_str);
516 _dbus_string_free (&address);
517 dbus_free(listen_fds);
518
519 return server;
520
521 failed_4:
522 _dbus_noncefile_delete (noncefile, NULL);
523
524 failed_3:
525 dbus_free (noncefile);
526
527 failed_2:
528 for (i = 0 ; i < nlisten_fds ; i++)
529 _dbus_close_socket (listen_fds[i], NULL);
530 dbus_free(listen_fds);
531
532 failed_1:
533 _dbus_string_free (&port_str);
534
535 failed_0:
536 _dbus_string_free (&address);
537
538 return NULL;
539}
540
553DBusServerListenResult
555 DBusServer **server_p,
556 DBusError *error)
557{
558 const char *method;
559
560 *server_p = NULL;
561
562 method = dbus_address_entry_get_method (entry);
563
564 if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
565 {
566 const char *host;
567 const char *port;
568 const char *bind;
569 const char *family;
570
571 host = dbus_address_entry_get_value (entry, "host");
572 bind = dbus_address_entry_get_value (entry, "bind");
573 port = dbus_address_entry_get_value (entry, "port");
574 family = dbus_address_entry_get_value (entry, "family");
575
576 *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
577 family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
578
579 if (*server_p)
580 {
581 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
582 return DBUS_SERVER_LISTEN_OK;
583 }
584 else
585 {
586 _DBUS_ASSERT_ERROR_IS_SET(error);
587 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
588 }
589 }
590 else
591 {
592 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
593 return DBUS_SERVER_LISTEN_NOT_HANDLED;
594 }
595}
596
606void
608 char *filename)
609{
610 DBusServerSocket *socket_server = (DBusServerSocket*) server;
611
612 socket_server->socket_name = filename;
613}
614
615
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:104
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
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:244
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:702
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
#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:727
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:935
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
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.
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening socket.
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, DBusSocket **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the 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:44
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:149
Object representing a transport such as a socket.
Implementation of DBusWatch.
Definition: dbus-watch.c:41