summaryrefslogtreecommitdiff
path: root/daemons/lvmetad/lvmetad-helper.c
blob: 3922cd71a29858632cd1d7f8aaa038d72acb134b (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * Copyright (C) 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define _XOPEN_SOURCE 500  /* pthread */
#define _GNU_SOURCE
#define _REENTRANT

#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <poll.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <grp.h>
#include <assert.h>
#include <errno.h>

#include "libudev.h"

#include "tool.h"

#include "daemon-io.h"
#include "daemon-server.h"
#include "daemon-log.h"
#include "lvm-version.h"
#include "lvmetad-internal.h"
#include "lvmetad-client.h"

/*
 * lvmetad_main is the main process that:
 * . forks threads to handle client (command) connections
 * . receives client requests from socket
 * . reads/writes cache state
 * . receives uevent messages
 * . sends lvmetad_helper pvscan requests for uevents
 * . sends lvmetad_helper client requests from lvmetactl
 *
 * lvmetad_helper is the helper process that:
 * . receives pvscan requests from lvmetad_main
 * . forks/execs pvscan commands for each request
 */

#define MAX_AV_COUNT 8

static int _log_debug_stderr;

#define log_debug(fmt, args...) \
do { \
	if (_log_debug_stderr) \
		fprintf(stderr, fmt "\n", ##args); \
} while (0)

static void run_path(struct helper_msg *hm)
{
	char arg[HELPER_ARGS_LEN];
	char *args = hm->args;
	char *av[MAX_AV_COUNT + 1]; /* +1 for NULL */
	int av_count = 0;
	int i, arg_len, args_len;

	for (i = 0; i < MAX_AV_COUNT + 1; i++)
		av[i] = NULL;

	av[av_count++] = strdup(hm->path);

	if (!args[0])
		goto run;

	/* this should already be done, but make sure */
	args[HELPER_ARGS_LEN - 1] = '\0';

	memset(&arg, 0, sizeof(arg));
	arg_len = 0;
	args_len = strlen(args);

	for (i = 0; i < args_len; i++) {
		if (!args[i])
			break;

		if (av_count == MAX_AV_COUNT)
			break;

		if (args[i] == '\\') {
			if (i == (args_len - 1))
				break;
			i++;

			if (args[i] == '\\') {
				arg[arg_len++] = args[i];
				continue;
			}
			if (isspace(args[i])) {
				arg[arg_len++] = args[i];
				continue;
			} else {
				break;
			}
		}

		if (isalnum(args[i]) || ispunct(args[i])) {
			arg[arg_len++] = args[i];
		} else if (isspace(args[i])) {
			if (arg_len)
				av[av_count++] = strdup(arg);

			memset(arg, 0, sizeof(arg));
			arg_len = 0;
		} else {
			break;
		}
	}

	if ((av_count < MAX_AV_COUNT) && arg_len) {
		av[av_count++] = strdup(arg);
	}
run:
	execvp(av[0], av);
}

static int read_from_main(int fd, struct helper_msg *hm)
{
	int rv;
 retry:
	rv = read(fd, hm, sizeof(struct helper_msg));
	if (rv == -1 && errno == EINTR)
		goto retry;

	if (rv != sizeof(struct helper_msg))
		return -1;
	return 0;
}

static int send_to_main(int fd, int type)
{
	struct helper_status hs;
	int rv;

	memset(&hs, 0, sizeof(hs));

	hs.type = type;

	rv = write(fd, &hs, sizeof(hs));

	if (rv == sizeof(hs))
		return 0;
	return -1;
}

#define INACTIVE_TIMEOUT_MS 10000
#define ACTIVE_TIMEOUT_MS 1000

int run_helper(int in_fd, int out_fd, int debug_stderr)
{
	char name[16];
	struct pollfd pollfd;
	struct helper_msg hm;
	unsigned int fork_count = 0;
	unsigned int wait_count = 0;
	int timeout = INACTIVE_TIMEOUT_MS;
	int rv, pid, status;

	_log_debug_stderr = debug_stderr;

	memset(name, 0, sizeof(name));
	sprintf(name, "%s", "lvmetad_helper");
	prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);

	memset(&pollfd, 0, sizeof(pollfd));
	pollfd.fd = in_fd;
	pollfd.events = POLLIN;

	/* Tell the main process we've started. */
	send_to_main(out_fd, HELPER_STARTED);

	while (1) {
		rv = poll(&pollfd, 1, timeout);
		if (rv == -1 && errno == EINTR)
			continue;

		if (rv < 0)
			exit(0);

		memset(&hm, 0, sizeof(hm));

		if (pollfd.revents & POLLIN) {
			rv = read_from_main(in_fd, &hm);
			if (rv)
				continue;

			if (hm.type == HELPER_MSG_RUNPATH) {
				pid = fork();
				if (!pid) {
					run_path(&hm);
					exit(-1);
				}

				fork_count++;

				/*
				log_debug("helper fork %d count %d %d %s %s",
					  pid, fork_count, wait_count,
					  hm.path, hm.args);
				*/
			}
		}

		if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL))
			exit(0);

		/* collect child exits until no more children exist (ECHILD)
		   or none are ready (WNOHANG) */

		while (1) {
			rv = waitpid(-1, &status, WNOHANG);
			if (rv > 0) {
				wait_count++;

				/*
				log_debug("helper wait %d count %d %d",
					  rv, fork_count, wait_count);
				*/
				continue;
			}

			/* no more children to wait for or no children
			   have exited */

			if (rv < 0 && errno == ECHILD)
				timeout = INACTIVE_TIMEOUT_MS;
			else
				timeout = ACTIVE_TIMEOUT_MS;
			break;
		}
	}

	return 0;
}

