summaryrefslogtreecommitdiff
path: root/lib/lvmpolld/lvmpolld-client.c
blob: ee8860fec85c56155fe0f8198e66b862f68c588d (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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"

#include "daemon-io.h"
#include "lvmpolld-client.h"
#include "lvmpolld-protocol.h"
#include "metadata-exported.h"
#include "polldaemon.h"
#include "toolcontext.h"

struct progress_info {
	unsigned error:1;
	unsigned finished:1;
	int cmd_signal;
	int cmd_retcode;
};

static int _lvmpolld_use;
static int _lvmpolld_connected;
static const char* _lvmpolld_socket;

static daemon_handle _lvmpolld = { .error = 0 };

static daemon_handle _lvmpolld_connect(const char *socket)
{
	daemon_info lvmpolld_info = {
		.path = "lvmpolld",
		.socket = socket ?: LVMPOLLD_SOCKET,
		.protocol = LVMPOLLD_PROTOCOL,
		.protocol_version = LVMPOLLD_PROTOCOL_VERSION
	};

	return daemon_open(lvmpolld_info);
}

void lvmpolld_set_active(int active)
{
	_lvmpolld_use = active;
}

void lvmpolld_set_socket(const char *socket)
{
	_lvmpolld_socket = socket;
}

int lvmpolld_use(void)
{
	if (!_lvmpolld_use)
		return 0;

	if (!_lvmpolld_connected && !_lvmpolld.error) {
		_lvmpolld = _lvmpolld_connect(_lvmpolld_socket);
		_lvmpolld_connected = _lvmpolld.socket_fd >= 0;
	}

	return _lvmpolld_connected;
}

void lvmpolld_disconnect(void)
{
	if (_lvmpolld_connected) {
		daemon_close(_lvmpolld);
		_lvmpolld_connected = 0;
	}
}

static void _process_error_response(daemon_reply rep)
{
	if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_FAILED))
		log_error("lvmpolld failed to process a request. The reason was: %s.",
			  daemon_reply_str(rep, "reason", "<empty>"));
	else if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_EINVAL))
		log_error("lvmpolld couldn't handle a request. "
			  "It might be due to daemon internal state. The reason was: %s.",
			  daemon_reply_str(rep, "reason", "<empty>"));
	else
		log_error("Unexpected response %s. The reason: %s.",
			  daemon_reply_str(rep, "response", "<empty>"),
			  daemon_reply_str(rep, "reason", "<empty>"));

	log_print_unless_silent("For more detailed information see lvmpolld log file.");
}

static struct progress_info _request_progress_info(const char *uuid, unsigned abort_polling)
{
	daemon_reply rep;
	const char *e = getenv("LVM_SYSTEM_DIR");
	struct progress_info ret = { .error = 1, .finished = 1 };
	daemon_request req = daemon_request_make(LVMPD_REQ_PROGRESS);

	if (!daemon_request_extend(req, LVMPD_PARM_LVID " = %s", uuid, NULL)) {
		log_error("Failed to create " LVMPD_REQ_PROGRESS " request.");
		goto out_req;
	}

	if (abort_polling &&
	    !daemon_request_extend(req, LVMPD_PARM_ABORT " = %d", abort_polling, NULL)) {
		log_error("Failed to create " LVMPD_REQ_PROGRESS " request.");
		goto out_req;
	}

	if (e &&
	    !(daemon_request_extend(req, LVMPD_PARM_SYSDIR " = %s",
				    e, NULL))) {
		log_error("Failed to create " LVMPD_REQ_PROGRESS " request.");
		goto out_req;
	}

	rep = daemon_send(_lvmpolld, req);
	if (rep.error) {
		log_error("Failed to process request with error %s (errno: %d).",
			  strerror(rep.error), rep.error);
		goto out_rep;
	}

	if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_IN_PROGRESS)) {
		ret.finished = 0;
		ret.error = 0;
	} else if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_FINISHED)) {
		if (!strcmp(daemon_reply_str(rep, "reason", ""), LVMPD_REAS_SIGNAL))
			ret.cmd_signal = daemon_reply_int(rep, LVMPD_PARM_VALUE, 0);
		else
			ret.cmd_retcode = daemon_reply_int(rep, LVMPD_PARM_VALUE, -1);
		ret.error = 0;
	} else if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_NOT_FOUND)) {
		log_verbose("No polling operation in progress regarding LV %s.", uuid);
		ret.error = 0;
	} else {
		_process_error_response(rep);
		stack;
	}

out_rep:
	daemon_reply_destroy(rep);
out_req:
	daemon_request_destroy(req);

	return ret;
}

/*
 * interval in seconds long
 * enough for more than a year
 * of waiting
 */
#define INTERV_SIZE 10

