summaryrefslogtreecommitdiff
path: root/libtac/lib/read_wait.c
blob: 36ea19e43a2ab0ce8b0d0fda4c82565be33215b6 (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
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
/* read_wait.c - Wait for data to read on a fd.
 *
 * Copyright (C) 2011, Darren Besler (dbesler@beehive.mb.ca)
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program - see the file COPYING.
 *
 * See `CHANGES' file for revision history.
 */

#define _GNU_SOURCE /*  for POLLRDHUP */

#include <sys/time.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "libtac.h"

/* FIONREAD support for sun */
#ifdef sun
#include <sys/filio.h>
#endif

static int delta_msecs(struct timeval *newer, struct timeval *older) {
    time_t deltasecs;
    suseconds_t deltausecs;
    struct timeval now;

    if (newer == NULL) {
        gettimeofday(&now, NULL);
        newer = &now;
    }

    deltasecs = newer->tv_sec - older->tv_sec;
    if ( newer->tv_usec < older->tv_usec ) {
        deltasecs--;
        deltausecs = (1000000+newer->tv_usec) - older->tv_usec;
    } else {
        deltausecs = newer->tv_usec - older->tv_usec;
    }
    return (deltasecs*1000)+(deltausecs/1000);
}


/*
 * tac_read_wait
 *
 * Parms:
 *   fd       - open fd to wait on till data avail, or timeout
 *   timeout  - maximum time to wait in milliseconds
 *   size     - amount of data to wait for
 *           0 : any amount of data
 *          >0 : amount of data to wait for
 *   timeleft - return of time left from timeout
 *
 * Returns:
 *    0 - data avail, no timeout
 *   -1 - timeout
 *    n - errno
 */

int tac_read_wait(int fd, int timeout, int size, int *time_left) {
    int retval = 0;
    int remaining;
    struct pollfd fds[1];

    struct timeval start;

    gettimeofday(&start, NULL);

    /* setup for read timeout.
    *   will use poll() as it provides greatest compatibility
    *   vs setsockopt(SO_RCVTIMEO) which isn't supported on Solaris
    */

    remaining = timeout;  /* in msecs */

    fds[0].fd = fd;
    /*
     * should probably have a feature test for POLLRDHUP for non-linux.
     * It's been in linux and glibc for many years
     */
    fds[0].events = POLLIN | POLLRDHUP;

    while (remaining > 0) {
        int rc;
        int avail = 0;
        rc = poll(fds, 1, remaining);
        remaining = timeout - delta_msecs(NULL, &start);
        if (remaining < 0)
            remaining = 0;
        if ( time_left != NULL ) {
            *time_left = remaining;
        }

        /* why did poll return */
        if (rc == 0) {    /* Receive timeout */
            retval = -1;
            break;
        }

        if (rc > 0) {     /* there is data available */
            if (size > 0 &&    /* check for enuf available? */
                ioctl(fd,FIONREAD,(char*)&avail) == 0 && avail < size) {
                if(fds[0].revents & POLLRDHUP) {
                    /* other side closed the socket, stop polling */
                    retval = -1;
                    break;
                }
                continue;   /* not enuf yet, wait for more */
            } else {
                break;
            }
        }

        if (rc < 0 && errno == EINTR) {   /* interrupt */
            continue;
        }

        /* all other conditions is an error */
        retval = errno;
        break;
    }
    return remaining == 0 ? -1 : retval;
}    /* read_wait */