void close_helper(daemon_state *s)
{
	lvmetad_state *ls = s->private;

	close(ls->helper_pr_fd);
	close(ls->helper_pw_fd);
	ls->helper_pr_fd = -1;
	ls->helper_pw_fd = -1;
	s->helper_fd = -1;
	s->helper_handler = NULL;
}

static void _send_helper_msg(daemon_state *s, struct helper_msg *hm)
{
	lvmetad_state *ls = s->private;
	int rv;

 retry:
	rv = write(ls->helper_pw_fd, hm, sizeof(struct helper_msg));
	if (rv == -1 && errno == EINTR)
		goto retry;

	if (rv == -1 && errno == EAGAIN) {
		return;
	}

	/* helper exited or closed fd, quit using helper */
	if (rv == -1 && errno == EPIPE) {
		ERROR(s, "send_helper EPIPE");
		close_helper(s);
		return;
	}

	if (rv != sizeof(struct helper_msg)) {
		/* this shouldn't happen */
		ERROR(s, "send_helper error %d %d", rv, errno);
		close_helper(s);
		return;
	}
}

/* send a request to helper process */

void send_helper_request(daemon_state *s, request r)
{
	struct helper_msg hm = { 0 };
	char *runpath = daemon_request_str(r, "runpath", NULL);
	char *runargs = daemon_request_str(r, "runargs", NULL);

	hm.type = HELPER_MSG_RUNPATH;
	memcpy(hm.path, runpath, HELPER_PATH_LEN);
	if (runargs)
		memcpy(hm.args, runargs, HELPER_ARGS_LEN);

	_send_helper_msg(s, &hm);
}

static void send_helper_pvscan_cache_name(daemon_state *s, const char *name)
{
	struct helper_msg hm = { 0 };

	hm.type = HELPER_MSG_RUNPATH;
	sprintf(hm.path, "pvscan");
	snprintf(hm.args, HELPER_ARGS_LEN-1, "--cache %s", name);

	_send_helper_msg(s, &hm);
}

static void send_helper_pvscan_cache_num(daemon_state *s, dev_t devt)
{
	struct helper_msg hm = { 0 };

	hm.type = HELPER_MSG_RUNPATH;
	sprintf(hm.path, "pvscan");
	snprintf(hm.args, HELPER_ARGS_LEN-1, "--cache --major %d --minor %d",
		 major(devt), minor(devt));

	_send_helper_msg(s, &hm);
}

static void send_helper_pvscan_cache_all(daemon_state *s)
{
	struct helper_msg hm = { 0 };

	hm.type = HELPER_MSG_RUNPATH;
	sprintf(hm.path, "pvscan");
	sprintf(hm.args, "--cache");

	_send_helper_msg(s, &hm);
}

/*
 * called in context of main lvmetad process
 * handles a message from helper process
 */

static int helper_handler(daemon_state *s)
{
	lvmetad_state *ls = s->private;
	struct helper_status hs;
	int rv;

	memset(&hs, 0, sizeof(hs));

	rv = read(ls->helper_pr_fd, &hs, sizeof(hs));
	if (!rv || rv == -EAGAIN)
		return -1;
	if (rv < 0) {
		ERROR(s, "handle_helper rv %d errno %d", rv, errno);
		goto fail;
	}
	if (rv != sizeof(hs)) {
		ERROR(s, "handle_helper recv size %d", rv);
		goto fail;
	}

	DEBUGLOG(s, "helper message type %d status %d", hs.type, hs.status);

	/*
	 * Run initial pvscan --cache to populate lvmetad cache.
	 *
	 * (Upon receiving HELPER_STARTED we know that the helper
	 * is ready to handle to running commands.)
	 *
	 * The udev monitor is enabled before this, so there should
	 * be no gap where new devs could be missed.  It's possible
	 * that new devs added during lvmetad startup could be scanned
	 * by this initial pvscan --cache, and then scanned again
	 * individually because of the monitor.  This possible repetition
	 * is harmless.
	 */
	if (hs.type == HELPER_STARTED)
		send_helper_pvscan_cache_all(s);

	return 0;

 fail:
	ERROR(s, "close helper connection");
	close_helper(s);
	return -1;
}

