summaryrefslogtreecommitdiff
path: root/lib/ZWait4Not.c
blob: d6c54096576de467508c875ef2001f1a66a04868 (plain)
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
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the ZCheckIfNotice/select loop used for waiting for
 * a notice, with a timeout.
 *
 *	Created by:	<Joe Random Hacker>
 *
 *	$Id$
 *
 *	Copyright (c) 1991 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include "mit-copyright.h"

#ifndef lint
static const char rcsid_ZWaitForNotice_c[] = "$Id$";
#endif

#include <internal.h>
#include <sys/socket.h>

Code_t
Z_WaitForNotice(ZNotice_t *notice,
		int (*pred) __P((ZNotice_t *, void *)),
		void *arg,
		int timeout)
{
  Code_t retval;
  struct timeval tv, t0;
  fd_set fdmask;
  int i, fd;

  retval = ZCheckIfNotice (notice, (struct sockaddr_in *) 0, pred,
			   (char *) arg);
  if (retval == ZERR_NONE)
    return ZERR_NONE;
  if (retval != ZERR_NONOTICE)
    return retval;

  fd = ZGetFD ();
  FD_ZERO (&fdmask);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  gettimeofday (&t0, (struct timezone *) 0);
  t0.tv_sec += timeout;
  while (1) {
    FD_SET (fd, &fdmask);
    i = select (fd + 1, &fdmask, (fd_set *) 0, (fd_set *) 0, &tv);
    if (i == 0)
      return ETIMEDOUT;
    if (i < 0 && errno != EINTR)
      return errno;
    if (i > 0) {
      retval = ZCheckIfNotice (notice, (struct sockaddr_in *) 0, pred,
			       (char *) arg);
      if (retval != ZERR_NONOTICE) /* includes ZERR_NONE */
	return retval;
    }
    gettimeofday (&tv, (struct timezone *) 0);
    tv.tv_usec = t0.tv_usec - tv.tv_usec;
    if (tv.tv_usec < 0) {
      tv.tv_usec += 1000000;
      tv.tv_sec = t0.tv_sec - tv.tv_sec - 1;
    }
    else
      tv.tv_sec = t0.tv_sec - tv.tv_sec;
  }
  /*NOTREACHED*/
}