File: null.c

package info (click to toggle)
fence-virt 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 772 kB
  • sloc: ansic: 10,257; cpp: 382; makefile: 272; sh: 214; lex: 113; yacc: 105
file content (212 lines) | stat: -rw-r--r-- 3,816 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
/*
  Copyright Red Hat, Inc. 2009

  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.
*/
/*
 * Author: Lon Hohberger <lhh at redhat.com>
 */

#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>

#include "simpleconfig.h"
#include "static_map.h"
#include "server_plugin.h"

#define NAME "null"
#define NULL_VERSION "0.8"

#define MAGIC 0x1e00017a

struct null_info {
	int magic;
	int pad;
	char *message;
};

#define VALIDATE(arg) \
do {\
	if (!arg || ((struct null_info *)arg)->magic != MAGIC) { \
		errno = EINVAL;\
		return -1; \
	} \
} while(0)


static int
null_null(const char *vm_name, void *priv)
{
	VALIDATE(priv);
	printf("[Null] Null operation on %s\n", vm_name);

	return 1;
}


static int
null_off(const char *vm_name, const char *src, uint32_t seqno, void *priv)
{
	VALIDATE(priv);
	printf("[Null] OFF operation on %s\n", vm_name);

	return 1;
}


static int
null_on(const char *vm_name, const char *src, uint32_t seqno, void *priv)
{
	VALIDATE(priv);
	printf("[Null] ON operation on %s\n", vm_name);

	return 1;
}


static int
null_devstatus(void *priv)
{
	printf("[Null] Device status\n");
	VALIDATE(priv);
	printf("[Null] Message for you: %s\n",
	       ((struct null_info *)priv)->message);

	return 0;
}


static int
null_status(const char *vm_name, void *priv)
{
	VALIDATE(priv);
	printf("[Null] STATUS operation on %s\n", vm_name);

	return 1;
}


static int
null_reboot(const char *vm_name, const char *src, uint32_t seqno, void *priv)
{
	VALIDATE(priv);
	printf("[Null] REBOOT operation on %s\n", vm_name);

	return 1;
}


static int
null_hostlist(hostlist_callback callback, void *arg, void *priv)
{
	VALIDATE(priv);
	printf("[Null] HOSTLIST operation\n");

	return 1;
}


static int
null_init(backend_context_t *c, config_object_t *config)
{
	char value[256];
	struct null_info *info = NULL;
	char *null_message = NULL;

	info = malloc(sizeof(*info));
	if (!info)
		return -1;

	memset(info, 0, sizeof(*info));

	if (sc_get(config, "backends/null/@message",
		   value, sizeof(value)) != 0) {
		snprintf(value, sizeof(value), "Hi!");
	}

	null_message = strdup(value);
	if (!null_message) {
		free(info);
		return -1;
	}

	info->magic = MAGIC;
	info->message = null_message;

	*c = (void *)info;
	return 0;
}


static int
null_shutdown(backend_context_t c)
{
	struct null_info *info = (struct null_info *)c;

	VALIDATE(info);
	info->magic = 0;
	free(info->message);
	free(info);

	return 0;
}


static fence_callbacks_t null_callbacks = {
	.null = null_null,
	.off = null_off,
	.on = null_on,
	.reboot = null_reboot,
	.status = null_status,
	.devstatus = null_devstatus,
	.hostlist = null_hostlist
};

static backend_plugin_t null_plugin = {
	.name = NAME,
	.version = NULL_VERSION,
	.callbacks = &null_callbacks,
	.init = null_init,
	.cleanup = null_shutdown,
};


#ifdef _MODULE
double
BACKEND_VER_SYM(void)
{
	return PLUGIN_VERSION_BACKEND;
}

const backend_plugin_t *
BACKEND_INFO_SYM(void)
{
	return &null_plugin;
}
#else
static void __attribute__((constructor))
null_register_plugin(void)
{
	plugin_reg_backend(&null_plugin);
}
#endif