Coverage report for dbus/dbus-server-unix.c.gcov
-: 0:Source:dbus-server-unix.c
-: 0:Graph:.libs/dbus-server-unix.gcno
-: 0:Data:.libs/dbus-server-unix.gcda
-: 0:Runs:11820
-: 0:Programs:5
-: 1:/* -*- mode: C; c-file-style: "gnu" -*- */
-: 2:/* dbus-server-unix.c Server implementation for Unix network protocols.
-: 3: *
-: 4: * Copyright (C) 2002, 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-: 21: *
-: 22: */
-: 23:
-: 24:#include "dbus-internals.h"
-: 25:#include "dbus-server-unix.h"
-: 26:#include "dbus-transport-unix.h"
-: 27:#include "dbus-connection-internal.h"
-: 28:#include "dbus-string.h"
-: 29:#include <sys/types.h>
-: 30:#include <unistd.h>
-: 31:
-: 32:/**
-: 33: * @defgroup DBusServerUnix DBusServer implementations for UNIX
-: 34: * @ingroup DBusInternals
-: 35: * @brief Implementation details of DBusServer on UNIX
-: 36: *
-: 37: * @{
-: 38: */
-: 39:/**
-: 40: *
-: 41: * Opaque object representing a Unix server implementation.
-: 42: */
-: 43:typedef struct DBusServerUnix DBusServerUnix;
-: 44:
-: 45:/**
-: 46: * Implementation details of DBusServerUnix. All members
-: 47: * are private.
-: 48: */
-: 49:struct DBusServerUnix
-: 50:{
-: 51: DBusServer base; /**< Parent class members. */
-: 52: int fd; /**< File descriptor or -1 if disconnected. */
-: 53: DBusWatch *watch; /**< File descriptor watch. */
-: 54: char *socket_name; /**< Name of domain socket, to unlink if appropriate */
-: 55:};
-: 56:
-: 57:static void
-: 58:unix_finalize (DBusServer *server)
function unix_finalize called 13 returned 100% blocks executed 71%
13: 59:{
13: 60: DBusServerUnix *unix_server = (DBusServerUnix*) server;
-: 61:
13: 62: _dbus_server_finalize_base (server);
call 0 returned 100%
-: 63:
13: 64: if (unix_server->watch)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 65: {
#####: 66: _dbus_watch_unref (unix_server->watch);
call 0 never executed
#####: 67: unix_server->watch = NULL;
-: 68: }
-: 69:
13: 70: dbus_free (unix_server->socket_name);
call 0 returned 100%
13: 71: dbus_free (server);
call 0 returned 100%
13: 72:}
-: 73:
-: 74:/**
-: 75: * @todo unreffing the connection at the end may cause
-: 76: * us to drop the last ref to the connection before
-: 77: * disconnecting it. That is invalid.
-: 78: *
-: 79: * @todo doesn't this leak a server refcount if
-: 80: * new_connection_function is NULL?
-: 81: */
-: 82:/* Return value is just for memory, not other failures. */
-: 83:static dbus_bool_t
-: 84:handle_new_client_fd_and_unlock (DBusServer *server,
-: 85: int client_fd)
function handle_new_client_fd_and_unlock called 2467 returned 100% blocks executed 79%
2467: 86:{
-: 87: DBusConnection *connection;
-: 88: DBusTransport *transport;
-: 89: DBusNewConnectionFunction new_connection_function;
-: 90: void *new_connection_data;
-: 91:
2467: 92: _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
call 0 returned 100%
-: 93:
2467: 94: HAVE_LOCK_CHECK (server);
call 0 returned 100%
-: 95:
2467: 96: if (!_dbus_set_fd_nonblocking (client_fd, NULL))
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 97: {
#####: 98: SERVER_UNLOCK (server);
call 0 never executed
call 1 never executed
#####: 99: return TRUE;
-: 100: }
-: 101:
2467: 102: transport = _dbus_transport_new_for_fd (client_fd, &server->guid_hex, NULL);
call 0 returned 100%
2467: 103: if (transport == NULL)
branch 0 taken 2% (fallthrough)
branch 1 taken 98%
-: 104: {
53: 105: close (client_fd);
call 0 returned 100%
53: 106: SERVER_UNLOCK (server);
call 0 returned 100%
call 1 returned 100%
53: 107: return FALSE;
-: 108: }
-: 109:
2414: 110: if (!_dbus_transport_set_auth_mechanisms (transport,
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 111: (const char **) server->auth_mechanisms))
-: 112: {
#####: 113: _dbus_transport_unref (transport);
call 0 never executed
#####: 114: SERVER_UNLOCK (server);
call 0 never executed
call 1 never executed
#####: 115: return FALSE;
-: 116: }
-: 117:
-: 118: /* note that client_fd is now owned by the transport, and will be
-: 119: * closed on transport disconnection/finalization
-: 120: */
-: 121:
2414: 122: connection = _dbus_connection_new_for_transport (transport);
call 0 returned 100%
2414: 123: _dbus_transport_unref (transport);
call 0 returned 100%
2414: 124: transport = NULL; /* now under the connection lock */
-: 125:
2414: 126: if (connection == NULL)
branch 0 taken 1% (fallthrough)
branch 1 taken 99%
-: 127: {
33: 128: SERVER_UNLOCK (server);
call 0 returned 100%
call 1 returned 100%
33: 129: return FALSE;
-: 130: }
-: 131:
-: 132: /* See if someone wants to handle this new connection, self-referencing
-: 133: * for paranoia.
-: 134: */
2381: 135: new_connection_function = server->new_connection_function;
2381: 136: new_connection_data = server->new_connection_data;
-: 137:
2381: 138: _dbus_server_ref_unlocked (server);
call 0 returned 100%
2381: 139: SERVER_UNLOCK (server);
call 0 returned 100%
call 1 returned 100%
-: 140:
2381: 141: if (new_connection_function)
branch 0 taken 100% (fallthrough)
branch 1 taken 0%
-: 142: {
2381: 143: (* new_connection_function) (server, connection,
call 0 returned 100%
-: 144: new_connection_data);
2381: 145: dbus_server_unref (server);
call 0 returned 100%
-: 146: }
-: 147:
-: 148: /* If no one grabbed a reference, the connection will die. */
2381: 149: dbus_connection_unref (connection);
call 0 returned 100%
-: 150:
2381: 151: return TRUE;
-: 152:}
-: 153:
-: 154:static dbus_bool_t
-: 155:unix_handle_watch (DBusWatch *watch,
-: 156: unsigned int flags,
-: 157: void *data)
function unix_handle_watch called 2467 returned 100% blocks executed 54%
2467: 158:{
2467: 159: DBusServer *server = data;
2467: 160: DBusServerUnix *unix_server = data;
-: 161:
2467: 162: SERVER_LOCK (server);
call 0 returned 100%
call 1 returned 100%
-: 163:
2467: 164: _dbus_assert (watch == unix_server->watch);
call 0 returned 100%
-: 165:
2467: 166: _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
call 0 returned 100%
-: 167:
2467: 168: if (flags & DBUS_WATCH_READABLE)
branch 0 taken 100% (fallthrough)
branch 1 taken 0%
-: 169: {
-: 170: int client_fd;
-: 171: int listen_fd;
-: 172:
2467: 173: listen_fd = dbus_watch_get_fd (watch);
call 0 returned 100%
-: 174:
2467: 175: client_fd = _dbus_accept (listen_fd);
call 0 returned 100%
-: 176:
2467: 177: if (client_fd < 0)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 178: {
-: 179: /* EINTR handled for us */
-: 180:
#####: 181: if (errno == EAGAIN || errno == EWOULDBLOCK)
call 0 never executed
branch 1 never executed
branch 2 never executed
call 3 never executed
branch 4 never executed
branch 5 never executed
#####: 182: _dbus_verbose ("No client available to accept after all\n");
call 0 never executed
-: 183: else
#####: 184: _dbus_verbose ("Failed to accept a client connection: %s\n",
call 0 never executed
call 1 never executed
call 2 never executed
-: 185: _dbus_strerror (errno));
-: 186:
#####: 187: SERVER_UNLOCK (server);
call 0 never executed
call 1 never executed
-: 188: }
-: 189: else
-: 190: {
2467: 191: _dbus_fd_set_close_on_exec (client_fd);
call 0 returned 100%
-: 192:
2467: 193: if (!handle_new_client_fd_and_unlock (server, client_fd))
call 0 returned 100%
branch 1 taken 3% (fallthrough)
branch 2 taken 97%
86: 194: _dbus_verbose ("Rejected client connection due to lack of memory\n");
call 0 returned 100%
-: 195: }
-: 196: }
-: 197:
2467: 198: if (flags & DBUS_WATCH_ERROR)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
#####: 199: _dbus_verbose ("Error on server listening socket\n");
call 0 never executed
-: 200:
2467: 201: if (flags & DBUS_WATCH_HANGUP)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
#####: 202: _dbus_verbose ("Hangup on server listening socket\n");
call 0 never executed
-: 203:
2467: 204: return TRUE;
-: 205:}
-: 206:
-: 207:static void
-: 208:unix_disconnect (DBusServer *server)
function unix_disconnect called 13 returned 100% blocks executed 100%
13: 209:{
13: 210: DBusServerUnix *unix_server = (DBusServerUnix*) server;
-: 211:
13: 212: HAVE_LOCK_CHECK (server);
call 0 returned 100%
-: 213:
13: 214: if (unix_server->watch)
branch 0 taken 100% (fallthrough)
branch 1 taken 0%
-: 215: {
13: 216: _dbus_server_remove_watch (server,
call 0 returned 100%
-: 217: unix_server->watch);
13: 218: _dbus_watch_unref (unix_server->watch);
call 0 returned 100%
13: 219: unix_server->watch = NULL;
-: 220: }
-: 221:
13: 222: close (unix_server->fd);
call 0 returned 100%
13: 223: unix_server->fd = -1;
-: 224:
13: 225: if (unix_server->socket_name != NULL)
branch 0 taken 38% (fallthrough)
branch 1 taken 62%
-: 226: {
-: 227: DBusString tmp;
5: 228: _dbus_string_init_const (&tmp, unix_server->socket_name);
call 0 returned 100%
5: 229: _dbus_delete_file (&tmp, NULL);
call 0 returned 100%
-: 230: }
-: 231:
13: 232: HAVE_LOCK_CHECK (server);
call 0 returned 100%
13: 233:}
-: 234:
-: 235:static const DBusServerVTable unix_vtable = {
-: 236: unix_finalize,
-: 237: unix_disconnect
-: 238:};
-: 239:
-: 240:/**
-: 241: * Creates a new server listening on the given file descriptor. The
-: 242: * file descriptor should be nonblocking (use
-: 243: * _dbus_set_fd_nonblocking() to make it so). The file descriptor
-: 244: * should be listening for connections, that is, listen() should have
-: 245: * been successfully invoked on it. The server will use accept() to
-: 246: * accept new client connections.
-: 247: *
-: 248: * @param fd the file descriptor.
-: 249: * @param address the server's address
-: 250: * @returns the new server, or #NULL if no memory.
-: 251: *
-: 252: */
-: 253:DBusServer*
-: 254:_dbus_server_new_for_fd (int fd,
-: 255: const DBusString *address)
function _dbus_server_new_for_fd called 13 returned 100% blocks executed 54%
13: 256:{
-: 257: DBusServerUnix *unix_server;
-: 258: DBusServer *server;
-: 259: DBusWatch *watch;
-: 260:
13: 261: unix_server = dbus_new0 (DBusServerUnix, 1);
call 0 returned 100%
13: 262: if (unix_server == NULL)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
#####: 263: return NULL;
-: 264:
13: 265: watch = _dbus_watch_new (fd,
call 0 returned 100%
-: 266: DBUS_WATCH_READABLE,
-: 267: TRUE,
-: 268: unix_handle_watch, unix_server,
-: 269: NULL);
13: 270: if (watch == NULL)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 271: {
#####: 272: dbus_free (unix_server);
call 0 never executed
#####: 273: return NULL;
-: 274: }
-: 275:
13: 276: if (!_dbus_server_init_base (&unix_server->base,
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 277: &unix_vtable, address))
-: 278: {
#####: 279: _dbus_watch_unref (watch);
call 0 never executed
#####: 280: dbus_free (unix_server);
call 0 never executed
#####: 281: return NULL;
-: 282: }
-: 283:
13: 284: server = (DBusServer*) unix_server;
-: 285:
13: 286: SERVER_LOCK (server);
call 0 returned 100%
call 1 returned 100%
-: 287:
13: 288: if (!_dbus_server_add_watch (&unix_server->base,
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 289: watch))
-: 290: {
#####: 291: SERVER_UNLOCK (server);
call 0 never executed
call 1 never executed
#####: 292: _dbus_server_finalize_base (&unix_server->base);
call 0 never executed
#####: 293: _dbus_watch_unref (watch);
call 0 never executed
#####: 294: dbus_free (unix_server);
call 0 never executed
#####: 295: return NULL;
-: 296: }
-: 297:
13: 298: unix_server->fd = fd;
13: 299: unix_server->watch = watch;
-: 300:
13: 301: SERVER_UNLOCK (server);
call 0 returned 100%
call 1 returned 100%
-: 302:
13: 303: return (DBusServer*) unix_server;
-: 304:}
-: 305:
-: 306:/**
-: 307: * Creates a new server listening on the given Unix domain socket.
-: 308: *
-: 309: * @param path the path for the domain socket.
-: 310: * @param abstract #TRUE to use abstract socket namespace
-: 311: * @param error location to store reason for failure.
-: 312: * @returns the new server, or #NULL on failure.
-: 313: */
-: 314:DBusServer*
-: 315:_dbus_server_new_for_domain_socket (const char *path,
-: 316: dbus_bool_t abstract,
-: 317: DBusError *error)
function _dbus_server_new_for_domain_socket called 5 returned 100% blocks executed 57%
5: 318:{
-: 319: DBusServer *server;
-: 320: DBusServerUnix *unix_server;
-: 321: int listen_fd;
-: 322: DBusString address;
-: 323: char *path_copy;
-: 324: DBusString path_str;
-: 325:
5: 326: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
branch 0 taken 60% (fallthrough)
branch 1 taken 40%
call 2 returned 100%
branch 3 taken 100% (fallthrough)
branch 4 taken 0%
call 5 returned 100%
-: 327:
5: 328: if (!_dbus_string_init (&address))
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 329: {
#####: 330: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 331: return NULL;
-: 332: }
-: 333:
5: 334: _dbus_string_init_const (&path_str, path);
call 0 returned 100%
5: 335: if ((abstract &&
branch 0 taken 60% (fallthrough)
branch 1 taken 40%
call 2 returned 100%
branch 3 taken 100% (fallthrough)
branch 4 taken 0%
branch 5 taken 40% (fallthrough)
branch 6 taken 60%
call 7 returned 100%
branch 8 taken 100% (fallthrough)
branch 9 taken 0%
call 10 returned 100%
branch 11 taken 0% (fallthrough)
branch 12 taken 100%
-: 336: !_dbus_string_append (&address, "unix:abstract=")) ||
-: 337: (!abstract &&
-: 338: !_dbus_string_append (&address, "unix:path=")) ||
-: 339: !_dbus_address_append_escaped (&address, &path_str))
-: 340: {
#####: 341: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 342: goto failed_0;
-: 343: }
-: 344:
5: 345: path_copy = _dbus_strdup (path);
call 0 returned 100%
5: 346: if (path_copy == NULL)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 347: {
#####: 348: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 349: goto failed_0;
-: 350: }
-: 351:
5: 352: listen_fd = _dbus_listen_unix_socket (path, abstract, error);
call 0 returned 100%
5: 353: _dbus_fd_set_close_on_exec (listen_fd);
call 0 returned 100%
-: 354:
5: 355: if (listen_fd < 0)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 356: {
#####: 357: _DBUS_ASSERT_ERROR_IS_SET (error);
branch 0 never executed
branch 1 never executed
call 2 never executed
branch 3 never executed
branch 4 never executed
call 5 never executed
#####: 358: goto failed_1;
-: 359: }
-: 360:
5: 361: server = _dbus_server_new_for_fd (listen_fd, &address);
call 0 returned 100%
5: 362: if (server == NULL)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 363: {
#####: 364: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 365: goto failed_2;
-: 366: }
-: 367:
5: 368: unix_server = (DBusServerUnix*) server;
5: 369: unix_server->socket_name = path_copy;
-: 370:
5: 371: _dbus_string_free (&address);
call 0 returned 100%
-: 372:
5: 373: return server;
-: 374:
#####: 375: failed_2:
#####: 376: _dbus_close (listen_fd, NULL);
call 0 never executed
#####: 377: failed_1:
#####: 378: dbus_free (path_copy);
call 0 never executed
#####: 379: failed_0:
#####: 380: _dbus_string_free (&address);
call 0 never executed
-: 381:
#####: 382: return NULL;
-: 383:}
-: 384:
-: 385:/**
-: 386: * Creates a new server listening on the given hostname and port.
-: 387: * If the hostname is NULL, listens on localhost.
-: 388: *
-: 389: * @param host the hostname to listen on.
-: 390: * @param port the port to listen on.
-: 391: * @param error location to store reason for failure.
-: 392: * @returns the new server, or #NULL on failure.
-: 393: */
-: 394:DBusServer*
-: 395:_dbus_server_new_for_tcp_socket (const char *host,
-: 396: dbus_uint32_t port,
-: 397: DBusError *error)
function _dbus_server_new_for_tcp_socket called 8 returned 100% blocks executed 63%
8: 398:{
-: 399: DBusServer *server;
-: 400: int listen_fd;
-: 401: DBusString address;
-: 402: DBusString host_str;
-: 403:
8: 404: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
call 2 never executed
branch 3 never executed
branch 4 never executed
call 5 returned 100%
-: 405:
8: 406: if (!_dbus_string_init (&address))
call 0 returned 100%
branch 1 taken 0% (fallthrough)
branch 2 taken 100%
-: 407: {
#####: 408: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 409: return NULL;
-: 410: }
-: 411:
8: 412: if (host == NULL)
branch 0 taken 50% (fallthrough)
branch 1 taken 50%
4: 413: host = "localhost";
-: 414:
8: 415: _dbus_string_init_const (&host_str, host);
call 0 returned 100%
8: 416: if (!_dbus_string_append (&address, "tcp:host=") ||
call 0 returned 100%
branch 1 taken 100% (fallthrough)
branch 2 taken 0%
call 3 returned 100%
branch 4 taken 100% (fallthrough)
branch 5 taken 0%
call 6 returned 100%
branch 7 taken 100% (fallthrough)
branch 8 taken 0%
call 9 returned 100%
branch 10 taken 0% (fallthrough)
branch 11 taken 100%
-: 417: !_dbus_address_append_escaped (&address, &host_str) ||
-: 418: !_dbus_string_append (&address, ",port=") ||
-: 419: !_dbus_string_append_int (&address, port))
-: 420: {
#####: 421: _dbus_string_free (&address);
call 0 never executed
#####: 422: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 423: return NULL;
-: 424: }
-: 425:
8: 426: listen_fd = _dbus_listen_tcp_socket (host, port, error);
call 0 returned 100%
8: 427: _dbus_fd_set_close_on_exec (listen_fd);
call 0 returned 100%
-: 428:
8: 429: if (listen_fd < 0)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 430: {
#####: 431: _dbus_string_free (&address);
call 0 never executed
#####: 432: return NULL;
-: 433: }
-: 434:
8: 435: server = _dbus_server_new_for_fd (listen_fd, &address);
call 0 returned 100%
8: 436: if (server == NULL)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
-: 437: {
#####: 438: dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call 0 never executed
#####: 439: close (listen_fd);
call 0 never executed
#####: 440: _dbus_string_free (&address);
call 0 never executed
#####: 441: return NULL;
-: 442: }
-: 443:
8: 444: _dbus_string_free (&address);
call 0 returned 100%
-: 445:
8: 446: return server;
-: 447:
-: 448:
-: 449:}
-: 450:
-: 451:/** @} */
-: 452: