summaryrefslogtreecommitdiffstats
path: root/support/include/ha-callout.h
blob: 11643361837af130dff1f14b5bf550908e6784a1 (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
/*
 * support/include/ha-callout.h
 *
 * High Availability NFS Callout support routines
 *
 * Copyright (c) 2004, Paul Clements, SteelEye Technology
 *
 * In order to implement HA NFS, we need several callouts at key
 * points in statd and mountd. These callouts all come to ha_callout(),
 * which, in turn, calls out to an ha-callout script (not part of nfs-utils;
 * defined by -H argument to rpc.statd and rpc.mountd).
 */
#ifndef HA_CALLOUT_H
#define HA_CALLOUT_H

#include <sys/wait.h>
#include <signal.h>

extern char *ha_callout_prog;

static inline void
ha_callout(char *event, char *arg1, char *arg2, int arg3)
{
	char buf[16]; /* should be plenty */
	pid_t pid;
	int ret = -1;
	struct sigaction oldact, newact;

	if (!ha_callout_prog) /* HA callout is not enabled */
		return;

	sprintf(buf, "%d", arg3);

	/* many daemons ignore SIGCHLD as tcpwrappers will
	 * fork a child to do logging.  We need to wait
	 * for a child here, so we need to un-ignore
	 * SIGCHLD temporarily
	 */
	newact.sa_handler = SIG_DFL;
	newact.sa_flags = 0;
	sigemptyset(&newact.sa_mask);
	sigaction(SIGCHLD, &newact, &oldact);
	pid = fork();
	switch (pid) {
		case 0: execl(ha_callout_prog, ha_callout_prog,
				event, arg1, arg2, 
			      arg3 < 0 ? NULL : buf,
			      NULL);
			perror("execl");
			exit(2);
		case -1: perror("fork");
			break;
		default: pid = waitpid(pid, &ret, 0);
  	}
	sigaction(SIGCHLD, &oldact, &newact);
	xlog(D_GENERAL, "ha callout returned %d\n", WEXITSTATUS(ret));
}

#endif