Home | History | Annotate | Download | only in tests
      1 /*
      2  * Check decoding of utimensat syscall.
      3  *
      4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "tests.h"
     31 #include <fcntl.h>
     32 #include <stdint.h>
     33 #include <stdio.h>
     34 #include <sys/stat.h>
     35 #include <sys/time.h>
     36 #include <unistd.h>
     37 #include <asm/unistd.h>
     38 
     39 #if defined __NR_utimensat && defined UTIME_NOW && defined UTIME_OMIT
     40 
     41 static void
     42 print_ts(const struct timespec *ts)
     43 {
     44 	printf("{tv_sec=%lld, tv_nsec=%llu}", (long long) ts->tv_sec,
     45 		zero_extend_signed_to_ull(ts->tv_nsec));
     46 	print_time_t_nsec(ts->tv_sec,
     47 			  zero_extend_signed_to_ull(ts->tv_nsec), 1);
     48 }
     49 
     50 static const char *errstr;
     51 
     52 static long
     53 k_utimensat(const kernel_ulong_t dirfd,
     54 	    const kernel_ulong_t pathname,
     55 	    const kernel_ulong_t times,
     56 	    const kernel_ulong_t flags)
     57 {
     58 	long rc = syscall(__NR_utimensat, dirfd, pathname, times, flags);
     59 	errstr = sprintrc(rc);
     60 	return rc;
     61 }
     62 
     63 int
     64 main(void)
     65 {
     66 	static const kernel_ulong_t bogus_fd =
     67 		(kernel_ulong_t) 0xbadc0deddeadbeef;
     68 	static const kernel_ulong_t kfdcwd =
     69 		(kernel_ulong_t) 0xdefaced00000000 | -100U;
     70 	static const char proto_fname[] = "utimensat\nfilename";
     71 	static const char qname[] = "\"utimensat\\nfilename\"";
     72 
     73 	char *const fname = tail_memdup(proto_fname, sizeof(proto_fname));
     74 	const kernel_ulong_t kfname = (uintptr_t) fname;
     75 	struct timespec *const ts = tail_alloc(sizeof(*ts) * 2);
     76 
     77 	(void) close(0);
     78 
     79 	/* dirfd */
     80 	k_utimensat(0, kfname, 0, 0);
     81 	printf("utimensat(0, %s, NULL, 0) = %s\n", qname, errstr);
     82 
     83 	k_utimensat(bogus_fd, kfname, 0, 0);
     84 	printf("utimensat(%d, %s, NULL, 0) = %s\n",
     85 	       (int) bogus_fd, qname, errstr);
     86 
     87 	k_utimensat(-100U, kfname, 0, 0);
     88 	printf("utimensat(AT_FDCWD, %s, NULL, 0) = %s\n", qname, errstr);
     89 
     90 	k_utimensat(kfdcwd, kfname, 0, 0);
     91 	printf("utimensat(AT_FDCWD, %s, NULL, 0) = %s\n", qname, errstr);
     92 
     93 	/* pathname */
     94 	k_utimensat(kfdcwd, 0, 0, 0);
     95 	printf("utimensat(AT_FDCWD, NULL, NULL, 0) = %s\n", errstr);
     96 
     97 	k_utimensat(kfdcwd, kfname + sizeof(proto_fname) - 1, 0, 0);
     98 	printf("utimensat(AT_FDCWD, \"\", NULL, 0) = %s\n", errstr);
     99 
    100 	fname[sizeof(proto_fname) - 1] = '+';
    101 	k_utimensat(kfdcwd, kfname, 0, 0);
    102 	fname[sizeof(proto_fname) - 1] = '\0';
    103 	printf("utimensat(AT_FDCWD, %p, NULL, 0) = %s\n", fname, errstr);
    104 
    105 	if (F8ILL_KULONG_SUPPORTED) {
    106 		k_utimensat(kfdcwd, f8ill_ptr_to_kulong(fname), 0, 0);
    107 		printf("utimensat(AT_FDCWD, %#jx, NULL, 0) = %s\n",
    108 		       (uintmax_t) f8ill_ptr_to_kulong(fname), errstr);
    109 	}
    110 
    111 	/* times */
    112 	k_utimensat(kfdcwd, kfname, (uintptr_t) (ts + 1), 0);
    113 	printf("utimensat(AT_FDCWD, %s, %p, 0) = %s\n",
    114 	       qname, ts + 1, errstr);
    115 
    116 	k_utimensat(kfdcwd, kfname, (uintptr_t) (ts + 2), 0);
    117 	printf("utimensat(AT_FDCWD, %s, %p, 0)"
    118 	       " = %s\n", qname, ts + 2, errstr);
    119 
    120 	ts[0].tv_sec = 1492358706;
    121 	ts[0].tv_nsec = 123456789;
    122 	ts[1].tv_sec = 1492357068;
    123 	ts[1].tv_nsec = 234567890;
    124 
    125 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, 0x100);
    126 	printf("utimensat(AT_FDCWD, %s, [", qname);
    127 	print_ts(&ts[0]);
    128 	printf(", ");
    129 	print_ts(&ts[1]);
    130 	printf("], AT_SYMLINK_NOFOLLOW) = %s\n", errstr);
    131 
    132 	ts[0].tv_sec = -1;
    133 	ts[0].tv_nsec = 2000000000;
    134 	ts[1].tv_sec = (time_t) -0x100000001LL;
    135 	ts[1].tv_nsec = 2345678900U;
    136 
    137 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, 0x100);
    138 	printf("utimensat(AT_FDCWD, %s, [", qname);
    139 	print_ts(&ts[0]);
    140 	printf(", ");
    141 	print_ts(&ts[1]);
    142 	printf("], AT_SYMLINK_NOFOLLOW) = %s\n", errstr);
    143 
    144 	ts[0].tv_sec = 0;
    145 	ts[0].tv_nsec = 0;
    146 	ts[1].tv_sec = (time_t) 0xcafef00ddeadbeefLL;
    147 	ts[1].tv_nsec = 0;
    148 
    149 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, 0x100);
    150 	printf("utimensat(AT_FDCWD, %s, [", qname);
    151 	print_ts(&ts[0]);
    152 	printf(", ");
    153 	print_ts(&ts[1]);
    154 	printf("], AT_SYMLINK_NOFOLLOW) = %s\n", errstr);
    155 
    156 	ts[0].tv_sec = 0xdeadbeefU;
    157 	ts[0].tv_nsec = 0xfacefeedU;
    158 	ts[1].tv_sec = (time_t) 0xcafef00ddeadbeefLL;
    159 	ts[1].tv_nsec = (long) 0xbadc0dedfacefeedLL;
    160 
    161 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, 0x100);
    162 	printf("utimensat(AT_FDCWD, %s, [", qname);
    163 	print_ts(&ts[0]);
    164 	printf(", ");
    165 	print_ts(&ts[1]);
    166 	printf("], AT_SYMLINK_NOFOLLOW) = %s\n", errstr);
    167 
    168 	ts[0].tv_nsec = UTIME_NOW;
    169 	ts[1].tv_nsec = UTIME_OMIT;
    170 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, 0x100);
    171 	printf("utimensat(AT_FDCWD, %s, [UTIME_NOW, UTIME_OMIT]"
    172 	       ", AT_SYMLINK_NOFOLLOW) = %s\n", qname, errstr);
    173 
    174 	if (F8ILL_KULONG_SUPPORTED) {
    175 		k_utimensat(kfdcwd, kfname, f8ill_ptr_to_kulong(ts), 0);
    176 		printf("utimensat(AT_FDCWD, %s, %#jx, 0) = %s\n",
    177 		       qname, (uintmax_t) f8ill_ptr_to_kulong(ts), errstr);
    178 	}
    179 
    180 	/* flags */
    181 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts,
    182 		    (kernel_ulong_t) 0xdefaced00000200);
    183 	printf("utimensat(AT_FDCWD, %s, [UTIME_NOW, UTIME_OMIT]"
    184 	       ", AT_REMOVEDIR) = %s\n",
    185 	       qname, errstr);
    186 
    187 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts,
    188 		    (kernel_ulong_t) 0xdefaced00000600);
    189 	printf("utimensat(AT_FDCWD, %s, [UTIME_NOW, UTIME_OMIT]"
    190 	       ", AT_REMOVEDIR|AT_SYMLINK_FOLLOW) = %s\n",
    191 	       qname, errstr);
    192 
    193 	k_utimensat(kfdcwd, kfname, (uintptr_t) ts, (kernel_ulong_t) -1ULL);
    194 	printf("utimensat(AT_FDCWD, %s, [UTIME_NOW, UTIME_OMIT]"
    195 	       ", AT_SYMLINK_NOFOLLOW|AT_REMOVEDIR|AT_SYMLINK_FOLLOW"
    196 	       "|AT_NO_AUTOMOUNT|AT_EMPTY_PATH|0xffffe0ff) = %s\n",
    197 	       qname, errstr);
    198 
    199 	puts("+++ exited with 0 +++");
    200 	return 0;
    201 }
    202 
    203 #else
    204 
    205 SKIP_MAIN_UNDEFINED("__NR_utimensat && UTIME_NOW && UTIME_OMIT")
    206 
    207 #endif
    208