/* create helper process */

int setup_helper(daemon_state *s)
{
	lvmetad_state *ls = s->private;
	int pid;
	int pw_fd = -1; /* parent write */
	int cr_fd = -1; /* child read */
	int pr_fd = -1; /* parent read */
	int cw_fd = -1; /* child write */
	int pfd[2];

	/* don't allow the main daemon thread to block */
	if (pipe2(pfd, O_NONBLOCK | O_CLOEXEC))
		return -errno;

	/* only available on rhel7 */
	/* fcntl(pfd[1], F_SETPIPE_SZ, 1024*1024); */

	cr_fd = pfd[0];
	pw_fd = pfd[1];

	if (pipe2(pfd, O_NONBLOCK | O_CLOEXEC)) {
		close(cr_fd);
		close(pw_fd);
		return -errno;
	}

	pr_fd = pfd[0];
	cw_fd = pfd[1];

	pid = fork();
	if (pid < 0) {
		close(cr_fd);
		close(pw_fd);
		close(pr_fd);
		close(cw_fd);
		return -errno;
	}

	if (pid) {
		close(cr_fd);
		close(cw_fd);
		ls->helper_pw_fd = pw_fd;
		ls->helper_pr_fd = pr_fd;
		ls->helper_pid = pid;
		s->helper_fd = pr_fd; /* libdaemon uses helper_fd in select */
		s->helper_handler = helper_handler;
		return 0;
	} else {
		close(pr_fd);
		close(pw_fd);
		run_helper(cr_fd, cw_fd, (s->foreground && strstr(ls->log_config, "debug")));
		exit(0);
	}
}

/*
 * called in context of main lvmetad process
 * handles a message from udev monitor
 * sends a message to lvmetad helper to scan a device
 */

static int monitor_handler(daemon_state *s)
{
	lvmetad_state *ls = s->private;
	struct udev_device *dev;
	dev_t devt;
	const char *name;
	const char *action;

	dev = udev_monitor_receive_device(ls->udev_mon);
	if (!dev)
		return 0;

	name = udev_device_get_devnode(dev);
	if (!name)
		goto out;

	devt = udev_device_get_devnum(dev);
	action = udev_device_get_action(dev);

	/*
	 * FIXME: there's a lot of magic in 69-dm-lvm-metad.rules.in
	 * some of which is probably needed here (especially related
	 * to scanning MD/DM/loop devs.)
	 */

	if (!strcmp(action, "add")) {
		DEBUGLOG(s, "monitor add %s", name);
		send_helper_pvscan_cache_name(s, name);

	} else if (!strcmp(action, "remove")) {
		DEBUGLOG(s, "monitor remove %s", name);
		send_helper_pvscan_cache_num(s, devt);
	}
out:
	udev_device_unref(dev);
	return 0;
}

/* create udev monitor */

void setup_udev_monitor(daemon_state *s)
{
	lvmetad_state *ls = s->private;
	int fd;
	int ret;

	/* FIXME: cleanup all libudev stuff */

	ls->udevh = udev_new();
	if (!ls->udevh) {
		ERROR(s, "Failed to monitor udev new.");
		return;
	}

	ls->udev_mon = udev_monitor_new_from_netlink(ls->udevh, "udev");
	if (!ls->udev_mon) {
		ERROR(s, "Failed to monitor udev netlink.");
		return;
	}

	ret = udev_monitor_filter_add_match_subsystem_devtype(ls->udev_mon, "block", "disk");
	if (ret < 0) {
		ERROR(s, "Failed to monitor udev filter.");
		return;
	}

	ret = udev_monitor_enable_receiving(ls->udev_mon);
	if (ret < 0) {
		ERROR(s, "Failed to monitor udev receive.");
		return;
	}

	/* udev_monitor_set_receive_buffer_size? */

	fd = udev_monitor_get_fd(ls->udev_mon);
	if (fd < 0) {
		ERROR(s, "Failed to monitor udev fd.");
		return;
	}

	ls->udev_fd = fd;
	s->monitor_fd = fd;
	s->monitor_handler = monitor_handler;
}