summaryrefslogtreecommitdiffstats
path: root/agents/virt/client/options.c
blob: ddd6bc4e014e071a27dfc6d05eb16678bf2f4a91 (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
/*
  Copyright Red Hat, Inc. 2006

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  This program 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
  MA 02139, USA.
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <libgen.h>

/* Local includes */
#include "xvm.h"
#include "simple_auth.h"
#include "mcast.h"
#include "tcp_listener.h"
#include "options.h"

#define SCHEMA_COMPAT '\xfe'


/* Assignment functions */

static inline void
assign_debug(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value) {
		/* GNU getopt sets optarg to NULL for options w/o a param
		   We rely on this here... */
		args->debug++;
		return;
	}

	args->debug = atoi(value);
	if (args->debug < 0) {
		args->debug = 1;
	}
}


static inline void
assign_family(fence_virt_args_t *args, struct arg_info *arg,
	      char *value)
{
	if (!value)
		return;

	if (!strcasecmp(value, "ipv4")) {
		args->net.family = PF_INET;
	} else if (!strcasecmp(value, "ipv6")) {
		args->net.family = PF_INET6;
	} else if (!strcasecmp(value, "auto")) {
		args->net.family = 0;
	} else {
		printf("Unsupported family: '%s'\n", value);
		args->flags |= F_ERR;
	}
}


static inline void
assign_address(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (args->net.addr)
		free(args->net.addr);
	args->net.addr = strdup(value);
}

static inline void
assign_ip_address(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (args->net.ipaddr)
		free(args->net.ipaddr);
	args->net.ipaddr = strdup(value);
}

static inline void
assign_channel_address(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (args->serial.address)
		free(args->serial.address);
	args->serial.address = strdup(value);
}


static inline void
assign_port(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	char *p;
	int ret;

	if (!value)
		return;

	ret = strtol(value, &p, 0);
	if (ret <= 0 || ret >= 65536 || *p != '\0') {
		printf("Invalid port: '%s'\n", value);
		args->flags |= F_ERR;
	} else
		args->net.port = ret;
}

static inline void
assign_cid(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	char *p;
	unsigned long ret;

	if (!value) {
		args->net.cid = 2;
		return;
	}

	ret = strtoul(value, &p, 0);
	if (!p || *p != '\0' || ret < 2 || ret >= 0xffffffff) {
		printf("Invalid CID: '%s'\n", value);
		args->flags |= F_ERR;
	} else
		args->net.cid = ret;
}

static inline void
assign_interface(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	int ret;

	if (!value)
		return;

	ret = if_nametoindex(value);
	if (ret <= 0) {
		printf("Invalid interface: %s: %s\n", value, strerror(errno));
		args->net.ifindex = 0;
	}

	args->net.ifindex = ret;
}


static inline void
assign_retrans(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	char *p;
	int ret;

	if (!value)
		return;

	ret = strtol(value, &p, 0);
	if (ret <= 0 || *p != '\0') {
		printf("Invalid retransmit time: '%s'\n", value);
		args->flags |= F_ERR;
	} else
		args->retr_time = ret;
}

static inline void
assign_hash(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (!strcasecmp(value, "none")) {
		args->net.hash = HASH_NONE;
	} else if (!strcasecmp(value, "sha1")) {
		args->net.hash = HASH_SHA1;
	} else if (!strcasecmp(value, "sha256")) {
		args->net.hash = HASH_SHA256;
	} else if (!strcasecmp(value, "sha512")) {
		args->net.hash = HASH_SHA512;
	} else {
		printf("Unsupported hash: %s\n", value);
		args->flags |= F_ERR;
	}
}


static inline void
assign_auth(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (!strcasecmp(value, "none")) {
		args->net.auth = AUTH_NONE;
	} else if (!strcasecmp(value, "sha1")) {
		args->net.auth = AUTH_SHA1;
	} else if (!strcasecmp(value, "sha256")) {
		args->net.auth = AUTH_SHA256;
	} else if (!strcasecmp(value, "sha512")) {
		args->net.auth = AUTH_SHA512;
	} else {
		printf("Unsupported auth type: %s\n", value);
		args->flags |= F_ERR;
	}
}

