summaryrefslogtreecommitdiffstats
path: root/tools/lvm.c
blob: 334fda5ae61439f20904e4c2e493b5ee35919bab (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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
/*
 * Copyright (C) 2001 Sistina Software (UK) Limited.
 *
 * This file is released under the GPL.
 */

#include "tools.h"

#include <assert.h>
#include <getopt.h>
#include <signal.h>
#include <syslog.h>
#include <libgen.h>
#include <sys/stat.h>
#include <ctype.h>
#include <limits.h>

#include "stub.h"
#include "vgcache.h"
#include "lvm-string.h"

#ifdef READLINE_SUPPORT
#include <readline/readline.h>
#include <readline/history.h>
#endif

/* define exported table of valid switches */
struct arg the_args[ARG_COUNT + 1] = {

#define arg(a, b, c, d) {b, "--" c, d, 0, NULL},
#include "args.h"
#undef arg

};

static int _array_size;
static int _num_commands;
static struct command *_commands;

/* Exported LVM1 disk format */
struct format_instance *fid;

/* Export command being processed */
struct command *the_command;

struct cmd_context *cmd;

/* Whether or not to dump persistent filter state */
static int _dump_filter;

static int _interactive;
static FILE *_log;

/*
 * Both verbose have a global setting which comes
 * from the command line that invoked the shell,
 * or the config file.  These are the 'default'
 * variables.  In addition people may set a level
 * for a single command.
 */
static int _default_debug;
static int _debug;

static int _default_verbose;
static int _verbose;


/*
 * The lvm_system_dir contains:
 *
 * o  The lvm configuration (lvm.conf)
 * o  The persistent filter cache (.cache)
 * o  Volume group backups (backups)
 *
 */
static char _system_dir[PATH_MAX] = "/etc/lvm";

/* static functions */
static void register_commands(void);
static struct command *find_command(const char *name);
static void register_command(const char *name, command_fn fn,
			     const char *desc, const char *usage, ...);
static void create_new_command(const char *name, command_fn command,
			       const char *desc, const char *usage,
			       int nargs, int *args);

static void alloc_command(void);
static void add_getopt_arg(int arg, char **ptr, struct option **o);
static int process_command_line(struct command *com, int *argc, char ***argv);
static struct arg *find_arg(struct command *com, int a);
static int process_common_commands(struct command *com);
static int run_command(int argc, char **argv);
static int init(void);
static void fin(void);
static int run_script(int argc, char **argv);

#ifdef READLINE_SUPPORT
static int shell(void);
#endif

static void display_help(void);

int main(int argc, char **argv)
{
	char *namebase, *base;
	int ret, alias = 0;

	if (!init())
		return -1;

	namebase = strdup(argv[0]);
	base = basename(namebase);
	if (strcmp(base, "lvm"))
		alias = 1;
	free(namebase);

	register_commands();

#ifdef READLINE_SUPPORT
	if (!alias && argc == 1) {
		ret = shell();
		goto out;
	}
#endif

	if (!alias) {
		if (argc < 2) {
			log_fatal("Please supply an LVM command.");
			display_help();
			ret = EINVALID_CMD_LINE;
			goto out;
		}

		argc--;
		argv++;
	}

	ret = run_command(argc, argv);
	if ((ret == ENO_SUCH_CMD) && (!alias))
		ret = run_script(argc, argv);
	if (ret == ENO_SUCH_CMD)
		log_error("No such command.  Try 'help'.");

      out:
	fin();
	return ret;
}

void usage(const char *name)
{
	struct command *com = find_command(name);

	if (!com)
		return;

	log_error("%s: %s\n\n%s", com->name, com->desc, com->usage);
}

int yes_no_arg(struct arg *a)
{
	a->sign = SIGN_NONE;

	if (!strcmp(a->value, "y"))
		a->i_value = 1;

	else if (!strcmp(a->value, "n"))
		a->i_value = 0;

	else
		return 0;

	return 1;
}

int _get_int_arg(struct arg *a, char **ptr)
{
	char *val;
	long v;

	val = a->value;
	switch (*val) {
	case '+':
		a->sign = SIGN_PLUS;
		val++;
		break;
	case '-':
		a->sign = SIGN_MINUS;
		val++;
		break;
	default:
		a->sign = SIGN_NONE;
	}

	if (!isdigit(*val))
		return 0;

	v = strtol(val, ptr, 10);

	if (*ptr == val)
		return 0;

	a->i_value = (uint32_t) v;
	return 1;
}

int size_arg(struct arg *a)
{
	char *ptr;
	int i;
	static char *suffixes = "kmgt";

	if (!_get_int_arg(a, &ptr))
		return 0;

	if (*ptr) {
		for (i = strlen(suffixes) - 1; i >= 0; i--)
			if (suffixes[i] == tolower((int) *ptr))
				break;

		if (i < 0)
			return 0;

		while (i-- > 0)
			a->i_value *= 1024;
	}

	return 1;

}

int int_arg(struct arg *a)
{
	char *ptr;

	if (!_get_int_arg(a, &ptr) || (*ptr) || (a->sign == SIGN_MINUS))
		return 0;

	return 1;
}

int int_arg_with_sign(struct arg *a)
{
	char *ptr;

	if (!_get_int_arg(a, &ptr) || (*ptr))
		return 0;

	return 1;
}

int string_arg(struct arg *a)
{
	return 1;
}

int permission_arg(struct arg *a)
{
	a->sign = SIGN_NONE;

	if ((!strcmp(a->value, "rw")) || (!strcmp(a->value, "wr")))
		a->i_value = LVM_READ | LVM_WRITE;

	else if (!strcmp(a->value, "r"))
		a->i_value = LVM_READ;

	else
		return 0;

	return 1;
}

char yes_no_prompt(const char *prompt, ...)
{
	int c = 0;
	va_list ap;

	while (c != 'y' && c != 'n') {
		if (c == '\n' || c == 0) {
			va_start(ap, prompt);
			vprintf(prompt, ap);
			va_end(ap);
		}
		c = tolower(getchar());
	}

	while (getchar() != '\n') ;

	return c;
}

static void register_commands()
{
#define xx(a, b, c...) register_command(# a, a, b, ## c, \
                                        debug_ARG, help_ARG, suspend_ARG, \
                                        version_ARG, verbose_ARG, \
					quiet_ARG, -1);
#include "commands.h"
#undef xx
}

