test-select.c 3.02 KB
Newer Older
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
/*
 * Copyright (C) 2002  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

/*
 * The lyskomd process assumes that select() returns with EINTR even
 * when a signal handle has been set up with SA_RESTART.  This program
 * checks if that assumption is true or not.  When started, it should
 * print a single line after 1 second:
 *
 *    PASS: select returns EINTR
 *
 * This program will fail if input is available on stdin.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
45
46
47
48
49
50
51
52
53
54
#ifdef TIME_WITH_SYS_TIME
#  include <sys/time.h>
#  include <time.h>
#else
#  ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
#endif
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <unistd.h>

#include "unused.h"

sig_atomic_t ctr = 0;

static void
my_sighandler(int UNUSED(x))
{
  ctr++;
}

int
main(void)
{
  fd_set r;
  FD_ZERO(&r);
  FD_SET(0, &r);
  struct timeval timeout;
#ifdef HAVE_STRUCT_SIGACTION
  struct sigaction act;
#endif
  int rv;
  int er;
  time_t before;
  time_t after;
  pid_t pid;

  if ((pid = fork()) < 0)
    {
      perror("fork");
      return 1;
    }

  if (pid == 0)
    {
      sleep(1);
      kill(getppid(), SIGHUP);
      sleep(5); /* avoid triggering a SIGCHLD */
      return 0;
    }

#ifdef HAVE_STRUCT_SIGACTION
  sigemptyset(&act.sa_mask);
  act.sa_flags = SA_RESTART;
  act.sa_handler = my_sighandler;
  if (sigaction(SIGHUP, &act, NULL) < 0)
  {
      perror("sigaction");
      return 1;
  }
#else
  if (signal(SIGHUP, my_sighandler) == SIG_ERR)
  {
      perror("signal");
      return 1;
  }
#endif

  timeout.tv_sec = 5;
  timeout.tv_usec = 0;

  time(&before);
  rv = select(1, &r, NULL, NULL, &timeout);
  er = errno;
  time(&after);

  if (rv == 0)
    printf("select: timeout\n");
  else if (rv > 0)
    printf("select: input available\n");
  else if (er != EINTR)
    printf("select: fail: %s\n", strerror(er));

  if (after - before < 3 && rv < 0 && er == EINTR && ctr == 1)
    printf("PASS: select returns EINTR\n");
  else
    printf("FAIL: select returns EINTR (got %d signals) (%d seconds)"
	   " (retval=%d)\n",
	   (int)ctr, (int)(after - before), (int)rv);

  return 0;
}