1*ddba0402SEnji Cooper /* $NetBSD: t_mknodat.c,v 1.4 2017/01/10 15:15:09 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2012 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Emmanuel Dreyfus.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*ddba0402SEnji Cooper __RCSID("$NetBSD: t_mknodat.c,v 1.4 2017/01/10 15:15:09 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <atf-c.h>
3557718be8SEnji Cooper #include <errno.h>
3657718be8SEnji Cooper #include <fcntl.h>
3757718be8SEnji Cooper #include <limits.h>
3857718be8SEnji Cooper #include <paths.h>
3957718be8SEnji Cooper #include <stdio.h>
4057718be8SEnji Cooper #include <string.h>
4157718be8SEnji Cooper #include <unistd.h>
4257718be8SEnji Cooper #include <sys/param.h>
4357718be8SEnji Cooper #include <sys/stat.h>
4457718be8SEnji Cooper
4557718be8SEnji Cooper #define DIR "dir"
4657718be8SEnji Cooper #define FILE "dir/openat"
4757718be8SEnji Cooper #define BASEFILE "openat"
4857718be8SEnji Cooper #define FILEERR "dir/openaterr"
4957718be8SEnji Cooper
5057718be8SEnji Cooper static dev_t get_devnull(void);
5157718be8SEnji Cooper
5257718be8SEnji Cooper static dev_t
get_devnull(void)5357718be8SEnji Cooper get_devnull(void)
5457718be8SEnji Cooper {
5557718be8SEnji Cooper struct stat st;
5657718be8SEnji Cooper
5757718be8SEnji Cooper if (stat(_PATH_DEVNULL, &st) != 0)
5857718be8SEnji Cooper return NODEV;
5957718be8SEnji Cooper
6057718be8SEnji Cooper return st.st_rdev;
6157718be8SEnji Cooper }
6257718be8SEnji Cooper
6357718be8SEnji Cooper
6457718be8SEnji Cooper ATF_TC(mknodat_fd);
ATF_TC_HEAD(mknodat_fd,tc)6557718be8SEnji Cooper ATF_TC_HEAD(mknodat_fd, tc)
6657718be8SEnji Cooper {
6757718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "See that mknodat works with fd");
6857718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
6957718be8SEnji Cooper }
ATF_TC_BODY(mknodat_fd,tc)7057718be8SEnji Cooper ATF_TC_BODY(mknodat_fd, tc)
7157718be8SEnji Cooper {
7257718be8SEnji Cooper int dfd;
7357718be8SEnji Cooper int fd;
7457718be8SEnji Cooper dev_t dev;
7557718be8SEnji Cooper mode_t mode = S_IFCHR|0600;
7657718be8SEnji Cooper
7757718be8SEnji Cooper ATF_REQUIRE((dev = get_devnull()) != NODEV);
7857718be8SEnji Cooper ATF_REQUIRE(mkdir(DIR, 0755) == 0);
7957718be8SEnji Cooper ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
8057718be8SEnji Cooper ATF_REQUIRE((fd = mknodat(dfd, BASEFILE, mode, dev)) != -1);
8157718be8SEnji Cooper ATF_REQUIRE(close(fd) == 0);
8257718be8SEnji Cooper ATF_REQUIRE(access(FILE, F_OK) == 0);
835d37d9ccSEnji Cooper (void)close(dfd);
8457718be8SEnji Cooper }
8557718be8SEnji Cooper
8657718be8SEnji Cooper ATF_TC(mknodat_fdcwd);
ATF_TC_HEAD(mknodat_fdcwd,tc)8757718be8SEnji Cooper ATF_TC_HEAD(mknodat_fdcwd, tc)
8857718be8SEnji Cooper {
8957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
9057718be8SEnji Cooper "See that mknodat works with fd as AT_FDCWD");
9157718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
9257718be8SEnji Cooper }
ATF_TC_BODY(mknodat_fdcwd,tc)9357718be8SEnji Cooper ATF_TC_BODY(mknodat_fdcwd, tc)
9457718be8SEnji Cooper {
9557718be8SEnji Cooper int fd;
9657718be8SEnji Cooper dev_t dev;
9757718be8SEnji Cooper mode_t mode = S_IFCHR|0600;
9857718be8SEnji Cooper
9957718be8SEnji Cooper ATF_REQUIRE((dev = get_devnull()) != NODEV);
10057718be8SEnji Cooper ATF_REQUIRE(mkdir(DIR, 0755) == 0);
10157718be8SEnji Cooper ATF_REQUIRE((fd = mknodat(AT_FDCWD, FILE, mode, dev)) != -1);
10257718be8SEnji Cooper ATF_REQUIRE(close(fd) == 0);
10357718be8SEnji Cooper ATF_REQUIRE(access(FILE, F_OK) == 0);
10457718be8SEnji Cooper }
10557718be8SEnji Cooper
10657718be8SEnji Cooper ATF_TC(mknodat_fdcwderr);
ATF_TC_HEAD(mknodat_fdcwderr,tc)10757718be8SEnji Cooper ATF_TC_HEAD(mknodat_fdcwderr, tc)
10857718be8SEnji Cooper {
10957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
11057718be8SEnji Cooper "See that mknodat fails with fd as AT_FDCWD and bad path");
11157718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
11257718be8SEnji Cooper }
ATF_TC_BODY(mknodat_fdcwderr,tc)11357718be8SEnji Cooper ATF_TC_BODY(mknodat_fdcwderr, tc)
11457718be8SEnji Cooper {
11557718be8SEnji Cooper int fd;
11657718be8SEnji Cooper dev_t dev;
11757718be8SEnji Cooper mode_t mode = S_IFCHR|0600;
11857718be8SEnji Cooper
11957718be8SEnji Cooper ATF_REQUIRE((dev = get_devnull()) != NODEV);
12057718be8SEnji Cooper ATF_REQUIRE((fd = mknodat(AT_FDCWD, FILEERR, mode, dev)) == -1);
12157718be8SEnji Cooper }
12257718be8SEnji Cooper
12357718be8SEnji Cooper ATF_TC(mknodat_fderr);
ATF_TC_HEAD(mknodat_fderr,tc)12457718be8SEnji Cooper ATF_TC_HEAD(mknodat_fderr, tc)
12557718be8SEnji Cooper {
12657718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "See that mknodat fails with fd as -1");
12757718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
12857718be8SEnji Cooper }
ATF_TC_BODY(mknodat_fderr,tc)12957718be8SEnji Cooper ATF_TC_BODY(mknodat_fderr, tc)
13057718be8SEnji Cooper {
13157718be8SEnji Cooper int fd;
13257718be8SEnji Cooper dev_t dev;
13357718be8SEnji Cooper mode_t mode = S_IFCHR|0600;
13457718be8SEnji Cooper
13557718be8SEnji Cooper ATF_REQUIRE((dev = get_devnull()) != NODEV);
13657718be8SEnji Cooper ATF_REQUIRE(mkdir(DIR, 0755) == 0);
13757718be8SEnji Cooper ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
13857718be8SEnji Cooper ATF_REQUIRE(close(fd) == 0);
13957718be8SEnji Cooper ATF_REQUIRE((fd = mknodat(-1, FILE, mode, dev)) == -1);
14057718be8SEnji Cooper }
14157718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)14257718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
14357718be8SEnji Cooper {
14457718be8SEnji Cooper
14557718be8SEnji Cooper ATF_TP_ADD_TC(tp, mknodat_fd);
14657718be8SEnji Cooper ATF_TP_ADD_TC(tp, mknodat_fdcwd);
14757718be8SEnji Cooper ATF_TP_ADD_TC(tp, mknodat_fdcwderr);
14857718be8SEnji Cooper ATF_TP_ADD_TC(tp, mknodat_fderr);
14957718be8SEnji Cooper
15057718be8SEnji Cooper return atf_no_error();
15157718be8SEnji Cooper }
152