bus-driverd.c revision af08d2f9cde8f46d9d3e731dbd1f06ffb3b08942
/***
This file is part of systemd.
Copyright 2013 Daniel Mack
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <locale.h>
#include <string.h>
#include <poll.h>
#include <netdb.h>
#include <unistd.h>
#include "kdbus.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "sd-daemon.h"
#include "sd-event.h"
#include "event-util.h"
#include "bus-util.h"
#include "bus-error.h"
#include "bus-message.h"
#include "bus-kernel.h"
#include "socket-util.h"
#include "util.h"
#include "build.h"
#include "strv.h"
#include "sd-id128.h"
#include "async.h"
#include "hashmap.h"
#include "def.h"
#include "unit-name.h"
#include "bus-control.h"
#include "cgroup-util.h"
#define CLIENTS_MAX 1024
#define MATCHES_MAX 1024
struct Match {
char *match;
};
struct Client {
unsigned n_matches;
char *watch;
};
struct Context {
};
static void match_free(Match *m) {
if (!m)
return;
if (m->client) {
if (first)
else
}
free(m);
}
static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
int r;
assert(c);
if (r < 0)
return r;
if (!m)
return -ENOMEM;
if (!m->match) {
r = -ENOMEM;
goto fail;
}
m->cookie = ++c->next_cookie;
if (r < 0) {
goto fail;
}
m->client = c;
c->n_matches++;
*_m = m;
m = NULL;
return 0;
fail:
match_free(m);
return r;
}
static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
static void client_free(Client *c) {
Match *m;
if (!c)
return;
if (c->context) {
if (c->watch)
}
while ((m = hashmap_first(c->matches)))
match_free(m);
hashmap_free(c->matches);
free(c);
}
static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
assert(m);
client_free(c);
return 0;
}
Client *c;
int r;
if (c) {
*_c = c;
return 0;
}
return -ENOBUFS;
if (r < 0)
return r;
if (!c)
return -ENOMEM;
if (r < 0)
goto fail;
"type='signal',"
"sender='org.freedesktop.DBus',"
"path='/org/freedesktop/DBus',"
"interface='org.freedesktop.DBus',"
"member='NameOwnerChanged',"
"arg0=':1.%llu'", (unsigned long long) id) < 0) {
r = -ENOMEM;
goto fail;
}
if (r < 0) {
goto fail;
}
*_c = c;
return 0;
fail:
client_free(c);
return r;
}
static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_components = 0;
char *arg0;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r == -ENOBUFS)
return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
if (r < 0)
return r;
if (c->n_matches >= MATCHES_MAX) {
r = sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u matches per client", MATCHES_MAX);
goto fail;
}
if (r < 0) {
r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
goto fail;
}
if (r < 0)
goto fail;
if (r < 0)
goto fail;
fail:
match_free(m);
if (c->n_matches <= 0)
client_free(c);
return r;
}
static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_components = 0;
char *arg0;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (!c)
return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
if (r < 0) {
r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
goto finish;
}
if (!normalized) {
r = -ENOMEM;
goto finish;
}
if (!m) {
r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.", normalized);
goto finish;
}
match_free(m);
if (c->n_matches <= 0)
client_free(c);
return r;
}
static int get_creds(sd_bus *bus, sd_bus_message *m, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
const char *name;
int r;
assert(m);
if (r < 0)
return r;
return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", name);
if (r < 0)
return r;
return -ENOTSUP;
*_creds = c;
c = NULL;
return 0;
}
static int driver_get_security_context(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
int r;
if (r < 0)
return r;
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
int r;
if (r < 0)
return r;
}
char buf[SD_ID128_STRING_MAX];
int r;
if (r < 0)
return r;
}
}
int r;
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
r = sd_bus_message_append_strv(reply, l);
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
/* Let's sort the names list to make it stable */
}
static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
int r;
if (r < 0)
return r;
/* Let's sort the names list to make it stable */
}
static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
struct kdbus_cmd_name_list cmd = {};
struct kdbus_name_list *name_list;
struct kdbus_cmd_name *name;
char *arg0;
int r;
if (r < 0)
return r;
if (r < 0)
return -errno;
char *n;
continue;
continue;
return -ENOMEM;
if (r < 0) {
free(n);
return -ENOMEM;
}
}
if (r < 0)
return -errno;
}
static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
const char *name;
int r;
if (r < 0)
return r;
return r;
return sd_bus_reply_method_return(m, "b", r >= 0);
}
static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
struct kdbus_cmd_name *n;
const char *name;
int r;
if (r < 0)
return r;
assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
/* This function is open-coded because we request the name 'on behalf'
* of the requesting connection */
if (r < 0)
return r;
if (r < 0) {
return -errno;
}
if (n->flags & KDBUS_NAME_IN_QUEUE)
}
static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
struct kdbus_cmd_name *n;
const char *name;
int r;
if (r < 0)
return r;
/* This function is open-coded because we request the name 'on behalf'
* of the requesting connection */
if (r < 0)
return r;
if (r < 0) {
if (errno == EADDRINUSE)
return -errno;
}
}
static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_strv_free_ char **t = NULL;
char *name, *u;
int r;
if (r < 0)
return r;
if (r >= 0)
if (r != -ENOENT)
return r;
path = unit_dbus_path_from_name(u);
if (!path)
return -ENOMEM;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"Triggers",
&t);
if (r < 0)
return r;
if (!t || !t[0] || t[1])
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit",
&reply,
"ss",
t[0],
"replace");
if (r < 0)
return r;
}
static int driver_update_environment(sd_bus*bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
int r;
if (r < 0)
return r;
_cleanup_free_ char *s = NULL;
const char *key;
const char *value;
if (r < 0)
return r;
if (!s)
return ENOMEM;
r = strv_extend(&args, s);
if (r < 0)
return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
}
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
if (!args)
return -EINVAL;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetEnvironment",
&msg);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return sd_bus_reply_method_return(m, NULL);
}
static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
}
static const sd_bus_vtable driver_vtable[] = {
SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_context, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
};
static int find_object(
const char *path,
const char *interface,
void *userdata,
void **ret_found,
/* We support the driver interface on exactly two different
* paths: the root and the entry point object. This is a bit
* different from the original dbus-daemon which supported it
* on any path. */
return 1;
return 1;
return 0;
}
static int node_enumerator(
const char *path,
void *userdata,
char ***ret_nodes,
char **l;
if (!l)
return -ENOMEM;
*ret_nodes = l;
return 0;
}
static int connect_bus(Context *c) {
int r;
assert(c);
r = sd_bus_default(&c->bus);
if (r < 0) {
return r;
}
log_error("Not running on kdbus");
return -EPERM;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
return 0;
}
static bool check_idle(void *userdata) {
assert(c);
return hashmap_isempty(c->clients);
}
Client *c;
int r;
log_open();
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
if (r < 0) {
goto finish;
}
r = connect_bus(&context);
if (r < 0)
goto finish;
r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
if (r < 0) {
goto finish;
}
client_free(c);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}