File: foreign.c

package info (click to toggle)
multipath-tools 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,992 kB
  • sloc: ansic: 63,788; perl: 1,622; makefile: 729; sh: 647; pascal: 150
file content (642 lines) | stat: -rw-r--r-- 13,845 bytes parent folder | download
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// SPDX-License-Identifier: GPL-2.0-or-later
/*
  Copyright (c) 2018 Martin Wilck, SUSE Linux GmbH
*/

#include <sys/sysmacros.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <fnmatch.h>
#include <dlfcn.h>
#include <libudev.h>
#include <regex.h>
#include "vector.h"
#include "debug.h"
#include "util.h"
#include "structs.h"
#include "structs_vec.h"
#include "print.h"
#include "foreign.h"
#include "strbuf.h"

static vector foreigns;
static const char *const foreign_dir = MULTIPATH_DIR;

/* This protects vector foreigns */
static pthread_rwlock_t foreign_lock = PTHREAD_RWLOCK_INITIALIZER;

static void rdlock_foreigns(void)
{
	pthread_rwlock_rdlock(&foreign_lock);
}

static void wrlock_foreigns(void)
{
	pthread_rwlock_wrlock(&foreign_lock);
}

static void unlock_foreigns(__attribute__((unused)) void *unused)
{
	pthread_rwlock_unlock(&foreign_lock);
}

