diff --git a/mio/bin/t01.c b/mio/bin/t01.c index 1bc3d9a..40db095 100644 --- a/mio/bin/t01.c +++ b/mio/bin/t01.c @@ -671,8 +671,12 @@ static int x = 0; int y; int z = 0; - //y = ++x; +#if defined(__ATOMIC_RELAXED) y = __atomic_add_fetch (&x, 1, __ATOMIC_RELAXED); +#else + // this is buggy.. + y = ++x; +#endif while ((n = read(iop->rfd, buf, MIO_COUNTOF(buf)))> 0) write (iop->wfd, buf, n); @@ -1031,6 +1035,7 @@ int main (int argc, char* argv[]) } + memset (&tcp_lstn, 0, MIO_SIZEOF(tcp_lstn)); tcp_lstn.backlogs = 100; if (mio_dev_sck_listen(tcp[1], &tcp_lstn) <= -1) { @@ -1139,7 +1144,8 @@ for (i = 0; i < 5; i++) dnc = mio_svc_dnc_start(mio, &servaddr, MIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */ htts = mio_svc_htts_start(mio, &htts_bind_addr, process_http_request); - mio_svc_htts_setservernamewithbcstr (htts, "MIO-HTTP"); + if (htts) mio_svc_htts_setservernamewithbcstr (htts, "MIO-HTTP"); + else MIO_INFO1 (mio, "UNABLE TO START HTTS - %js\n", mio_geterrmsg(mio)); #if 1 { @@ -1258,6 +1264,7 @@ if (!mio_svc_dnc_resolve(dnc, "google.com", MIO_DNS_RRT_SOA, MIO_SVC_DNC_RESOLVE } #endif +#if 0 { mio_dev_pipe_t* pp; mio_dev_pipe_make_t mi; @@ -1281,6 +1288,7 @@ for (i = 0; i < 20; i++) mio_dev_thr_write (tt, "hello, world", 12, MIO_NULL); mio_dev_thr_write (tt, MIO_NULL, 0, MIO_NULL); } +#endif mio_loop (mio); diff --git a/mio/lib/http-svr.c b/mio/lib/http-svr.c index a5300c7..28b4290 100644 --- a/mio/lib/http-svr.c +++ b/mio/lib/http-svr.c @@ -395,7 +395,7 @@ mio_svc_htts_t* mio_svc_htts_start (mio_t* mio, const mio_skad_t* bind_addr, mio MIO_MEMSET (&info, 0, MIO_SIZEOF(info)); info.b.localaddr = *bind_addr; - info.b.options = MIO_DEV_SCK_BIND_REUSEADDR | MIO_DEV_SCK_BIND_REUSEPORT; + info.b.options = MIO_DEV_SCK_BIND_REUSEADDR | MIO_DEV_SCK_BIND_REUSEPORT | MIO_DEV_SCK_BIND_IGNERR; /*info.b.options |= MIO_DEV_SCK_BIND_SSL; */ info.b.ssl_certfile = "localhost.crt"; info.b.ssl_keyfile = "localhost.key"; @@ -826,6 +826,7 @@ static int cgi_peer_on_read (mio_dev_pro_t* pro, mio_dev_pro_sid_t sid, const vo MIO_ASSERT (mio, !(cgi_state->over & CGI_STATE_OVER_READ_FROM_PEER)); +printf ("FEED %d BYTES TO HTRD\n", (int)dlen); if (mio_htrd_feed(cgi_state->peer_htrd, data, dlen, &rem) <= -1) { MIO_DEBUG3 (mio, "HTTPS(%p) - unable to feed peer into to htrd - peer %p(pid=%u)\n", cgi_state->htts, pro, (unsigned int)pro->child_pid); @@ -964,7 +965,9 @@ static int cgi_peer_htrd_push_content (mio_htrd_t* htrd, mio_htre_t* req, const mio_bch_t lbuf[16]; mio_oow_t llen; - llen = mio_fmt_uintmax_to_bcstr(lbuf, MIO_COUNTOF(lbuf) - 2, dlen, 16 | MIO_FMT_UINTMAX_UPPERCASE, 0, '\0', MIO_NULL); + /* mio_fmt_uintmax_to_bcstr() null-terminates the output. only MIO_COUNTOF(lbuf) - 1 + * is enough to hold '\r' and '\n' at the back without '\0'. */ + llen = mio_fmt_uintmax_to_bcstr(lbuf, MIO_COUNTOF(lbuf) - 1, dlen, 16 | MIO_FMT_UINTMAX_UPPERCASE, 0, '\0', MIO_NULL); lbuf[llen++] = '\r'; lbuf[llen++] = '\n'; diff --git a/mio/lib/mio-sck.h b/mio/lib/mio-sck.h index af57579..edc7450 100644 --- a/mio/lib/mio-sck.h +++ b/mio/lib/mio-sck.h @@ -315,6 +315,7 @@ enum mio_dev_sck_bind_option_t /* TODO: more options --- SO_RCVBUF, SO_SNDBUF, SO_RCVTIMEO, SO_SNDTIMEO, SO_KEEPALIVE */ /* BINDTODEVICE??? */ + MIO_DEV_SCK_BIND_IGNERR = (1 << 14), /* ignore non-critical error in binding */ MIO_DEV_SCK_BIND_SSL = (1 << 15) }; typedef enum mio_dev_sck_bind_option_t mio_dev_sck_bind_option_t; diff --git a/mio/lib/mio.c b/mio/lib/mio.c index d95628d..344f5ff 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -1429,12 +1429,11 @@ static int __dev_writev (mio_dev_t* dev, mio_iovec_t* iov, mio_iolen_t iovcnt, c len = 0; for (i = 0; i < iovcnt; i++) len += iov[i].iov_len; + urem = len; if (!MIO_WQ_IS_EMPTY(&dev->wq)) { - /* the writing queue is not empty. - * enqueue this request immediately */ - urem = len; + /* if the writing queue is not empty, enqueue this request immediately */ goto enqueue_data; } @@ -1523,7 +1522,7 @@ enqueue_data: /* queue the remaining data*/ q = (mio_wq_t*)mio_allocmem(mio, MIO_SIZEOF(*q) + (dstaddr? dstaddr->len: 0) + urem); - if (!q) return -1; + if (MIO_UNLIKELY(!q)) return -1; q->tmridx = MIO_TMRIDX_INVALID; q->dev = dev; @@ -1542,7 +1541,7 @@ enqueue_data: q->ptr = (mio_uint8_t*)(q + 1) + q->dstaddr.len; q->len = urem; - q->olen = len; + q->olen = len; /* original length to use when invoking on_write() */ for (i = index, j = 0; i < iovcnt; i++) { MIO_MEMCPY (&q->ptr[j], iov[i].iov_ptr, iov[i].iov_len); diff --git a/mio/lib/mio.h b/mio/lib/mio.h index 027dcda..b2b1e7c 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -1003,6 +1003,12 @@ MIO_EXPORT void mio_freemem ( void* ptr ); +MIO_EXPORT void mio_addcfmb ( + mio_t* mio, + mio_cfmb_t* cfmb, + mio_cfmb_checker_t checker +); + /* ========================================================================= * STRING ENCODING CONVERSION * ========================================================================= */ diff --git a/mio/lib/pro.c b/mio/lib/pro.c index 9c51dde..12d003f 100644 --- a/mio/lib/pro.c +++ b/mio/lib/pro.c @@ -205,8 +205,19 @@ static pid_t standard_fork_and_exec (mio_dev_pro_t* dev, int pfds[], mio_dev_pro devnull = open("/dev/null", O_RDWR, 0); #endif if (devnull == MIO_SYSHND_INVALID) goto slave_oops; + + if ((mi->flags & MIO_DEV_PRO_INTONUL) && dup2(devnull, 0) == -1) goto slave_oops; + if ((mi->flags & MIO_DEV_PRO_OUTTONUL) && dup2(devnull, 1) == -1) goto slave_oops; + if ((mi->flags & MIO_DEV_PRO_ERRTONUL) && dup2(devnull, 2) == -1) goto slave_oops; + + close (devnull); + devnull = MIO_SYSHND_INVALID; } + if (mi->flags & MIO_DEV_PRO_DROPIN) close (0); + if (mi->flags & MIO_DEV_PRO_DROPOUT) close (1); + if (mi->flags & MIO_DEV_PRO_DROPERR) close (2); + execv (param->argv[0], param->argv); /* if exec fails, free 'param' parameter which is an inherited pointer */ diff --git a/mio/lib/sck.c b/mio/lib/sck.c index a015e8c..4e4a85f 100644 --- a/mio/lib/sck.c +++ b/mio/lib/sck.c @@ -809,8 +809,11 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt(rdev->hnd, SOL_SOCKET, SO_REUSEADDR, &v, MIO_SIZEOF(v)) == -1) { - mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEADDR"); - return -1; + if (!(bnd->options & MIO_DEV_SCK_BIND_IGNERR)) + { + mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEADDR"); + return -1; + } } /* ignore it if not available #else @@ -826,8 +829,11 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt(rdev->hnd, SOL_SOCKET, SO_REUSEPORT, &v, MIO_SIZEOF(v)) == -1) { - mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEPORT"); - return -1; + if (!(bnd->options & MIO_DEV_SCK_BIND_IGNERR)) + { + mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEPORT"); + return -1; + } } /* ignore it if not available #else diff --git a/mio/lib/thr.c b/mio/lib/thr.c index 0facd23..26e924f 100644 --- a/mio/lib/thr.c +++ b/mio/lib/thr.c @@ -34,6 +34,17 @@ /* ========================================================================= */ +struct mio_dev_thr_info_t +{ + MIO_CFMB_HEADER; + + mio_dev_thr_func_t thr_func; + mio_dev_thr_iopair_t thr_iop; + void* thr_ctx; + pthread_t thr_hnd; + int thr_done; +}; + struct slave_info_t { mio_dev_thr_make_t* mi; @@ -48,19 +59,6 @@ static mio_dev_thr_slave_t* make_slave (mio_t* mio, slave_info_t* si); /* ========================================================================= */ -struct mio_dev_thr_info_t -{ - MIO_CFMB_HEADER; - - mio_dev_thr_func_t thr_func; - mio_dev_thr_iopair_t thr_iop; - void* thr_ctx; - pthread_t thr_hnd; - int thr_done; -}; - -typedef struct mio_dev_thr_info_t mio_dev_thr_info_t; - static void free_thr_info_resources (mio_t* mio, mio_dev_thr_info_t* ti) {