Skip to content

Commit 78fc12a

Browse files
author
George Wang
committed
Merge branch 'PHP-7.4'
2 parents bc4201f + 27bd16e commit 78fc12a

File tree

3 files changed

+234
-14
lines changed

3 files changed

+234
-14
lines changed

sapi/litespeed/lsapi_main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#include "lscriu.c"
5353
#endif
5454

55-
#define SAPI_LSAPI_MAX_HEADER_LENGTH 2048
55+
#define SAPI_LSAPI_MAX_HEADER_LENGTH LSAPI_RESP_HTTP_HEADER_MAX
5656

5757
/* Key for each cache entry is dirname(PATH_TRANSLATED).
5858
*
@@ -591,7 +591,7 @@ static int sapi_lsapi_activate()
591591
static sapi_module_struct lsapi_sapi_module =
592592
{
593593
"litespeed",
594-
"LiteSpeed V7.6",
594+
"LiteSpeed V7.7",
595595

596596
php_lsapi_startup, /* startup */
597597
php_module_shutdown_wrapper, /* shutdown */
@@ -1705,7 +1705,7 @@ PHP_FUNCTION(litespeed_response_headers)
17051705
if ( h->header_len > 0 ) {
17061706
p = strchr( h->header, ':' );
17071707
len = p - h->header;
1708-
if (( p )&&( len > 0 )) {
1708+
if (p && len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) {
17091709
memmove( headerBuf, h->header, len );
17101710
while( len > 0 && (isspace( headerBuf[len-1])) ) {
17111711
--len;

sapi/litespeed/lsapilib.c

+223-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ enum
126126
LSAPI_STATE_ACCEPTING,
127127
};
128128

129-
typedef struct _lsapi_child_status
129+
typedef struct lsapi_child_status
130130
{
131131
int m_pid;
132132
long m_tmStart;
@@ -790,10 +790,10 @@ static int lsapi_load_lve_lib(void)
790790
int uid = getuid();
791791
if ( uid )
792792
{
793-
setreuid( s_uid, uid );
793+
if (setreuid( s_uid, uid )) {};
794794
if ( !(*fp_lve_is_available)() )
795795
s_enable_lve = 0;
796-
setreuid( uid, s_uid );
796+
if (setreuid( uid, s_uid )) {};
797797
}
798798
}
799799
}
@@ -898,7 +898,7 @@ static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char
898898
if ( pReq )
899899
LSAPI_Write_Stderr_r( pReq, achError, n );
900900
else
901-
write( STDERR_FILENO, achError, n );
901+
if (write( STDERR_FILENO, achError, n )) {};
902902
return 0;
903903
}
904904

@@ -2775,6 +2775,9 @@ int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork )
27752775
g_prefork_server->m_iMaxIdleChildren = 1;
27762776
g_prefork_server->m_iChildrenMaxIdleTime = 300;
27772777
g_prefork_server->m_iMaxReqProcessTime = 3600;
2778+
2779+
setsid();
2780+
27782781
return 0;
27792782
}
27802783

@@ -2830,6 +2833,11 @@ static lsapi_child_status * find_child_status( int pid )
28302833
{
28312834
if ( pStatus->m_pid == pid )
28322835
{
2836+
if (pid == 0)
2837+
{
2838+
memset(pStatus, 0, sizeof( *pStatus ) );
2839+
pStatus->m_pid = -1;
2840+
}
28332841
if ( pStatus + 1 > g_prefork_server->m_pChildrenStatusCur )
28342842
g_prefork_server->m_pChildrenStatusCur = pStatus + 1;
28352843
return pStatus;
@@ -2928,7 +2936,10 @@ static void lsapi_sigchild( int signal )
29282936
static int lsapi_init_children_status(void)
29292937
{
29302938
int size = 4096;
2931-
int max_children = g_prefork_server->m_iMaxChildren
2939+
int max_children;
2940+
if (g_prefork_server->m_pChildrenStatus)
2941+
return 0;
2942+
max_children = g_prefork_server->m_iMaxChildren
29322943
+ g_prefork_server->m_iExtraChildren;
29332944

29342945
char * pBuf;
@@ -2949,6 +2960,8 @@ static int lsapi_init_children_status(void)
29492960
s_accepting_workers = s_busy_workers + 1;
29502961
s_global_counter = s_accepting_workers + 1;
29512962
s_avail_pages = (size_t *)(s_global_counter + 1);
2963+
2964+
setsid();
29522965
return 0;
29532966
}
29542967

@@ -3118,8 +3131,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31183131

31193132
lsapi_init_children_status();
31203133

3121-
setsid();
3122-
31233134
act.sa_flags = 0;
31243135
act.sa_handler = lsapi_sigchild;
31253136
sigemptyset(&(act.sa_mask));
@@ -3141,7 +3152,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31413152
perror( "Can't set signals" );
31423153
return -1;
31433154
}
3144-
s_stop = 0;
3155+
31453156
while( !s_stop )
31463157
{
31473158
if (s_proc_group_timer_cb != NULL) {
@@ -3219,8 +3230,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32193230
{
32203231
wait_secs = 0;
32213232
child_status = find_child_status( 0 );
3222-
if ( child_status )
3223-
memset( child_status, 0, sizeof( *child_status ) );
32243233

32253234
sigemptyset( &mask );
32263235
sigaddset( &mask, SIGCHLD );
@@ -3312,6 +3321,210 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
33123321
}
33133322

33143323

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

sapi/litespeed/lsapilib.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct LSAPI_key_value_pair
6868
int valLen;
6969
};
7070

71-
71+
struct lsapi_child_status;
7272
#define LSAPI_MAX_RESP_HEADERS 1000
7373

7474
typedef struct lsapi_request
@@ -89,6 +89,7 @@ typedef struct lsapi_request
8989
char * m_pRespHeaderBuf;
9090
char * m_pRespHeaderBufEnd;
9191
char * m_pRespHeaderBufPos;
92+
struct lsapi_child_status * child_status;
9293

9394

9495
struct iovec * m_pIovec;
@@ -393,6 +394,12 @@ void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
393394

394395
int LSAPI_Inc_Req_Processed(int cnt);
395396

397+
int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq);
398+
399+
int LSAPI_Postfork_Child(LSAPI_Request * pReq);
400+
401+
int LSAPI_Postfork_Parent(LSAPI_Request * pReq);
402+
396403
#define LSAPI_LOG_LEVEL_BITS 0xff
397404
#define LSAPI_LOG_FLAG_NONE 0
398405
#define LSAPI_LOG_FLAG_DEBUG 1

0 commit comments

Comments
 (0)