1 /* $OpenBSD: descrip.c,v 1.2 2021/09/27 18:10:24 bluhm Exp $ */
2 /*
3 * Copyright (c) 2018 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/types.h>
19 #include <sys/event.h>
20 #include <sys/time.h>
21 #include <sys/wait.h>
22
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <unistd.h>
27
28 int
main(int argc,char ** argv)29 main(int argc, char **argv)
30 {
31 int fd, kq, status;
32 pid_t pf, pw;
33
34 kq = kqueue();
35 assert(kq == 3);
36
37 fd = open("/etc/hosts", O_RDONLY);
38 assert(fd == 4);
39
40 pf = fork();
41 if (pf == 0) {
42 /*
43 * The existing kq fd should have been closed across fork,
44 * hence we expect fd 3 to be reallocated on this kqueue call.
45 */
46 kq = kqueue();
47 assert(kq == 3);
48
49 /*
50 * fd 4 should still be open and allocated across fork,
51 * hence opening another file should result in fd 5 being
52 * allocated.
53 */
54 fd = open("/etc/hosts", O_RDONLY);
55 assert(fd == 5);
56
57 _exit(0);
58 }
59 assert(pf > 0);
60
61 pw = wait(&status);
62 assert(pf == pw);
63 assert(status == 0);
64
65 return 0;
66 }
67