blob: 2200bed2bcff5d34c01c81086674c0c5f0370cd2 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11003
4#define _GNU_SOURCE
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11005#include <systemd/sd-bus.h>
6
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11007#include "common.h"
8#include "dbus.h"
Andrew Jeffery68023072018-08-06 10:08:11 +09309#include "mbox.h"
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +093010#include "control_dbus.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110011
Andrew Jefferyba0bbab2018-08-06 09:43:57 +093012int mboxd_dbus_init(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110013{
14 int rc;
15
16 rc = sd_bus_default_system(&context->bus);
17 if (rc < 0) {
18 MSG_ERR("Failed to connect to the system bus: %s\n",
19 strerror(-rc));
20 return rc;
21 }
22
Andrew Jeffery68023072018-08-06 10:08:11 +093023 rc = control_legacy_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110024 if (rc < 0) {
Andrew Jeffery68023072018-08-06 10:08:11 +093025 MSG_ERR("Failed to initialise legacy DBus interface: %s\n",
26 strerror(-rc));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110027 return rc;
28 }
29
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +093030 rc = control_dbus_init(context);
31 if (rc < 0) {
32 MSG_ERR("Failed to initialise DBus control interface: %s\n",
33 strerror(-rc));
34 return rc;
35 }
36
37 rc = sd_bus_request_name(context->bus, MBOX_DBUS_NAME,
38 SD_BUS_NAME_ALLOW_REPLACEMENT |
39 SD_BUS_NAME_REPLACE_EXISTING);
40 if (rc < 0) {
41 MSG_ERR("Failed to request bus name: %s\n", strerror(-rc));
42 return rc;
43 }
44
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110045 rc = sd_bus_get_fd(context->bus);
46 if (rc < 0) {
47 MSG_ERR("Failed to get bus fd: %s\n", strerror(-rc));
48 return rc;
49 }
50
51 context->fds[DBUS_FD].fd = rc;
52
53 return 0;
54}
55
Andrew Jefferyba0bbab2018-08-06 09:43:57 +093056void mboxd_dbus_free(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110057{
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +093058 control_dbus_free(context);
Andrew Jeffery68023072018-08-06 10:08:11 +093059 control_legacy_free(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110060 sd_bus_unref(context->bus);
61}