summaryrefslogtreecommitdiff
path: root/usr/src/test/zfs-tests/tests/functional/resilver/sysevent.c
blob: 62a8b2faa23dee5dd2f7cb6259b75a9e5aa135e8 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at http://smartos.org/CDDL
 *
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file.
 *
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2020 Joyent, Inc.
 *
 */

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <libsysevent.h>
#include <sys/sysevent/eventdefs.h>

FILE *out;

static void
process_event(sysevent_t *ev)
{
	char *class = NULL;
	char *subclass = NULL;

	/* get sysevent metadata and add to the nvlist */
	class = sysevent_get_class_name(ev);
	subclass = sysevent_get_subclass_name(ev);

	if (class == NULL || subclass == NULL)
		errx(EXIT_FAILURE, "failed to retrieve sysevent metadata");

	VERIFY0(strcmp(class, EC_ZFS));

	flockfile(out);
	(void) fprintf(out, "Received %s.%s event\n", class, subclass);
	(void) fflush(out);
	funlockfile(out);
}

static void
child_fatal(int fd, const char *msg, ...)
{
	va_list ap;
	int fail = EXIT_FAILURE;

	va_start(ap, msg);
	(void) vfprintf(stderr, msg, ap);
	va_end(ap);
	(void) fputc('\n', stderr);

	(void) write(fd, &fail, sizeof (fail));
	(void) close(fd);
	exit(EXIT_FAILURE);
}

static void
do_child(int fd, char * const subclasses[], size_t n)
{
	sysevent_handle_t *handle;
	int ret = 0;

	if ((handle = sysevent_bind_handle(process_event)) == NULL) {
		child_fatal(fd, "sysevent_bind_handle() failed: %s",
		    strerror(errno));
	}

	if (sysevent_subscribe_event(handle, EC_ZFS,
	    (const char **)subclasses, n) != 0) {
		child_fatal(fd, "failed to subscribe to sysevents: %s",
		    strerror(errno));
	}

	(void) write(fd, &ret, sizeof (ret));
	(void) close(fd);

	/* leave stderr open so any errors get captured by test harness */
	(void) fclose(stdin);
	(void) fclose(stdout);

	for (;;)
		(void) pause();
}

static void
usage(const char *name)
{
	(void) fprintf(stderr, "Usage: %s [-o outfile] zfs_event...\n", name);
	exit(2);
}

int
main(int argc, char * const argv[])
{
	const char *outfile = NULL;
	pid_t child;
	int fds[2];
	int ret = 0;
	int c;

	while ((c = getopt(argc, argv, "o:")) != -1) {
		switch (c) {
		case 'o':
			outfile = optarg;
			break;
		case '?':
			(void) fprintf(stderr, "Invalid option -%c\n", optopt);
			usage(argv[0]);
		}
	}

	if (outfile != NULL) {
		if ((out = fopen(optarg, "w")) == NULL)
			err(EXIT_FAILURE, "unable to open %s", optarg);
	} else {
		out = stdout;
	}

	VERIFY0(pipe(fds));

	switch (child = fork()) {
	case -1:
		err(EXIT_FAILURE, "unable to fork");
	case 0:
		do_child(fds[1], argv + optind, (size_t)(argc - optind));
		break;
	default:
		break;
	}

	(void) close(fds[1]);

	if (read(fds[0], &ret, sizeof (ret)) < 0)
		err(EXIT_FAILURE, "failure waiting on child");

	if (ret != 0)
		return (ret);

	(void) close(fds[0]);
	(void) printf("%d\n", child);
	return (0);
}