Module Name: src Committed By: jruoho Date: Sat Sep 17 18:52:21 UTC 2011
Modified Files: src/tests/lib/libc/sys: Makefile t_timer_create.c Log Message: Simplify. To generate a diff of this commit: cvs rdiff -u -r1.7 -r1.8 src/tests/lib/libc/sys/Makefile cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/sys/t_timer_create.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libc/sys/Makefile diff -u src/tests/lib/libc/sys/Makefile:1.7 src/tests/lib/libc/sys/Makefile:1.8 --- src/tests/lib/libc/sys/Makefile:1.7 Mon Jul 18 23:16:11 2011 +++ src/tests/lib/libc/sys/Makefile Sat Sep 17 18:52:21 2011 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.7 2011/07/18 23:16:11 jym Exp $ +# $NetBSD: Makefile,v 1.8 2011/09/17 18:52:21 jruoho Exp $ MKMAN= no @@ -32,6 +32,7 @@ TESTS_C+= t_msync TESTS_C+= t_nanosleep TESTS_C+= t_poll +TESTS_C+= t_ptrace TESTS_C+= t_revoke TESTS_C+= t_select TESTS_C+= t_setrlimit @@ -46,7 +47,6 @@ SRCS.t_mprotect= t_mprotect.c ${SRCS_EXEC_PROT} LDADD.t_getpid+= -lpthread -LDADD.t_timer_create+= -lpthread WARNS= 4 Index: src/tests/lib/libc/sys/t_timer_create.c diff -u src/tests/lib/libc/sys/t_timer_create.c:1.1 src/tests/lib/libc/sys/t_timer_create.c:1.2 --- src/tests/lib/libc/sys/t_timer_create.c:1.1 Thu Jul 7 06:57:54 2011 +++ src/tests/lib/libc/sys/t_timer_create.c Sat Sep 17 18:52:21 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: t_timer_create.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */ +/* $NetBSD: t_timer_create.c,v 1.2 2011/09/17 18:52:21 jruoho Exp $ */ /*- * Copyright (c) 2010 The NetBSD Foundation, Inc. @@ -26,320 +26,148 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include <atf-c.h> #include <errno.h> -#include <pthread.h> +#include <stdio.h> #include <signal.h> #include <string.h> #include <time.h> #include <unistd.h> -#include <atf-c.h> - -#include "../../../h_macros.h" - -static void timer_signal_create(clockid_t, int); -static void timer_signal_handler(int, siginfo_t *, void *); -static int timer_wait(time_t); - -#if 0 -/* - * XXX: SIGEV_THREAD is not yet supported. - */ -static void timer_thread_create(clockid_t); -static void timer_thread_handler(sigval_t); -#endif - static timer_t t; -static bool error; -static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; - -ATF_TC(timer_create_bogus); -ATF_TC_HEAD(timer_create_bogus, tc) -{ - - /* Cf. PR lib/42434. */ - atf_tc_set_md_var(tc, "descr", - "Checks timer_create(2)'s error checking"); -} - -ATF_TC_BODY(timer_create_bogus, tc) -{ - struct sigevent evt; - - (void)memset(&evt, 0, sizeof(struct sigevent)); - - evt.sigev_signo = -1; - evt.sigev_notify = SIGEV_SIGNAL; - - if (timer_create(CLOCK_REALTIME, &evt, &t) == 0) - goto fail; - - evt.sigev_signo = SIGUSR1; - evt.sigev_notify = SIGEV_THREAD + 100; - - if (timer_create(CLOCK_REALTIME, &evt, &t) == 0) - goto fail; - - evt.sigev_signo = SIGUSR1; - evt.sigev_value.sival_int = 0; - evt.sigev_notify = SIGEV_SIGNAL; +static bool fail = true; - if (timer_create(CLOCK_REALTIME + 100, &evt, &t) == 0) - goto fail; - - t = 0; - - return; - -fail: - atf_tc_fail("timer_create() successful with bogus values"); -} - -ATF_TC(timer_create_signal_realtime); -ATF_TC_HEAD(timer_create_signal_realtime, tc) -{ - - atf_tc_set_md_var(tc, "descr", - "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " - "SIGEV_SIGNAL"); -} +static void timer_signal_handler(int, siginfo_t *, void *); +static void timer_signal_create(clockid_t); -ATF_TC_BODY(timer_create_signal_realtime, tc) +static void +timer_signal_handler(int signo, siginfo_t *si, void *osi) { - int i, signals[6] = { - SIGALRM, SIGIO, SIGPROF, SIGUSR1, SIGUSR2, -1 - }; + timer_t *tp; - for (i = 0; signals[i] > 0; i++) - timer_signal_create(CLOCK_REALTIME, signals[i]); -} + tp = si->si_value.sival_ptr; -ATF_TC(timer_create_signal_monotonic); -ATF_TC_HEAD(timer_create_signal_monotonic, tc) -{ + if (*tp == t && signo == SIGALRM) + fail = false; - atf_tc_set_md_var(tc, "descr", - "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " - "SIGEV_SIGNAL"); -} - -ATF_TC_BODY(timer_create_signal_monotonic, tc) -{ - int i, signals[6] = { - SIGALRM, SIGIO, SIGPROF, SIGUSR1, SIGUSR2, -1 - }; - - for (i = 0; signals[i] > 0; i++) - timer_signal_create(CLOCK_MONOTONIC, signals[i]); + (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo)); } static void -timer_signal_create(clockid_t cid, int sig) +timer_signal_create(clockid_t cid) { struct itimerspec tim; struct sigaction act; struct sigevent evt; - const char *errstr; sigset_t set; - error = true; + t = 0; + fail = true; (void)memset(&evt, 0, sizeof(struct sigevent)); (void)memset(&act, 0, sizeof(struct sigaction)); (void)memset(&tim, 0, sizeof(struct itimerspec)); + /* + * Set handler. + */ act.sa_flags = SA_SIGINFO; act.sa_sigaction = timer_signal_handler; - (void)sigemptyset(&act.sa_mask); + ATF_REQUIRE(sigemptyset(&set) == 0); + ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0); - if (sigaction(sig, &act, NULL) != 0) { - errstr = "sigaction()"; - goto fail; - } - - (void)sigemptyset(&set); - (void)sigaddset(&set, sig); - - if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) { - errstr = "sigprocmask()"; - goto fail; - } - - evt.sigev_signo = sig; + /* + * Block SIGALRM while configuring the timer. + */ + ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0); + ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0); + ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0); + + /* + * Create the timer (SIGEV_SIGNAL). + */ + evt.sigev_signo = SIGALRM; evt.sigev_value.sival_ptr = &t; evt.sigev_notify = SIGEV_SIGNAL; - if (timer_create(cid, &evt, &t) != 0) { - errstr = "timer_create()"; - goto fail; - } - - tim.it_value.tv_sec = 0; - tim.it_value.tv_nsec = 1000 * 1000; - - if (timer_settime(t, 0, &tim, NULL) != 0) { - errstr = "timer_settime()"; - goto fail; - } - - if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { - errstr = "sigprocmask()"; - goto fail; - } - - errno = timer_wait(1); - - if (errno != 0) { - errstr = "timer_wait()"; - goto fail; - } - - return; + ATF_REQUIRE(timer_create(cid, &evt, &t) == 0); -fail: - atf_tc_fail_errno("%s failed (sig %d, clock %d)", errstr, sig, cid); -} - -static void -timer_signal_handler(int signo, siginfo_t *si, void *osi) -{ - timer_t *tp; + /* + * Start the timer. After this, unblock the signal. + */ + tim.it_value.tv_sec = 1; + tim.it_value.tv_nsec = 0; - if (pthread_mutex_lock(&mtx) != 0) - return; + ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0); - tp = si->si_value.sival_ptr; + (void)sigprocmask(SIG_UNBLOCK, &set, NULL); + (void)sleep(2); - if (*tp == t) - error = false; - - (void)pthread_cond_signal(&cond); - (void)pthread_mutex_unlock(&mtx); - (void)signal(signo, SIG_IGN); + if (fail != false) + atf_tc_fail("timer failed to fire"); } -static int -timer_wait(time_t wait) +ATF_TC(timer_create_err); +ATF_TC_HEAD(timer_create_err, tc) { - struct timespec ts; - int rv; + /* Cf. PR lib/42434. */ + atf_tc_set_md_var(tc, "descr", "Check errors from timer_create(2)"); +} - rv = pthread_mutex_lock(&mtx); +ATF_TC_BODY(timer_create_err, tc) +{ + struct sigevent ev; - if (rv != 0) - return rv; + (void)memset(&ev, 0, sizeof(struct sigevent)); errno = 0; + ev.sigev_signo = -1; + ev.sigev_notify = SIGEV_SIGNAL; - if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { - - if (errno == 0) - errno = EFAULT; - - return errno; - } + ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); - ts.tv_sec += wait; - rv = pthread_cond_timedwait(&cond, &mtx, &ts); - - if (rv != 0) - return rv; - - rv = pthread_mutex_unlock(&mtx); - - if (rv != 0) - return rv; - - if (error != false) - return EPROCUNAVAIL; + errno = 0; + ev.sigev_signo = SIGUSR1; + ev.sigev_notify = SIGEV_THREAD + 100; - return timer_delete(t); + ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); } -#if 0 -ATF_TC(timer_create_thread); -ATF_TC_HEAD(timer_create_thread, tc) +ATF_TC(timer_create_real); +ATF_TC_HEAD(timer_create_real, tc) { atf_tc_set_md_var(tc, "descr", - "Checks timer_create(2) and sigevent(3), SIGEV_THREAD"); + "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " + "SIGEV_SIGNAL"); } -ATF_TC_BODY(timer_create_thread, tc) +ATF_TC_BODY(timer_create_real, tc) { - timer_thread_create(CLOCK_REALTIME); + timer_signal_create(CLOCK_REALTIME); } -static void -timer_thread_create(clockid_t cid) +ATF_TC(timer_create_mono); +ATF_TC_HEAD(timer_create_mono, tc) { - struct itimerspec tim; - struct sigevent evt; - const char *errstr; - error = true; - - (void)memset(&evt, 0, sizeof(struct sigevent)); - (void)memset(&tim, 0, sizeof(struct itimerspec)); - - evt.sigev_notify = SIGEV_THREAD; - evt.sigev_value.sival_ptr = &t; - evt.sigev_notify_function = timer_thread_handler; - evt.sigev_notify_attributes = NULL; - - if (timer_create(cid, &evt, &t) != 0) { - errstr = "timer_create()"; - goto fail; - } - - tim.it_value.tv_sec = 1; - tim.it_value.tv_nsec = 0; - - if (timer_settime(t, 0, &tim, NULL) != 0) { - errstr = "timer_settime()"; - goto fail; - } - - errno = timer_wait(3); - - if (errno != 0) { - errstr = "timer_wait()"; - goto fail; - } - - return; - -fail: - atf_tc_fail_errno("%s failed (clock %d)", errstr, cid); + atf_tc_set_md_var(tc, "descr", + "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " + "SIGEV_SIGNAL"); } -static void -timer_thread_handler(sigval_t sv) +ATF_TC_BODY(timer_create_mono, tc) { - timer_t *tp; - - if (pthread_mutex_lock(&mtx) != 0) - return; - - tp = sv.sival_ptr; - - if (*tp == t) - error = false; - - (void)pthread_cond_signal(&cond); - (void)pthread_mutex_unlock(&mtx); + timer_signal_create(CLOCK_MONOTONIC); } -#endif ATF_TP_ADD_TCS(tp) { - ATF_TP_ADD_TC(tp, timer_create_bogus); - ATF_TP_ADD_TC(tp, timer_create_signal_realtime); - ATF_TP_ADD_TC(tp, timer_create_signal_monotonic); - /* ATF_TP_ADD_TC(tp, timer_create_thread); */ + ATF_TP_ADD_TC(tp, timer_create_err); + ATF_TP_ADD_TC(tp, timer_create_real); + ATF_TP_ADD_TC(tp, timer_create_mono); return atf_no_error(); }