From 99cf88a17a11832b751fe789ef6875339f019afb Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 6 Feb 2023 22:30:56 +0900 Subject: [PATCH] fixing fcgi issues --- lib/fcgi-cli.c | 35 ++++--- lib/hio-fcgi.h | 13 ++- lib/hio-http.h | 22 +++- lib/hio.c | 9 +- lib/hio.h | 42 +++++--- lib/http-cgi.c | 11 +- lib/http-fcgi.c | 273 +++++++++++++++++++++++++++++++++--------------- lib/http-file.c | 9 +- lib/http-thr.c | 3 - lib/http-txt.c | 9 +- lib/thr.c | 2 +- 11 files changed, 288 insertions(+), 140 deletions(-) diff --git a/lib/fcgi-cli.c b/lib/fcgi-cli.c index cc6a858..dd746ab 100644 --- a/lib/fcgi-cli.c +++ b/lib/fcgi-cli.c @@ -43,6 +43,8 @@ struct hio_svc_fcgic_t struct hio_svc_fcgic_conn_t { + HIO_CFMB_HEADER; + hio_svc_fcgic_t* fcgic; hio_skad_t addr; hio_dev_sck_t* dev; @@ -76,16 +78,6 @@ struct hio_svc_fcgic_conn_t hio_svc_fcgic_conn_t* next; }; -struct hio_svc_fcgic_sess_t -{ - int active; - hio_oow_t sid; - hio_svc_fcgic_conn_t* conn; - hio_svc_fcgic_on_read_t on_read; - hio_svc_fcgic_on_untie_t on_untie; - void* ctx; -}; - struct fcgic_sck_xtn_t { hio_svc_fcgic_conn_t* conn; @@ -340,9 +332,7 @@ static int make_connection_socket (hio_svc_fcgic_conn_t* conn) return -1; } -printf ("MAKING CONNECTION %p %p\n", conn->dev, sck); HIO_ASSERT (hio, conn->dev == HIO_NULL); - conn->dev = sck; return 0; } @@ -388,6 +378,13 @@ static hio_svc_fcgic_conn_t* get_connection (hio_svc_fcgic_t* fcgic, const hio_s return conn; } +static void destroy_connection_memory (hio_t* hio, hio_cfmb_t* cfmb) +{ + hio_svc_fcgic_conn_t* conn = (hio_svc_fcgic_conn_t*)cfmb; + if (conn->sess.ptr) hio_freemem (hio, conn->sess.ptr); + hio_freemem (hio, conn); +} + static void free_connections (hio_svc_fcgic_t* fcgic) { hio_t* hio = fcgic->hio; @@ -404,8 +401,9 @@ static void free_connections (hio_svc_fcgic_t* fcgic) sck_xtn->conn = HIO_NULL; hio_dev_sck_halt (conn->dev); } - hio_freemem (hio, conn->sess.ptr); - hio_freemem (hio, conn); + + /* delay destruction of conn->session.ptr and conn */ + hio_addcfmb (hio, conn, HIO_NULL, destroy_connection_memory); conn = next; } } @@ -425,7 +423,7 @@ static hio_svc_fcgic_sess_t* new_session (hio_svc_fcgic_t* fcgic, const hio_skad hio_svc_fcgic_sess_t* newptr; newcapa = conn->sess.capa + CONN_SESS_INC; - newptr = hio_reallocmem(hio, conn->sess.ptr, HIO_SIZEOF(*sess) * newcapa); + newptr = (hio_svc_fcgic_sess_t*)hio_reallocmem(hio, conn->sess.ptr, HIO_SIZEOF(*sess) * newcapa); if (HIO_UNLIKELY(!newptr)) return HIO_NULL; for (i = conn->sess.capa ; i < newcapa; i++) @@ -508,6 +506,12 @@ void hio_svc_fcgic_stop (hio_svc_fcgic_t* fcgic) hio_svc_fcgic_sess_t* hio_svc_fcgic_tie (hio_svc_fcgic_t* fcgic, const hio_skad_t* addr, hio_svc_fcgic_on_read_t on_read, hio_svc_fcgic_on_untie_t on_untie, void* ctx) { /* TODO: reference counting for safety?? */ +#if 0 + hio_svc_fcgic_sess_t* sess; + sess = new_session(fcgic, addr, on_read, on_untie, ctx); + if (HIO_UNLIKELY(!sess)) return -1; + return sess->sid; +#endif return new_session(fcgic, addr, on_read, on_untie, ctx); } @@ -631,6 +635,7 @@ int hio_svc_fcgic_writestdin (hio_svc_fcgic_sess_t* sess, const void* data, hio_ hio_iovec_t iov[2]; hio_fcgi_record_header_t h; +printf (">>>>>>>>>>>>>>>>>>>>>>[%p] %p\n", sess, sess->conn); if (!sess->conn->dev) { /* TODO: set error **/ diff --git a/lib/hio-fcgi.h b/lib/hio-fcgi.h index d0a2ed0..7599f2f 100644 --- a/lib/hio-fcgi.h +++ b/lib/hio-fcgi.h @@ -120,8 +120,6 @@ struct hio_svc_fcgic_tmout_t typedef struct hio_svc_fcgic_sess_t hio_svc_fcgic_sess_t; typedef struct hio_svc_fcgic_conn_t hio_svc_fcgic_conn_t; -/* ---------------------------------------------------------------- */ - typedef int (*hio_svc_fcgic_on_read_t) ( hio_svc_fcgic_sess_t* sess, const void* data, @@ -134,6 +132,17 @@ typedef void (*hio_svc_fcgic_on_untie_t) ( void* ctx; ); +struct hio_svc_fcgic_sess_t +{ + int active; + hio_oow_t sid; + hio_svc_fcgic_conn_t* conn; + hio_svc_fcgic_on_read_t on_read; + hio_svc_fcgic_on_untie_t on_untie; + void* ctx; +}; + + /* ---------------------------------------------------------------- */ #if defined(__cplusplus) extern "C" { diff --git a/lib/hio-http.h b/lib/hio-http.h index 5d89502..570f53c 100644 --- a/lib/hio-http.h +++ b/lib/hio-http.h @@ -100,9 +100,27 @@ struct hio_svc_htts_task_t HIO_SVC_HTTS_TASK_HEADER; }; +#define HIO_SVC_HTTS_TASK_RCUP(task) (++(task)->task_refcnt) -#define HIO_SVC_HTTS_TASK_REF(task, var) do { (var) = (task); ++(task)->task_refcnt; } while(0) -#define HIO_SVC_HTTS_TASK_UNREF(task_var) do { if (--(task_var)->task_refcnt == 0) { hio_svc_htts_task_t* __task_tmp = (hio_svc_htts_task_t*)(task_var); (task_var) = HIO_NULL; hio_svc_htts_task_kill(__task_tmp); } else { (task_var) = HIO_NULL; } } while(0) +#define HIO_SVC_HTTS_TASK_RCDOWN(task_var) do { \ + if (--(task_var)->task_refcnt == 0) hio_svc_htts_task_kill(task_var); \ +} while(0) + +#define HIO_SVC_HTTS_TASK_REF(task, var) do { \ + (var) = (task); \ + HIO_SVC_HTTS_TASK_RCUP(task); \ +} while(0) + +#define HIO_SVC_HTTS_TASK_UNREF(task_var) do { \ + if (--(task_var)->task_refcnt == 0) { \ + hio_svc_htts_task_t* __task_tmp = (hio_svc_htts_task_t*)(task_var); \ + (task_var) = HIO_NULL; \ + hio_svc_htts_task_kill(__task_tmp); \ + } \ + else { \ + (task_var) = HIO_NULL; \ + } \ +} while(0) /* -------------------------------------------------------------- */ diff --git a/lib/hio.c b/lib/hio.c index 89dcd3d..d3c20cb 100644 --- a/lib/hio.c +++ b/lib/hio.c @@ -862,10 +862,11 @@ static void clear_unneeded_cfmbs (hio_t* hio) while (!HIO_CFMBL_IS_NIL_CFMB(&hio->cfmb, cur)) { next = HIO_CFMBL_NEXT_CFMB(cur); - if (cur->cfmb_checker(hio, cur)) + if (!cur->cfmb_checker || cur->cfmb_checker(hio, cur)) { HIO_CFMBL_UNLINK_CFMB (cur); - hio_freemem (hio, cur); + /*hio_freemem (hio, cur);*/ + cur->cfmb_freeer (hio, cur); } cur = next; } @@ -879,6 +880,7 @@ static void kill_all_halted_devices (hio_t* hio) hio_dev_t* dev = HIO_DEVL_FIRST_DEV(&hio->hltdev); HIO_DEBUG1 (hio, "MIO - Killing HALTED device %p\n", dev); hio_dev_kill (dev); + HIO_DEBUG1 (hio, "MIO - Killed HALTED device %p\n", dev); } } @@ -2035,9 +2037,10 @@ void hio_freemem (hio_t* hio, void* ptr) } /* ------------------------------------------------------------------------ */ -void hio_addcfmb (hio_t* hio, hio_cfmb_t* cfmb, hio_cfmb_checker_t checker) +void hio_addcfmb (hio_t* hio, hio_cfmb_t* cfmb, hio_cfmb_checker_t checker, hio_cfmb_freeer_t freeer) { cfmb->cfmb_checker = checker; + cfmb->cfmb_freeer = freeer? freeer: hio_freemem; HIO_CFMBL_APPEND_CFMB (&hio->cfmb, cfmb); } diff --git a/lib/hio.h b/lib/hio.h index cbe65d7..3860ca1 100644 --- a/lib/hio.h +++ b/lib/hio.h @@ -481,7 +481,8 @@ typedef enum hio_dev_event_t hio_dev_event_t; hio_t* hio; \ hio_cfmb_t* cfmb_next; \ hio_cfmb_t* cfmb_prev; \ - hio_cfmb_checker_t cfmb_checker + hio_cfmb_checker_t cfmb_checker; \ + hio_cfmb_freeer_t cfmb_freeer typedef struct hio_cfmb_t hio_cfmb_t; @@ -490,6 +491,11 @@ typedef int (*hio_cfmb_checker_t) ( hio_cfmb_t* cfmb ); +typedef int (*hio_cfmb_freeer_t) ( + hio_t* hio, + hio_cfmb_t* cfmb +); + struct hio_cfmb_t { HIO_CFMB_HEADER; @@ -612,6 +618,17 @@ typedef enum hio_log_mask_t hio_log_mask_t; #define HIO_LOG8(hio,mask,fmt,a1,a2,a3,a4,a5,a6,a7,a8) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, fmt, a1, a2, a3, a4, a5, a6, a7, a8); } while(0) #define HIO_LOG9(hio,mask,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9); } while(0) +#define HIO_DLOG0(hio,mask,fmt) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__); } while(0) +#define HIO_DLOG1(hio,mask,fmt,a1) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1); } while(0) +#define HIO_DLOG2(hio,mask,fmt,a1,a2) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2); } while(0) +#define HIO_DLOG3(hio,mask,fmt,a1,a2,a3) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3); } while(0) +#define HIO_DLOG4(hio,mask,fmt,a1,a2,a3,a4) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4); } while(0) +#define HIO_DLOG5(hio,mask,fmt,a1,a2,a3,a4,a5) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4, a5); } while(0) +#define HIO_DLOG6(hio,mask,fmt,a1,a2,a3,a4,a5,a6) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4, a5, a6); } while(0) +#define HIO_DLOG7(hio,mask,fmt,a1,a2,a3,a4,a5,a6,a7) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4, a5, a6, a7); } while(0) +#define HIO_DLOG8(hio,mask,fmt,a1,a2,a3,a4,a5,a6,a7,a8) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4, a5, a6, a7, a8); } while(0) +#define HIO_DLOG9(hio,mask,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9) do { if (HIO_LOG_ENABLED(hio,mask)) hio_logbfmt(hio, mask, ("%s:%u " fmt), __FILE__, (unsigned int)__LINE__, a1, a2, a3, a4, a5, a6, a7, a8, a9); } while(0) + #if defined(HIO_BUILD_RELEASE) /* [NOTE] * get rid of debugging message totally regardless of @@ -628,16 +645,16 @@ typedef enum hio_log_mask_t hio_log_mask_t; # define HIO_DEBUG8(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8) # define HIO_DEBUG9(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9) #else -# define HIO_DEBUG0(hio,fmt) HIO_LOG0(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt) -# define HIO_DEBUG1(hio,fmt,a1) HIO_LOG1(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1) -# define HIO_DEBUG2(hio,fmt,a1,a2) HIO_LOG2(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2) -# define HIO_DEBUG3(hio,fmt,a1,a2,a3) HIO_LOG3(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3) -# define HIO_DEBUG4(hio,fmt,a1,a2,a3,a4) HIO_LOG4(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4) -# define HIO_DEBUG5(hio,fmt,a1,a2,a3,a4,a5) HIO_LOG5(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5) -# define HIO_DEBUG6(hio,fmt,a1,a2,a3,a4,a5,a6) HIO_LOG6(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6) -# define HIO_DEBUG7(hio,fmt,a1,a2,a3,a4,a5,a6,a7) HIO_LOG7(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7) -# define HIO_DEBUG8(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8) HIO_LOG8(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7, a8) -# define HIO_DEBUG9(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9) HIO_LOG9(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) +# define HIO_DEBUG0(hio,fmt) HIO_DLOG0(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt) +# define HIO_DEBUG1(hio,fmt,a1) HIO_DLOG1(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1) +# define HIO_DEBUG2(hio,fmt,a1,a2) HIO_DLOG2(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2) +# define HIO_DEBUG3(hio,fmt,a1,a2,a3) HIO_DLOG3(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3) +# define HIO_DEBUG4(hio,fmt,a1,a2,a3,a4) HIO_DLOG4(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4) +# define HIO_DEBUG5(hio,fmt,a1,a2,a3,a4,a5) HIO_DLOG5(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5) +# define HIO_DEBUG6(hio,fmt,a1,a2,a3,a4,a5,a6) HIO_DLOG6(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6) +# define HIO_DEBUG7(hio,fmt,a1,a2,a3,a4,a5,a6,a7) HIO_DLOG7(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7) +# define HIO_DEBUG8(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8) HIO_DLOG8(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7, a8) +# define HIO_DEBUG9(hio,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9) HIO_DLOG9(hio, HIO_LOG_DEBUG | HIO_LOG_UNTYPED, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) #endif #define HIO_INFO0(hio,fmt) HIO_LOG0(hio, HIO_LOG_INFO | HIO_LOG_UNTYPED, fmt) @@ -1153,7 +1170,8 @@ HIO_EXPORT void hio_freemem ( HIO_EXPORT void hio_addcfmb ( hio_t* hio, hio_cfmb_t* cfmb, - hio_cfmb_checker_t checker + hio_cfmb_checker_t checker, + hio_cfmb_freeer_t freeer ); /* ========================================================================= diff --git a/lib/http-cgi.c b/lib/http-cgi.c index f8ee716..5eb3332 100644 --- a/lib/http-cgi.c +++ b/lib/http-cgi.c @@ -94,9 +94,6 @@ typedef struct cgi_peer_xtn_t cgi_peer_xtn_t; static void cgi_halt_participating_devices (cgi_t* cgi) { - HIO_ASSERT (cgi->htts->hio, cgi->client != HIO_NULL); - HIO_ASSERT (cgi->htts->hio, cgi->csck != HIO_NULL); - HIO_DEBUG5 (cgi->htts->hio, "HTTS(%p) - cgi(t=%p,c=%p(%d),p=%p) Halting participating devices\n", cgi->htts, cgi, cgi->csck, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); if (cgi->csck) hio_dev_sck_halt (cgi->csck); @@ -325,7 +322,7 @@ static void cgi_on_kill (hio_svc_htts_task_t* task) cgi->client_htrd_recbs_changed = 0; if (cgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (cgi); /* detach from the htts service only if it's attached */ - HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killed the task\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - cgi(t=%p,c=%p[%d],p=%p) - killed the task\n", cgi->htts, cgi, cgi->client, (cgi->csck? cgi->csck->hnd: -1), cgi->peer); } static void cgi_peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid) @@ -971,6 +968,8 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r /* ensure that you call this function before any contents is received */ HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0); + HIO_ASSERT (hio, cli->sck == csck); + HIO_MEMSET (&fc, 0, HIO_SIZEOF(fc)); fc.cli = cli; @@ -993,14 +992,14 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (HIO_UNLIKELY(!cgi)) goto oops; cgi->options = options; - cgi->csck = csck; - cgi->client = cli; /*cgi->num_pending_writes_to_client = 0; cgi->num_pending_writes_to_peer = 0;*/ cgi->req_method = hio_htre_getqmethodtype(req); cgi->req_version = *hio_htre_getversion(req); cgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &cgi->req_content_length); + cgi->csck = csck; + cgi->client = cli; /* remember the client socket's io event handlers */ cgi->client_org_on_read = csck->on_read; cgi->client_org_on_write = csck->on_write; diff --git a/lib/http-fcgi.c b/lib/http-fcgi.c index 14d3349..4f81ea4 100644 --- a/lib/http-fcgi.c +++ b/lib/http-fcgi.c @@ -62,22 +62,17 @@ struct fcgi_peer_xtn_t }; typedef struct fcgi_peer_xtn_t fcgi_peer_xtn_t; +static void unbind_task_from_client (fcgi_t* fcgi, int rcdown); +static void unbind_task_from_peer (fcgi_t* fcgi, int rcdown); + static void fcgi_halt_participating_devices (fcgi_t* fcgi) { - HIO_ASSERT (fcgi->htts->hio, fcgi->client != HIO_NULL); - HIO_ASSERT (fcgi->htts->hio, fcgi->csck != HIO_NULL); - /* TODO: include fcgi session id in the output in place of peer??? */ - HIO_DEBUG3 (fcgi->htts->hio, "HTTS(%p) - Halting participating devices in fcgi state %p(client=%p)\n", fcgi->htts, fcgi, fcgi->csck); + HIO_DEBUG5 (fcgi->htts->hio, "HTTS(%p) - cgi(t=%p,c=%p(%d),p=%p) Halting participating devices\n", fcgi->htts, fcgi, fcgi->csck, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); - hio_dev_sck_halt (fcgi->csck); - /* check for peer as it may not have been started */ - if (fcgi->peer) - { - hio_svc_fcgic_untie (fcgi->peer); /* this is not a delayed operation unlike hio_dev_sck_halt. TODO: check if this is a buggy idea */ - fcgi->peer = HIO_NULL; - } + if (fcgi->csck) hio_dev_sck_halt (fcgi->csck); + unbind_task_from_peer (fcgi, 1); } static int fcgi_write_to_client (fcgi_t* fcgi, const void* data, hio_iolen_t dlen) @@ -227,27 +222,35 @@ static HIO_INLINE void fcgi_mark_over (fcgi_t* fcgi, int over_bits) if (!(old_over & FCGI_OVER_READ_FROM_PEER) && (fcgi->over & FCGI_OVER_READ_FROM_PEER)) { - if (fcgi->peer) hio_svc_fcgic_untie(fcgi->peer); /* the untie callback will reset fcgi->peer to HIO_NULL */ + if (fcgi->peer) + { + hio_svc_fcgic_untie(fcgi->peer); /* the untie callback will reset fcgi->peer to HIO_NULL */ + HIO_SVC_HTTS_TASK_RCDOWN((hio_svc_htts_task_t*)fcgi); /* ref down from fcgi->peer->ctx. unable to use UNREF() */ + } } if (old_over != FCGI_OVER_ALL && fcgi->over == FCGI_OVER_ALL) { /* ready to stop */ - if (fcgi->peer) hio_svc_fcgic_untie(fcgi->peer); + if (fcgi->peer) + { + hio_svc_fcgic_untie(fcgi->peer); + HIO_SVC_HTTS_TASK_RCDOWN((hio_svc_htts_task_t*)fcgi); /* ref down from fcgi->peer->ctx. unable to use UNREF() */ + } if (fcgi->csck) { if (fcgi->keep_alive && !fcgi->client_eof_detected) { - /* how to arrange to delete this fcgi object and put the socket back to the normal waiting state??? */ +/* how to arrange to delete this fcgi object and put the socket back to the normal waiting state??? */ HIO_ASSERT (fcgi->htts->hio, fcgi->client->task == (hio_svc_htts_task_t*)fcgi); + unbind_task_from_client (fcgi, 1); - HIO_SVC_HTTS_TASK_UNREF (fcgi->client->task); - /* fcgi must not be accessed from here down as it could have been destroyed */ + /* fcgi must not be accessed from here down as it could have been destroyed in unbind_task_from_client() */ } else { - HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", fcgi->htts, fcgi->csck); + HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", fcgi->htts, fcgi->csck); hio_dev_sck_shutdown (fcgi->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); hio_dev_sck_halt (fcgi->csck); } @@ -260,17 +263,21 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task) fcgi_t* fcgi = (fcgi_t*)task; hio_t* hio = fcgi->htts->hio; - HIO_DEBUG2 (hio, "HTTS(%p) - killing fcgi client(%p)\n", fcgi->htts, fcgi->csck); + HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killing the task\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); - if (fcgi->peer) - { - hio_svc_fcgic_untie(fcgi->peer); - } + /* [NOTE] + * 1. if hio_svc_htts_task_kill() is called, fcgi->peer, fcgi->peer_htrd, fcgi->csck, + * fcgi->client may not not null. + * 2. this call-back function doesn't decrement the reference count on fcgi because + * this is the fcgi destruction call-back. + */ + + if (fcgi->peer) hio_svc_fcgic_untie (fcgi->peer); if (fcgi->peer_htrd) { fcgi_peer_xtn_t* pxtn = hio_htrd_getxtn(fcgi->peer_htrd); - pxtn->fcgi = HIO_NULL; /* peer->fcgi many not be NULL if the resource is killed regardless of the reference count */ + pxtn->fcgi = HIO_NULL; hio_htrd_close (fcgi->peer_htrd); fcgi->peer_htrd = HIO_NULL; @@ -279,31 +286,15 @@ static void fcgi_on_kill (hio_svc_htts_task_t* task) if (fcgi->csck) { HIO_ASSERT (hio, fcgi->client != HIO_NULL); - - if (fcgi->client_org_on_read) fcgi->csck->on_read = fcgi->client_org_on_read; - if (fcgi->client_org_on_write) fcgi->csck->on_write = fcgi->client_org_on_write; - if (fcgi->client_org_on_disconnect) fcgi->csck->on_disconnect = fcgi->client_org_on_disconnect; - if (fcgi->client_htrd_recbs_changed) hio_htrd_setrecbs (fcgi->client->htrd, &fcgi->client_htrd_org_recbs); - - if (!fcgi->client_disconnected) - { - if (!fcgi->keep_alive || hio_dev_sck_read(fcgi->csck, 1) <= -1) - { - HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", fcgi->htts, fcgi->csck); - hio_dev_sck_halt (fcgi->csck); - } - } + unbind_task_from_client (fcgi, 0); } - fcgi->client_org_on_read = HIO_NULL; - fcgi->client_org_on_write = HIO_NULL; - fcgi->client_org_on_disconnect = HIO_NULL; - fcgi->client_htrd_recbs_changed = 0; + /* detach from the htts service only if it's attached */ + if (fcgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (fcgi); - if (fcgi->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (fcgi); /* detach from the htts service only if it's attached */ + HIO_DEBUG5 (hio, "HTTS(%p) - fcgi(t=%p,c=%p[%d],p=%p) - killed the task\n", fcgi->htts, fcgi, fcgi->client, (fcgi->csck? fcgi->csck->hnd: -1), fcgi->peer); } - static void fcgi_peer_on_untie (hio_svc_fcgic_sess_t* peer, void* ctx) { fcgi_t* fcgi = (fcgi_t*)ctx; @@ -312,6 +303,8 @@ static void fcgi_peer_on_untie (hio_svc_fcgic_sess_t* peer, void* ctx) * fcgi_halt_participating_devices() calls hio_svc_fcgi_untie() again * to cause an infinite loop if we don't reset fcgi->peer to HIO_NULL here */ fcgi->peer = HIO_NULL; +// TTTT + //fcgi_write_last_chunk_to_client (fcgi); fcgi_halt_participating_devices (fcgi); /* TODO: kill the session only??? */ } @@ -461,7 +454,6 @@ static int peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) static int peer_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) { - /* client request got completed */ fcgi_peer_xtn_t* peer = hio_htrd_getxtn(htrd); fcgi_t* fcgi = peer->fcgi; @@ -538,10 +530,8 @@ static int fcgi_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); fcgi_t* fcgi = (fcgi_t*)cli->task; - //if (!fcgi) return 0; - /* indicate end of STDIN */ - if (fcgi_write_stdin_to_peer(fcgi->peer, HIO_NULL, 0) <= -1) return -1; + if (fcgi_write_stdin_to_peer(fcgi, HIO_NULL, 0) <= -1) return -1; fcgi_mark_over (fcgi, FCGI_OVER_READ_FROM_CLIENT); return 0; @@ -557,7 +547,7 @@ static int fcgi_client_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, con HIO_ASSERT (sck->hio, cli->sck == sck); /* write the contents to fcgi server as stdin*/ - return fcgi_write_stdin_to_peer(fcgi->peer, data, dlen); + return fcgi_write_stdin_to_peer(fcgi, data, dlen); } static hio_htrd_recbs_t fcgi_client_htrd_recbs = @@ -570,21 +560,25 @@ static hio_htrd_recbs_t fcgi_client_htrd_recbs = static void fcgi_client_on_disconnect (hio_dev_sck_t* sck) { hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); + hio_t* hio = sck->hio; hio_svc_htts_t* htts = cli->htts; fcgi_t* fcgi = (fcgi_t*)cli->task; - hio_t* hio = sck->hio; - HIO_ASSERT (hio, sck == fcgi->csck); HIO_DEBUG4 (hio, "HTTS(%p) - fcgi(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, fcgi, cli, sck); - fcgi->client_disconnected = 1; - fcgi->csck = HIO_NULL; - fcgi->client = HIO_NULL; - if (fcgi->client_org_on_disconnect) + /* fcgi may be null if there is no associated task or + * the previously associated one is already gone */ + if (fcgi) { - fcgi->client_org_on_disconnect (sck); - /* this original callback destroys the associated resource. - * cgi must not be accessed from here down */ + HIO_ASSERT (hio, sck == fcgi->csck); + + /* set fcgi->client_disconnect to 1 before unbind_task_from_client() + * because fcgi can be destroyed if its reference count hits 0 */ + fcgi->client_disconnected = 1; + unbind_task_from_client (fcgi, 1); + + /* this is the original callback restored in unbind_task_from_client() */ + if (sck->on_disconnect) sck->on_disconnect (sck); } HIO_DEBUG4 (hio, "HTTS(%p) - fcgi(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, fcgi, cli, sck); @@ -622,7 +616,7 @@ static int fcgi_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t { /* indicate eof to the write side */ int n; - n = fcgi_write_stdin_to_peer(fcgi->peer, HIO_NULL, 0); + n = fcgi_write_stdin_to_peer(fcgi, HIO_NULL, 0); fcgi_mark_over (fcgi, FCGI_OVER_READ_FROM_CLIENT); if (n <= -1) goto oops; } @@ -763,12 +757,143 @@ oops: return -1; } +static void bind_task_to_client (fcgi_t* fcgi, hio_dev_sck_t* csck) +{ + hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); + + HIO_ASSERT (fcgi->htts->hio, cli->sck == csck); + HIO_ASSERT (fcgi->htts->hio, cli->task == HIO_NULL); + + fcgi->csck = csck; + fcgi->client = cli; + + /* remember the client socket's io event handlers */ + fcgi->client_org_on_read = csck->on_read; + fcgi->client_org_on_write = csck->on_write; + fcgi->client_org_on_disconnect = csck->on_disconnect; + + /* set new io events handlers on the client socket */ + csck->on_read = fcgi_client_on_read; + csck->on_write = fcgi_client_on_write; + csck->on_disconnect = fcgi_client_on_disconnect; + + cli->task = (hio_svc_htts_task_t*)fcgi; + HIO_SVC_HTTS_TASK_RCUP (fcgi); +} + +static void unbind_task_from_client (fcgi_t* fcgi, int rcdown) +{ + hio_dev_sck_t* csck = fcgi->csck; + + HIO_ASSERT (fcgi->htts->hio, fcgi->client != HIO_NULL); + HIO_ASSERT (fcgi->htts->hio, fcgi->csck != HIO_NULL); + HIO_ASSERT (fcgi->htts->hio, fcgi->client->task == (hio_svc_htts_task_t*)fcgi); + HIO_ASSERT (fcgi->htts->hio, fcgi->client->htrd != HIO_NULL); + + if (fcgi->client_htrd_recbs_changed) + { + hio_htrd_setrecbs (fcgi->client->htrd, &fcgi->client_htrd_org_recbs); + fcgi->client_htrd_recbs_changed = 0; + } + + if (fcgi->client_org_on_read) + { + csck->on_read = fcgi->client_org_on_read; + fcgi->client_org_on_read = HIO_NULL; + } + + if (fcgi->client_org_on_write) + { + csck->on_write = fcgi->client_org_on_write; + fcgi->client_org_on_write = HIO_NULL; + } + + if (fcgi->client_org_on_disconnect) + { + csck->on_disconnect = fcgi->client_org_on_disconnect; + fcgi->client_org_on_disconnect = HIO_NULL; + } + + /* there is some ordering issue in using HIO_SVC_HTTS_TASK_UNREF() + * because it can destroy the fcgi itself. so reset fcgi->client->task + * to null and call RCDOWN() later */ + fcgi->client->task = HIO_NULL; + fcgi->client = HIO_NULL; + fcgi->csck = HIO_NULL; + + if (!fcgi->client_disconnected && (!fcgi->keep_alive || hio_dev_sck_read(csck, 1) <= -1)) + { + HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", fcgi->htts, csck); + hio_dev_sck_halt (csck); + } + + if (rcdown) HIO_SVC_HTTS_TASK_RCDOWN ((hio_svc_htts_task_t*)fcgi); +} + +static int bind_task_to_peer (fcgi_t* fcgi, const hio_skad_t* fcgis_addr) +{ + hio_htrd_t* htrd; + fcgi_peer_xtn_t* pxtn; + + htrd = hio_htrd_open(fcgi->htts->hio, HIO_SIZEOF(*pxtn)); + if (HIO_UNLIKELY(!htrd)) return -1; + + hio_htrd_setoption (htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE); + hio_htrd_setrecbs (htrd, &peer_htrd_recbs); + + fcgi->peer = hio_svc_fcgic_tie(fcgi->htts->fcgic, fcgis_addr, fcgi_peer_on_read, fcgi_peer_on_untie, fcgi); + if (HIO_UNLIKELY(!fcgi->peer)) + { + hio_htrd_close (htrd); + return -1; + } + + pxtn = hio_htrd_getxtn(htrd); + pxtn->fcgi = fcgi; + fcgi->peer_htrd = htrd; + + HIO_SVC_HTTS_TASK_RCUP (fcgi); /* for peer_htrd extension */ + HIO_SVC_HTTS_TASK_RCUP (fcgi); /* for fcgi->peer->ctx in the tie() */ + + return 0; +} + +static void unbind_task_from_peer (fcgi_t* fcgi, int rcdown) +{ + int n = 0; + + if (fcgi->peer_htrd) + { + hio_htrd_close (fcgi->peer_htrd); + fcgi->peer_htrd = HIO_NULL; + n++; + } + + if (fcgi->peer) + { + /* hio-svc_fcgic_untie() is not a delayed operation unlike hio_dev_sck_halt(). + * TODO: check if this is a buggy idea */ + hio_svc_fcgic_untie (fcgi->peer); + fcgi->peer = HIO_NULL; + n++; + + } + + + if (rcdown) + { + while (n > 0) + { + n--; + HIO_SVC_HTTS_TASK_RCDOWN((hio_svc_htts_task_t*)fcgi); + } + } +} + int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, const hio_bch_t* docroot, const hio_bch_t* script, int options) { hio_t* hio = htts->hio; - hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); fcgi_t* fcgi = HIO_NULL; - fcgi_peer_xtn_t* pxtn; /* ensure that you call this function before any contents is received */ HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0); @@ -782,36 +907,14 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* fcgi = (fcgi_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*fcgi), fcgi_on_kill); if (HIO_UNLIKELY(!fcgi)) goto oops; - fcgi->csck = csck; - fcgi->client = cli; /*fcgi->num_pending_writes_to_client = 0; fcgi->num_pending_writes_to_peer = 0;*/ fcgi->req_method = hio_htre_getqmethodtype(req); fcgi->req_version = *hio_htre_getversion(req); fcgi->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &fcgi->req_content_length); - /* remember the client socket's io event handlers */ - fcgi->client_org_on_read = csck->on_read; - fcgi->client_org_on_write = csck->on_write; - fcgi->client_org_on_disconnect = csck->on_disconnect; - /* set new io events handlers on the client socket */ - csck->on_read = fcgi_client_on_read; - csck->on_write = fcgi_client_on_write; - csck->on_disconnect = fcgi_client_on_disconnect; - - HIO_ASSERT (hio, cli->task == HIO_NULL); - HIO_SVC_HTTS_TASK_REF ((hio_svc_htts_task_t*)fcgi, cli->task); /* cli->task = fcgi */ - - fcgi->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*pxtn)); - if (HIO_UNLIKELY(!fcgi->peer_htrd)) goto oops; - hio_htrd_setoption (fcgi->peer_htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE); - hio_htrd_setrecbs (fcgi->peer_htrd, &peer_htrd_recbs); - pxtn = hio_htrd_getxtn(fcgi->peer_htrd); - HIO_SVC_HTTS_TASK_REF (fcgi, pxtn->fcgi); /* peer->fcgi in htrd = fcgi */ - - /* create a session in in the fcgi client service */ - fcgi->peer = hio_svc_fcgic_tie(htts->fcgic, fcgis_addr, fcgi_peer_on_read, fcgi_peer_on_untie, fcgi); - if (HIO_UNLIKELY(!fcgi->peer)) goto oops; + bind_task_to_client (fcgi, csck); + if (bind_task_to_peer(fcgi, fcgis_addr) <= -1) goto oops; /* send FCGI_BEGIN_REQUEST */ if (hio_svc_fcgic_beginrequest(fcgi->peer) <= -1) goto oops; @@ -892,7 +995,7 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* { /* no content to be uploaded from the client */ /* indicate end of stdin to the peer and disable input wathching from the client */ - if (fcgi_write_stdin_to_peer(fcgi->peer, HIO_NULL, 0) <= -1) goto oops; + if (fcgi_write_stdin_to_peer(fcgi, HIO_NULL, 0) <= -1) goto oops; fcgi_mark_over (fcgi, FCGI_OVER_READ_FROM_CLIENT | FCGI_OVER_WRITE_TO_PEER); } #if defined(FCGI_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) diff --git a/lib/http-file.c b/lib/http-file.c index 2215d95..3c3a89f 100644 --- a/lib/http-file.c +++ b/lib/http-file.c @@ -97,9 +97,6 @@ static int file_send_contents_to_client (file_t* file); static void file_halt_participating_devices (file_t* file) { - HIO_ASSERT (file->htts->hio, file->client != HIO_NULL); - HIO_ASSERT (file->htts->hio, file->csck != HIO_NULL); - HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) Halting participating devices\n", file->htts, (int)file->csck->hnd, (int)file->peer); /* only the client socket device. @@ -800,9 +797,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* file->options = options; file->cbs = cbs; /* the given pointer must outlive the lifespan of the while file handling cycle. */ - file->csck = csck; - file->client = cli; - file->sendfile_ok = hio_dev_sck_sendfileok(cli->sck); + file->sendfile_ok = hio_dev_sck_sendfileok(csck); /*file->num_pending_writes_to_client = 0; file->num_pending_writes_to_peer = 0;*/ file->req_version = *hio_htre_getversion(req); @@ -813,6 +808,8 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* file->req_qpath_ending_with_slash = (hio_htre_getqpathlen(req) > 0 && hio_htre_getqpath(req)[hio_htre_getqpathlen(req) - 1] == '/'); file->req_qpath_is_root = (hio_htre_getqpathlen(req) == 1 && hio_htre_getqpath(req)[0] == '/'); + file->csck = csck; + file->client = cli; file->client_org_on_read = csck->on_read; file->client_org_on_write = csck->on_write; file->client_org_on_disconnect = csck->on_disconnect; diff --git a/lib/http-thr.c b/lib/http-thr.c index 572f333..185d9f4 100644 --- a/lib/http-thr.c +++ b/lib/http-thr.c @@ -97,9 +97,6 @@ typedef struct thr_peer_xtn_t thr_peer_xtn_t; static void thr_task_halt_participating_devices (thr_task_t* thr_task) { - HIO_ASSERT (thr_task->htts->hio, thr_task->client != HIO_NULL); - HIO_ASSERT (thr_task->htts->hio, thr_task->csck != HIO_NULL); - HIO_DEBUG4 (thr_task->htts->hio, "HTTS(%p) - Halting participating devices in thr task %p(csck=%p,peer=%p)\n", thr_task->htts, thr_task, thr_task->csck, thr_task->peer); if (thr_task->csck) hio_dev_sck_halt (thr_task->csck); diff --git a/lib/http-txt.c b/lib/http-txt.c index 3a11124..1ba7edf 100644 --- a/lib/http-txt.c +++ b/lib/http-txt.c @@ -58,10 +58,8 @@ typedef struct txt_t txt_t; static void txt_halt_participating_devices (txt_t* txt) { - HIO_ASSERT (txt->htts->hio, txt->client != HIO_NULL); - HIO_ASSERT (txt->htts->hio, txt->csck != HIO_NULL); HIO_DEBUG3 (txt->htts->hio, "HTTS(%p) - Halting participating devices in txt state %p(client=%p)\n", txt->htts, txt, txt->csck); - hio_dev_sck_halt (txt->csck); + if (txt->csck) hio_dev_sck_halt (txt->csck); } static int txt_write_to_client (txt_t* txt, const void* data, hio_iolen_t dlen) @@ -334,18 +332,19 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r /* ensure that you call this function before any contents is received */ HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0); + HIO_ASSERT (hio, cli->sck == csck); txt = (txt_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*txt), txt_on_kill); if (HIO_UNLIKELY(!txt)) goto oops; txt->options = options; - txt->csck = csck; - txt->client = cli; /*txt->num_pending_writes_to_client = 0;*/ txt->req_method = hio_htre_getqmethodtype(req); txt->req_version = *hio_htre_getversion(req); txt->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &txt->req_content_length); + txt->csck = csck; + txt->client = cli; txt->client_org_on_read = csck->on_read; txt->client_org_on_write = csck->on_write; txt->client_org_on_disconnect = csck->on_disconnect; diff --git a/lib/thr.c b/lib/thr.c index b63c082..9cc4159 100644 --- a/lib/thr.c +++ b/lib/thr.c @@ -323,7 +323,7 @@ static int dev_thr_kill_master (hio_dev_t* dev, int force) hio_freemem (hio, ti); #else /* schedule a resource destroyer */ - hio_addcfmb (hio, ti, ready_to_free_thr_info); + hio_addcfmb (hio, ti, ready_to_free_thr_info, HIO_NULL); #endif }