Skip to content

Commit 27bd16e

Browse files
author
George Wang
committed
Merge branch 'PHP-7.3' into PHP-7.4
2 parents 3deb6b5 + 874284d commit 27bd16e

File tree

3 files changed

+234
-14
lines changed

3 files changed

+234
-14
lines changed

sapi/litespeed/lsapi_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#include "lscriu.c"
5454
#endif
5555

56-
#define SAPI_LSAPI_MAX_HEADER_LENGTH 2048
56+
#define SAPI_LSAPI_MAX_HEADER_LENGTH LSAPI_RESP_HTTP_HEADER_MAX
5757

5858
/* Key for each cache entry is dirname(PATH_TRANSLATED).
5959
*
@@ -603,7 +603,7 @@ static int sapi_lsapi_activate()
603603
static sapi_module_struct lsapi_sapi_module =
604604
{
605605
"litespeed",
606-
"LiteSpeed V7.6",
606+
"LiteSpeed V7.7",
607607

608608
php_lsapi_startup, /* startup */
609609
php_module_shutdown_wrapper, /* shutdown */
@@ -1736,7 +1736,7 @@ PHP_FUNCTION(litespeed_response_headers)
17361736
if ( h->header_len > 0 ) {
17371737
p = strchr( h->header, ':' );
17381738
len = p - h->header;
1739-
if (( p )&&( len > 0 )) {
1739+
if (p && len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) {
17401740
memmove( headerBuf, h->header, len );
17411741
while( len > 0 && (isspace( headerBuf[len-1])) ) {
17421742
--len;

sapi/litespeed/lsapilib.c

Lines changed: 223 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ enum
128128
LSAPI_STATE_ACCEPTING,
129129
};
130130

131-
typedef struct _lsapi_child_status
131+
typedef struct lsapi_child_status
132132
{
133133
int m_pid;
134134
long m_tmStart;
@@ -792,10 +792,10 @@ static int lsapi_load_lve_lib(void)
792792
int uid = getuid();
793793
if ( uid )
794794
{
795-
setreuid( s_uid, uid );
795+
if (setreuid( s_uid, uid )) {};
796796
if ( !(*fp_lve_is_available)() )
797797
s_enable_lve = 0;
798-
setreuid( uid, s_uid );
798+
if (setreuid( uid, s_uid )) {};
799799
}
800800
}
801801
}
@@ -900,7 +900,7 @@ static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char
900900
if ( pReq )
901901
LSAPI_Write_Stderr_r( pReq, achError, n );
902902
else
903-
write( STDERR_FILENO, achError, n );
903+
if (write( STDERR_FILENO, achError, n )) {};
904904
return 0;
905905
}
906906

@@ -2777,6 +2777,9 @@ int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork )
27772777
g_prefork_server->m_iMaxIdleChildren = 1;
27782778
g_prefork_server->m_iChildrenMaxIdleTime = 300;
27792779
g_prefork_server->m_iMaxReqProcessTime = 3600;
2780+
2781+
setsid();
2782+
27802783
return 0;
27812784
}
27822785