static void register_command(const char *name, command_fn fn,
			     const char *desc, const char *usage, ...)
{
	int nargs = 0, i;
	int *args;
	va_list ap;

	/* count how many arguments we have */
	va_start(ap, usage);
	while (va_arg(ap, int) >= 0)
		 nargs++;
	va_end(ap);

	/* allocate space for them */
	if (!(args = dbg_malloc(sizeof(*args) * nargs))) {
		log_fatal("Out of memory.");
		exit(ECMD_FAILED);
	}

	/* fill them in */
	va_start(ap, usage);
	for (i = 0; i < nargs; i++)
		args[i] = va_arg(ap, int);
	va_end(ap);

	/* enter the command in the register */
	create_new_command(name, fn, desc, usage, nargs, args);
}

static struct command *find_command(const char *name)
{
	int i;
	char *namebase, *base;

	namebase = strdup(name);
	base = basename(namebase);

	for (i = 0; i < _num_commands; i++) {
		if (!strcmp(base, _commands[i].name))
			break;
	}

	free(namebase);

	if (i >= _num_commands)
		return 0;

	return _commands + i;
}

static void create_new_command(const char *name, command_fn command,
			       const char *desc, const char *usage,
			       int nargs, int *args)
{
	struct command *nc;

	alloc_command();

	nc = _commands + _num_commands++;

	nc->name = name;
	nc->desc = desc;
	nc->usage = usage;
	nc->fn = command;
	nc->num_args = nargs;
	nc->valid_args = args;
}

static void __alloc(int size)
{
	if (!(_commands = dbg_realloc(_commands, sizeof(*_commands) * size))) {
		log_fatal("Couldn't allocate memory.");
		exit(ECMD_FAILED);
	}

	_array_size = size;
}

static void alloc_command(void)
{
	if (!_array_size)
		__alloc(32);

	if (_array_size <= _num_commands)
		__alloc(2 * _array_size);
}

/*
 * Sets up the short and long argument.  If there
 * is no short argument then the index of the
 * argument in the the_args array is set as the
 * long opt value.  Yuck.  Of course this means we
 * can't have more than 'a' long arguments.  Since
 * we have only 1 ATM (--version) I think we can
 * live with this restriction.
 */