static inline void
assign_key(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	struct stat st;

	if (!value)
		return;

	if (args->net.key_file)
		free(args->net.key_file);
	args->net.key_file = strdup(value);

	if (stat(value, &st) == -1) {
		printf("Invalid key file: '%s' (%s)\n", value,
		       strerror(errno));
		args->flags |= F_ERR;
	}
}


static inline void
assign_op(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	if (!strcasecmp(value, "null")) {
		args->op = FENCE_NULL;
	} else if (!strcasecmp(value, "on")) {
		args->op = FENCE_ON;
	} else if (!strcasecmp(value, "off")) {
		args->op = FENCE_OFF;
	} else if (!strcasecmp(value, "reboot")) {
		args->op = FENCE_REBOOT;
	} else if (!strcasecmp(value, "status")) {
		args->op = FENCE_STATUS;
	} else if (!strcasecmp(value, "monitor")) {
		args->op = FENCE_DEVSTATUS;
	} else if (!strcasecmp(value, "list") || !strcasecmp(value, "list-status")) {
		args->op = FENCE_HOSTLIST;
	} else if (!strcasecmp(value, "metadata")) {
		args->op = FENCE_METADATA;
	} else if (!strcasecmp(value, "validate-all")) {
		args->op = FENCE_VALIDATEALL;
	} else {
		printf("Unsupported operation: %s\n", value);
		args->flags |= F_ERR;
	}
}


static inline void
assign_device(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	args->serial.device = strdup(value);
}


static inline void
assign_params(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value)
		return;

	args->serial.speed = strdup(value);
}


static inline void
assign_domain(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (args->domain) {
		printf("Domain/UUID may not be specified more than once\n");
		args->flags |= F_ERR;
		return;
	}

	if (!value)
		return;

	args->domain = strdup(value);

	if (strlen(value) <= 0) {
		printf("Invalid domain name\n");
		args->flags |= F_ERR;
	}

	if (strlen(value) >= MAX_DOMAINNAME_LENGTH) {
		errno = ENAMETOOLONG;
		printf("Invalid domain name: '%s' (%s)\n",
		       value, strerror(errno));
		args->flags |= F_ERR;
	}
}


static inline void
assign_uuid_lookup(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	if (!value) {
		/* GNU getopt sets optarg to NULL for options w/o a param
		   We rely on this here... */
		args->flags |= F_USE_UUID;
		return;
	}

	args->flags |= ( !!atoi(value) ? F_USE_UUID : 0);
}


static inline void
assign_timeout(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	char *p;
	int ret;

	if (!value)
		return;

	ret = strtol(value, &p, 0);
	if (ret <= 0 || *p != '\0') {
		printf("Invalid timeout: '%s'\n", value);
		args->flags |= F_ERR;
	} else
		args->timeout = ret;
}

static inline void
assign_delay(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	char *p;
	int ret;

	if (!value)
		return;

	ret = strtol(value, &p, 0);
	if (ret < 0 || *p != '\0') {
		printf("Invalid delay: '%s'\n", value);
		args->flags |= F_ERR;
	} else
		args->delay = ret;
}

static inline void
assign_help(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	args->flags |= F_HELP;
}


static inline void
assign_version(fence_virt_args_t *args, struct arg_info *arg, char *value)
{
	args->flags |= F_VERSION;
}


static void
print_desc_xml(const char *desc)
{
	const char *d;

	for (d = desc; *d; d++) {
		switch (*d) {
		case '<':
			printf("&lt;");
			break;
		case '>':
			printf("&gt;");
			break;
		default:
			printf("%c", *d);
		}
	}
}


/** ALL valid command line and stdin arguments for this fencing agent */
static struct arg_info _arg_info[] = {
	{ '\xff', NULL, "agent",
	  NULL, 0, 0, "string", NULL,
	  "Not user serviceable",
	  NULL },

	{ '\xff', NULL, "self",
	  NULL, 0, 0, "string", NULL,
	  "Not user serviceable", 
	  NULL },

	{ '\xff', NULL, "nodename",
	  NULL, 0, 0, "string", NULL,
	  "Not user serviceable", 
	  NULL },

	{ 'd', "-d", "debug",
	  NULL, 0, 0, "boolean", NULL,
	  "Specify (stdin) or increment (command line) debug level",
	  assign_debug },

