summaryrefslogtreecommitdiffstats
path: root/agents/zvm/fence_zvm.c
blob: 6f1469ecbb22096db299a698acf848d6c8cde803 (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
/*
 * fence_zvm.c: SMAPI interface for managing zVM Guests
 *
 * Copyright (C) 2012 Sine Nomine Associates
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 * Neale Ferguson <neale@sinenomine.net>
 *
 */

#ifdef __s390__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netiucv/iucv.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <getopt.h>
#include <ctype.h>
#include <syslog.h>
#include "fence_zvm.h"

#define MIN(a,b)	((a) < (b) ? (a) : (b))
#define DEFAULT_TIMEOUT 300
#define DEFAULT_DELAY   0

#define ACT_OFFON 	0
#define ACT_OFF		1
#define ACT_ON		2
#define ACT_METADATA	3
#define ACT_STATUS	4
#define ACT_MONITOR	5
#define ACT_LIST	6
#define ACT_HELP	7

static int zvm_smapi_reportError (void *, void *);

static struct option longopts[] = {
	{"action", required_argument, NULL, 'o'},
	{"delay", required_argument, NULL, 'h'},
	{"help", no_argument, NULL, 'h'},
	{"ip", required_argument, NULL, 'a'},
	{"plug", required_argument, NULL, 'n'},
	{"timeout", required_argument, NULL, 'T'},
	{"zvmsys", required_argument, NULL, 'z'},
	{NULL, 0, NULL, 0}
};

static const char *optString = "a:ho:n:T:";

static int zvm_metadata (void);
static int usage (void);

/**
 * zvm_smapi_open:
 * @zvm: z/VM driver information
 *
 * Opens a connection with the z/VM SMAPI server
 */
int
zvm_smapi_open (zvm_driver_t * zvm)
{
	int rc = -1, sockaddrlen;
	static char iucvprog[9] = "DMSRSRQU\0";
	struct sockaddr_iucv siucv_addr;
	const struct sockaddr *siucv_ptr = (void *) &siucv_addr;

	if ((zvm->sd = socket (AF_IUCV, SOCK_STREAM, IPPROTO_IP)) != -1) {
		memset (&siucv_addr, 0, sizeof (siucv_addr));
		siucv_addr.siucv_family = AF_IUCV;
		siucv_addr.siucv_port = 0;
		siucv_addr.siucv_addr = 0;
		memset (&siucv_addr.siucv_nodeid, ' ', 8);
		memset (&siucv_addr.siucv_user_id, ' ', 8);
		memset (&siucv_addr.siucv_name, ' ', 8);
		sockaddrlen = sizeof (siucv_addr);
		if ((rc = bind (zvm->sd, siucv_ptr, sockaddrlen)) != -1) {
			memcpy (&siucv_addr.siucv_user_id, zvm->smapiSrv,
				strlen (zvm->smapiSrv));
			memcpy (&siucv_addr.siucv_name, &iucvprog, 8);
			memcpy (&siucv_addr.siucv_nodeid, zvm->node,
				strlen (zvm->node));
			rc = connect (zvm->sd,
				      (__CONST_SOCKADDR_ARG) siucv_ptr,
				      sockaddrlen);
		}
		if (rc == -1) {
			syslog (LOG_ERR, "Error connecting to %s - %m",
				zvm->smapiSrv);
			close (zvm->sd);
		}
	}
	return (rc);
}

/**
 * zvm_smapi_imageRecycle
 * @zvm: z/VM driver information
 *
 * Deactivates a virtual image
 */
int
zvm_smapi_imageRecycle (zvm_driver_t * zvm)
{
	struct _inPlist
	{
		int32_t lPlist;
		int32_t lFName;
		char fName[13];
		int32_t lUser;
		int32_t lPass;
		int32_t lTarget;
		char target[0];
	} __attribute__((__packed__)) * inPlist;
	int32_t lInPlist;
	struct _outPlist
	{
		smapiOutHeader_t hdr;
		int32_t nActive;
		int32_t nInActive;
		int32_t lFail;
		char failArray[0];
	} *outPlist = NULL;
	void *pOut = NULL;
	int32_t lRsp;
	uint32_t reqId;
	int rc;

	/*
	 * Implement any delay
	 */
	if (zvm->delay > 0)
		sleep (zvm->delay);

	lInPlist = sizeof (*inPlist) + strlen (zvm->target);
	inPlist = malloc (lInPlist);
	if (inPlist != NULL) {
		inPlist->lPlist = lInPlist - sizeof (inPlist->lPlist);
		inPlist->lFName = sizeof (inPlist->fName);
		memcpy (inPlist->fName, Image_Recycle,
			sizeof (inPlist->fName));
		inPlist->lUser = inPlist->lPass = 0;
		inPlist->lTarget = strlen (zvm->target);
		memcpy (inPlist->target, zvm->target, inPlist->lTarget);
		if ((rc =
		     zvm_smapi_send (zvm, inPlist, &reqId, lInPlist)) != -1) {
			if ((rc = zvm_smapi_recv (zvm, &pOut, &lRsp)) != -1) {
				outPlist = pOut;
				if (outPlist->hdr.rc == 0) {
					syslog (LOG_INFO,
						"Recycling of %s successful",
						zvm->target);
					rc = 0;
				}
				else {
					if ((outPlist->hdr.rc ==
					     RCERR_IMAGEOP) & ((outPlist->hdr.
								reason ==
								RS_NOT_ACTIVE)
							       | (outPlist->
								  hdr.
								  reason ==
								  RS_BEING_DEACT)))
					{
						syslog (LOG_INFO,
							"Recycling of %s successful",
							zvm->target);
						rc = 0;
					}
					else {
						rc = outPlist->hdr.rc;
						zvm->reason =
							outPlist->hdr.reason;
						(void) zvm_smapi_reportError
							(inPlist, outPlist);
					}
				}
			}
		}
		free (inPlist);
		free (outPlist);
	}
	else {
		syslog (LOG_ERR, "%s - cannot allocate parameter list",
			__func__);
		rc = -1;
	}
	return (rc);
}

/**
 * zvm_smapi_imageDeactivate
 * @zvm: z/VM driver information
 *
 * Deactivates a virtual image
 */
int
zvm_smapi_imageDeactivate (zvm_driver_t * zvm)
{
	struct _inPlist
	{
		int32_t lPlist;
		int32_t lFName;
		char fName[16];
		int32_t lUser;
		int32_t lPass;
		int32_t lTarget;
		char target[0];
	} __attribute__((__packed__)) * inPlist;
	struct _deactTime
	{
		int32_t lForceTime;
		char forceTime[5];
	} __attribute__((__packed__)) * deactTime;
	int32_t lInPlist;
	struct _outPlist
	{
		smapiOutHeader_t hdr;
		int32_t nActive;
		int32_t nInActive;
		int32_t lFail;
		char failArray[0];
	} *outPlist = NULL;
	void *pOut = NULL;
	int32_t lRsp;
	uint32_t reqId;
	int rc;

	/*
	 * Implement any delay
	 */
	if (zvm->delay > 0)
		sleep (zvm->delay);

	lInPlist =
		sizeof (*inPlist) + strlen (zvm->target) +
		sizeof (*deactTime);
	inPlist = malloc (lInPlist);
	if (inPlist != NULL) {
		inPlist->lPlist = lInPlist - sizeof (inPlist->lPlist);
		inPlist->lFName = sizeof (inPlist->fName);
		memcpy (inPlist->fName, Image_Deactivate,
			sizeof (inPlist->fName));
		deactTime =
			(void *) ((intptr_t) inPlist + sizeof (*inPlist) +
				  strlen (zvm->target));
		deactTime->lForceTime = sizeof (deactTime->forceTime);
		memcpy (deactTime->forceTime, "IMMED",
			sizeof (deactTime->forceTime));
		inPlist->lUser = inPlist->lPass = 0;
		inPlist->lTarget = strlen (zvm->target);
		memcpy (inPlist->target, zvm->target, inPlist->lTarget);
		if ((rc =
		     zvm_smapi_send (zvm, inPlist, &reqId, lInPlist)) != -1) {
			if ((rc = zvm_smapi_recv (zvm, &pOut, &lRsp)) != -1) {
				outPlist = pOut;
				if (outPlist->hdr.rc == 0) {
					syslog (LOG_INFO,
						"Deactivation of %s successful",
						zvm->target);
					rc = 0;
				}
				else {
					if ((outPlist->hdr.rc ==
					     RCERR_IMAGEOP) & ((outPlist->hdr.
								reason ==
								RS_NOT_ACTIVE)
							       | (outPlist->
								  hdr.
								  reason ==
								  RS_BEING_DEACT)))
					{
						syslog (LOG_INFO,
							"Deactivation of %s successful",
							zvm->target);
						rc = 0;
					}
					else {
						rc = outPlist->hdr.rc;
						zvm->reason =
							outPlist->hdr.reason;
						(void) zvm_smapi_reportError
							(inPlist, outPlist);
					}
				}
			}
		}
		free (inPlist);
		free (outPlist);
	}
	else {
		syslog (LOG_ERR, "%s - cannot allocate parameter list",
			__func__);
		rc = -1;
	}
	return (rc);
}

/**
 * zvm_smapi_imageActivate
 * @zvm: z/VM driver information
 *
 * Deactivates a virtual image
 */
int
zvm_smapi_imageActivate (zvm_driver_t * zvm)
{
	struct _inPlist
	{
		int32_t lPlist;
		int32_t lFName;
		char fName[14];
		int32_t lUser;
		int32_t lPass;
		int32_t lTarget;
		char target[0];
	} __attribute__((__packed__)) * inPlist;
	int32_t lInPlist;
	struct _outPlist
	{
		smapiOutHeader_t hdr;
		int32_t nActive;
		int32_t nInActive;
		int32_t lFail;
		char failArray[0];
	} *outPlist = NULL;
	void *pOut = NULL;
	int32_t lRsp;
	uint32_t reqId;
	int rc;

	/*
	 * Implement any delay
	 */
	if (zvm->delay > 0)
		sleep (zvm->delay);

	lInPlist = sizeof (*inPlist) + strlen (zvm->target);
	inPlist = malloc (lInPlist);
	if (inPlist != NULL) {
		inPlist->lPlist = lInPlist - sizeof (inPlist->lPlist);
		inPlist->lFName = sizeof (inPlist->fName);
		memcpy (inPlist->fName, Image_Activate,
			sizeof (inPlist->fName));
		inPlist->lUser = inPlist->lPass = 0;
		inPlist->lTarget = strlen (zvm->target);
		memcpy (inPlist->target, zvm->target, inPlist->lTarget);
		if ((rc =
		     zvm_smapi_send (zvm, inPlist, &reqId, lInPlist)) != -1) {
			if ((rc = zvm_smapi_recv (zvm, &pOut, &lRsp)) != -1) {
				outPlist = pOut;
				if (outPlist->hdr.rc == 0) {
					syslog (LOG_INFO,
						"Activation of %s successful",
						zvm->target);
					rc = 0;
				}
				else {
					if ((outPlist->hdr.rc ==
					     RCERR_IMAGEOP) & (outPlist->hdr.
							       reason ==
							       RS_ALREADY_ACTIVE))
					{
						syslog (LOG_INFO,
							"Activation of %s successful",
							zvm->target);
						rc = 0;
					}
					else {
						rc = outPlist->hdr.rc;
						zvm->reason =
							outPlist->hdr.reason;
						(void) zvm_smapi_reportError
							(inPlist, outPlist);
					}
				}
			}
		}
		free (inPlist);
		free (outPlist);
	}
	else {
		syslog (LOG_ERR, "%s - cannot allocate parameter list",
			__func__);
		rc = -1;
	}
	return (rc);
}

/**
 * zvm_smapi_imageQuery
 * @zvm: z/VM driver information
 *
 * Queries the state of a virtual image
 */
int
zvm_smapi_imageQuery (zvm_driver_t * zvm)
{
	struct _inPlist
	{
		int32_t lPlist;
		int32_t lFName;
		char fName[18];
		int32_t lUser;
		int32_t lPass;
		int32_t lTarget;
		char target[0];
	} __attribute__((__packed__)) * inPlist;
	int32_t lInPlist;
	struct _outPlist
	{
		smapiOutHeader_t hdr;
		int32_t lNames;
		char nameArray[0];
	} *outPlist = NULL;
	void *pOut = NULL;
	int32_t lRsp;
	uint32_t reqId;
	int rc;

	/*
	 * Implement any delay
	 */
	if (zvm->delay > 0)
		sleep (zvm->delay);

	lInPlist = sizeof (*inPlist) + strlen (zvm->target);
	inPlist = malloc (lInPlist);
	if (inPlist != NULL) {
		inPlist->lPlist = lInPlist - sizeof (inPlist->lPlist);
		inPlist->lFName = sizeof (inPlist->fName);
		memcpy (inPlist->fName, Image_Status_Query,
			sizeof (inPlist->fName));
		inPlist->lUser = inPlist->lPass = 0;
		inPlist->lTarget = strlen (zvm->target);
		memcpy (inPlist->target, zvm->target, inPlist->lTarget);
		if ((rc =
		     zvm_smapi_send (zvm, inPlist, &reqId, lInPlist)) != -1) {
			if ((rc = zvm_smapi_recv (zvm, &pOut, &lRsp)) != -1) {
				outPlist = pOut;
				if (outPlist->hdr.rc == 0) {
					if (outPlist->hdr.reason == 0) {
						syslog (LOG_INFO,
							"Node %s is active",
							zvm->target);
						rc = 0;
					}
					else {
						syslog (LOG_INFO,
							"Node %s is inactive",
							zvm->target);
						rc = 2;
					}
				}
				else {
					rc = 1;
					zvm->reason = outPlist->hdr.reason;
					(void) zvm_smapi_reportError (inPlist,
								      outPlist);
				}
			}
		}
		free (inPlist);
		free (outPlist);
	}
	else {
		syslog (LOG_ERR, "%s - cannot allocate parameter list",
			__func__);
		rc = -1;
	}
	return (rc);
}

/**
 * zvm_smapi_send:
 * @zvm: z/VM driver information
 * @reqid: Returned request id
 * @req: Request parameter list
 * @lSend: Length of request
 *
 * Send a request to the SMAPI server and retrieve the request id
 */
int
zvm_smapi_send (zvm_driver_t * zvm, void *req, uint32_t * reqId,
		int32_t lSend)
{
	int rc, nFds;
	fd_set readFds;
	struct timeval timeout;

	timeout.tv_sec = 30;
	timeout.tv_usec = 0;
	zvm->reason = -1;
	if ((rc = zvm_smapi_open (zvm)) == 0) {
		rc = send (zvm->sd, req, lSend, 0);
		if (rc != -1) {
			FD_ZERO (&readFds);
			FD_SET (zvm->sd, &readFds);
			nFds = zvm->sd + 1;

			if ((rc =
			     select (nFds, &readFds, NULL, NULL,
				     &timeout)) != -1) {
				/*
				 * Get request ID
				 */
				rc = recv (zvm->sd, reqId, sizeof (*reqId),
					   0);
				if (rc == -1)
					syslog (LOG_ERR,
						"Error receiving from SMAPI - %m");
			}
		}
		else
			syslog (LOG_ERR, "Error sending to SMAPI - %m");
	}
	return (rc);
}

/**
 * zvm_smapi_recv:
 * @zvm: z/VM driver information
 * @req: Returned response parameter list
 * @lRsp: Length of response
 *
 * Receive a response from the SMAPI server
 */
int
zvm_smapi_recv (zvm_driver_t * zvm, void **rsp, int32_t * lRsp)
{
	int rc, lRem = 0, nFds;
	void *pRecv = rsp;
	fd_set readFds;
	smapiOutHeader_t *out;
	struct timeval timeout;

	timeout.tv_sec = 30;
	timeout.tv_usec = 0;
	FD_ZERO (&readFds);
	FD_SET (zvm->sd, &readFds);
	nFds = zvm->sd + 1;

	zvm->reason = -1;
	if ((rc = select (nFds, &readFds, NULL, NULL, &timeout)) != -1) {
		/*
		 * Get response length
		 */
		if ((rc = recv (zvm->sd, lRsp, sizeof (*lRsp), 0)) != -1) {
			lRem = *lRsp;
			if (*rsp == NULL)
				*rsp = malloc (*lRsp + sizeof (out->outLen));
			out = *rsp;
			out->outLen = *lRsp;
			pRecv = &out->reqId;
			while (lRem > 0) {
				if ((rc =
				     recv (zvm->sd, pRecv, lRem, 0)) != -1) {
					lRem -= rc;
					pRecv = (void *) ((uintptr_t) pRecv +
							  rc);
				}
				else
					syslog (LOG_ERR,
						"Error receiving from SMAPI - %m");
				(void) zvm_smapi_close (zvm);
				return (rc);
			}
			zvm->reason = out->reason;
		}
	}
	else
		syslog (LOG_ERR, "Error receiving from SMAPI - %m");

	(void) zvm_smapi_close (zvm);

	return (rc);
}

/**
 * zvm_smapi_close:
 * @zvm: z/VM driver information
 *
 * Close a connection with the z/VM SMAPI server
 */
int
zvm_smapi_close (zvm_driver_t * zvm)
{
	close (zvm->sd);
	return (0);
}

/**
 * zvm_smapi_reportError
 * @inHdr - Input parameter list header
 * @outHdr - Output parameter list header
 *
 * Report an error from the SMAPI server
 */
static int
zvm_smapi_reportError (void *inHdr, void *oHdr)
{
	struct _inParm
	{
		int32_t lPlist;
		int32_t lFName;
		char fName[0];
	} *inParm = inHdr;
	smapiOutHeader_t *outHdr = oHdr;
	char fName[64];

	memset (fName, 0, sizeof (fName));
	memcpy (fName, inParm->fName, inParm->lFName);
	syslog (LOG_ERR, "%s - returned (%d,%d)",
		fName, outHdr->rc, outHdr->reason);
	return (-1);
}


/**
 * trim - Trim spaces from string
 * @str - Pointer to string
 *
 */
static int
trim (char *str)
{
	char *p;
	int len;

	if (!str)
		return (0);

	len = strlen (str);

	while (len--) {
		if (isspace (str[len])) {
			str[len] = 0;
		}
		else {
			break;
		}
	}

	for (p = str; *p && isspace (*p); p++);

	memmove (str, p, strlen (p) + 1);

	return (strlen (str));
}

/**
 * zvm_metadata - Show fence metadata 
 * @self - Path to this executable
 *
 */
static int
zvm_metadata ()
{
	fprintf (stdout, "<?xml version=\"1.0\" ?>\n");
	fprintf (stdout, "<resource-agent name=\"fence_zvm\"");
	fprintf (stdout,
		 " shortdesc=\"Fence agent for use with z/VM Virtual Machines\">\n");
	fprintf (stdout, "<longdesc>");
	fprintf (stdout,
		 "The fence_zvm agent is intended to be used with with z/VM SMAPI service.");
	fprintf (stdout, "</longdesc>\n");
	fprintf (stdout, "<vendor-url>http://www.ibm.com</vendor-url>\n");

	fprintf (stdout, "<parameters>\n");

	fprintf (stdout,
		 "\t<parameter name=\"port\" unique=\"1\" required=\"1\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"-n, --plug\" />\n");
	fprintf (stdout, "\t\t<content type=\"string\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Name of the Virtual Machine to be fenced");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout,
		 "\t<parameter name=\"ipaddr\" unique=\"1\" required=\"1\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"-a, --ip\" />\n");
	fprintf (stdout, "\t\t<content type=\"string\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Name of the SMAPI IUCV Server Virtual Machine");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout,
		 "\t<parameter name=\"zvmsys\" unique=\"1\" required=\"0\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"--zvmsys\" />\n");
	fprintf (stdout, "\t\t<content type=\"string\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Node of the SMAPI IUCV Server Virtual Machine");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout,
		 "\t<parameter name=\"action\" unique=\"1\" required=\"0\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"-o, --action\" />\n");
	fprintf (stdout, "\t\t<content type=\"string\" default=\"off\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Fencing action");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout,
		 "\t<parameter name=\"delay\" unique=\"1\" required=\"0\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"--delay\" />\n");
	fprintf (stdout, "\t\t<content type=\"string\" default=\"0\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Time to delay fencing action in seconds");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout,
		 "\t<parameter name=\"usage\" unique=\"1\" required=\"0\">\n");
	fprintf (stdout, "\t\t<getopt mixed=\"-h, --help\" />\n");
	fprintf (stdout, "\t\t<content type=\"boolean\" />\n");
	fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
		 "Print usage");
	fprintf (stdout, "\t</parameter>\n");

	fprintf (stdout, "</parameters>\n");

	fprintf (stdout, "<actions>\n");
	fprintf (stdout, "\t<action name=\"off\" />\n");
	fprintf (stdout, "\t<action name=\"on\" automatic=\"0\" />\n");
	fprintf (stdout, "\t<action name=\"list\" />\n");
	fprintf (stdout, "\t<action name=\"metadata\" />\n");
	fprintf (stdout, "\t<action name=\"monitor\" />\n");
	fprintf (stdout, "\t<action name=\"reboot\" />\n");
	fprintf (stdout, "\t<action name=\"status\" />\n");
	fprintf (stdout, "</actions>\n");

	fprintf (stdout, "</resource-agent>\n");

	return (0);

}