#define get_dlsym(foreign, sym, lbl)					\
	do {								\
		foreign->sym =	dlsym(foreign->handle, #sym);		\
		if (foreign->sym == NULL) {				\
			condlog(0, "%s: symbol \"%s\" not found in \"%s\"", \
				__func__, #sym, foreign->name);		\
			goto lbl;					\
		}							\
	} while(0)

static void free_foreign(struct foreign *fgn)
{
	struct context *ctx;

	if (fgn == NULL)
		return;

	ctx = fgn->context;
	fgn->context = NULL;
	if (ctx != NULL)
		fgn->cleanup(ctx);

	if (fgn->handle != NULL)
		dlclose(fgn->handle);
	free(fgn);
}

void cleanup_foreign__(void)
{
	struct foreign *fgn;
	int i;

	if (foreigns == NULL)
		return;

	vector_foreach_slot_backwards(foreigns, fgn, i) {
		vector_del_slot(foreigns, i);
		free_foreign(fgn);
	}
	vector_free(foreigns);
	foreigns = NULL;
}

void cleanup_foreign(void)
{
	wrlock_foreigns();
	cleanup_foreign__();
	unlock_foreigns(NULL);
}

static const char foreign_pattern[] = "libforeign-*.so";

static int select_foreign_libs(const struct dirent *di)
{

	return fnmatch(foreign_pattern, di->d_name, FNM_FILE_NAME) == 0;
}

static void free_pre(void *arg)
{
	regex_t **pre = arg;

	if (pre != NULL && *pre != NULL) {
		regfree(*pre);
		free(*pre);
		*pre = NULL;
	}
}

static int _init_foreign(const char *enable)
{
	char pathbuf[PATH_MAX];
	struct dirent **di = NULL;
	struct scandir_result sr;
	int r, i;
	regex_t *enable_re = NULL;

	foreigns = vector_alloc();
	if (foreigns == NULL)
		return -ENOMEM;

	pthread_cleanup_push(free_pre, &enable_re);
	enable_re = calloc(1, sizeof(*enable_re));
	if (enable_re) {
		const char *str = enable ? enable : DEFAULT_ENABLE_FOREIGN;

		r = regcomp(enable_re, str, REG_EXTENDED|REG_NOSUB);
		if (r != 0) {
			char errbuf[64];

			(void)regerror(r, enable_re, errbuf, sizeof(errbuf));
			condlog (2, "%s: error compiling enable_foreign = \"%s\": \"%s\"",
				 __func__, str, errbuf);
			goto out_free_pre;
		}
	}

	r = scandir(foreign_dir, &di, select_foreign_libs, alphasort);

	if (r == 0) {
		condlog(3, "%s: no foreign multipath libraries found",
			__func__);
		goto out_free_pre;
	} else if (r < 0) {
		r = -errno;
		condlog(1, "%s: error scanning foreign multipath libraries: %m",
			__func__);
		cleanup_foreign__();
		goto out_free_pre;
	}

	sr.di = di;
	sr.n = r;
	pthread_cleanup_push_cast(free_scandir_result, &sr);
	for (i = 0; i < r; i++) {
		const char *msg, *fn, *c;
		struct foreign *fgn;
		size_t len, namesz;

		fn = di[i]->d_name;

		len = strlen(fn);
		c = strchr(fn, '-');
		if (len < sizeof(foreign_pattern) - 1 || c == NULL) {
			condlog(0, "%s: bad file name %s, fnmatch error?",
				__func__, fn);
			continue;
		}
		c++;
		condlog(4, "%s: found %s", __func__, fn);

		namesz = len - sizeof(foreign_pattern) + 3;
		fgn = malloc(sizeof(*fgn) + namesz);
		if (fgn == NULL)
			continue;
		memset(fgn, 0, sizeof(*fgn));
		strlcpy((char*)fgn + offsetof(struct foreign, name), c, namesz);

		if (enable_re != NULL) {
			int ret = regexec(enable_re, fgn->name, 0, NULL, 0);

			if (ret == REG_NOMATCH) {
				condlog(3, "%s: foreign library \"%s\" is not enabled",
					__func__, fgn->name);
				free(fgn);
				continue;
			} else if (ret != 0)
				/* assume it matches */
				condlog(2, "%s: error %d in regexec() for %s",
					__func__, ret, fgn->name);
		}

		snprintf(pathbuf, sizeof(pathbuf), "%s/%s", foreign_dir, fn);
		fgn->handle = dlopen(pathbuf, RTLD_NOW|RTLD_LOCAL);
		msg = dlerror();
		if (fgn->handle == NULL) {
			condlog(1, "%s: failed to dlopen %s: %s", __func__,
				pathbuf, msg);
			goto dl_err;
		}

		get_dlsym(fgn, init, dl_err);
		get_dlsym(fgn, cleanup, dl_err);
		get_dlsym(fgn, add, dl_err);
		get_dlsym(fgn, change, dl_err);
		get_dlsym(fgn, delete, dl_err);
		get_dlsym(fgn, delete_all, dl_err);
		get_dlsym(fgn, check, dl_err);
		get_dlsym(fgn, lock, dl_err);
		get_dlsym(fgn, unlock, dl_err);
		get_dlsym(fgn, get_multipaths, dl_err);
		get_dlsym(fgn, release_multipaths, dl_err);
		get_dlsym(fgn, get_paths, dl_err);
		get_dlsym(fgn, release_paths, dl_err);

		fgn->context = fgn->init(LIBMP_FOREIGN_API, fgn->name);
		if (fgn->context == NULL) {
			condlog(0, "%s: init() failed for %s", __func__, fn);
			goto dl_err;
		}

		if (!vector_alloc_slot(foreigns)) {
			goto dl_err;
		}

		vector_set_slot(foreigns, fgn);
		condlog(3, "foreign library \"%s\" loaded successfully",
			fgn->name);

		continue;

	dl_err:
		free_foreign(fgn);
	}
	r = 0;
	pthread_cleanup_pop(1); /* free_scandir_result */
out_free_pre:
	pthread_cleanup_pop(1); /* free_pre */
	return r;
}

int init_foreign(const char *enable)
{
	int ret;

	wrlock_foreigns();

	if (foreigns != NULL) {
		unlock_foreigns(NULL);
		condlog(0, "%s: already initialized", __func__);
		return -EEXIST;
	}

	pthread_cleanup_push(unlock_foreigns, NULL);
	ret = _init_foreign(enable);
	pthread_cleanup_pop(1);

	return ret;
}

int add_foreign(struct udev_device *udev)
{
	struct foreign *fgn;
	dev_t dt;
	int j;
	int r = FOREIGN_IGNORED;

	if (udev == NULL) {
		condlog(1, "%s called with NULL udev", __func__);
		return FOREIGN_ERR;
	}

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return FOREIGN_ERR;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	dt = udev_device_get_devnum(udev);
	vector_foreach_slot(foreigns, fgn, j) {
		r = fgn->add(fgn->context, udev);

		if (r == FOREIGN_CLAIMED) {
			condlog(3, "%s: foreign \"%s\" claims device %d:%d",
				__func__, fgn->name, major(dt), minor(dt));
			break;
		} else if (r == FOREIGN_OK) {
			condlog(4, "%s: foreign \"%s\" owns device %d:%d",
				__func__, fgn->name, major(dt), minor(dt));
			break;
		} else if (r != FOREIGN_IGNORED) {
			condlog(1, "%s: unexpected return value %d from \"%s\"",
				__func__, r, fgn->name);
		}
	}

	pthread_cleanup_pop(1);
	return r;
}

int change_foreign(struct udev_device *udev)
{
	struct foreign *fgn;
	int j;
	dev_t dt;
	int r = FOREIGN_IGNORED;

	if (udev == NULL) {
		condlog(1, "%s called with NULL udev", __func__);
		return FOREIGN_ERR;
	}

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return FOREIGN_ERR;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	dt = udev_device_get_devnum(udev);
	vector_foreach_slot(foreigns, fgn, j) {
		r = fgn->change(fgn->context, udev);

		if (r == FOREIGN_OK) {
			condlog(4, "%s: foreign \"%s\" completed %d:%d",
				__func__, fgn->name, major(dt), minor(dt));
			break;
		} else if (r != FOREIGN_IGNORED) {
			condlog(1, "%s: unexpected return value %d from \"%s\"",
				__func__, r, fgn->name);
		}
	}

	pthread_cleanup_pop(1);
	return r;
}

int delete_foreign(struct udev_device *udev)
{
	struct foreign *fgn;
	int j;
	dev_t dt;
	int r = FOREIGN_IGNORED;

	if (udev == NULL) {
		condlog(1, "%s called with NULL udev", __func__);
		return FOREIGN_ERR;
	}

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return FOREIGN_ERR;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	dt = udev_device_get_devnum(udev);
	vector_foreach_slot(foreigns, fgn, j) {
		r = fgn->delete(fgn->context, udev);

		if (r == FOREIGN_OK) {
			condlog(3, "%s: foreign \"%s\" deleted device %d:%d",
				__func__, fgn->name, major(dt), minor(dt));
			break;
		} else if (r != FOREIGN_IGNORED) {
			condlog(1, "%s: unexpected return value %d from \"%s\"",
				__func__, r, fgn->name);
		}
	}

	pthread_cleanup_pop(1);
	return r;
}

int delete_all_foreign(void)
{
	struct foreign *fgn;
	int j;

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return FOREIGN_ERR;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, j) {
		int r;

		r = fgn->delete_all(fgn->context);
		if (r != FOREIGN_IGNORED && r != FOREIGN_OK) {
			condlog(1, "%s: unexpected return value %d from \"%s\"",
				__func__, r, fgn->name);
		}
	}

	pthread_cleanup_pop(1);
	return FOREIGN_OK;
}

void check_foreign(void)
{
	struct foreign *fgn;
	int j;

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, j) {
		fgn->check(fgn->context);
	}

	pthread_cleanup_pop(1);
}

