D-Bus  1.14.99
dbus-server-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 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-server-launchd.h"
28 #include "dbus-transport-unix.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-sysdeps-unix.h"
31 #include "dbus-string.h"
32 
52 DBusServerListenResult
54  DBusServer **server_p,
55  DBusError *error)
56 {
57  const char *method;
58 
59  *server_p = NULL;
60 
61  method = dbus_address_entry_get_method (entry);
62  if (strcmp (method, "systemd") == 0)
63  {
64  int i, n;
65  DBusSocket *fds;
66  DBusString address;
67 
68  n = _dbus_listen_systemd_sockets (&fds, error);
69  if (n < 0)
70  {
71  _DBUS_ASSERT_ERROR_IS_SET (error);
72  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
73  }
74 
75  if (!_dbus_string_init (&address))
76  goto systemd_oom;
77 
78  for (i = 0; i < n; i++)
79  {
80  if (i > 0)
81  {
82  if (!_dbus_string_append (&address, ";"))
83  goto systemd_oom;
84  }
85  if (!_dbus_append_address_from_socket (fds[i], &address, error))
86  goto systemd_err;
87  }
88 
89  *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL, error);
90  if (*server_p == NULL)
91  goto systemd_err;
92 
93  dbus_free (fds);
94  _dbus_string_free (&address);
95 
96  return DBUS_SERVER_LISTEN_OK;
97 
98  systemd_oom:
99  _DBUS_SET_OOM (error);
100  systemd_err:
101  for (i = 0; i < n; i++)
102  {
103  _dbus_close_socket (&fds[i], NULL);
104  }
105  dbus_free (fds);
106  _dbus_string_free (&address);
107 
108  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
109  }
110 #ifdef DBUS_ENABLE_LAUNCHD
111  else if (strcmp (method, "launchd") == 0)
112  {
113  const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
114  if (launchd_env_var == NULL)
115  {
116  _dbus_set_bad_address (error, "launchd", "env", NULL);
117  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
118  }
119  *server_p = _dbus_server_new_for_launchd (launchd_env_var, error);
120 
121  if (*server_p != NULL)
122  {
123  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
124  return DBUS_SERVER_LISTEN_OK;
125  }
126  else
127  {
128  _DBUS_ASSERT_ERROR_IS_SET(error);
129  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
130  }
131  }
132 #endif
133  else
134  {
135  /* If we don't handle the method, we return NULL with the
136  * error unset
137  */
138  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
139  return DBUS_SERVER_LISTEN_NOT_HANDLED;
140  }
141 }
142 
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition: dbus-address.c:68
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
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
DBusServer * _dbus_server_new_for_launchd(const char *launchd_env_var, DBusError *error)
Creates a new server from launchd.
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_platform_specific(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry in a platform-specific way, creating a platform-specific server ...
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:980
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
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
int _dbus_listen_systemd_sockets(DBusSocket **fds, DBusError *error)
Acquires one or more sockets passed in from systemd.
dbus_bool_t _dbus_append_address_from_socket(DBusSocket fd, DBusString *address, DBusError *error)
Read the address from the socket and append it to the string.
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
Object representing an exception.
Definition: dbus-errors.h:49
Internals of DBusServer object.
Socket interface.
Definition: dbus-sysdeps.h:181