/**
 * get_options_stdin - get options from stdin
 * @zvm - Pointer to driver information
 *
 */
static int
get_options_stdin (zvm_driver_t * zvm)
{
	char buf[1024], *endPtr, *opt, *arg;
	int32_t lSrvName, lSrvNode, lTarget;
	int fence = ACT_OFFON;

	while (fgets (buf, sizeof (buf), stdin) != 0) {
		if (trim (buf) == 0) {
			continue;
		}
		if (buf[0] == '#') {
			continue;
		}

		opt = buf;

		if ((arg = strchr (opt, '=')) != 0) {
			*arg = 0;
			arg++;
		}
		else {
			continue;
		}

		if (trim (arg) == 0)
			continue;

		if (!strcasecmp (opt, "action")) {
			if (strcasecmp (arg, "reboot") == 0) {
				fence = ACT_OFFON;
			}
			else if (strcasecmp (arg, "off") == 0) {
				fence = ACT_OFF;
			}
			else if (strcasecmp (arg, "on") == 0) {
				fence = ACT_ON;
			}
			else if (strcasecmp (arg, "metadata") == 0) {
				fence = ACT_METADATA;
			}
			else if (strcasecmp (arg, "status") == 0) {
				fence = ACT_STATUS;
			}
			else if (strcasecmp (arg, "monitor") == 0) {
				fence = ACT_MONITOR;
			}
			else if (strcasecmp (arg, "list") == 0) {
				fence = ACT_LIST;
			}
			else {
				fence = ACT_HELP;
			}
		}
		else if (!strcasecmp (opt, "ipaddr")) {
			lSrvName = MIN (strlen (arg), sizeof (zvm->smapiSrv));
			memcpy (zvm->smapiSrv, arg, lSrvName);
			continue;
		}
		else if (!strcasecmp (opt, "port")) {
			lTarget =
				MIN (strlen (arg), sizeof (zvm->target) - 1);
			strncpy (zvm->target, arg, lTarget);
			continue;
		}
		else if (!strcasecmp (opt, "timeout")) {
			zvm->timeOut = strtoul (arg, &endPtr, 10);
			if (*endPtr != 0) {
				syslog (LOG_WARNING,
					"Invalid timeout value specified %s "
					"defaulting to %d", arg,
					DEFAULT_DELAY);
				zvm->timeOut = DEFAULT_DELAY;
			}
		}
		else if (!strcasecmp (opt, "zvmsys")) {
			lSrvNode = MIN (strlen (arg), sizeof (zvm->node));
			memcpy (zvm->node, arg, lSrvNode);
			continue;
		}
		else if (!strcasecmp (opt, "delay")) {
			zvm->delay = strtoul (arg, &endPtr, 10);
			if (*endPtr != 0) {
				syslog (LOG_WARNING,
					"Invalid delay value specified %s "
					"defaulting to %d", arg,
					DEFAULT_DELAY);
				zvm->delay = DEFAULT_DELAY;
			}
		}
		else if (!strcasecmp (opt, "help")) {
			fence = ACT_HELP;
		}
	}
	return (fence);
}

