xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/zed_fd_spill-zedlet.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*
2  * Permission to use, copy, modify, and/or distribute this software for
3  * any purpose with or without fee is hereby granted.
4  *
5  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
6  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
7  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
8  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
9  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
10  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
11  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12  */
13 
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 
23 int main(void) {
24 	if (fork()) {
25 		int err;
26 		wait(&err);
27 		return (err);
28 	}
29 
30 	char buf[64];
31 	sprintf(buf, "/tmp/zts-zed_fd_spill-logdir/%d", getppid());
32 	int fd = creat(buf, 0644);
33 	if (fd == -1) {
34 		(void) fprintf(stderr, "creat(%s) failed: %s\n", buf,
35 		    strerror(errno));
36 		exit(EXIT_FAILURE);
37 	}
38 
39 	if (dup2(fd, STDOUT_FILENO) == -1) {
40 		close(fd);
41 		(void) fprintf(stderr, "dup2(%s, STDOUT_FILENO) failed: %s\n",
42 		    buf, strerror(errno));
43 		exit(EXIT_FAILURE);
44 	}
45 
46 	snprintf(buf, sizeof (buf), "/proc/%d/fd", getppid());
47 	execlp("ls", "ls", buf, NULL);
48 	_exit(127);
49 }
50