File: select.c

package info (click to toggle)
notion 3%2B2015061300-2
  • links: PTS, VCS
  • area: non-free
  • in suites: stretch
  • size: 4,904 kB
  • ctags: 6,037
  • sloc: ansic: 46,788; sh: 2,005; makefile: 548; perl: 270
file content (152 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (3)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * libmainloop/select.c
 * 
 * Partly based on a contributed code.
 *
 * See the included file LICENSE for details.
 */

#include <signal.h>
#include <sys/select.h>
#include <errno.h>
#include <string.h>

#include <libtu/types.h>
#include <libtu/misc.h>
#include <libtu/dlist.h>

#include "select.h"
#include "signal.h"


/*{{{ File descriptor management */


static WInputFd *input_fds=NULL;

static WInputFd *find_input_fd(int fd)
{
    WInputFd *tmp=input_fds;
    
    while(tmp){
        if(tmp->fd==fd)
            break;
        tmp=tmp->next;
    }
    return tmp;
}

bool mainloop_register_input_fd(int fd, void *data,
                                void (*callback)(int fd, void *d))
{
    WInputFd *tmp;
    
    if(find_input_fd(fd)!=NULL)
        return FALSE;
    
    tmp=ALLOC(WInputFd);
    if(tmp==NULL)
        return FALSE;
    
    tmp->fd=fd;
    tmp->data=data;
    tmp->process_input_fn=callback;
    
    LINK_ITEM(input_fds, tmp, next, prev);
    
    return TRUE;
}

void mainloop_unregister_input_fd(int fd)
{
    WInputFd *tmp=find_input_fd(fd);
    
    if(tmp!=NULL){
        UNLINK_ITEM(input_fds, tmp, next, prev);
        free(tmp);
    }
}

static void set_input_fds(fd_set *rfds, int *nfds)
{
    WInputFd *tmp=input_fds;
    
    while(tmp){
        FD_SET(tmp->fd, rfds);
        if(tmp->fd>*nfds)
            *nfds=tmp->fd;
        tmp=tmp->next;
    }
}

static void check_input_fds(fd_set *rfds)
{
    WInputFd *tmp=input_fds, *next=NULL;
    
    while(tmp){
        next=tmp->next;
        if(FD_ISSET(tmp->fd, rfds))
            tmp->process_input_fn(tmp->fd, tmp->data);
        tmp=next;
    }
}

/*}}}*/


/*{{{ Select */

void mainloop_select()
{
    fd_set rfds;
    int nfds=0;
    int ret=0;
    
    
#if _POSIX_SELECT || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
    {
        sigset_t oldmask;

        FD_ZERO(&rfds);
        set_input_fds(&rfds, &nfds);
        
        mainloop_block_signals(&oldmask);
        
        if(!mainloop_unhandled_signals())
            ret=pselect(nfds+1, &rfds, NULL, NULL, NULL, &oldmask);
        
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
    }
#else
    #warning "pselect() unavailable -- using dirty hacks"
    {
        struct timeval tv={0, 0};
        bool to;
        
        /* If there are timers, make sure we return from select with 
         * some delay, if the timer signal happens right before
         * entering select(). Race conditions with other signals
         * we'll just have to ignore without pselect().
         */
        do{
            bool to;
            FD_ZERO(&rfds);
            set_input_fds(&rfds, &nfds);
            
            to=libmainloop_get_timeout(&tv);
            
            if(mainloop_unhandled_signals()){
                ret=0;
                break;
            }
            
            ret=select(nfds+1, &rfds, NULL, NULL, to ? &tv : NULL);
        }while(ret<0 && errno==EINTR && !mainloop_unhandled_signals());
    }
#endif
    if(ret>0)
        check_input_fds(&rfds);
}


/*}}}*/