/**
 * get_options - get options from the command line
 * @argc - Count of arguments
 * @argv - Array of character strings
 * @zvm - Pointer to driver information
 *
 */
static int
get_options (int argc, char **argv, zvm_driver_t * zvm)
{
	int c, fence = ACT_OFFON;
	int32_t lSrvName, lSrvNode, lTarget;
	char *endPtr;

	while ((c =
		getopt_long (argc, argv, optString, longopts, NULL)) != -1) {
		switch (c) {
		case 'n':
			lTarget = MIN (strlen (optarg), sizeof (zvm->target));
			memcpy (zvm->target, optarg, lTarget);
			break;
		case 'o':
			if (strcasecmp (optarg, "reboot") == 0) {
				fence = ACT_OFFON;
			}
			else if (strcasecmp (optarg, "off") == 0) {
				fence = ACT_OFF;
			}
			else if (strcasecmp (optarg, "on") == 0) {
				fence = ACT_ON;
			}
			else if (strcasecmp (optarg, "metadata") == 0) {
				fence = ACT_METADATA;
			}
			else if (strcasecmp (optarg, "status") == 0) {
				fence = ACT_STATUS;
			}
			else if (strcasecmp (optarg, "monitor") == 0) {
				fence = ACT_MONITOR;
			}
			else if (strcasecmp (optarg, "list") == 0) {
				fence = ACT_LIST;
			}
			else {
				fence = ACT_HELP;
			}
			break;
		case 'a':
			lSrvName =
				MIN (strlen (optarg), sizeof (zvm->smapiSrv));
			memcpy (zvm->smapiSrv, optarg, lSrvName);
			break;
		case 'T':
			zvm->timeOut = strtoul (optarg, &endPtr, 10);
			if (*endPtr != 0) {
				syslog (LOG_WARNING,
					"Invalid timeout value specified: %s - "
					"defaulting to %d", optarg,
					DEFAULT_TIMEOUT);
				zvm->timeOut = DEFAULT_TIMEOUT;
			}
			break;
		case 'd':
			zvm->delay = strtoul (optarg, &endPtr, 10);
			if (*endPtr != 0) {
				syslog (LOG_WARNING,
					"Invalid delay value specified: %s - "
					"defaulting to %d", optarg,
					DEFAULT_DELAY);
				zvm->delay = DEFAULT_DELAY;
			}
			break;
		case 'z':
			lSrvNode = MIN (strlen (optarg), sizeof (zvm->node));
			memcpy (zvm->node, optarg, lSrvNode);
			break;
		default:
			fence = ACT_HELP;
		}
	}
	return (fence);
}

