2021-03-14 06:34:21 +08:00
|
|
|
/* Test for select timeout.
|
2022-01-02 02:54:23 +08:00
|
|
|
Copyright (C) 2021-2022 Free Software Foundation, Inc.
|
2021-03-14 06:34:21 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
2021-06-16 08:00:50 +08:00
|
|
|
#include <intprops.h>
|
2021-03-26 03:57:45 +08:00
|
|
|
#include <support/capture_subprocess.h>
|
2021-03-14 06:34:21 +08:00
|
|
|
#include <support/check.h>
|
2021-04-01 00:53:34 +08:00
|
|
|
#include <support/support.h>
|
2021-03-14 06:34:21 +08:00
|
|
|
#include <support/timespec.h>
|
2021-03-26 03:57:45 +08:00
|
|
|
#include <support/xunistd.h>
|
|
|
|
#include <support/xtime.h>
|
2021-04-08 18:39:32 +08:00
|
|
|
#include <support/xsignal.h>
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
struct child_args
|
|
|
|
{
|
|
|
|
int fds[2][2];
|
|
|
|
struct timeval tmo;
|
|
|
|
};
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
static void
|
|
|
|
do_test_child (void *clousure)
|
2021-03-14 06:34:21 +08:00
|
|
|
{
|
2021-03-26 03:57:45 +08:00
|
|
|
struct child_args *args = (struct child_args *) clousure;
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
close (args->fds[0][1]);
|
|
|
|
close (args->fds[1][0]);
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO (&rfds);
|
|
|
|
FD_SET (args->fds[0][0], &rfds);
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
struct timespec ts = xclock_now (CLOCK_REALTIME);
|
|
|
|
ts = timespec_add (ts, (struct timespec) { args->tmo.tv_sec, 0 });
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
int r = select (args->fds[0][0] + 1, &rfds, NULL, NULL, &args->tmo);
|
|
|
|
TEST_COMPARE (r, 0);
|
2021-03-14 06:34:21 +08:00
|
|
|
|
2021-04-01 00:53:34 +08:00
|
|
|
if (support_select_modifies_timeout ())
|
|
|
|
{
|
|
|
|
TEST_COMPARE (args->tmo.tv_sec, 0);
|
|
|
|
TEST_COMPARE (args->tmo.tv_usec, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:34:21 +08:00
|
|
|
TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts);
|
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
xwrite (args->fds[1][1], "foo", 3);
|
2021-03-14 06:34:21 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:39:32 +08:00
|
|
|
static void
|
|
|
|
do_test_child_alarm (void *clousure)
|
|
|
|
{
|
2021-06-16 08:00:50 +08:00
|
|
|
struct child_args *args = (struct child_args *) clousure;
|
2021-04-08 18:39:32 +08:00
|
|
|
|
2021-06-16 08:00:50 +08:00
|
|
|
support_create_timer (0, 100000000, false, NULL);
|
|
|
|
struct timeval tv = { .tv_sec = args->tmo.tv_sec, .tv_usec = 0 };
|
2021-04-08 18:39:32 +08:00
|
|
|
int r = select (0, NULL, NULL, NULL, &tv);
|
|
|
|
TEST_COMPARE (r, -1);
|
2021-06-16 08:00:50 +08:00
|
|
|
if (args->tmo.tv_sec > INT_MAX)
|
|
|
|
TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_COMPARE (errno, EINTR);
|
|
|
|
if (support_select_modifies_timeout ())
|
|
|
|
TEST_VERIFY (tv.tv_sec < args->tmo.tv_sec);
|
|
|
|
}
|
2021-04-08 18:39:32 +08:00
|
|
|
}
|
|
|
|
|
2021-03-14 06:34:21 +08:00
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2021-03-26 03:57:45 +08:00
|
|
|
struct child_args args;
|
|
|
|
|
|
|
|
xpipe (args.fds[0]);
|
|
|
|
xpipe (args.fds[1]);
|
|
|
|
|
|
|
|
/* The child select should timeout and write on its pipe end. */
|
|
|
|
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 250000 };
|
|
|
|
{
|
|
|
|
struct support_capture_subprocess result;
|
|
|
|
result = support_capture_subprocess (do_test_child, &args);
|
|
|
|
support_capture_subprocess_check (&result, "tst-select-child", 0,
|
|
|
|
sc_allow_none);
|
|
|
|
}
|
|
|
|
|
2021-04-01 00:53:34 +08:00
|
|
|
if (support_select_normalizes_timeout ())
|
|
|
|
{
|
|
|
|
/* This is handled as 1 second instead of failing with EINVAL. */
|
|
|
|
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 1000000 };
|
|
|
|
struct support_capture_subprocess result;
|
|
|
|
result = support_capture_subprocess (do_test_child, &args);
|
|
|
|
support_capture_subprocess_check (&result, "tst-select-child", 0,
|
|
|
|
sc_allow_none);
|
|
|
|
}
|
|
|
|
|
2021-03-26 03:57:45 +08:00
|
|
|
/* Same as before, but simulating polling. */
|
|
|
|
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 0 };
|
|
|
|
{
|
|
|
|
struct support_capture_subprocess result;
|
|
|
|
result = support_capture_subprocess (do_test_child, &args);
|
|
|
|
support_capture_subprocess_check (&result, "tst-select-child", 0,
|
|
|
|
sc_allow_none);
|
|
|
|
}
|
|
|
|
|
|
|
|
xclose (args.fds[0][0]);
|
|
|
|
xclose (args.fds[1][1]);
|
|
|
|
|
2021-06-16 08:00:50 +08:00
|
|
|
args.tmo = (struct timeval) { .tv_sec = 10, .tv_usec = 0 };
|
|
|
|
{
|
|
|
|
struct support_capture_subprocess result;
|
|
|
|
result = support_capture_subprocess (do_test_child_alarm, &args);
|
|
|
|
support_capture_subprocess_check (&result, "tst-select-child", 0,
|
|
|
|
sc_allow_none);
|
|
|
|
}
|
|
|
|
|
|
|
|
args.tmo = (struct timeval) { .tv_sec = TYPE_MAXIMUM (time_t),
|
|
|
|
.tv_usec = 0 };
|
2021-04-08 18:39:32 +08:00
|
|
|
{
|
|
|
|
struct support_capture_subprocess result;
|
2021-06-16 08:00:50 +08:00
|
|
|
result = support_capture_subprocess (do_test_child_alarm, &args);
|
2021-04-08 18:39:32 +08:00
|
|
|
support_capture_subprocess_check (&result, "tst-select-child", 0,
|
|
|
|
sc_allow_none);
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:00:50 +08:00
|
|
|
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 0 };
|
2021-03-26 03:57:45 +08:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO (&rfds);
|
|
|
|
FD_SET (args.fds[1][0], &rfds);
|
|
|
|
|
|
|
|
int r = select (args.fds[1][0] + 1, &rfds, NULL, NULL, &args.tmo);
|
|
|
|
TEST_COMPARE (r, 1);
|
|
|
|
}
|
2021-03-14 06:34:21 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <support/test-driver.c>
|