Home | History | Annotate | Download | only in tests
      1 /*
      2  * Check decoding of nanosleep 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 <assert.h>
     33 #include <stdio.h>
     34 #include <stdint.h>
     35 #include <signal.h>
     36 #include <time.h>
     37 #include <sys/time.h>
     38 
     39 static void
     40 handler(int signo)
     41 {
     42 }
     43 
     44 int
     45 main(void)
     46 {
     47 	struct {
     48 		struct timespec ts;
     49 		uint32_t pad[2];
     50 	} req = {
     51 		.ts.tv_nsec = 0xc0de1,
     52 		.pad = { 0xdeadbeef, 0xbadc0ded }
     53 	}, rem = {
     54 		.ts = { .tv_sec = 0xc0de2, .tv_nsec = 0xc0de3 },
     55 		.pad = { 0xdeadbeef, 0xbadc0ded }
     56 	};
     57 	const sigset_t set = {};
     58 	const struct sigaction act = { .sa_handler = handler };
     59 	const struct itimerval itv = { .it_value.tv_usec = 111111 };
     60 
     61 	if (nanosleep(&req.ts, NULL))
     62 		perror_msg_and_fail("nanosleep");
     63 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, NULL) = 0\n",
     64 	       (long long) req.ts.tv_sec,
     65 	       zero_extend_signed_to_ull(req.ts.tv_nsec));
     66 
     67 	assert(nanosleep(NULL, &rem.ts) == -1);
     68 	printf("nanosleep(NULL, %p) = -1 EFAULT (%m)\n", &rem.ts);
     69 
     70 	if (nanosleep(&req.ts, &rem.ts))
     71 		perror_msg_and_fail("nanosleep");
     72 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, %p) = 0\n",
     73 	       (long long) req.ts.tv_sec,
     74 	       zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
     75 
     76 	req.ts.tv_nsec = 1000000000;
     77 	assert(nanosleep(&req.ts, &rem.ts) == -1);
     78 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
     79 	       (long long) req.ts.tv_sec,
     80 	       zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
     81 
     82 	req.ts.tv_sec = 0xdeadbeefU;
     83 	req.ts.tv_nsec = 0xfacefeedU;
     84 	assert(nanosleep(&req.ts, &rem.ts) == -1);
     85 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
     86 	       (long long) req.ts.tv_sec,
     87 	       zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
     88 
     89 	req.ts.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
     90 	req.ts.tv_nsec = (long) 0xbadc0dedfacefeedLL;
     91 	assert(nanosleep(&req.ts, &rem.ts) == -1);
     92 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
     93 	       (long long) req.ts.tv_sec,
     94 	       zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
     95 
     96 	req.ts.tv_sec = -1;
     97 	req.ts.tv_nsec = -1;
     98 	assert(nanosleep(&req.ts, &rem.ts) == -1);
     99 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}, %p) = -1 EINVAL (%m)\n",
    100 	       (long long) req.ts.tv_sec,
    101 	       zero_extend_signed_to_ull(req.ts.tv_nsec), &rem.ts);
    102 
    103 	assert(sigaction(SIGALRM, &act, NULL) == 0);
    104 	assert(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
    105 
    106 	if (setitimer(ITIMER_REAL, &itv, NULL))
    107 		perror_msg_and_skip("setitimer");
    108 
    109 	req.ts.tv_sec = 0;
    110 	req.ts.tv_nsec = 999999999;
    111 	assert(nanosleep(&req.ts, &rem.ts) == -1);
    112 	printf("nanosleep({tv_sec=%lld, tv_nsec=%llu}"
    113 	       ", {tv_sec=%lld, tv_nsec=%llu})"
    114 	       " = ? ERESTART_RESTARTBLOCK (Interrupted by signal)\n",
    115 	       (long long) req.ts.tv_sec,
    116 	       zero_extend_signed_to_ull(req.ts.tv_nsec),
    117 	       (long long) rem.ts.tv_sec,
    118 	       zero_extend_signed_to_ull(rem.ts.tv_nsec));
    119 	puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
    120 
    121 	puts("+++ exited with 0 +++");
    122 	return 0;
    123 }
    124