about summary refs log tree commit diff
path: root/misc/tst-select.c
blob: 5ad057cd51d0f45c3c95addff96b24abeff9953f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Test for select timeout.
   Copyright (C) 2021 Free Software Foundation, Inc.
   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>
#include <support/capture_subprocess.h>
#include <support/check.h>
#include <support/timespec.h>
#include <support/xunistd.h>
#include <support/xtime.h>

struct child_args
{
  int fds[2][2];
  struct timeval tmo;
};

static void
do_test_child (void *clousure)
{
  struct child_args *args = (struct child_args *) clousure;

  close (args->fds[0][1]);
  close (args->fds[1][0]);

  fd_set rfds;
  FD_ZERO (&rfds);
  FD_SET (args->fds[0][0], &rfds);

  struct timespec ts = xclock_now (CLOCK_REALTIME);
  ts = timespec_add (ts, (struct timespec) { args->tmo.tv_sec, 0 });

  int r = select (args->fds[0][0] + 1, &rfds, NULL, NULL, &args->tmo);
  TEST_COMPARE (r, 0);

  TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts);

  xwrite (args->fds[1][1], "foo", 3);
}

static int
do_test (void)
{
  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);
  }

  /* 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]);

  {
    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);
  }

  return 0;
}

#include <support/test-driver.c>