static void add_getopt_arg(int arg, char **ptr, struct option **o)
{
	struct arg *a = the_args + arg;

	if (a->short_arg) {
		*(*ptr)++ = a->short_arg;

		if (a->fn)
			*(*ptr)++ = ':';
	}

	if (*(a->long_arg + 2)) {
		(*o)->name = a->long_arg + 2;
		(*o)->has_arg = a->fn ? 1 : 0;
		(*o)->flag = NULL;
		(*o)->val = arg;
		(*o)++;
	}
}

static int process_command_line(struct command *com, int *argc, char ***argv)
{
	int i, opt;
	char str[((ARG_COUNT + 1) * 2) + 1], *ptr = str;
	struct option opts[ARG_COUNT + 1], *o = opts;
	struct arg *a;

	for (i = 0; i < ARG_COUNT; i++) {
		struct arg *a = the_args + i;

		/* zero the count and arg */
		a->count = 0;
		a->value = 0;
		a->i_value = 0;
	}

	/* fill in the short and long opts */
	for (i = 0; i < com->num_args; i++)
		add_getopt_arg(com->valid_args[i], &ptr, &o);

	*ptr = '\0';
	memset(o, 0, sizeof(*o));

	/* initialise getopt_long & scan for command line switches */
	optarg = 0;
	optind = 0;
	while ((opt = getopt_long(*argc, *argv, str, opts, NULL)) >= 0) {

		a = find_arg(com, opt);

		if (!a) {
			log_fatal("Unrecognised option.");
			return 0;
		}

		if (a->fn) {

			if (!optarg) {
				log_error("Option requires argument.");
				return 0;
			}

			a->value = optarg;

			if (!a->fn(a)) {
				log_error("Invalid argument %s",
					  optarg);
				return 0;
			}
		}

		a->count++;
	}

	*argc -= optind;
	*argv += optind;
	return 1;
}

static struct arg *find_arg(struct command *com, int opt)
{
	struct arg *a;
	int i, arg;

	for (i = 0; i < com->num_args; i++) {
		arg = com->valid_args[i];
		a = the_args + arg;

		/*
		 * opt should equal either the
		 * short arg, or the index into
		 * 'the_args'.
		 */
		if ((a->short_arg && (opt == a->short_arg)) || (opt == arg))
			return a;
	}

	return 0;
}

/* FIXME: define CMD_SUCCEEDED, and return this instead of zero. */
static int process_common_commands(struct command *com)
{
	int l;
	char backup_dir[PATH_MAX];

	if (arg_count(suspend_ARG))
		kill(getpid(), SIGSTOP);

	/*
	 * debug
	 */
	_debug = _default_debug;
	if (arg_count(debug_ARG))
		_debug = arg_count(debug_ARG);


	/*
	 * verbose
	 */
	_verbose = _default_verbose;
	if (arg_count(verbose_ARG))
		_verbose = arg_count(verbose_ARG);


	if (arg_count(quiet_ARG)) {
		_debug = 0;
		_verbose = 0;
	}


	if ((l = arg_count(test_ARG))) {
		log_error("Test mode. Metadata will NOT be updated.");
		init_test(l);
	}

	if (arg_count(help_ARG)) {
		usage(com->name);
		return ECMD_PROCESSED;
	}

	if (arg_count(version_ARG)) {
		/* FIXME: Add driver and software version */
		log_error("%s: ", com->name);
		return ECMD_PROCESSED;
	}

	/* Set autobackup if command takes this option */
	for (l = 0; l < com->num_args; l++)
		if (com->valid_args[l] == autobackup_ARG) {
			if (snprintf(backup_dir, sizeof(backup_dir),
				     "%s/backup", _system_dir) < 0) {
				log_err("Backup path too long.");
				return ECMD_FAILED;
			}

			if (autobackup_init("/etc/lvm/backup"))
				return EINVALID_CMD_LINE;
			else
				break;
		}

	return 0;
}

int help(int argc, char **argv)
{
	if (!argc)
		display_help();
	else {
		int i;
		for (i = 0; i < argc; i++)
			usage(argv[i]);
	}

	return 0;
}

static void display_help(void)
{
	int i;

	log_error("Available lvm commands:");
	log_error("Use 'lvm help <command>' for more information");
	log_error(" ");

	for (i = 0; i < _num_commands; i++) {
		struct command *com = _commands + i;

		log_error("%-16.16s%s", com->name, com->desc);
	}
}