/* Call this after get_path_layout */
void foreign_path_layout(fieldwidth_t *width)
{
	struct foreign *fgn;
	int i;

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);

		vec = fgn->get_paths(fgn->context);
		if (vec != NULL) {
			get_path_layout__(vec, LAYOUT_RESET_NOT, width);
		}
		fgn->release_paths(fgn->context, vec);

		pthread_cleanup_pop(1);
	}

	pthread_cleanup_pop(1);
}

/* Call this after get_multipath_layout */
void foreign_multipath_layout(fieldwidth_t *width)
{
	struct foreign *fgn;
	int i;

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);

		vec = fgn->get_multipaths(fgn->context);
		if (vec != NULL) {
			get_multipath_layout__(vec, LAYOUT_RESET_NOT, width);
		}
		fgn->release_multipaths(fgn->context, vec);

		pthread_cleanup_pop(1);
	}

	pthread_cleanup_pop(1);
}

static int snprint_foreign_topology__(struct strbuf *buf, int verbosity, const fieldwidth_t *p_width)
{
	struct foreign *fgn;
	int i;
	size_t initial_len = get_strbuf_len(buf);

	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;
		const struct gen_multipath *gm;
		int j;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);

		vec = fgn->get_multipaths(fgn->context);
		if (vec != NULL) {
			vector_foreach_slot(vec, gm, j) {
				if (snprint_multipath_topology__(gm, buf, verbosity, p_width) < 0)
					break;
			}
		}
		fgn->release_multipaths(fgn->context, vec);
		pthread_cleanup_pop(1);
	}

	return get_strbuf_len(buf) - initial_len;
}

