1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_paths.c,v 1.16 2015/05/07 06:23:23 pgoyette Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_paths.c,v 1.16 2015/05/07 06:23:23 pgoyette Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <sys/param.h>
3511be35a1SLionel Sambuc #include <sys/stat.h>
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc #include <errno.h>
3811be35a1SLionel Sambuc #include <fcntl.h>
3911be35a1SLionel Sambuc #include <paths.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <string.h>
4211be35a1SLionel Sambuc #include <unistd.h>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #include <atf-c.h>
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc #define PATH_DEV __BIT(0) /* A device node */
4711be35a1SLionel Sambuc #define PATH_DIR __BIT(1) /* A directory */
4811be35a1SLionel Sambuc #define PATH_FILE __BIT(2) /* A file */
4911be35a1SLionel Sambuc #define PATH_ROOT __BIT(3) /* Access for root only */
50*0a6a1f1dSLionel Sambuc #define PATH_OPT __BIT(3) /* Optional, ENODEV if not supported */
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc static const struct {
5311be35a1SLionel Sambuc const char *path;
5411be35a1SLionel Sambuc int flags;
5511be35a1SLionel Sambuc } paths[] = {
5611be35a1SLionel Sambuc { _PATH_AUDIO, PATH_DEV },
5711be35a1SLionel Sambuc { _PATH_AUDIO0, PATH_DEV },
5811be35a1SLionel Sambuc { _PATH_AUDIOCTL, PATH_DEV },
5911be35a1SLionel Sambuc { _PATH_AUDIOCTL0, PATH_DEV },
6011be35a1SLionel Sambuc { _PATH_BPF, PATH_DEV | PATH_ROOT },
6111be35a1SLionel Sambuc { _PATH_CLOCKCTL, PATH_DEV | PATH_ROOT },
6211be35a1SLionel Sambuc { _PATH_CONSOLE, PATH_DEV | PATH_ROOT },
6311be35a1SLionel Sambuc { _PATH_CONSTTY, PATH_DEV | PATH_ROOT },
6411be35a1SLionel Sambuc { _PATH_CPUCTL, PATH_DEV },
6511be35a1SLionel Sambuc { _PATH_CSMAPPER, PATH_DIR },
6611be35a1SLionel Sambuc { _PATH_DEFTAPE, PATH_DEV | PATH_ROOT },
6711be35a1SLionel Sambuc { _PATH_DEVCDB, PATH_FILE },
6811be35a1SLionel Sambuc { _PATH_DEVDB, PATH_FILE },
6911be35a1SLionel Sambuc { _PATH_DEVNULL, PATH_DEV },
7011be35a1SLionel Sambuc { _PATH_DRUM, PATH_DEV | PATH_ROOT },
7111be35a1SLionel Sambuc { _PATH_ESDB, PATH_DIR },
7211be35a1SLionel Sambuc { _PATH_FTPUSERS, PATH_FILE },
7311be35a1SLionel Sambuc { _PATH_GETTYTAB, PATH_FILE },
7411be35a1SLionel Sambuc { _PATH_I18NMODULE, PATH_DIR },
7511be35a1SLionel Sambuc { _PATH_ICONV, PATH_DIR },
7611be35a1SLionel Sambuc { _PATH_KMEM, PATH_DEV | PATH_ROOT },
7711be35a1SLionel Sambuc { _PATH_KSYMS, PATH_DEV },
7811be35a1SLionel Sambuc { _PATH_KVMDB, PATH_FILE },
7911be35a1SLionel Sambuc { _PATH_LOCALE, PATH_DIR },
8011be35a1SLionel Sambuc { _PATH_MAILDIR, PATH_DIR },
8111be35a1SLionel Sambuc { _PATH_MAN, PATH_DIR },
8211be35a1SLionel Sambuc { _PATH_MEM, PATH_DEV | PATH_ROOT },
8311be35a1SLionel Sambuc { _PATH_MIXER, PATH_DEV },
8411be35a1SLionel Sambuc { _PATH_MIXER0, PATH_DEV },
8511be35a1SLionel Sambuc { _PATH_NOLOGIN, PATH_FILE },
86*0a6a1f1dSLionel Sambuc { _PATH_POWER, PATH_DEV | PATH_ROOT | PATH_OPT },
8711be35a1SLionel Sambuc { _PATH_PRINTCAP, PATH_FILE },
8811be35a1SLionel Sambuc { _PATH_PUD, PATH_DEV | PATH_ROOT },
8911be35a1SLionel Sambuc { _PATH_PUFFS, PATH_DEV | PATH_ROOT },
9011be35a1SLionel Sambuc { _PATH_RANDOM, PATH_DEV },
9111be35a1SLionel Sambuc { _PATH_SENDMAIL, PATH_FILE },
9211be35a1SLionel Sambuc { _PATH_SHELLS, PATH_FILE },
9311be35a1SLionel Sambuc { _PATH_SKEYKEYS, PATH_FILE | PATH_ROOT },
9411be35a1SLionel Sambuc { _PATH_SOUND, PATH_DEV },
9511be35a1SLionel Sambuc { _PATH_SOUND0, PATH_DEV },
96*0a6a1f1dSLionel Sambuc { _PATH_SYSMON, PATH_DEV | PATH_OPT },
9711be35a1SLionel Sambuc { _PATH_TTY, PATH_DEV },
9811be35a1SLionel Sambuc { _PATH_UNIX, PATH_FILE | PATH_ROOT },
9911be35a1SLionel Sambuc { _PATH_URANDOM, PATH_DEV },
10011be35a1SLionel Sambuc { _PATH_VIDEO, PATH_DEV },
10111be35a1SLionel Sambuc { _PATH_VIDEO0, PATH_DEV },
102*0a6a1f1dSLionel Sambuc { _PATH_WATCHDOG, PATH_DEV | PATH_OPT },
10311be35a1SLionel Sambuc
10411be35a1SLionel Sambuc { _PATH_DEV, PATH_DIR },
10511be35a1SLionel Sambuc { _PATH_DEV_PTS, PATH_DIR },
10611be35a1SLionel Sambuc { _PATH_EMUL_AOUT, PATH_DIR },
10711be35a1SLionel Sambuc { _PATH_TMP, PATH_DIR },
10811be35a1SLionel Sambuc { _PATH_VARDB, PATH_DIR },
10911be35a1SLionel Sambuc { _PATH_VARRUN, PATH_DIR },
11011be35a1SLionel Sambuc { _PATH_VARTMP, PATH_DIR },
11111be35a1SLionel Sambuc
11211be35a1SLionel Sambuc { _PATH_BSHELL, PATH_FILE },
11311be35a1SLionel Sambuc { _PATH_CSHELL, PATH_FILE },
11411be35a1SLionel Sambuc { _PATH_VI, PATH_FILE },
11511be35a1SLionel Sambuc };
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambuc ATF_TC(paths);
ATF_TC_HEAD(paths,tc)11811be35a1SLionel Sambuc ATF_TC_HEAD(paths, tc)
11911be35a1SLionel Sambuc {
12011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>");
12111be35a1SLionel Sambuc }
12211be35a1SLionel Sambuc
ATF_TC_BODY(paths,tc)12311be35a1SLionel Sambuc ATF_TC_BODY(paths, tc)
12411be35a1SLionel Sambuc {
12511be35a1SLionel Sambuc struct stat st;
12611be35a1SLionel Sambuc uid_t uid;
12711be35a1SLionel Sambuc mode_t m;
12811be35a1SLionel Sambuc size_t i;
12911be35a1SLionel Sambuc int fd;
13011be35a1SLionel Sambuc
131*0a6a1f1dSLionel Sambuc #if defined(__sparc__)
13211be35a1SLionel Sambuc atf_tc_skip("PR port-sparc/45580");
133*0a6a1f1dSLionel Sambuc #endif
13411be35a1SLionel Sambuc
13511be35a1SLionel Sambuc uid = getuid();
13611be35a1SLionel Sambuc
13711be35a1SLionel Sambuc for (i = 0; i < __arraycount(paths); i++) {
13811be35a1SLionel Sambuc
13911be35a1SLionel Sambuc (void)fprintf(stderr, "testing '%s'\n", paths[i].path);
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc errno = 0;
14211be35a1SLionel Sambuc fd = open(paths[i].path, O_RDONLY);
14311be35a1SLionel Sambuc
14411be35a1SLionel Sambuc if (fd < 0) {
14511be35a1SLionel Sambuc
14611be35a1SLionel Sambuc switch (errno) {
14711be35a1SLionel Sambuc
148*0a6a1f1dSLionel Sambuc case ENODEV:
149*0a6a1f1dSLionel Sambuc if ((paths[i].flags & PATH_OPT) == 0) {
150*0a6a1f1dSLionel Sambuc
151*0a6a1f1dSLionel Sambuc atf_tc_fail("Required path %s does "
152*0a6a1f1dSLionel Sambuc "not exist", paths[i].path);
153*0a6a1f1dSLionel Sambuc }
154*0a6a1f1dSLionel Sambuc break;
155*0a6a1f1dSLionel Sambuc
15611be35a1SLionel Sambuc case EPERM: /* FALLTHROUGH */
15711be35a1SLionel Sambuc case EACCES: /* FALLTHROUGH */
15811be35a1SLionel Sambuc
15911be35a1SLionel Sambuc if ((paths[i].flags & PATH_ROOT) == 0) {
16011be35a1SLionel Sambuc
161*0a6a1f1dSLionel Sambuc atf_tc_fail("UID %u failed to open %s, "
162*0a6a1f1dSLionel Sambuc "error %d", (uint32_t)uid,
163*0a6a1f1dSLionel Sambuc paths[i].path, errno);
16411be35a1SLionel Sambuc }
16511be35a1SLionel Sambuc
16611be35a1SLionel Sambuc case EBUSY: /* FALLTHROUGH */
16711be35a1SLionel Sambuc case ENXIO: /* FALLTHROUGH */
16811be35a1SLionel Sambuc case ENOENT: /* FALLTHROUGH */
16911be35a1SLionel Sambuc
17011be35a1SLionel Sambuc default:
17111be35a1SLionel Sambuc continue;
17211be35a1SLionel Sambuc }
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc
17511be35a1SLionel Sambuc (void)memset(&st, 0, sizeof(struct stat));
17611be35a1SLionel Sambuc
17711be35a1SLionel Sambuc ATF_REQUIRE(fstat(fd, &st) == 0);
17811be35a1SLionel Sambuc
17911be35a1SLionel Sambuc m = st.st_mode;
18011be35a1SLionel Sambuc
18111be35a1SLionel Sambuc if ((paths[i].flags & PATH_DEV) != 0) {
18211be35a1SLionel Sambuc
18311be35a1SLionel Sambuc ATF_CHECK(S_ISBLK(m) != 0 || S_ISCHR(m) != 0);
18411be35a1SLionel Sambuc
18511be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_DIR) == 0);
18611be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_FILE) == 0);
18711be35a1SLionel Sambuc }
18811be35a1SLionel Sambuc
18911be35a1SLionel Sambuc if ((paths[i].flags & PATH_DIR) != 0) {
19011be35a1SLionel Sambuc
19111be35a1SLionel Sambuc ATF_CHECK(S_ISDIR(m) != 0);
19211be35a1SLionel Sambuc
19311be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_DEV) == 0);
19411be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_FILE) == 0);
19511be35a1SLionel Sambuc }
19611be35a1SLionel Sambuc
19711be35a1SLionel Sambuc if ((paths[i].flags & PATH_FILE) != 0) {
19811be35a1SLionel Sambuc
19911be35a1SLionel Sambuc ATF_CHECK(S_ISREG(m) != 0);
20011be35a1SLionel Sambuc
20111be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_DEV) == 0);
20211be35a1SLionel Sambuc ATF_CHECK((paths[i].flags & PATH_DIR) == 0);
20311be35a1SLionel Sambuc }
20411be35a1SLionel Sambuc
20511be35a1SLionel Sambuc ATF_REQUIRE(close(fd) == 0);
20611be35a1SLionel Sambuc }
20711be35a1SLionel Sambuc }
20811be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)20911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
21011be35a1SLionel Sambuc {
21111be35a1SLionel Sambuc
21211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, paths);
21311be35a1SLionel Sambuc
21411be35a1SLionel Sambuc return atf_no_error();
21511be35a1SLionel Sambuc }
216