static int _process_poll_init(const struct cmd_context *cmd, const char *poll_type,
			      const struct poll_operation_id *id, const struct daemon_parms *parms)
{
	char *str;
	daemon_reply rep;
	daemon_request req;
	const char *e = getenv("LVM_SYSTEM_DIR");
	int r = 0; 

	str = dm_malloc(INTERV_SIZE * sizeof(char));
	if (!str)
		return r;

	if (snprintf(str, INTERV_SIZE, "%u", parms->interval) >= INTERV_SIZE) {
		log_warn("Interval string conversion got truncated.");
		str[INTERV_SIZE - 1] = '\0';
	}

	req = daemon_request_make(poll_type);
	if (!daemon_request_extend(req, LVMPD_PARM_LVID " = %s", id->uuid,
					LVMPD_PARM_VGNAME " = %s", id->vg_name,
					LVMPD_PARM_LVNAME " = %s", id->lv_name,
					LVMPD_PARM_INTERVAL " = %s", str,
					"cmdline = %s", cmd->cmd_line, /* FIXME: debug param only */
					NULL)) {
		log_error("Failed to create %s request.", poll_type);
		goto out_req;
	}

	if (parms->aborting &&
	    !(daemon_request_extend(req, LVMPD_PARM_ABORT " = %d", parms->aborting, NULL))) {
		log_error("Failed to create %s request." , poll_type);
		goto out_req;
	}

	if (cmd->handles_missing_pvs &&
	    !(daemon_request_extend(req, LVMPD_PARM_HANDLE_MISSING_PVS " = %d",
				    cmd->handles_missing_pvs, NULL))) {
		log_error("Failed to create %s request." , poll_type);
		goto out_req;
	}

	if (e &&
	    !(daemon_request_extend(req, LVMPD_PARM_SYSDIR " = %s",
				    e, NULL))) {
		log_error("Failed to create %s request." , poll_type);
		goto out_req;
	}

	rep = daemon_send(_lvmpolld, req);

	if (rep.error) {
		log_error("Failed to process request with error %s (errno: %d).",
			  strerror(rep.error), rep.error);
		goto out_rep;
	}

	if (!strcmp(daemon_reply_str(rep, "response", ""), LVMPD_RESP_OK))
		r = 1;
	else {
		_process_error_response(rep);
		stack;
	}

out_rep:
	daemon_reply_destroy(rep);
out_req:
	daemon_request_destroy(req);
	dm_free(str);

	return r;
}

int lvmpolld_poll_init(const struct cmd_context *cmd, const struct poll_operation_id *id,
		       const struct daemon_parms *parms)
{
	int r = 0;

	if (!id->uuid) {
		log_error(INTERNAL_ERROR "Use of lvmpolld requires uuid set");
		return 0;
	}

	if (!id->vg_name) {
		log_error(INTERNAL_ERROR "Use of lvmpolld requires vgname set");
		return 0;
	}

	if (!id->lv_name) {
		log_error(INTERNAL_ERROR "Use of lvmpolld requires lvname set");
		return 0;
	}

	if (parms->lv_type & PVMOVE) {
		log_verbose("lvmpolld: Requesting pvmove%s", parms->aborting ? " abort." : ".");
		r =  _process_poll_init(cmd, LVMPD_REQ_PVMOVE, id, parms);
	} else if (parms->lv_type & CONVERTING) {
		log_verbose("lvmpolld: Requesting convert mirror.");
		r =  _process_poll_init(cmd, LVMPD_REQ_CONVERT, id, parms);
	} else if (parms->lv_type & MERGING) {
		if (parms->lv_type & SNAPSHOT) {
			log_verbose("lvmpolld: Requesting snapshot merge.");
			r =  _process_poll_init(cmd, LVMPD_REQ_MERGE, id, parms);
		}
		else if (parms->lv_type & THIN_VOLUME) {
			log_verbose("lvmpolld: Thin snapshot merge.");
			r = _process_poll_init(cmd, LVMPD_REQ_MERGE_THIN, id, parms);
		}
		else {
			log_error(INTERNAL_ERROR "Unsupported poll operation.");
		}
	} else
		log_error(INTERNAL_ERROR "Unsupported poll operation");

	return r;
}

int lvmpolld_request_info(const struct poll_operation_id *id, const struct daemon_parms *parms, unsigned *finished)
{
	struct progress_info info;
	int ret = 0;

	*finished = 1;

	if (!id->uuid) {
		log_error(INTERNAL_ERROR "use of lvmpolld requires uuid being set");
		return 0;
	}

	info = _request_progress_info(id->uuid, parms->aborting);
	*finished = info.finished;

	if (info.error)
		return_0;

	if (info.finished) {
		if (info.cmd_signal)
			log_error("Polling command got terminated by signal (%d).",
				  info.cmd_signal);
		else if (info.cmd_retcode)
			log_error("Polling command exited with return code: %d.",
				  info.cmd_retcode);
		else  {
			log_verbose("Polling finished successfully.");
			ret = 1;
		}
	} else
		ret = 1;

	return ret;
}