Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/ |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | #include <ngx_event.h> |
| 10 | |
| 11 | |
| 12 | #if (TEST_BUILD_RTSIG) |
| 13 | |
| 14 | #define F_SETSIG 10 |
| 15 | #define SIGRTMIN 33 |
| 16 | #define si_fd __spare__[0] |
| 17 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 18 | int sigtimedwait(const sigset_t *set, siginfo_t *info, |
| 19 | const struct timespec *timeout); |
| 20 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 21 | |
| 22 | int sigtimedwait(const sigset_t *set, siginfo_t *info, |
| 23 | const struct timespec *timeout) |
| 24 | { |
| 25 | return -1; |
| 26 | } |
| 27 | |
| 28 | #endif |
| 29 | |
| 30 | |
| 31 | typedef struct { |
| 32 | int signo; |
| 33 | } ngx_rtsig_conf_t; |
| 34 | |
| 35 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 36 | extern ngx_event_module_t ngx_poll_module_ctx; |
| 37 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 38 | static int ngx_rtsig_init(ngx_cycle_t *cycle); |
| 39 | static void ngx_rtsig_done(ngx_cycle_t *cycle); |
| 40 | static int ngx_rtsig_add_connection(ngx_connection_t *c); |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 41 | static int ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 42 | static int ngx_rtsig_process_events(ngx_log_t *log); |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 43 | static int ngx_rtsig_process_overlow(ngx_log_t *log); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 44 | |
| 45 | static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle); |
| 46 | static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf); |
| 47 | |
| 48 | |
| 49 | static sigset_t set; |
| 50 | |
| 51 | |
| 52 | static ngx_str_t rtsig_name = ngx_string("rtsig"); |
| 53 | |
| 54 | static ngx_command_t ngx_rtsig_commands[] = { |
| 55 | |
| 56 | {ngx_string("rtsig_signo"), |
| 57 | NGX_EVENT_CONF|NGX_CONF_TAKE1, |
| 58 | ngx_conf_set_num_slot, |
| 59 | 0, |
| 60 | offsetof(ngx_rtsig_conf_t, signo), |
| 61 | NULL}, |
| 62 | |
| 63 | ngx_null_command |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | ngx_event_module_t ngx_rtsig_module_ctx = { |
| 68 | &rtsig_name, |
| 69 | ngx_rtsig_create_conf, /* create configuration */ |
| 70 | ngx_rtsig_init_conf, /* init configuration */ |
| 71 | |
| 72 | { |
| 73 | NULL, /* add an event */ |
| 74 | NULL, /* delete an event */ |
| 75 | NULL, /* enable an event */ |
| 76 | NULL, /* disable an event */ |
| 77 | ngx_rtsig_add_connection, /* add an connection */ |
| 78 | ngx_rtsig_del_connection, /* delete an connection */ |
| 79 | ngx_rtsig_process_events, /* process the events */ |
| 80 | ngx_rtsig_init, /* init the events */ |
| 81 | ngx_rtsig_done, /* done the events */ |
| 82 | } |
| 83 | |
| 84 | }; |
| 85 | |
| 86 | ngx_module_t ngx_rtsig_module = { |
| 87 | NGX_MODULE, |
| 88 | &ngx_rtsig_module_ctx, /* module context */ |
| 89 | ngx_rtsig_commands, /* module directives */ |
| 90 | NGX_EVENT_MODULE, /* module type */ |
| 91 | NULL, /* init module */ |
| 92 | NULL /* init child */ |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | static int ngx_rtsig_init(ngx_cycle_t *cycle) |
| 97 | { |
| 98 | ngx_rtsig_conf_t *rtscf; |
| 99 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 100 | if (ngx_poll_module_ctx.actions.init(cycle) == NGX_ERROR) { |
| 101 | return NGX_ERROR; |
| 102 | } |
| 103 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 104 | rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module); |
| 105 | |
| 106 | sigemptyset(&set); |
| 107 | sigaddset(&set, rtscf->signo); |
| 108 | sigaddset(&set, SIGIO); |
| 109 | |
| 110 | if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { |
| 111 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 112 | "sigprocmask() failed"); |
| 113 | return NGX_ERROR; |
| 114 | } |
| 115 | |
| 116 | ngx_io = ngx_os_io; |
| 117 | |
| 118 | ngx_event_actions = ngx_rtsig_module_ctx.actions; |
| 119 | |
| 120 | ngx_event_flags = NGX_USE_SIGIO_EVENT; |
| 121 | |
| 122 | return NGX_OK; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static void ngx_rtsig_done(ngx_cycle_t *cycle) |
| 127 | { |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 128 | ngx_poll_module_ctx.actions.done(cycle); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | static int ngx_rtsig_add_connection(ngx_connection_t *c) |
| 133 | { |
| 134 | ngx_rtsig_conf_t *rtscf; |
| 135 | |
| 136 | rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module); |
| 137 | |
| 138 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
| 139 | "rtsig add connection: fd:%d signo:%d", c->fd, rtscf->signo); |
| 140 | |
| 141 | if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) { |
| 142 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
| 143 | "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed"); |
| 144 | return NGX_ERROR; |
| 145 | } |
| 146 | |
| 147 | if (fcntl(c->fd, F_SETSIG, rtscf->signo) == -1) { |
| 148 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
| 149 | "fcntl(F_SETSIG) failed"); |
| 150 | return NGX_ERROR; |
| 151 | } |
| 152 | |
| 153 | if (fcntl(c->fd, F_SETOWN, ngx_getpid()) == -1) { |
| 154 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
| 155 | "fcntl(F_SETOWN) failed"); |
| 156 | return NGX_ERROR; |
| 157 | } |
| 158 | |
| 159 | #if (HAVE_ONESIGFD) |
| 160 | if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) { |
| 161 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
| 162 | "fcntl(F_SETAUXFL) failed"); |
| 163 | return NGX_ERROR; |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | c->read->active = 1; |
| 168 | c->write->active = 1; |
| 169 | |
| 170 | return NGX_OK; |
| 171 | } |
| 172 | |
| 173 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 174 | static int ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags) |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 175 | { |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 176 | if (!(flags & NGX_CLOSE_EVENT)) { |
| 177 | if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) { |
| 178 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
| 179 | "fcntl(O_RDWR|O_NONBLOCK) failed"); |
| 180 | return NGX_ERROR; |
| 181 | } |
| 182 | } |
| 183 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 184 | c->read->active = 0; |
| 185 | c->write->active = 0; |
| 186 | |
| 187 | return NGX_OK; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | int ngx_rtsig_process_events(ngx_log_t *log) |
| 192 | { |
| 193 | int signo; |
| 194 | ngx_int_t instance, i; |
| 195 | size_t n; |
| 196 | ngx_msec_t timer; |
| 197 | ngx_err_t err; |
| 198 | ngx_cycle_t **cycle; |
| 199 | siginfo_t si; |
| 200 | struct timeval tv; |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 201 | struct timespec ts, *tp; |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 202 | struct sigaction sa; |
| 203 | ngx_connection_t *c; |
| 204 | ngx_epoch_msec_t delta; |
| 205 | ngx_rtsig_conf_t *rtscf; |
| 206 | |
| 207 | timer = ngx_event_find_timer(); |
| 208 | ngx_old_elapsed_msec = ngx_elapsed_msec; |
| 209 | |
| 210 | if (timer) { |
| 211 | ts.tv_sec = timer / 1000; |
| 212 | ts.tv_nsec = (timer % 1000) * 1000000; |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 213 | tp = &ts; |
| 214 | |
| 215 | } else { |
| 216 | tp = NULL; |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0, "rtsig timer: %d", timer); |
| 220 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 221 | /* Linux sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */ |
| 222 | |
| 223 | signo = sigtimedwait(&set, &si, tp); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 224 | |
| 225 | if (signo == -1) { |
| 226 | err = ngx_errno; |
| 227 | } else { |
| 228 | err = 0; |
| 229 | } |
| 230 | |
| 231 | ngx_gettimeofday(&tv); |
| 232 | ngx_time_update(tv.tv_sec); |
| 233 | |
| 234 | delta = ngx_elapsed_msec; |
| 235 | ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec; |
| 236 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 237 | if (err) { |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 238 | ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT, |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 239 | log, err, "sigtimedwait() failed"); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 240 | return NGX_ERROR; |
| 241 | } |
| 242 | |
| 243 | if (timer) { |
| 244 | delta = ngx_elapsed_msec - delta; |
| 245 | |
| 246 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0, |
| 247 | "rtsig timer: %d, delta: %d", timer, (int) delta); |
| 248 | } |
| 249 | |
| 250 | ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0, |
| 251 | "signo:%d fd:%d band:%X", signo, si.si_fd, si.si_band); |
| 252 | |
| 253 | rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module); |
| 254 | |
| 255 | if (signo == rtscf->signo) { |
| 256 | |
| 257 | /* TODO: old_cycles */ |
| 258 | c = &ngx_cycle->connections[si.si_fd]; |
| 259 | |
| 260 | /* TODO: stale signals */ |
| 261 | |
| 262 | if (si.si_band & (POLLIN|POLLHUP|POLLERR)) { |
| 263 | if (c->read->active) { |
| 264 | c->read->ready = 1; |
| 265 | c->read->event_handler(c->read); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) { |
| 270 | if (c->write->active) { |
| 271 | c->write->ready = 1; |
| 272 | c->write->event_handler(c->write); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | } else if (signo == SIGIO) { |
| 277 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
| 278 | "signal queue overflowed: " |
| 279 | "SIGIO, fd:%d, band:%X", si.si_fd, si.si_band); |
| 280 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 281 | /* TODO: flush all the used RT signals */ |
| 282 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 283 | ngx_memzero(&sa, sizeof(struct sigaction)); |
| 284 | sa.sa_handler = SIG_DFL; |
| 285 | sigemptyset(&sa.sa_mask); |
| 286 | if (sigaction(rtscf->signo, &sa, NULL) == -1) { |
| 287 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
| 288 | "sigaction(%d, SIG_DFL) failed", rtscf->signo); |
| 289 | } |
| 290 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 291 | ngx_event_actions = ngx_poll_module_ctx.actions; |
| 292 | ngx_event_actions.process = ngx_rtsig_process_overflow; |
| 293 | ngx_event_flags = NGX_OVERFLOW_EVENT |
| 294 | |NGX_USE_LEVEL_EVENT|NGX_USE_ONESHOT_EVENT; |
| 295 | |
| 296 | /* STUB: add events. WHAT to do with fcntl()s ? */ |
| 297 | |
| 298 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 299 | } else { |
| 300 | ngx_log_error(NGX_LOG_ALERT, log, 0, |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 301 | "sigtimedwait() returned unexpected signal: %d", signo); |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 302 | return NGX_ERROR; |
| 303 | } |
| 304 | |
| 305 | if (timer != (ngx_msec_t) -1 && delta) { |
| 306 | ngx_event_expire_timers((ngx_msec_t) delta); |
| 307 | } |
| 308 | |
| 309 | return NGX_OK; |
| 310 | } |
| 311 | |
| 312 | |
Igor Sysoev | 22a7c50 | 2004-02-17 21:11:27 +0000 | [diff] [blame^] | 313 | static int ngx_rtsig_process_overlow(ngx_log_t *log) |
| 314 | { |
| 315 | if (ngx_poll_module_ctx.actions.process(log) == NGX_OK) { |
| 316 | ngx_event_actions = ngx_rtsig_module_ctx.actions; |
| 317 | ngx_event_flags = NGX_USE_SIGIO_EVENT; |
| 318 | } |
| 319 | |
| 320 | return NGX_OK; |
| 321 | } |
| 322 | |
| 323 | |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 324 | static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle) |
| 325 | { |
| 326 | ngx_rtsig_conf_t *rtscf; |
| 327 | |
| 328 | ngx_test_null(rtscf, ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t)), |
| 329 | NGX_CONF_ERROR); |
| 330 | |
| 331 | rtscf->signo = NGX_CONF_UNSET; |
| 332 | |
| 333 | return rtscf; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf) |
| 338 | { |
| 339 | ngx_rtsig_conf_t *rtscf = conf; |
| 340 | |
| 341 | /* LinuxThreads use the first 3 RT signals */ |
| 342 | ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10); |
| 343 | |
| 344 | return NGX_CONF_OK; |
| 345 | } |