static int run_command(int argc, char **argv)
{
	int ret = 0;

	if (!(the_command = find_command(argv[0])))
		return ENO_SUCH_CMD;

	if (!process_command_line(the_command, &argc, &argv)) {
		log_error("Error during parsing of command line.");
		return EINVALID_CMD_LINE;
	}

	if ((ret = process_common_commands(the_command)))
		return ret;

	init_debug(_debug);
	init_verbose(_verbose);

	ret = the_command->fn(argc, argv);

	/*
	 * set the debug and verbose levels back
	 * to the global default.
	 */
	init_debug(_default_debug);
	init_verbose(_default_verbose);

	/*
	 * free off any memory the command used.
	 */
	pool_empty(cmd->mem);

	if (ret == EINVALID_CMD_LINE && !_interactive)
		usage(the_command->name);


	return ret;
}

static int split(char *str, int *argc, char **argv, int max)
{
	char *b = str, *e;
	*argc = 0;

	while (*b) {
		while (*b && isspace(*b))
			b++;

		if ((!*b) || (*b == '#'))
			break;

		e = b;
		while (*e && !isspace(*e))
			e++;

		argv[(*argc)++] = b;
		if (!*e)
			break;
		*e++ = '\0';
		b = e;
		if (*argc == max)
			break;
	}

	return *argc;
}

static void __init_log(struct config_file *cf)
{
	char *open_mode = "a";

	const char *log_file = find_config_str(cf->root, "log/file", '/', 0);

	if (find_config_int(cf->root, "log/overwrite", '/', 0))
		open_mode = "w";

	if (log_file) {
		/* set up the logging */
		if (!(_log = fopen(log_file, open_mode)))
			log_error("Couldn't open log file %s", log_file);
		else
			init_log(_log);
	}

	_default_debug = find_config_int(cf->root, "log/level", '/', 0);
	init_debug(_default_debug);

	_default_verbose = find_config_int(cf->root, "log/verbose", '/', 0);
	init_verbose(_default_verbose);
}

static int dev_cache_setup(struct config_file *cf)
{
	struct config_node *cn;
	struct config_value *cv;

	if (!dev_cache_init()) {
		stack;
		return 0;
	}

	if (!(cn = find_config_node(cf->root, "devices/scan", '/'))) {
		if (!dev_cache_add_dir("/dev")) {
			log_error("Failed to add /dev to internal "
				  "device cache");
			return 0;
		}
		log_verbose
		    ("device/scan not in config file: Defaulting to /dev");
		return 1;
	}

	for (cv = cn->v; cv; cv = cv->next) {
		if (cv->type != CFG_STRING) {
			log_error("Invalid string in config file: "
				  "devices/scan");
			return 0;
		}

		if (!dev_cache_add_dir(cv->v.str)) {
			log_error("Failed to add %s to internal device cache",
				  cv->v.str);
			return 0;
		}
	}

	return 1;
}

static struct dev_filter *filter_components_setup(struct config_file *cf)
{
	struct config_node *cn;
	struct dev_filter *f1, *f2, *f3;

	if (!(f2 = lvm_type_filter_create()))
		return 0;

	if (!(cn = find_config_node(cf->root, "devices/filter", '/'))) {
		log_debug("devices/filter not found in config file: no regex "
			  "filter installed");
		return f2;
	}

	if (!(f1 = regex_filter_create(cn->v))) {
		log_error("Failed to create regex device filter");
		return f2;
	}

	if (!(f3 = composite_filter_create(2, f1, f2))) {
		log_error("Failed to create composite device filter");
		return f2;
	}

	return f3;
}

static struct dev_filter *filter_setup(struct config_file *cf)
{
	const char *lvm_cache;
	struct dev_filter *f3, *f4;
	struct stat st;
	char cache_file[PATH_MAX];

	_dump_filter = 0;

	if (!(f3 = filter_components_setup(cmd->cf)))
		return 0;

	if (lvm_snprintf(cache_file, sizeof(cache_file),
			 "%s/.cache", _system_dir) < 0) {
		log_err("Persistent cache filename too long ('%s/.cache').",
			_system_dir);
		return 0;
	}

	lvm_cache = find_config_str(cf->root, "devices/cache", '/',
				    cache_file);

	if (!(f4 = persistent_filter_create(f3, lvm_cache))) {
		log_error("Failed to create persistent device filter");
		return 0;
	}

