1214117Sjamie/* $NetBSD: t_paths.c,v 1.14 2014/11/04 00:20:19 justin Exp $ */ 2223190Sjamie 3214117Sjamie/*- 4214117Sjamie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5214117Sjamie * All rights reserved. 6214117Sjamie * 7214117Sjamie * This code is derived from software contributed to The NetBSD Foundation 8214117Sjamie * by Jukka Ruohonen. 9214117Sjamie * 10214117Sjamie * Redistribution and use in source and binary forms, with or without 11214117Sjamie * modification, are permitted provided that the following conditions 12214117Sjamie * are met: 13214117Sjamie * 1. Redistributions of source code must retain the above copyright 14214117Sjamie * notice, this list of conditions and the following disclaimer. 15214117Sjamie * 2. Redistributions in binary form must reproduce the above copyright 16214117Sjamie * notice, this list of conditions and the following disclaimer in the 17214117Sjamie * documentation and/or other materials provided with the distribution. 18214117Sjamie * 19214117Sjamie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20214117Sjamie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21214117Sjamie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22214117Sjamie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23214117Sjamie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24214117Sjamie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25214117Sjamie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26214117Sjamie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27214117Sjamie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28214117Sjamie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29214117Sjamie * POSSIBILITY OF SUCH DAMAGE. 30214117Sjamie */ 31214783Sjamie#include <sys/cdefs.h> 32214117Sjamie__RCSID("$NetBSD: t_paths.c,v 1.14 2014/11/04 00:20:19 justin Exp $"); 33214117Sjamie 34214117Sjamie#include <sys/param.h> 35214117Sjamie#include <sys/stat.h> 36214117Sjamie 37214117Sjamie#include <errno.h> 38214117Sjamie#include <fcntl.h> 39214117Sjamie#include <paths.h> 40214117Sjamie#include <stdio.h> 41214117Sjamie#include <string.h> 42214117Sjamie#include <unistd.h> 43214117Sjamie 44214117Sjamie#include <atf-c.h> 45214117Sjamie 46214117Sjamie#define PATH_DEV __BIT(0) /* A device node */ 47214117Sjamie#define PATH_DIR __BIT(1) /* A directory */ 48214117Sjamie#define PATH_FILE __BIT(2) /* A file */ 49214117Sjamie#define PATH_ROOT __BIT(3) /* Access for root only */ 50214117Sjamie 51214117Sjamiestatic const struct { 52214117Sjamie const char *path; 53214117Sjamie int flags; 54214117Sjamie} paths[] = { 55214117Sjamie { _PATH_AUDIO, PATH_DEV }, 56214117Sjamie { _PATH_AUDIO0, PATH_DEV }, 57214117Sjamie { _PATH_AUDIOCTL, PATH_DEV }, 58214117Sjamie { _PATH_AUDIOCTL0, PATH_DEV }, 59214117Sjamie { _PATH_BPF, PATH_DEV | PATH_ROOT }, 60214423Sjamie { _PATH_CLOCKCTL, PATH_DEV | PATH_ROOT }, 61214423Sjamie { _PATH_CONSOLE, PATH_DEV | PATH_ROOT }, 62214423Sjamie { _PATH_CONSTTY, PATH_DEV | PATH_ROOT }, 63214423Sjamie { _PATH_CPUCTL, PATH_DEV }, 64214423Sjamie { _PATH_CSMAPPER, PATH_DIR }, 65214423Sjamie { _PATH_DEFTAPE, PATH_DEV | PATH_ROOT }, 66214423Sjamie { _PATH_DEVCDB, PATH_FILE }, 67214423Sjamie { _PATH_DEVDB, PATH_FILE }, 68214423Sjamie { _PATH_DEVNULL, PATH_DEV }, 69214423Sjamie { _PATH_DRUM, PATH_DEV | PATH_ROOT }, 70214423Sjamie { _PATH_ESDB, PATH_DIR }, 71214423Sjamie { _PATH_FTPUSERS, PATH_FILE }, 72214423Sjamie { _PATH_GETTYTAB, PATH_FILE }, 73214423Sjamie { _PATH_I18NMODULE, PATH_DIR }, 74214423Sjamie { _PATH_ICONV, PATH_DIR }, 75214423Sjamie { _PATH_KMEM, PATH_DEV | PATH_ROOT }, 76214423Sjamie { _PATH_KSYMS, PATH_DEV }, 77214423Sjamie { _PATH_KVMDB, PATH_FILE }, 78214423Sjamie { _PATH_LOCALE, PATH_DIR }, 79214423Sjamie { _PATH_MAILDIR, PATH_DIR }, 80214423Sjamie { _PATH_MAN, PATH_DIR }, 81214423Sjamie { _PATH_MEM, PATH_DEV | PATH_ROOT }, 82214423Sjamie { _PATH_MIXER, PATH_DEV }, 83214423Sjamie { _PATH_MIXER0, PATH_DEV }, 84214423Sjamie { _PATH_NOLOGIN, PATH_FILE }, 85214423Sjamie { _PATH_POWER, PATH_DEV | PATH_ROOT }, 86214117Sjamie { _PATH_PRINTCAP, PATH_FILE }, 87214423Sjamie { _PATH_PUD, PATH_DEV | PATH_ROOT }, 88214117Sjamie { _PATH_PUFFS, PATH_DEV | PATH_ROOT }, 89214783Sjamie { _PATH_RANDOM, PATH_DEV }, 90223189Sjamie { _PATH_SENDMAIL, PATH_FILE }, 91214423Sjamie { _PATH_SHELLS, PATH_FILE }, 92214423Sjamie { _PATH_SKEYKEYS, PATH_FILE | PATH_ROOT }, 93214423Sjamie { _PATH_SOUND, PATH_DEV }, 94214423Sjamie { _PATH_SOUND0, PATH_DEV }, 95214423Sjamie { _PATH_SYSMON, PATH_DEV }, 96214423Sjamie { _PATH_TTY, PATH_DEV }, 97214423Sjamie { _PATH_UNIX, PATH_FILE | PATH_ROOT }, 98214423Sjamie { _PATH_URANDOM, PATH_DEV }, 99214423Sjamie { _PATH_VIDEO, PATH_DEV }, 100214423Sjamie { _PATH_VIDEO0, PATH_DEV }, 101214423Sjamie 102214423Sjamie { _PATH_DEV, PATH_DIR }, 103214423Sjamie { _PATH_DEV_PTS, PATH_DIR }, 104214423Sjamie { _PATH_EMUL_AOUT, PATH_DIR }, 105214423Sjamie { _PATH_TMP, PATH_DIR }, 106214423Sjamie { _PATH_VARDB, PATH_DIR }, 107214423Sjamie { _PATH_VARRUN, PATH_DIR }, 108214423Sjamie { _PATH_VARTMP, PATH_DIR }, 109214117Sjamie 110214117Sjamie { _PATH_BSHELL, PATH_FILE }, 111214117Sjamie { _PATH_CSHELL, PATH_FILE }, 112214117Sjamie { _PATH_VI, PATH_FILE }, 113214117Sjamie}; 114214117Sjamie 115214117SjamieATF_TC(paths); 116214117SjamieATF_TC_HEAD(paths, tc) 117214117Sjamie{ 118214117Sjamie atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>"); 119214117Sjamie} 120214117Sjamie 121214117SjamieATF_TC_BODY(paths, tc) 122214117Sjamie{ 123214117Sjamie struct stat st; 124214117Sjamie uid_t uid; 125214117Sjamie mode_t m; 126214117Sjamie size_t i; 127214117Sjamie int fd; 128214117Sjamie 129214117Sjamie#if defined(__sparc__) 130214117Sjamie atf_tc_skip("PR port-sparc/45580"); 131214117Sjamie#endif 132214117Sjamie 133214117Sjamie uid = getuid(); 134214117Sjamie 135214117Sjamie for (i = 0; i < __arraycount(paths); i++) { 136214117Sjamie 137214117Sjamie (void)fprintf(stderr, "testing '%s'\n", paths[i].path); 138214117Sjamie 139214117Sjamie errno = 0; 140214117Sjamie fd = open(paths[i].path, O_RDONLY); 141214117Sjamie 142214117Sjamie if (fd < 0) { 143214117Sjamie 144214117Sjamie switch (errno) { 145214117Sjamie 146214117Sjamie case EPERM: /* FALLTHROUGH */ 147214117Sjamie case EACCES: /* FALLTHROUGH */ 148214117Sjamie 149214117Sjamie if ((paths[i].flags & PATH_ROOT) == 0) { 150214117Sjamie 151214117Sjamie atf_tc_fail("UID %u failed to open %s", 152214117Sjamie (uint32_t)uid, paths[i].path); 153214117Sjamie } 154214117Sjamie 155214117Sjamie case EBUSY: /* FALLTHROUGH */ 156214423Sjamie case ENXIO: /* FALLTHROUGH */ 157214117Sjamie case ENOENT: /* FALLTHROUGH */ 158214117Sjamie 159214117Sjamie default: 160214117Sjamie continue; 161214117Sjamie } 162214117Sjamie } 163214117Sjamie 164214117Sjamie (void)memset(&st, 0, sizeof(struct stat)); 165214117Sjamie 166214423Sjamie ATF_REQUIRE(fstat(fd, &st) == 0); 167214117Sjamie 168214117Sjamie m = st.st_mode; 169214117Sjamie 170214117Sjamie if ((paths[i].flags & PATH_DEV) != 0) { 171214423Sjamie 172214117Sjamie ATF_CHECK(S_ISBLK(m) != 0 || S_ISCHR(m) != 0); 173214117Sjamie 174214117Sjamie ATF_CHECK((paths[i].flags & PATH_DIR) == 0); 175214423Sjamie ATF_CHECK((paths[i].flags & PATH_FILE) == 0); 176214117Sjamie } 177214117Sjamie 178214117Sjamie if ((paths[i].flags & PATH_DIR) != 0) { 179214117Sjamie 180214117Sjamie ATF_CHECK(S_ISDIR(m) != 0); 181214117Sjamie 182223188Sjamie ATF_CHECK((paths[i].flags & PATH_DEV) == 0); 183214117Sjamie ATF_CHECK((paths[i].flags & PATH_FILE) == 0); 184214117Sjamie } 185214117Sjamie 186214117Sjamie if ((paths[i].flags & PATH_FILE) != 0) { 187214117Sjamie 188214117Sjamie ATF_CHECK(S_ISREG(m) != 0); 189214117Sjamie 190214117Sjamie ATF_CHECK((paths[i].flags & PATH_DEV) == 0); 191214117Sjamie ATF_CHECK((paths[i].flags & PATH_DIR) == 0); 192214117Sjamie } 193214117Sjamie 194214117Sjamie ATF_REQUIRE(close(fd) == 0); 195214117Sjamie } 196214117Sjamie} 197214117Sjamie 198214117SjamieATF_TP_ADD_TCS(tp) 199214117Sjamie{ 200214117Sjamie 201214117Sjamie ATF_TP_ADD_TC(tp, paths); 202214117Sjamie 203214117Sjamie return atf_no_error(); 204214117Sjamie} 205214117Sjamie