summaryrefslogtreecommitdiff
path: root/src/util/systemd_dbus.c
blob: 37154b62af54480f7bf086010f05efc6cd9ff75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * This file is part of activationd.
 *
 * Copyright © 2019 Samsung Electronics
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <systemd/sd-bus.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

#include "common.h"
#include "systemd_dbus.h"
#include "log.h"

static sd_bus *systemd_bus = NULL;
static bool epc_systemd_private = false;

int epc_dbus_call(char *service, char *obj, char *interface, char *method,
					 sd_bus_error *error, sd_bus_message **out_msg,
					 char *types, ...)
{
	_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
	_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
	_cleanup_(sd_bus_error_free) sd_bus_error error_buf = SD_BUS_ERROR_NULL;
	int ret;

	if (!error)
		error = &error_buf;

	ret = epc_acquire_systemd_bus(&bus);
	if (ret < 0)
		return ret;

	ret = sd_bus_message_new_method_call(bus, &m, service,
										 obj, interface, method);
	if (ret < 0)
		goto fail;

	if (types) {
		va_list args;

		va_start(args, types);
		ret = sd_bus_message_appendv(m, types, args);
		va_end(args);
		if (ret < 0)
			goto fail;
	}

	return sd_bus_call(bus, m, 0, error, out_msg);

fail:
	return sd_bus_error_set_errno(error, ret);
}

static int get_bus_by_address(const char *addr, sd_bus **bus)
{
	_cleanup_(sd_bus_unrefp) sd_bus *rbus = NULL;
	int ret;

	ret = sd_bus_new(&rbus);
	if (ret < 0)
		return ret;

	ret = sd_bus_set_address(rbus, addr);
	if (ret < 0)
		return ret;

	ret = sd_bus_start(rbus);
	if (ret < 0)
		return ret;

	*bus = rbus;
	rbus = NULL;
	return 0;
}

int epc_acquire_systemd_bus(sd_bus **bus)
{
	int ret;

	if (systemd_bus) {
		*bus = systemd_bus;
		sd_bus_ref(systemd_bus);
		return 0;
	}

	if (!epc_systemd_private) {
		ret = sd_bus_open_system(bus);
		if (ret < 0)
			return ret;
	} else {
		ret = get_bus_by_address(SYSTEMD_KERNEL_BUS, bus);
		if (ret < 0) {
			ret = get_bus_by_address(SYSTEMD_PRIVATE_BUS, bus);
			if (ret < 0)
				return ret;
		}
	}

	systemd_bus = *bus;

	return 0;
}

void epc_set_systemd_private(bool priv)
{
	epc_systemd_private = priv;
}