1*323118b1Schristos /* $NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $ */
285bf85b7Schristos
385bf85b7Schristos /*-
485bf85b7Schristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
585bf85b7Schristos * All rights reserved.
685bf85b7Schristos *
785bf85b7Schristos * This code is derived from software contributed to The NetBSD Foundation
885bf85b7Schristos * by Christos Zoulas.
985bf85b7Schristos *
1085bf85b7Schristos * Redistribution and use in source and binary forms, with or without
1185bf85b7Schristos * modification, are permitted provided that the following conditions
1285bf85b7Schristos * are met:
1385bf85b7Schristos * 1. Redistributions of source code must retain the above copyright
1485bf85b7Schristos * notice, this list of conditions and the following disclaimer.
1585bf85b7Schristos * 2. Redistributions in binary form must reproduce the above copyright
1685bf85b7Schristos * notice, this list of conditions and the following disclaimer in the
1785bf85b7Schristos * documentation and/or other materials provided with the distribution.
1885bf85b7Schristos *
1985bf85b7Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2085bf85b7Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2185bf85b7Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2285bf85b7Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2385bf85b7Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2485bf85b7Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2585bf85b7Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2685bf85b7Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2785bf85b7Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2885bf85b7Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2985bf85b7Schristos * POSSIBILITY OF SUCH DAMAGE.
3085bf85b7Schristos */
3185bf85b7Schristos
3285bf85b7Schristos #include <sys/cdefs.h>
3385bf85b7Schristos __COPYRIGHT("@(#) Copyright (c) 2008\
3485bf85b7Schristos The NetBSD Foundation, inc. All rights reserved.");
35*323118b1Schristos __RCSID("$NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $");
3685bf85b7Schristos
3785bf85b7Schristos #include <sys/types.h>
3885bf85b7Schristos #include <sys/select.h>
3985bf85b7Schristos
4085bf85b7Schristos #include <atf-c.h>
4185bf85b7Schristos #include <time.h>
4285bf85b7Schristos #include <errno.h>
4385bf85b7Schristos #include <lwp.h>
4485bf85b7Schristos #include <signal.h>
4585bf85b7Schristos #include <pthread.h>
4685bf85b7Schristos #include <stdio.h>
4785bf85b7Schristos #include <sched.h>
4885bf85b7Schristos #include <unistd.h>
4985bf85b7Schristos
5085bf85b7Schristos static void
sighandler(int signo __unused)5185bf85b7Schristos sighandler(int signo __unused)
5285bf85b7Schristos {
5385bf85b7Schristos }
5485bf85b7Schristos
5585bf85b7Schristos struct info {
5685bf85b7Schristos void (*fun)(struct timespec *);
5785bf85b7Schristos struct timespec ts;
5885bf85b7Schristos };
5985bf85b7Schristos
6085bf85b7Schristos static void
timeleft__lwp_park(struct timespec * ts)6185bf85b7Schristos timeleft__lwp_park(struct timespec *ts)
6285bf85b7Schristos {
6385bf85b7Schristos ATF_REQUIRE_ERRNO(EINTR, _lwp_park(CLOCK_MONOTONIC, TIMER_RELTIME,
6485bf85b7Schristos ts, 0, ts, NULL) == -1);
6585bf85b7Schristos }
6685bf85b7Schristos
6785bf85b7Schristos #if 0
6885bf85b7Schristos static void
6985bf85b7Schristos timeleft_pselect(struct timespec *ts)
7085bf85b7Schristos {
7185bf85b7Schristos ATF_REQUIRE_ERRNO(EINTR, pselect(1, NULL, NULL, NULL, ts, NULL));
7285bf85b7Schristos }
7385bf85b7Schristos #endif
7485bf85b7Schristos
7585bf85b7Schristos static void *
runner(void * arg)7685bf85b7Schristos runner(void *arg)
7785bf85b7Schristos {
7885bf85b7Schristos struct info *i = arg;
7985bf85b7Schristos (*i->fun)(&i->ts);
8085bf85b7Schristos return NULL;
8185bf85b7Schristos }
8285bf85b7Schristos
8385bf85b7Schristos static void
tester(void (* fun)(struct timespec *))8485bf85b7Schristos tester(void (*fun)(struct timespec *))
8585bf85b7Schristos {
8685bf85b7Schristos const struct timespec ts = { 5, 0 };
8742bad958Schristos const struct timespec sts = { 1, 0 };
8885bf85b7Schristos struct info i = { fun, ts };
8985bf85b7Schristos pthread_t thr;
9085bf85b7Schristos
9185bf85b7Schristos ATF_REQUIRE(signal(SIGINT, sighandler) == 0);
9285bf85b7Schristos ATF_REQUIRE(pthread_create(&thr, NULL, runner, &i) == 0);
9385bf85b7Schristos
94*323118b1Schristos ATF_REQUIRE(nanosleep(&sts, NULL) == 0);
95*323118b1Schristos ATF_REQUIRE(pthread_kill(thr, SIGINT) == 0);
96*323118b1Schristos ATF_REQUIRE(pthread_join(thr, NULL) == 0);
9785bf85b7Schristos printf("Orig time %ju.%lu\n", (intmax_t)ts.tv_sec, ts.tv_nsec);
9885bf85b7Schristos printf("Time left %ju.%lu\n", (intmax_t)i.ts.tv_sec, i.ts.tv_nsec);
9985bf85b7Schristos ATF_REQUIRE(timespeccmp(&i.ts, &ts, <));
10085bf85b7Schristos }
10185bf85b7Schristos
10285bf85b7Schristos ATF_TC(timeleft__lwp_park);
ATF_TC_HEAD(timeleft__lwp_park,tc)10385bf85b7Schristos ATF_TC_HEAD(timeleft__lwp_park, tc)
10485bf85b7Schristos {
10585bf85b7Schristos atf_tc_set_md_var(tc, "descr", "Checks that _lwp_park(2) returns "
10685bf85b7Schristos "the time left to sleep after interrupted");
10785bf85b7Schristos }
10885bf85b7Schristos
ATF_TC_BODY(timeleft__lwp_park,tc)10985bf85b7Schristos ATF_TC_BODY(timeleft__lwp_park, tc)
11085bf85b7Schristos {
11185bf85b7Schristos tester(timeleft__lwp_park);
11285bf85b7Schristos }
11385bf85b7Schristos
11485bf85b7Schristos #if 0
11585bf85b7Schristos ATF_TC(timeleft_pselect);
11685bf85b7Schristos ATF_TC_HEAD(timeleft_pselect, tc)
11785bf85b7Schristos {
11885bf85b7Schristos atf_tc_set_md_var(tc, "descr", "Checks that pselect(2) returns "
11985bf85b7Schristos "the time left to sleep after interrupted");
12085bf85b7Schristos }
12185bf85b7Schristos
12285bf85b7Schristos ATF_TC_BODY(timeleft_pselect, tc)
12385bf85b7Schristos {
12485bf85b7Schristos tester(timeleft_pselect);
12585bf85b7Schristos }
12685bf85b7Schristos #endif
12785bf85b7Schristos
ATF_TP_ADD_TCS(tp)12885bf85b7Schristos ATF_TP_ADD_TCS(tp)
12985bf85b7Schristos {
13085bf85b7Schristos
13185bf85b7Schristos ATF_TP_ADD_TC(tp, timeleft__lwp_park);
13285bf85b7Schristos #if 0
13385bf85b7Schristos ATF_TP_ADD_TC(tp, timeleft_pselect);
13485bf85b7Schristos #endif
13585bf85b7Schristos
13685bf85b7Schristos return atf_no_error();
13785bf85b7Schristos }
138