xref: /netbsd-src/tests/kernel/t_fcntl.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: t_fcntl.c,v 1.2 2019/10/20 16:02:11 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <atf-c.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 ATF_TC(getpath);
42 ATF_TC_HEAD(getpath, tc)
43 {
44 
45 	atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH");
46 }
47 
48 static const struct {
49 	const char *name;
50 	int rv;
51 } files[] = {
52 	{ "/bin/ls", 0 },
53 	{ "/bin/sh", 0 },
54 	{ "/dev/zero", 0 },
55 	{ "/dev/null", 0 },
56 	{ "/sbin/chown", 0 },
57 	{ "/", ENOENT },
58 };
59 
60 ATF_TC_BODY(getpath, tc)
61 {
62 	char path[MAXPATHLEN];
63 	int fd, rv;
64 
65 	for (size_t i = 0; i < __arraycount(files); i++) {
66 		fd = open(files[i].name, O_RDONLY|O_NOFOLLOW);
67 		ATF_REQUIRE_MSG(fd != -1, "Cannot open `%s'", files[i].name);
68 		rv = fcntl(fd, F_GETPATH, path);
69 		if (files[i].rv) {
70 			ATF_REQUIRE_MSG(errno == files[i].rv,
71 			    "Unexpected error %d != %d for `%s'", errno,
72 			    files[i].rv, files[i].name);
73 		} else {
74 			ATF_REQUIRE_MSG(rv != -1,
75 			    "Can't get path for `%s' (%s)", files[i].name,
76 			    strerror(errno));
77 			ATF_REQUIRE_MSG(strcmp(files[i].name, path) == 0,
78 			    "Bad name `%s' != `%s'", path, files[i].name);
79 		close(fd);
80 		}
81 	}
82 }
83 
84 ATF_TP_ADD_TCS(tp)
85 {
86 	ATF_TP_ADD_TC(tp, getpath);
87 
88 	return atf_no_error();
89 }
90