	{ 'i', "-i <family>", "ip_family",
	  NULL, 0, 0, "string", "auto",
	  "IP Family ([auto], ipv4, ipv6)",
	  assign_family },

	{ 'a', "-a <address>", "multicast_address",
	  NULL, 0, 0, "string", NULL,
	  "Multicast address (default=" IPV4_MCAST_DEFAULT " / " IPV6_MCAST_DEFAULT ")",
	  assign_address },

	{ 'T', "-T <address>", "ipaddr",
          NULL, 0, 0, "string", "127.0.0.1",
	  "IP address to connect to in TCP mode (default=" IPV4_TCP_ADDR_DEFAULT " / " IPV6_TCP_ADDR_DEFAULT ")",
	  assign_ip_address },

	{ 'S', "-S <cid>", "vsock",
          NULL, 0, 0, "integer", "2",
	  "vm socket CID to connect to in vsock mode",
	  assign_cid },

	{ 'A', "-A <address>", "channel_address",
          NULL, 0, 0, "string", "10.0.2.179",
	  "VM Channel IP address (default=" DEFAULT_CHANNEL_IP ")",
	  assign_channel_address },

	{ 'p', "-p <port>", "ipport",
          NULL, 0, 0, "string", "1229",
	  "TCP, Multicast, VMChannel, or VM socket port (default=1229)",
	  assign_port },

	{ 'I', "-I <interface>", "interface",
	  NULL, 0, 0, "string", NULL,
	  "Network interface name to listen on",
	  assign_interface },

	{ 'r', "-r <retrans>", "retrans", 
	  NULL, 0, 0, "string", "20",
	  "Multicast retransmit time (in 1/10sec; default=20)",
	  assign_retrans },

	{ 'c', "-c <hash>", "hash",
	  NULL, 0, 0, "string", "sha256",
	  "Packet hash strength (none, sha1, [sha256], sha512)",
	  assign_hash },

	{ 'C', "-C <auth>", "auth",
	  NULL, 0, 0, "string", "sha256",
	  "Authentication (none, sha1, [sha256], sha512)",
	  assign_auth },

	{ 'k', "-k <file>", "key_file",
	  NULL, 0, 0, "string", DEFAULT_KEY_FILE, 
	  "Shared key file (default=" DEFAULT_KEY_FILE ")",
	  assign_key },

	{ 'D', "-D <device>", "serial_device",
	  NULL, 0, 0, "string", DEFAULT_SERIAL_DEVICE,
	  "Serial device (default=" DEFAULT_SERIAL_DEVICE  ")",
	  assign_device },

	{ 'P', "-P <param>", "serial_params",
	  NULL, 0, 0, "string", DEFAULT_SERIAL_SPEED,
	  "Serial Parameters (default=" DEFAULT_SERIAL_SPEED ")",
	  assign_params },

	{ '\xff', NULL, "option",
	  /* Deprecated */
	  NULL, 0, 0, "string", "reboot",
	  "Fencing option (null, off, on, [reboot], status, list, list-status, monitor, validate-all, metadata)",
	  assign_op },

	{ 'o', "-o <operation>", "action",
	  NULL, 0, 0, "string", "reboot",
	  "Fencing action (null, off, on, [reboot], status, list, list-status, monitor, validate-all, metadata)",
	  assign_op },

	{ 'n', "-n <domain>", "plug",
	  "port", 0, 0, "string", NULL,
	  "Virtual Machine (domain name) to fence",
	  assign_domain },

	{ 'H', "-H <domain>", "port",
	  NULL, 1, 0, "string", NULL,
	  "Virtual Machine (domain name) to fence",
	  assign_domain },

	{ SCHEMA_COMPAT, NULL, "domain",
	  NULL, 0, 0, "string", NULL,
	  "Virtual Machine (domain name) to fence (deprecated; use port)",
	  assign_domain },

	{ 'u', "-u", "use_uuid",
	  NULL, 0, 0, "string", "0",
	  "Treat [domain] as UUID instead of domain name. This is provided for compatibility with older fence_xvmd installations.",
	  assign_uuid_lookup },

	{ 't', "-t <timeout>", "timeout",
	  NULL, 0, 0, "string", "30",
	  "Fencing timeout (in seconds; default=30)",
	  assign_timeout },

	{ 'h', "-h", NULL,
	  NULL, 0, 0, "boolean", "0",
 	  "Help",
	  assign_help },