/**
 * usage - display command syntax and parameters
 *
 */
static int
usage ()
{
	printf ("Usage: fence_zvm [options]\n\n"
		"\tWhere [options] =\n"
		"\t-o --action [action] - \"off\", \"on\", \"list\", \"metadata\", "
		"\"monitor\", \"reboot\", \"status\"\n"
		"\t--delay [seconds]    - Time to delay fencing action in seconds\n"
		"\t-n --plug [target]   - Name of virtual machine to fence\n"
		"\t-a --ip [server]     - Name of SMAPI IUCV Request server\n"
		"\t-T --timeout [secs]  - Time to wait for fence in seconds - currently ignored\n"
		"\t--zvmsys [node]      - z/VM Node on which SMAPI server lives\n"
		"\t-h --help            - Display this usage information\n");
	return (0);
}

/**
 * check_param - Check that mandatory parameters have been specified
 * @zvm - Pointer to driver information
 *
 */
static int
check_parm (zvm_driver_t * zvm)
{
	int rc;

	if (zvm->smapiSrv[0] != 0) {
		if (zvm->target[0] != 0) {
			rc = 0;
		}
		else {
			syslog (LOG_ERR, "Missing fence target name");
			rc = 2;
		}
	}
	else {
		syslog (LOG_ERR, "Missing SMAPI server name");
		rc = 1;
	}
	return (rc);
}

