| 1 | /* Test of nanosleep() function.
|
|---|
| 2 | Copyright (C) 2009-2022 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 | /* Written by Eric Blake <ebb9@byu.net>, 2009. */
|
|---|
| 18 |
|
|---|
| 19 | #include <config.h>
|
|---|
| 20 |
|
|---|
| 21 | #include <time.h>
|
|---|
| 22 |
|
|---|
| 23 | #include "signature.h"
|
|---|
| 24 | SIGNATURE_CHECK (nanosleep, int, (struct timespec const *, struct timespec *));
|
|---|
| 25 |
|
|---|
| 26 | #include <errno.h>
|
|---|
| 27 | #include <signal.h>
|
|---|
| 28 | #include <unistd.h>
|
|---|
| 29 |
|
|---|
| 30 | #include "macros.h"
|
|---|
| 31 |
|
|---|
| 32 | #if HAVE_DECL_ALARM
|
|---|
| 33 | static void
|
|---|
| 34 | handle_alarm (int sig)
|
|---|
| 35 | {
|
|---|
| 36 | if (sig != SIGALRM)
|
|---|
| 37 | _exit (1);
|
|---|
| 38 | }
|
|---|
| 39 | #endif
|
|---|
| 40 |
|
|---|
| 41 | int
|
|---|
| 42 | main (void)
|
|---|
| 43 | {
|
|---|
| 44 | struct timespec ts;
|
|---|
| 45 |
|
|---|
| 46 | /* Check that negative nanosecond values cause failure. */
|
|---|
| 47 | ts.tv_sec = 1;
|
|---|
| 48 | ts.tv_nsec = -1;
|
|---|
| 49 | errno = 0;
|
|---|
| 50 | ASSERT (nanosleep (&ts, NULL) == -1);
|
|---|
| 51 | ASSERT (errno == EINVAL);
|
|---|
| 52 |
|
|---|
| 53 | ts.tv_sec = 1000;
|
|---|
| 54 | ts.tv_nsec = -1;
|
|---|
| 55 | errno = 0;
|
|---|
| 56 | ASSERT (nanosleep (&ts, NULL) == -1);
|
|---|
| 57 | ASSERT (errno == EINVAL);
|
|---|
| 58 |
|
|---|
| 59 | /* Check that too large nanosecond values cause failure. */
|
|---|
| 60 | ts.tv_sec = 1000;
|
|---|
| 61 | ts.tv_nsec = 1000000000;
|
|---|
| 62 | errno = 0;
|
|---|
| 63 | ASSERT (nanosleep (&ts, NULL) == -1);
|
|---|
| 64 | ASSERT (errno == EINVAL);
|
|---|
| 65 |
|
|---|
| 66 | /* Check successful call. */
|
|---|
| 67 | ts.tv_sec = 0;
|
|---|
| 68 | ts.tv_nsec = 1;
|
|---|
| 69 | ASSERT (nanosleep (&ts, &ts) == 0);
|
|---|
| 70 | /* Remaining time is only defined on EINTR failure; but on success,
|
|---|
| 71 | it is typically either 0 or unchanged from input. At any rate,
|
|---|
| 72 | it shouldn't be randomly changed to unrelated values. */
|
|---|
| 73 | ASSERT (ts.tv_sec == 0);
|
|---|
| 74 | ASSERT (ts.tv_nsec == 0 || ts.tv_nsec == 1);
|
|---|
| 75 | ts.tv_nsec = 0;
|
|---|
| 76 | ASSERT (nanosleep (&ts, NULL) == 0);
|
|---|
| 77 |
|
|---|
| 78 | #if HAVE_DECL_ALARM
|
|---|
| 79 | {
|
|---|
| 80 | const time_t pentecost = 50 * 24 * 60 * 60; /* 50 days. */
|
|---|
| 81 | signal (SIGALRM, handle_alarm);
|
|---|
| 82 | alarm (1);
|
|---|
| 83 | ts.tv_sec = pentecost;
|
|---|
| 84 | ts.tv_nsec = 999999999;
|
|---|
| 85 | errno = 0;
|
|---|
| 86 | ASSERT (nanosleep (&ts, &ts) == -1);
|
|---|
| 87 | ASSERT (errno == EINTR);
|
|---|
| 88 | ASSERT (pentecost - 10 < ts.tv_sec && ts.tv_sec <= pentecost);
|
|---|
| 89 | ASSERT (0 <= ts.tv_nsec && ts.tv_nsec <= 999999999);
|
|---|
| 90 | }
|
|---|
| 91 | #endif
|
|---|
| 92 |
|
|---|
| 93 | return 0;
|
|---|
| 94 | }
|
|---|