	{ '?', "-?", NULL,
	  NULL, 0, 0, "boolean", "0",
 	  "Help (alternate)", 
	  assign_help },

	{ 'w', "-w <delay>", "delay",
	  NULL, 0, 0, "string", "0",
	  "Fencing delay (in seconds; default=0)",
	  assign_delay },

	{ 'V', "-V", NULL,
	  NULL, 0, 0, "boolean", "0",
 	  "Display version and exit", 
	  assign_version },

	/* Terminator */
	{ 0, NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL }
};


static struct arg_info *
find_arg_by_char(char arg)
{
	int x = 0;

	for (x = 0; _arg_info[x].opt != 0; x++) {
		if (_arg_info[x].opt == arg)
			return &_arg_info[x];
	}

	return NULL;
}


static struct arg_info *
find_arg_by_string(char *arg)
{
	int x = 0;

	for (x = 0; _arg_info[x].opt != 0; x++) {
		if (!_arg_info[x].stdin_opt)
			continue;
		if (!strcasecmp(_arg_info[x].stdin_opt, arg))
			return &_arg_info[x];
	}

	return NULL;
}


/* ============================================================= */

/**
  Initialize an args structure.

  @param args		Pointer to args structure to initialize.
 */
void
args_init(fence_virt_args_t *args)
{
	args->domain = NULL;
	//args->uri = NULL;
	args->op = FENCE_REBOOT;
	args->net.key_file = strdup(DEFAULT_KEY_FILE);
	args->net.hash = DEFAULT_HASH;
	args->net.auth = DEFAULT_AUTH;
	args->net.addr = NULL;
	args->net.ipaddr = NULL;
	args->net.cid = 0;
	args->net.port = DEFAULT_MCAST_PORT;
	args->net.ifindex = 0;
	args->net.family = 0;  /* auto */
	args->serial.device = NULL;
	args->serial.speed = strdup(DEFAULT_SERIAL_SPEED);
	args->serial.address = strdup(DEFAULT_CHANNEL_IP);
	args->timeout = 30;
	args->retr_time = 20;
	args->flags = 0;
	args->debug = 0;
	args->delay = 0;
}


#define _pr_int(piece) printf("  %s = %d\n", #piece, piece)
#define _pr_str(piece) printf("  %s = %s\n", #piece, piece)


/**
  Prints out the contents of an args structure for debugging.

  @param args		Pointer to args structure to print out.
 */
void
args_print(fence_virt_args_t *args)
{
	printf("-- args @ %p --\n", args);
	_pr_str(args->domain);
	_pr_int(args->op);
	_pr_int(args->mode);
	_pr_int(args->debug);
	_pr_int(args->timeout);
	_pr_int(args->delay);
	_pr_int(args->retr_time);
	_pr_int(args->flags);

	_pr_str(args->net.addr);
	_pr_str(args->net.ipaddr);
	_pr_int(args->net.cid);
	_pr_str(args->net.key_file);
	_pr_int(args->net.port);
	_pr_int(args->net.hash);
	_pr_int(args->net.auth);
	_pr_int(args->net.family);
	_pr_int(args->net.ifindex);

	_pr_str(args->serial.device);
	_pr_str(args->serial.speed);
	_pr_str(args->serial.address);
	printf("-- end args --\n");
}


/**
  Print out arguments and help information based on what is allowed in
  the getopt string optstr.

  @param progname	Program name.
  @param optstr		Getopt(3) style options string
  @param print_stdin	0 = print command line options + description,
			1 = print fence-style stdin args + description
 */
static char *
find_rev(const char *start, char *curr, char c)
{

	while (curr > start) {
		if (*curr == c)
			return curr;
		--curr;
	}

	return NULL;
}


static void
output_help_text(int arg_width, int help_width, const char *arg, const char *desc)
{
	char out_buf[4096];
	char *p, *start;
	const char *arg_print = arg;
	int len;

	memset(out_buf, 0, sizeof(out_buf));
	strncpy(out_buf, desc, sizeof(out_buf) - 1);
	start = out_buf;

	do {
		p = NULL;
		len = strlen(start);
		if (len > help_width) {
			p = start + help_width;
			p = find_rev(start, p, ' ');
			if (p) {
				*p = 0;
				p++;
			}
		}
		printf("  %*.*s  %*.*s\n",
		       -arg_width, arg_width,
		       arg_print,
		       -help_width, help_width,
		       start);
		if (!p)
			return;
		if (arg == arg_print)
			arg_print = " ";
		start = p;
	} while(1);
}


