summaryrefslogtreecommitdiff
path: root/poll/unix/wakeup.c
blob: bb03564448426081d805debb01e410566950aa76 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "apr.h"
#include "apr_poll.h"
#include "apr_time.h"
#include "apr_portable.h"
#include "apr_atomic.h"
#include "apr_arch_file_io.h"
#include "apr_arch_networkio.h"
#include "apr_arch_poll_private.h"
#include "apr_arch_inherit.h"

#if !APR_FILES_AS_SOCKETS

#ifdef WIN32

apr_status_t apr_poll_create_wakeup_socket(apr_pool_t *pool, apr_pollfd_t *pfd,
                                           apr_socket_t **wakeup_socket)
{
    apr_status_t rv;

    if ((rv = apr_socket_pipe_create(&wakeup_socket[0], &wakeup_socket[1],
                                          pool)) != APR_SUCCESS)
        return rv;

    pfd->reqevents = APR_POLLIN;
    pfd->desc_type = APR_POLL_SOCKET;
    pfd->desc.s = wakeup_socket[0];
    return APR_SUCCESS;
}

apr_status_t apr_poll_close_wakeup_socket(apr_socket_t **wakeup_socket)
{
    apr_status_t rv0 = APR_SUCCESS;
    apr_status_t rv1 = APR_SUCCESS;

    /* Close both sides of the wakeup pipe */
    if (wakeup_socket[0]) {
        rv0 = apr_socket_pipe_close(wakeup_socket[0]);
        wakeup_socket[0] = NULL;
    }
    if (wakeup_socket[1]) {
        rv1 = apr_socket_pipe_close(wakeup_socket[1]);
        wakeup_socket[1] = NULL;
    }
    return rv0 ? rv0 : rv1;
}

#else /* !WIN32 */

apr_status_t apr_poll_create_wakeup_pipe(apr_pollfd_t *pfd, apr_file_t **wakeup_pipe)
{
    return APR_ENOTIMPL;
}

apr_status_t apr_poll_close_wakeup_pipe(apr_file_t **wakeup_pipe)
{
    return APR_ENOTIMPL;
}

#endif /* !WIN32 */

#else  /* APR_FILES_AS_SOCKETS */

apr_status_t apr_poll_create_wakeup_pipe(apr_pool_t *pool, apr_pollfd_t *pfd,
                                         apr_file_t **wakeup_pipe)
{
    apr_status_t rv;

    /* Read end of the pipe is non-blocking */
    if ((rv = apr_file_pipe_create_ex(&wakeup_pipe[0], &wakeup_pipe[1],
                                      APR_WRITE_BLOCK, pool)))
        return rv;

    pfd->p = pool;
    pfd->reqevents = APR_POLLIN;
    pfd->desc_type = APR_POLL_FILE;
    pfd->desc.f = wakeup_pipe[0];

    {
        int flags;

        if ((flags = fcntl(wakeup_pipe[0]->filedes, F_GETFD)) == -1)
            return errno;

        flags |= FD_CLOEXEC;
        if (fcntl(wakeup_pipe[0]->filedes, F_SETFD, flags) == -1)
            return errno;
    }
    {
        int flags;

        if ((flags = fcntl(wakeup_pipe[1]->filedes, F_GETFD)) == -1)
            return errno;

        flags |= FD_CLOEXEC;
        if (fcntl(wakeup_pipe[1]->filedes, F_SETFD, flags) == -1)
            return errno;
    }

    return APR_SUCCESS;
}

apr_status_t apr_poll_close_wakeup_pipe(apr_file_t **wakeup_pipe)
{
    apr_status_t rv0 = APR_SUCCESS;
    apr_status_t rv1 = APR_SUCCESS;

    /* Close both sides of the wakeup pipe */
    if (wakeup_pipe[0]) {
        rv0 = apr_file_close(wakeup_pipe[0]);
        wakeup_pipe[0] = NULL;
    }
    if (wakeup_pipe[1]) {
        rv1 = apr_file_close(wakeup_pipe[1]);
        wakeup_pipe[1] = NULL;
    }
    return rv0 ? rv0 : rv1;
}

#endif /* APR_FILES_AS_SOCKETS */

#if WAKEUP_USES_PIPE
/* Read and discard whatever is in the wakeup pipe.
 */
void apr_poll_drain_wakeup_pipe(volatile apr_uint32_t *wakeup_set, apr_file_t **wakeup_pipe)
{
    char ch;

    (void)apr_file_getc(&ch, wakeup_pipe[0]);
    apr_atomic_set32(wakeup_set, 0);
}
#else
/* Read and discard whatever is in the wakeup socket.
 */
void apr_poll_drain_wakeup_socket(volatile apr_uint32_t *wakeup_set, apr_socket_t **wakeup_socket)
{
    char ch;
    apr_size_t len;

    (void)apr_socket_recv(wakeup_socket[0], &ch, &len);
    apr_atomic_set32(wakeup_set, 0);
}
#endif