int
main (int argc, char **argv)
{
	zvm_driver_t zvm;
	int fence, rc = 0;

	openlog ("fence_zvm", LOG_CONS | LOG_PID, LOG_DAEMON);
	memset (&zvm, 0, sizeof (zvm));
	zvm.timeOut = DEFAULT_TIMEOUT;
	zvm.delay = DEFAULT_DELAY;

	if (argc > 1)
		fence = get_options (argc, argv, &zvm);
	else
		fence = get_options_stdin (&zvm);

	switch (fence) {
	case ACT_OFFON:	// OFFON
		if ((rc = check_parm (&zvm)) == 0)
			rc = zvm_smapi_imageRecycle (&zvm);
		break;
	case ACT_OFF:		// OFF
		if ((rc = check_parm (&zvm)) == 0)
			rc = zvm_smapi_imageDeactivate (&zvm);
		break;
	case ACT_ON:		// ON
		if ((rc = check_parm (&zvm)) == 0)
			rc = zvm_smapi_imageActivate (&zvm);
		break;
	case ACT_METADATA:	// METADATA
		rc = zvm_metadata ();
		break;
	case ACT_STATUS:	// STATUS
		if ((rc = check_parm (&zvm)) == 0)
			rc = zvm_smapi_imageQuery (&zvm);
		break;
	case ACT_MONITOR:	// MONITOR
		rc = 0;
		break;
	case ACT_LIST:		// LIST
		printf ("N/A");
		rc = 0;
		break;
	case ACT_HELP:
		rc = usage ();
	}
	closelog ();
	return (rc);
}
#else
#include <syslog.h>
int
main (int argc, char **argv)
{
	openlog ("fence_zvm", LOG_CONS | LOG_PID, LOG_DAEMON);
	syslog (LOG_ERR,
		"Fencing of a z/VM agent is not possible on this platform\n");
	closelog ();
	return (-1);
}
#endif