void
args_usage(char *progname, const char *optstr, int print_stdin)
{
	int x;
	struct arg_info *arg;

	if (!print_stdin) {
		if (progname) {
			printf("usage: %s [args]\n\nNOTE: reboot-action does not power on nodes that are powered off.\n\n", progname);
		} else {
			printf("usage: fence_virt [args]\n\nNOTE: reboot-action does not power on nodes that are powered off.\n\n");
		}
	}

	for (x = 0; x < strlen(optstr); x++) {
		arg = find_arg_by_char(optstr[x]);
		if (!arg || arg->deprecated)
			continue;

		if (print_stdin) {
			if (arg && arg->stdin_opt)
				output_help_text(20, 55, arg->stdin_opt, arg->desc);
		} else {
			output_help_text(20, 55, arg->opt_desc, arg->desc);
		}
	}

	printf("\n");
}


void
args_metadata(char *progname, const char *optstr)
{
	int x;
	struct arg_info *arg;

	printf("<?xml version=\"1.0\" ?>\n");
	printf("<resource-agent name=\"%s\" shortdesc=\"Fence agent for virtual machines\">\n", basename(progname));
	printf("<longdesc>%s is an I/O Fencing agent which can be used with "
	       "virtual machines.\n\nNOTE: reboot-action does not power on nodes that are powered off."
	       "</longdesc>\n", basename(progname));
	printf("<vendor-url>https://libvirt.org</vendor-url>\n");
	printf("<parameters>\n");

	for (x = 0; x < strlen(optstr); x++) {
		arg = find_arg_by_char(optstr[x]);
		if (!arg)
			continue;
		if (!arg->stdin_opt)
			continue;

		if (arg->obsoletes)
			printf("\t<parameter name=\"%s\" unique=\"0\" required=\"%d\" obsoletes=\"%s\">\n", arg->stdin_opt, (!strcmp(arg->content_type, "boolean") || arg->default_value) ? 0 : 1, arg->obsoletes);
		else if (arg->deprecated)
			printf("\t<parameter name=\"%s\" unique=\"0\" required=\"%d\" deprecated=\"%d\">\n", arg->stdin_opt, (!strcmp(arg->content_type, "boolean") || arg->default_value) ? 0 : 1, arg->deprecated);
		else
			printf("\t<parameter name=\"%s\" unique=\"0\" required=\"%d\">\n", arg->stdin_opt, (!strcmp(arg->content_type, "boolean") || arg->default_value || !strcmp(arg->stdin_opt, "multicast_address")) ? 0 : 1);

                printf("\t\t<getopt mixed=\"-%c\" />\n",arg->opt);
                if (arg->default_value) {
                  printf("\t\t<content type=\"%s\" default=\"%s\" />\n", arg->content_type, arg->default_value);
                } else {
                  printf("\t\t<content type=\"%s\" />\n", arg->content_type);
                }
		printf("\t\t<shortdesc lang=\"en\">");
		print_desc_xml(arg->desc);
		printf("</shortdesc>\n");
		printf("\t</parameter>\n");
	}

	for (x = 0; _arg_info[x].opt != 0; x++) {
		if (_arg_info[x].opt != SCHEMA_COMPAT)
			continue;

		arg = &_arg_info[x];

		printf("\t<parameter name=\"%s\" unique=\"0\" required=\"%d\" deprecated=\"1\">\n", arg->stdin_opt,
			(!strcmp(arg->content_type, "boolean") || arg->default_value || !strcmp(arg->stdin_opt, "domain")) ? 0 : 1);
		printf("\t\t<getopt mixed=\"\" />\n");
                if (arg->default_value) {
                  printf("\t\t<content type=\"%s\" default=\"%s\" />\n", arg->content_type, arg->default_value);
                } else {
                  printf("\t\t<content type=\"%s\" />\n", arg->content_type);
                }
		printf("\t\t<shortdesc lang=\"en\">");
		print_desc_xml(arg->desc);
		printf("</shortdesc>\n");
		printf("\t</parameter>\n");
	}

	printf("</parameters>\n");
	printf("<actions>\n");
	printf("\t<action name=\"null\" />\n");	
	printf("\t<action name=\"on\" />\n");
	printf("\t<action name=\"off\" />\n");
	printf("\t<action name=\"reboot\" />\n");
	printf("\t<action name=\"metadata\" />\n");	
	printf("\t<action name=\"status\" />\n");	
	printf("\t<action name=\"monitor\" />\n");	
	printf("\t<action name=\"list\" />\n");	
	printf("\t<action name=\"list-status\" />\n");	
	printf("\t<action name=\"validate-all\" />\n");	
	printf("</actions>\n");
	printf("</resource-agent>\n");
}