	/* Should we ever dump persistent filter state? */
	if (find_config_int(cf->root, "devices/write_cache_state", '/', 1))
		_dump_filter = 1;

	if (!stat(lvm_cache, &st) && !persistent_filter_load(f4))
		log_verbose("Failed to load existing device cache from %s",
			    lvm_cache);

	return f4;
}

static int _get_env_vars(void)
{
	const char *e;

	if ((e = getenv("LVM_SYSTEM_DIR"))) {
		if (snprintf(_system_dir, sizeof(_system_dir), "%s", e) < 0) {
			log_err("LVM_SYSTEM_DIR environment variable "
				"is too long.");
			return 0;
		}
	}

	return 1;
}

static int init(void)
{
	struct stat info;
	char config_file[PATH_MAX];

	if (!_get_env_vars())
		return 0;

	if (!(cmd = dbg_malloc(sizeof(*cmd)))) {
		log_error("Failed to allocate command context");
		return 0;
	}

	/* FIXME: Override from config file. (Append trailing slash if reqd) */
	cmd->dev_dir = "/dev/";
	dm_set_dev_dir(cmd->dev_dir);

	if (!(cmd->cf = create_config_file())) {
		stack;
		return 0;
	}

	/* Use LOG_USER for syslog messages by default */
	init_syslog(LOG_USER);

	/* send log messages to stderr for now */
	init_log(stderr);

	if (snprintf(config_file, sizeof(config_file),
		     "%s/lvm.conf", _system_dir) < 0) {
		log_err("lvm_system_dir was too long");
		return 0;
	}

	if (stat(config_file, &info) != -1) {
		/* we've found a config file */
		if (!read_config(cmd->cf, config_file)) {
			log_error("Failed to load config file %s",
				  config_file);
			return 0;
		}

		__init_log(cmd->cf);
	}

	dm_log_init(print_log);

	if (!dev_cache_setup(cmd->cf))
		return 0;

	if (!(cmd->filter = filter_setup(cmd->cf))) {
		log_error("Failed to set up internal device filters");
		return 0;
	}

	if (!(cmd->mem = pool_create(4 * 1024))) {
		log_error("Command pool creation failed");
		return 0;
	}

	if (!(fid = create_lvm1_format(cmd)))
		return 0;

	return 1;
}

static void __fin_commands(void)
{
	int i;

	for (i = 0; i < _num_commands; i++)
		dbg_free(_commands[i].valid_args);

	dbg_free(_commands);
}

static void fin(void)
{
	if (_dump_filter)
		persistent_filter_dump(cmd->filter);

	fid->ops->destroy(fid);
	cmd->filter->destroy(cmd->filter);
	pool_destroy(cmd->mem);
	vgcache_destroy();
	dev_cache_exit();
	destroy_config_file(cmd->cf);
	dbg_free(cmd);
	__fin_commands();
	dump_memory();
	fin_log();

	if (_log)
		fclose(_log);
}

static int run_script(int argc, char **argv)
{
	FILE *script;

	char buffer[CMD_LEN];
	int ret = 0;
	int magic_number = 0;

	if ((script = fopen(argv[0], "r")) == NULL)
		return ENO_SUCH_CMD;

	while (fgets(buffer, sizeof(buffer), script) != NULL) {
		if (!magic_number) {
			if (buffer[0] == '#' && buffer[1] == '!')
				magic_number = 1;
			else
				return ENO_SUCH_CMD;
		}
		if ((strlen(buffer) == sizeof(buffer) - 1)
		    && (buffer[sizeof(buffer) - 1] - 2 != '\n')) {
			buffer[50] = '\0';
			log_error("Line too long (max 255) beginning: %s",
				  buffer);
			ret = EINVALID_CMD_LINE;
			break;
		}
		if (split(buffer, &argc, argv, MAX_ARGS) == MAX_ARGS) {
			buffer[50] = '\0';
			log_error("Too many arguments: %s", buffer);
			ret = EINVALID_CMD_LINE;
			break;
		}
		if (!argc)
			continue;
		if (!strcmp(argv[0], "quit"))
			break;
		run_command(argc, argv);
	}

	fclose(script);
	return ret;
}

