Home | History | Annotate | Download | only in tests
      1 /*
      2  * Check decoding of utime syscall.
      3  *
      4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * Copyright (c) 2015-2017 The strace developers.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "tests.h"
     32 #include <asm/unistd.h>
     33 
     34 #ifdef __NR_utime
     35 
     36 # include <time.h>
     37 # include <utime.h>
     38 # include <errno.h>
     39 # include <stdio.h>
     40 # include <unistd.h>
     41 
     42 static long
     43 k_utime(const void *const filename, const void *const times)
     44 {
     45 	return syscall(__NR_utime, filename, times);
     46 }
     47 
     48 int
     49 main(void)
     50 {
     51 	static const char *const dummy_str = "dummy filename";
     52 
     53 	const time_t t = 1492350678;
     54 	const struct utimbuf u = { .actime = t, .modtime = t };
     55 	const struct utimbuf *const tail_u = tail_memdup(&u, sizeof(u));
     56 	const char *const dummy_filename =
     57 		tail_memdup(dummy_str, sizeof(dummy_str) - 1);
     58 
     59 	long rc = k_utime("", NULL);
     60 	printf("utime(\"\", NULL) = %s\n", sprintrc(rc));
     61 
     62 	rc = k_utime(dummy_filename + sizeof(dummy_str), tail_u + 1);
     63 	printf("utime(%p, %p) = %s\n", dummy_filename + sizeof(dummy_str),
     64 	       tail_u + 1, sprintrc(rc));
     65 
     66 	rc = k_utime(dummy_filename, (struct tm *) tail_u + 1);
     67 	printf("utime(%p, %p) = %s\n",
     68 	       dummy_filename, (struct tm *) tail_u + 1, sprintrc(rc));
     69 
     70 	rc = k_utime("utime\nfilename", tail_u);
     71 	const char *errstr = sprintrc(rc);
     72 	printf("utime(\"utime\\nfilename\", {actime=%lld", (long long) t);
     73 	print_time_t_nsec(t, 0, 1);
     74 	printf(", modtime=%lld", (long long) t);
     75 	print_time_t_nsec(t, 0, 1);
     76 	printf("}) = %s\n", errstr);
     77 
     78 	puts("+++ exited with 0 +++");
     79 	return 0;
     80 }
     81 
     82 #else
     83 
     84 SKIP_MAIN_UNDEFINED("__NR_utime")
     85 
     86 #endif
     87