/**
  Remove leading and trailing whitespace from a line of text.

  @param line		Line to clean up
  @param linelen	Max size of line
  @return		0 on success, -1 on failure
 */
static int
cleanup(char *line, size_t linelen)
{
	char *p;
	int x;
	
	/* Remove leading whitespace. */
	p = line;
	for (x = 0; x < linelen; x++) {
		switch (line[x]) {
		case '\t':
		case ' ':
			break;
		case '\n':
		case '\r':
			return -1;
		default:
			goto eol;
		}
	}
eol:
	/* Move the remainder down by as many whitespace chars as we
	   chewed up */
	if (x)
		memmove(p, &line[x], linelen-x);

	/* Remove trailing whitespace. */
	for (x=0; x < linelen; x++) {
		switch(line[x]) {
		case '\t':
		case ' ':
		case '\r':
		case '\n':
			line[x] = 0;
		case 0:
		/* End of line */
			return 0;
		}
	}

	return -1;
}


/**
  Parse args from stdin and assign to the specified args structure.
  
  @param optstr		Command line option string in getopt(3) format
  @param args		Args structure to fill in.
 */
void
args_get_stdin(const char *optstr, fence_virt_args_t *args)
{
	char in[256];
	char *name, *val;
	struct arg_info *arg;

	while (fgets(in, sizeof(in), stdin)) {

		if (in[0] == '#')
			continue;

		if (cleanup(in, sizeof(in)) == -1)
			continue;

		name = in;
		if ((val = strchr(in, '='))) {
			*val = 0;
			++val;
		}

		arg = find_arg_by_string(name);
		if (!arg || (arg->opt != '\xff' && 
			     arg->opt != SCHEMA_COMPAT &&
			     !strchr(optstr, arg->opt))) {
			fprintf(stderr,
			        "Parse error: Ignoring unknown option '%s'\n",
			        name);
			continue;
		}

		if (arg->assign)
			arg->assign(args, arg, val);
	}
}


/**
  Parse args from stdin and assign to the specified args structure.
  
  @param optstr		Command line option string in getopt(3) format
  @param args		Args structure to fill in.
 */
void
args_get_getopt(int argc, char **argv, const char *optstr, fence_virt_args_t *args)
{
	int opt;
	struct arg_info *arg;

	while ((opt = getopt(argc, argv, optstr)) != EOF) {

		arg = find_arg_by_char(opt);

		if (!arg) {
			args->flags |= F_ERR;
			continue;
		}

		if (arg->assign)
			arg->assign(args, arg, optarg);
	}
}


void
args_finalize(fence_virt_args_t *args)
{
	char *addr = NULL;

	if (!args->net.addr) {
		switch(args->net.family) {
		case 0:
		case PF_INET:
			addr = (char *)IPV4_MCAST_DEFAULT;
			break;
		case PF_INET6:
			addr = (char *)IPV6_MCAST_DEFAULT;
			break;
		default:
			args->flags |= F_ERR;
		break;
		}
	}

	if (!args->net.addr)
		args->net.addr = addr;

	if (!args->net.addr) {
		printf("No multicast address available\n");
		args->flags |= F_ERR;
	}

	if (!args->net.addr)
		return;
	if (args->net.family)
		return;

	/* Set family */
	if (strchr(args->net.addr, ':'))
		args->net.family = PF_INET6;
	if (strchr(args->net.addr, '.'))
		args->net.family = PF_INET;
	if (!args->net.family) {
		printf("Could not determine address family\n");
		args->flags |= F_ERR;
	}
}