#ifdef READLINE_SUPPORT
/* List matching commands */
static char *_list_cmds(const char *text, int state)
{
	static int i = 0;
	static int len = 0;

	/* Initialise if this is a new completion attempt */
	if (!state) {
		i = 0;
		len = strlen(text);
	}

	while (i < _num_commands)
		if (!strncmp(text, _commands[i++].name, len))
			return strdup(_commands[i - 1].name);

	return NULL;
}

/* List matching arguments */
static char *_list_args(const char *text, int state)
{
	static int match_no = 0;
	static int len = 0;
	static struct command *com;

	/* Initialise if this is a new completion attempt */
	if (!state) {
		char *s = rl_line_buffer;
		int j = 0;

		match_no = 0;
		com = NULL;
		len = strlen(text);

		/* Find start of first word in line buffer */
		while (isspace(*s))
			s++;

		/* Look for word in list of commands */
		for (j = 0; j < _num_commands; j++) {
			char *p;
			char *q = s;

			p = (char *) _commands[j].name;
			while (*p == *q) {
				p++;
				q++;
			}
			if ((!*p) && *q == ' ') {
				com = _commands + j;
				break;
			}
		}

		if (!com)
			return NULL;
	}

	/* Short form arguments */
	if (len < 3) {
		while (match_no < com->num_args) {
			char s[3];
			char c;
			if (!(c = (the_args +
				   com->valid_args[match_no++])->short_arg))
				continue;

			sprintf(s, "-%c", c);
			if (!strncmp(text, s, len))
				return strdup(s);
		}
	}

	/* Long form arguments */
	if (match_no < com->num_args)
		match_no = com->num_args;

	while (match_no - com->num_args < com->num_args) {
		char *l;
		l = (the_args +
		     com->valid_args[match_no++ - com->num_args])->long_arg;
		if (!strncmp(text, l, len))
			return strdup(l);
	}

	return NULL;
}

/* Custom completion function */
static char **_completion(const char *text, int start_pos, int end_pos)
{
	char **match_list = NULL;
	int p = 0;

	while (isspace((int) *(rl_line_buffer + p)))
		p++;

	/* First word should be one of our commands */
	if (start_pos == p)
		match_list = rl_completion_matches(text, _list_cmds);

	else if (*text == '-')
		match_list = rl_completion_matches(text, _list_args);
	/* else other args */

	/* No further completion */
	rl_attempted_completion_over = 1;
	return match_list;
}

static int _hist_file(char *buffer, size_t size)
{
	char *e = getenv("HOME");

	if (lvm_snprintf(buffer, size, "%s/.lvm_history", e) < 0) {
		log_err("History file path too long (%s/.lvm_history).", e);
		return 0;
	}

	return 1;
}

#define MAX_HISTORY 100
static void _read_history(void)
{
	char hist_file[PATH_MAX];

	if (!_hist_file(hist_file, sizeof(hist_file)))
		return;

	if (read_history(hist_file))
		log_very_verbose("Couldn't read history from %s.", hist_file);

	stifle_history(MAX_HISTORY);
}

static void _write_history(void)
{
	char hist_file[PATH_MAX];

	if (!_hist_file(hist_file, sizeof(hist_file)))
		return;

	if (write_history(hist_file))
		log_very_verbose("Couldn't write history to %s.", hist_file);
}

static int shell(void)
{
	int argc, ret;
	char *input = NULL, *args[MAX_ARGS], **argv;

	rl_readline_name = "lvm";
	rl_attempted_completion_function = (CPPFunction *) _completion;

	_read_history();

	_interactive = 1;
	while (1) {
		free(input);
		input = readline("lvm> ");

		/* EOF */
		if (!input) {
			printf("\n");
			break;
		}

		/* empty line */
		if (!*input)
			continue;

		add_history(input);

		argv = args;

		if (split(input, &argc, argv, MAX_ARGS) == MAX_ARGS) {
			log_error("Too many arguments, sorry.");
			continue;
		}

		if (!strcmp(argv[0], "lvm")) {
			argv++;
			argc--;
		}

		if (!argc)
			continue;

		if (!strcmp(argv[0], "quit")) {
			remove_history(history_length - 1);
			log_error("Exiting.");
			break;
		}

		ret = run_command(argc, argv);
		if (ret == ENO_SUCH_CMD)
			log_error("No such command '%s'.  Try 'help'.",
				  argv[0]);
	}

	_write_history();
	free(input);
	return 0;
}
#endif