@@ -2832,6 +2835,11 @@ static lsapi_child_status * find_child_status( int pid )
28322835
{
28332836
if ( pStatus->m_pid == pid )
28342837
{
2838+
if (pid == 0)
2839+
{
2840+
memset(pStatus, 0, sizeof( *pStatus ) );
2841+
pStatus->m_pid = -1;
2842+
}
28352843
if ( pStatus + 1 > g_prefork_server->m_pChildrenStatusCur )
28362844
g_prefork_server->m_pChildrenStatusCur = pStatus + 1;
28372845
return pStatus;
@@ -2930,7 +2938,10 @@ static void lsapi_sigchild( int signal )
29302938
static int lsapi_init_children_status(void)
29312939
{
29322940
int size = 4096;
2933-
int max_children = g_prefork_server->m_iMaxChildren
2941+
int max_children;
2942+
if (g_prefork_server->m_pChildrenStatus)
2943+
return 0;
2944+
max_children = g_prefork_server->m_iMaxChildren
29342945
+ g_prefork_server->m_iExtraChildren;
29352946

29362947
char * pBuf;
@@ -2951,6 +2962,8 @@ static int lsapi_init_children_status(void)
29512962
s_accepting_workers = s_busy_workers + 1;
29522963
s_global_counter = s_accepting_workers + 1;
29532964
s_avail_pages = (size_t *)(s_global_counter + 1);
2965+
2966+
setsid();
29542967
return 0;
29552968
}
29562969

@@ -3120,8 +3133,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31203133

31213134
lsapi_init_children_status();
31223135

3123-
setsid();
3124-
31253136
act.sa_flags = 0;
31263137
act.sa_handler = lsapi_sigchild;
31273138
sigemptyset(&(act.sa_mask));
@@ -3143,7 +3154,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31433154
perror( "Can't set signals" );
31443155
return -1;
31453156
}
3146-
s_stop = 0;
3157+
31473158
while( !s_stop )
31483159
{
31493160
if (s_proc_group_timer_cb != NULL) {
@@ -3221,8 +3232,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32213232
{
32223233
wait_secs = 0;
32233234
child_status = find_child_status( 0 );
3224-
if ( child_status )
3225-
memset( child_status, 0, sizeof( *child_status ) );
32263235

32273236
sigemptyset( &mask );
32283237
sigaddset( &mask, SIGCHLD );
@@ -3314,6 +3323,210 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
33143323
}
33153324

33163325

3326+
static struct sigaction old_term, old_quit, old_int,
3327+
old_usr1, old_child;
3328+
3329+
3330+
int LSAPI_Postfork_Child(LSAPI_Request * pReq)
3331+
{
3332+
int max_children = g_prefork_server->m_iMaxChildren;
3333+
s_pid = getpid();
3334+
__sync_lock_test_and_set(&pReq->child_status->m_pid, s_pid);
3335+
s_worker_status = pReq->child_status;
3336+
3337+
setsid();
3338+
g_prefork_server = NULL;
3339+
s_ppid = getppid();
3340+
s_req_processed = 0;
3341+
s_proc_group_timer_cb = NULL;
3342+
3343+
if (pthread_atfork_func)
3344+
(*pthread_atfork_func)(NULL, NULL, set_skip_write);
3345+
3346+
__sync_lock_test_and_set(&s_worker_status->m_state,
3347+
LSAPI_STATE_CONNECTED);
3348+
if (s_busy_workers)
3349+
__sync_add_and_fetch(s_busy_workers, 1);
3350+
lsapi_set_nblock( pReq->m_fd, 0 );
3351+
//keep it open if busy_count is used.
3352+
if (s_busy_workers
3353+
&& *s_busy_workers > (max_children >> 1))
3354+
s_keepListener = 1;
3355+
if ((s_uid == 0 || !s_keepListener || !is_enough_free_mem())
3356+
&& pReq->m_fdListen != -1 )
3357+
{
3358+
close(pReq->m_fdListen);
3359+
pReq->m_fdListen = -1;
3360+
}
3361+
3362+
//init_conn_key( pReq->m_fd );
3363+
lsapi_notify_pid(pReq->m_fd);
3364+
s_notified_pid = 1;
3365+
//if ( s_accept_notify )
3366+
// return notify_req_received( pReq->m_fd );
3367+
return 0;
3368+
}
3369+
3370+
3371+
int LSAPI_Postfork_Parent(LSAPI_Request * pReq)
3372+
{
3373+
++g_prefork_server->m_iCurChildren;
3374+
if (pReq->child_status)
3375+
{
3376+
time_t curTime = time( NULL );
3377+
pReq->child_status->m_tmWaitBegin = curTime;
3378+
pReq->child_status->m_tmStart = curTime;
3379+
}
3380+
close(pReq->m_fd);
3381+
pReq->m_fd = -1;
3382+
return 0;
3383+
}
3384+
3385+
3386+
int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
3387+
{
3388+
time_t lastTime = 0;
3389+
time_t curTime = 0;
3390+
fd_set readfds;
3391+
struct timeval timeout;
3392+
int wait_secs = 0;
3393+
int ret = 0;
3394+
3395+
lsapi_prefork_server * pServer = g_prefork_server;
3396+
3397+
struct sigaction act;
3398+
3399+
lsapi_init_children_status();
3400+
3401+
act.sa_flags = 0;
3402+
act.sa_handler = lsapi_sigchild;
3403+
sigemptyset(&(act.sa_mask));
3404+
if (sigaction(SIGCHLD, &act, &old_child))
3405+
{
3406+
perror( "Can't set signal handler for SIGCHILD" );
3407+
return -1;
3408+
}
3409+
3410+
/* Set up handler to kill children upon exit */
3411+
act.sa_flags = 0;
3412+
act.sa_handler = lsapi_cleanup;
3413+
sigemptyset(&(act.sa_mask));
3414+
if (sigaction(SIGTERM, &act, &old_term) ||
3415+
sigaction(SIGINT, &act, &old_int ) ||
3416+
sigaction(SIGUSR1, &act, &old_usr1) ||
3417+
sigaction(SIGQUIT, &act, &old_quit))
3418+
{
3419+
perror( "Can't set signals" );
3420+
return -1;
3421+
}
3422+
s_stop = 0;
3423+
pReq->m_reqState = 0;
3424+
3425+
while(!s_stop)
3426+
{
3427+
if (s_proc_group_timer_cb != NULL) {
3428+
s_proc_group_timer_cb(&s_ignore_pid);
3429+
}
3430+
3431+
curTime = time(NULL);
3432+
if (curTime != lastTime)
3433+
{
3434+
lastTime = curTime;
3435+
if (lsapi_parent_dead())
3436+
break;
3437+
lsapi_check_child_status(curTime);
3438+
if (pServer->m_iServerMaxIdle)
3439+
{
3440+
if (pServer->m_iCurChildren <= 0)
3441+
{
3442+
++wait_secs;
3443+
if ( wait_secs > pServer->m_iServerMaxIdle )
3444+
return -1;
3445+
}
3446+
else
3447+
wait_secs = 0;
3448+
}
3449+
}
3450+
3451+
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
3452+
*s_avail_pages = sysconf(_SC_AVPHYS_PAGES);
3453+
// lsapi_log("Memory total: %zd, free: %zd, free %%%zd\n",
3454+
// s_total_pages, *s_avail_pages, *s_avail_pages * 100 / s_total_pages);
3455+
3456+
#endif
3457+
FD_ZERO(&readfds);
3458+
FD_SET(pServer->m_fd, &readfds);
3459+
timeout.tv_sec = 1;
3460+
timeout.tv_usec = 0;
3461+
ret = (*g_fnSelect)(pServer->m_fd+1, &readfds, NULL, NULL, &timeout);
3462+
if (ret == 1 )
3463+
{
3464+
int accepting = 0;
3465+
if (s_accepting_workers)
3466+
accepting = __sync_add_and_fetch(s_accepting_workers, 0);
3467+
3468+
if (pServer->m_iCurChildren > 0
3469+
&& accepting > 0)
3470+
{
3471+
usleep( 400);
3472+
while(accepting-- > 0)
3473+
sched_yield();
3474+
continue;
3475+
}
3476+
}
3477+
else if (ret == -1)
3478+
{
3479+
if (errno == EINTR)
3480+
continue;
3481+
/* perror( "select()" ); */
3482+
break;
3483+
}
3484+
else
3485+
{
3486+
continue;
3487+
}
3488+
3489+
if (pServer->m_iCurChildren >=
3490+
pServer->m_iMaxChildren + pServer->m_iExtraChildren)
3491+
{
3492+
lsapi_log("Reached max children process limit: %d, extra: %d,"
3493+
" current: %d, busy: %d, please increase LSAPI_CHILDREN.\n",
3494+
pServer->m_iMaxChildren, pServer->m_iExtraChildren,
3495+
pServer->m_iCurChildren,
3496+
s_busy_workers ? *s_busy_workers : -1);
3497+
usleep(100000);
3498+
continue;
3499+
}
3500+
3501+
pReq->m_fd = lsapi_accept(pServer->m_fd);
3502+
if (pReq->m_fd != -1)
3503+
{
3504+
wait_secs = 0;
3505+
pReq->child_status = find_child_status(0);
3506+
3507+
ret = 0;
3508+
break;
3509+
}
3510+
else
3511+
{
3512+
if ((errno == EINTR) || (errno == EAGAIN))
3513+
continue;
3514+
perror( "accept() failed" );
3515+
ret = -1;
3516+
break;
3517+
}
3518+
}
3519+
3520+
sigaction(SIGCHLD, &old_child, 0);
3521+
sigaction(SIGTERM, &old_term, 0);
3522+
sigaction(SIGQUIT, &old_quit, 0);
3523+
sigaction(SIGINT, &old_int, 0);
3524+
sigaction(SIGUSR1, &old_usr1, 0);
3525+
3526+
return ret;
3527+
}
3528+
3529+
33173530
void lsapi_perror( const char * pMessage, int err_no )
33183531
{
33193532
lsapi_log("%s, errno: %d (%s)\n", pMessage, err_no,

sapi/litespeed/lsapilib.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct LSAPI_key_value_pair
7070
int valLen;
7171
};
7272

73-
73+
struct lsapi_child_status;
7474
#define LSAPI_MAX_RESP_HEADERS 1000
7575

7676
typedef struct lsapi_request
@@ -91,6 +91,7 @@ typedef struct lsapi_request
9191
char * m_pRespHeaderBuf;
9292
char * m_pRespHeaderBufEnd;
9393
char * m_pRespHeaderBufPos;
94+
struct lsapi_child_status * child_status;
9495

9596

9697
struct iovec * m_pIovec;
@@ -395,6 +396,12 @@ void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
395396

396397
int LSAPI_Inc_Req_Processed(int cnt);
397398

399+
int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq);
400+
401+
int LSAPI_Postfork_Child(LSAPI_Request * pReq);
402+
403+
int LSAPI_Postfork_Parent(LSAPI_Request * pReq);
404+
398405
#define LSAPI_LOG_LEVEL_BITS 0xff
399406
#define LSAPI_LOG_FLAG_NONE 0
400407
#define LSAPI_LOG_FLAG_DEBUG 1

0 commit comments

Comments
 (0)