File: cli_dbus.c

package info (click to toggle)
libteam 1.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,344 kB
  • sloc: ansic: 17,494; sh: 1,242; python: 613; makefile: 102
file content (226 lines) | stat: -rw-r--r-- 5,485 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *   cli_dbus.c - Teamd daemon control library D-Bus client
 *   Copyright (C) 2013-2015 Jiri Pirko <jiri@resnulli.us>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it 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.
 *
 *   This library 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 this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#ifdef ENABLE_DBUS

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dbus/dbus.h>
#include <teamdctl.h>
#include "teamdctl_private.h"
#include "../teamd/teamd_dbus_common.h"

struct cli_dbus_priv {
	DBusConnection *conn;
	char *service_name;
};

static int cli_dbus_check_error_msg(struct teamdctl *tdc, DBusMessage *msg)
{
	DBusMessageIter args;
	dbus_bool_t dbres;
	char *param = NULL;
	const char *err_msg;

	err_msg = dbus_message_get_error_name(msg);
	if (!err_msg)
		return 0;
	err(tdc, "dbus: Error message received: \"%s\"", err_msg);

	dbres = dbus_message_iter_init(msg, &args);
	if (dbres == TRUE) {
		if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
			err(tdc, "dbus: Received argument is not string as expected.");
			return -EINVAL;
		}
		dbus_message_iter_get_basic(&args, &param);
		err(tdc, "dbus: Error message content: \"%s\"", param);
	}
	return -EINVAL;
}

static int cli_dbus_get_reply_str(struct teamdctl *tdc, char **p_reply,
				  DBusMessage *msg)
{
	DBusMessageIter args;
	dbus_bool_t dbres;
	char *param = NULL;

	dbres = dbus_message_iter_init(msg, &args);
	if (dbres == FALSE) {
		err(tdc, "Failed, no data received.");
		return -EINVAL;
	}

	if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
		err(tdc, "dbus: Received argument is not string as expected.");
		return -EINVAL;
	}
	dbus_message_iter_get_basic(&args, &param);
	*p_reply = param;
	return 0;
}

static int cli_dbus_method_call(struct teamdctl *tdc, const char *method_name,
				char **p_reply, void *priv,
				const char *fmt, va_list ap)
{
	struct cli_dbus_priv *cli_dbus = priv;
	char *str;
	DBusMessage *msg;
	DBusMessageIter iter;
	dbus_bool_t dbres;
	DBusPendingCall *pending;
	char *reply;
	int err;

	dbg(tdc, "dbus: Calling method \"%s\"", method_name);
	msg = dbus_message_new_method_call(cli_dbus->service_name,
					   TEAMD_DBUS_PATH, TEAMD_DBUS_IFACE,
					   method_name);
	if (!msg) {
		err(tdc, "dbus: Failed to create message.");
		return -ENOMEM;
	}
	dbus_message_iter_init_append(msg, &iter);
	while (*fmt) {
		switch (*fmt++) {
		case 's': /* string */
			str = va_arg(ap, char *);
			dbres = dbus_message_iter_append_basic(&iter,
							       DBUS_TYPE_STRING,
							       &str);
			if (dbres == FALSE) {
				err(tdc, "dbus: Failed to construct message.");
				err = -ENOMEM;
				goto free_msg;
			}
			break;
		default:
			err(tdc, "dbus: Unknown argument type requested.");
			err = -EINVAL;
			goto free_msg;
		}
	}

	dbres = dbus_connection_send_with_reply(cli_dbus->conn, msg,
						&pending, TEAMDCTL_REPLY_TIMEOUT);
	if (dbres == FALSE) {
		err(tdc, "dbus: Send with reply failed.");
		err = -ENOMEM;
		goto free_msg;
	}
	if (!pending) {
		err(tdc, "dbus: Pending call not created.");
		err = -ENOMEM;
		goto free_msg;
	}

	dbus_pending_call_block(pending);

	dbus_message_unref(msg);
	msg = dbus_pending_call_steal_reply(pending);
	dbus_pending_call_unref(pending);
	if (!msg) {
		err(tdc, "dbus: Failed to get reply.");
		err = -EINVAL;
		goto out;
	}

	err = cli_dbus_check_error_msg(tdc, msg);
	if (err)
		goto free_msg;

	if (p_reply) {
		err = cli_dbus_get_reply_str(tdc, &reply, msg);
		if (err)
			goto free_msg;

		reply = strdup(reply);
		if (!reply) {
			err = -ENOMEM;
			goto free_msg;
		}
		*p_reply = reply;
	}

free_msg:
	dbus_message_unref(msg);
out:
	return err;
}

static int cli_dbus_init(struct teamdctl *tdc, const char *team_name, void *priv)
{
	struct cli_dbus_priv *cli_dbus = priv;
	DBusError error;
	int ret;
	int err;

	ret = asprintf(&cli_dbus->service_name, TEAMD_DBUS_SERVICE ".%s",
		       team_name);
	if (ret == -1)
		return -errno;

	dbus_error_init(&error);
	cli_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if (!cli_dbus->conn) {
		err(tdc, "dbus: Could not acquire the system bus: %s - %s",
			 error.name, error.message);
		err = -EINVAL;
		goto free_service_name;
	}
	err = 0;
	goto free_error;

free_service_name:
	free(cli_dbus->service_name);
free_error:
	dbus_error_free(&error);
	return err;
}

void cli_dbus_fini(struct teamdctl *tdc, void *priv)
{
	struct cli_dbus_priv *cli_dbus = priv;

	free(cli_dbus->service_name);
	dbus_connection_unref(cli_dbus->conn);
}

static const struct teamdctl_cli cli_dbus = {
	.name = "dbus",
	.init = cli_dbus_init,
	.fini = cli_dbus_fini,
	.test_method_call_required = true,
	.method_call = cli_dbus_method_call,
	.priv_size = sizeof(struct cli_dbus_priv),
};

const struct teamdctl_cli *teamdctl_cli_dbus_get(void)
{
	return &cli_dbus;
}

#endif /* ENABLE_DBUS */