summaryrefslogtreecommitdiffstats
path: root/lib/stonith/meatclient.c
blob: e95dc0eac3aa467c016dff76044b3e787f75440c (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
/*
 * Stonith client for Human Operator Stonith device
 *
 * Copyright (c) 2001 Gregor Binder <gbinder@sysfive.com>
 *
 *   This program is a rewrite of the "do_meatware" program by
 *   David C. Teigland <teigland@sistina.com> originally appeared
 *   in the GFS stomith meatware agent.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <lha_internal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stonith/stonith.h>
#include <glib.h>

#define OPTIONS "c:w"

void usage(const char * cmd);

void
usage(const char * cmd)
{
	fprintf(stderr, "usage: %s -c node [-w]\n", cmd);
	exit(S_INVAL);
}

extern char *	optarg;
extern int	optind, opterr, optopt;
int
main(int argc, char** argv)
{
	char *		cmdname;
	const char *	meatpipe_pr = HA_VARRUNDIR "/meatware";	/* if you intend to
							 change this, modify
							 meatware.c as well */
	char *		opthost = NULL;
	int		clearhost = 0;

	int		c, argcount, waitmode = 0;
	int		errors = 0;

	if ((cmdname = strrchr(argv[0], '/')) == NULL) {
		cmdname = argv[0];
	}else{
		++cmdname;
	}

	while ((c = getopt(argc, argv, OPTIONS)) != -1) {
		switch(c) {
		case 'c':	opthost = optarg;
				++clearhost;
				break;
		case 'w':	++waitmode;
				break;
		default:	++errors;
				break;
		}
	}
	argcount = argc - optind;
	if (!(argcount == 0) || !opthost) {
		errors++;
	}

	if (errors) {
		usage(cmdname);
	}
	
	strdown(opthost);

	if (clearhost) {

		int rc, fd;
		char resp[3];

		char line[256];
		char meatpipe[256];

		gboolean waited=FALSE;

		snprintf(meatpipe, 256, "%s.%s", meatpipe_pr, opthost);

		while(1) {
			fd = open(meatpipe, O_WRONLY | O_NONBLOCK);
			if (fd >= 0)
				break;
			if (!waitmode || (errno != ENOENT && errno != ENXIO)) {
				if (waited) printf("\n");
				snprintf(line, sizeof(line)
				,	"Meatware_IPC failed: %s", meatpipe);
				perror(line);
				exit(S_BADHOST);
			}
			printf("."); fflush(stdout); waited=TRUE;
			sleep(1);
		}
		if (waited) printf("\n");

		printf("\nWARNING!\n\n"
			"If node \"%s\" has not been manually power-cycled or "
			"disconnected from all shared resources and networks, "
			"data on shared disks may become corrupted and "
			"migrated services might not work as expected.\n"
			"Please verify that the name or address above "
			"corresponds to the node you just rebooted.\n\n"
			"PROCEED? [yN] ", opthost);

		rc = scanf("%s", resp);

		if (rc == 0 || rc == EOF || tolower(resp[0] != 'y')) {
			printf("Meatware_client: operation canceled.\n");
			exit(S_INVAL);
		}

		sprintf(line, "meatware reply %s", opthost);

		rc = write(fd, line, 256);

		if (rc < 0) {
			sprintf(line, "Meatware_IPC failed: %s", meatpipe);
			perror(line);
			exit(S_OOPS);
		}
    
		printf("Meatware_client: reset confirmed.\n");
	}

	exit(S_OK);
}