1 /* $NetBSD: t_utimensat.c,v 1.9 2024/08/10 15:20:22 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 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 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_utimensat.c,v 1.9 2024/08/10 15:20:22 riastradh Exp $"); 33 34 #include <sys/param.h> 35 36 #include <sys/stat.h> 37 #include <sys/statvfs.h> 38 #include <sys/time.h> 39 40 #include <atf-c.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <limits.h> 44 #include <paths.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include "h_macros.h" 50 51 #define DIR "dir" 52 #define FILE "dir/utimensat" 53 #define BASEFILE "utimensat" 54 #define LINK "dir/symlink" 55 #define BASELINK "symlink" 56 #define FILEERR "dir/symlink" 57 58 static const struct timespec tptr[] = { 59 { 0x12345678, 987654321 }, 60 { 0x15263748, 123456789 }, 61 }; 62 63 static void 64 checkstattime(const struct stat *st, const struct statvfs *fs) 65 { 66 67 if ((fs->f_flag & ST_NOATIME) == 0) { 68 ATF_CHECK_EQ_MSG(st->st_atimespec.tv_sec, tptr[0].tv_sec, 69 "st->st_atimespec.tv_sec=%lld tptr[0].tv_sec=%lld", 70 (long long)st->st_atimespec.tv_sec, 71 (long long)tptr[0].tv_sec); 72 ATF_CHECK_EQ_MSG(st->st_atimespec.tv_nsec, tptr[0].tv_nsec, 73 "st->st_atimespec.tv_nsec=%ld tptr[0].tv_nsec=%ld", 74 (long)st->st_atimespec.tv_nsec, (long)tptr[0].tv_nsec); 75 } 76 ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_sec, tptr[1].tv_sec, 77 "st->st_mtimespec.tv_sec=%lld tptr[1].tv_sec=%lld", 78 (long long)st->st_mtimespec.tv_sec, (long long)tptr[1].tv_sec); 79 ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_nsec, tptr[1].tv_nsec, 80 "st->st_mtimespec.tv_nsec=%ld tptr[1].tv_nsec=%ld", 81 (long)st->st_mtimespec.tv_nsec, (long)tptr[1].tv_nsec); 82 } 83 84 ATF_TC(utimensat_fd); 85 ATF_TC_HEAD(utimensat_fd, tc) 86 { 87 atf_tc_set_md_var(tc, "descr", "See that utimensat works with fd"); 88 } 89 ATF_TC_BODY(utimensat_fd, tc) 90 { 91 int dfd; 92 int fd; 93 struct stat st; 94 struct statvfs fs; 95 96 RL(mkdir(DIR, 0755)); 97 RL(fd = open(FILE, O_CREAT|O_RDWR, 0644)); 98 RL(close(fd)); 99 100 RL(dfd = open(DIR, O_RDONLY, 0)); 101 RL(utimensat(dfd, BASEFILE, tptr, 0)); 102 RL(close(dfd)); 103 104 RL(stat(FILE, &st)); 105 RL(statvfs(FILE, &fs)); 106 checkstattime(&st, &fs); 107 } 108 109 ATF_TC(utimensat_fdcwd); 110 ATF_TC_HEAD(utimensat_fdcwd, tc) 111 { 112 atf_tc_set_md_var(tc, "descr", 113 "See that utimensat works with fd as AT_FDCWD"); 114 } 115 ATF_TC_BODY(utimensat_fdcwd, tc) 116 { 117 int fd; 118 struct stat st; 119 struct statvfs fs; 120 121 RL(mkdir(DIR, 0755)); 122 RL(fd = open(FILE, O_CREAT|O_RDWR, 0644)); 123 RL(close(fd)); 124 125 RL(chdir(DIR)); 126 RL(utimensat(AT_FDCWD, BASEFILE, tptr, 0)); 127 128 RL(stat(BASEFILE, &st)); 129 RL(statvfs(BASEFILE, &fs)); 130 checkstattime(&st, &fs); 131 } 132 133 ATF_TC(utimensat_fdcwderr); 134 ATF_TC_HEAD(utimensat_fdcwderr, tc) 135 { 136 atf_tc_set_md_var(tc, "descr", 137 "See that utimensat fails with fd as AT_FDCWD and bad path"); 138 } 139 ATF_TC_BODY(utimensat_fdcwderr, tc) 140 { 141 RL(mkdir(DIR, 0755)); 142 ATF_CHECK_ERRNO(ENOENT, utimensat(AT_FDCWD, FILEERR, tptr, 0) == -1); 143 } 144 145 ATF_TC(utimensat_fderr1); 146 ATF_TC_HEAD(utimensat_fderr1, tc) 147 { 148 atf_tc_set_md_var(tc, "descr", 149 "See that utimensat fail with bad path"); 150 } 151 ATF_TC_BODY(utimensat_fderr1, tc) 152 { 153 int dfd; 154 155 RL(mkdir(DIR, 0755)); 156 RL(dfd = open(DIR, O_RDONLY, 0)); 157 ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, FILEERR, tptr, 0) == -1); 158 RL(close(dfd)); 159 } 160 161 ATF_TC(utimensat_fderr2); 162 ATF_TC_HEAD(utimensat_fderr2, tc) 163 { 164 atf_tc_set_md_var(tc, "descr", 165 "See that utimensat fails with bad fdat"); 166 } 167 ATF_TC_BODY(utimensat_fderr2, tc) 168 { 169 int dfd; 170 int fd; 171 char cwd[MAXPATHLEN]; 172 173 RL(mkdir(DIR, 0755)); 174 RL(fd = open(FILE, O_CREAT|O_RDWR, 0644)); 175 RL(close(fd)); 176 177 RL(dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)); 178 ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASEFILE, tptr, 0) == -1); 179 RL(close(dfd)); 180 } 181 182 ATF_TC(utimensat_fderr3); 183 ATF_TC_HEAD(utimensat_fderr3, tc) 184 { 185 atf_tc_set_md_var(tc, "descr", 186 "See that utimensat fails with fd as -1"); 187 } 188 ATF_TC_BODY(utimensat_fderr3, tc) 189 { 190 int fd; 191 192 RL(mkdir(DIR, 0755)); 193 RL(fd = open(FILE, O_CREAT|O_RDWR, 0644)); 194 RL(close(fd)); 195 196 ATF_CHECK_ERRNO(EBADF, utimensat(-1, FILE, tptr, 0) == -1); 197 } 198 199 ATF_TC(utimensat_fdlink); 200 ATF_TC_HEAD(utimensat_fdlink, tc) 201 { 202 atf_tc_set_md_var(tc, "descr", "See that utimensat works on symlink"); 203 } 204 ATF_TC_BODY(utimensat_fdlink, tc) 205 { 206 int dfd; 207 struct stat st; 208 struct statvfs fs; 209 210 RL(mkdir(DIR, 0755)); 211 RL(symlink(FILE, LINK)); /* NB: FILE does not exists */ 212 213 RL(dfd = open(DIR, O_RDONLY, 0)); 214 215 ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASELINK, tptr, 0) == -1); 216 217 RL(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW)); 218 219 RL(close(dfd)); 220 221 RL(lstat(LINK, &st)); 222 RL(statvfs(DIR, &fs)); /* XXX should do lstatvfs(LINK, &fs) */ 223 checkstattime(&st, &fs); 224 } 225 226 ATF_TP_ADD_TCS(tp) 227 { 228 229 ATF_TP_ADD_TC(tp, utimensat_fd); 230 ATF_TP_ADD_TC(tp, utimensat_fdcwd); 231 ATF_TP_ADD_TC(tp, utimensat_fdcwderr); 232 ATF_TP_ADD_TC(tp, utimensat_fderr1); 233 ATF_TP_ADD_TC(tp, utimensat_fderr2); 234 ATF_TP_ADD_TC(tp, utimensat_fderr3); 235 ATF_TP_ADD_TC(tp, utimensat_fdlink); 236 237 return atf_no_error(); 238 } 239