int snprint_foreign_topology(struct strbuf *buf, int verbosity, const fieldwidth_t *p_width)
{
	int rc;

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return 0;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);
	rc = snprint_foreign_topology__(buf, verbosity, p_width);
	pthread_cleanup_pop(1);
	return rc;
}

void print_foreign_topology(int verbosity)
{
	STRBUF_ON_STACK(buf);
	struct foreign *fgn;
	int i;
	fieldwidth_t *p_width __attribute__((cleanup(cleanup_ucharp))) = NULL;

	if ((p_width = alloc_path_layout()) == NULL)
		return;
	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);
	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);
		vec = fgn->get_paths(fgn->context);
		get_path_layout__(vec, LAYOUT_RESET_NOT, p_width);
		fgn->release_paths(fgn->context, vec);
		pthread_cleanup_pop(1);
	}
	snprint_foreign_topology__(&buf, verbosity, p_width);
	pthread_cleanup_pop(1);
	printf("%s", get_strbuf_str(&buf));
}

int snprint_foreign_paths(struct strbuf *buf, const char *style,
			  const fieldwidth_t *width)
{
	struct foreign *fgn;
	int i;
	size_t initial_len = get_strbuf_len(buf);

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return 0;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;
		const struct gen_path *gp;
		int j, ret = 0;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);

		vec = fgn->get_paths(fgn->context);
		if (vec != NULL) {
			vector_foreach_slot(vec, gp, j) {
				ret = snprint_path__(gp, buf, style, width);
				if (ret < 0)
					break;
			}
		}
		fgn->release_paths(fgn->context, vec);
		pthread_cleanup_pop(1);
		if (ret < 0)
			break;
	}

	pthread_cleanup_pop(1);
	return get_strbuf_len(buf) - initial_len;
}

int snprint_foreign_multipaths(struct strbuf *buf, const char *style,
			       const fieldwidth_t *width)
{
	struct foreign *fgn;
	int i;
	size_t initial_len = get_strbuf_len(buf);

	rdlock_foreigns();
	if (foreigns == NULL) {
		unlock_foreigns(NULL);
		return 0;
	}
	pthread_cleanup_push(unlock_foreigns, NULL);

	vector_foreach_slot(foreigns, fgn, i) {
		const struct vector_s *vec;
		const struct gen_multipath *gm;
		int j, ret = 0;

		fgn->lock(fgn->context);
		pthread_cleanup_push(fgn->unlock, fgn->context);

		vec = fgn->get_multipaths(fgn->context);
		if (vec != NULL) {
			vector_foreach_slot(vec, gm, j) {
				ret = snprint_multipath__(gm, buf,
							 style, width);
				if (ret < 0)
					break;
			}
		}
		fgn->release_multipaths(fgn->context, vec);
		pthread_cleanup_pop(1);
		if (ret < 0)
			break;
	}

	pthread_cleanup_pop(1);
	return get_strbuf_len(buf) - initial_len;
}