126
126
LSAPI_STATE_ACCEPTING ,
127
127
};
128
128
129
- typedef struct _lsapi_child_status
129
+ typedef struct lsapi_child_status
130
130
{
131
131
int m_pid ;
132
132
long m_tmStart ;
@@ -790,10 +790,10 @@ static int lsapi_load_lve_lib(void)
790
790
int uid = getuid ();
791
791
if ( uid )
792
792
{
793
- setreuid ( s_uid , uid );
793
+ if ( setreuid ( s_uid , uid )) {} ;
794
794
if ( !(* fp_lve_is_available )() )
795
795
s_enable_lve = 0 ;
796
- setreuid ( uid , s_uid );
796
+ if ( setreuid ( uid , s_uid )) {} ;
797
797
}
798
798
}
799
799
}
@@ -898,7 +898,7 @@ static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char
898
898
if ( pReq )
899
899
LSAPI_Write_Stderr_r ( pReq , achError , n );
900
900
else
901
- write ( STDERR_FILENO , achError , n );
901
+ if ( write ( STDERR_FILENO , achError , n )) {} ;
902
902
return 0 ;
903
903
}
904
904
@@ -2775,6 +2775,9 @@ int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork )
2775
2775
g_prefork_server -> m_iMaxIdleChildren = 1 ;
2776
2776
g_prefork_server -> m_iChildrenMaxIdleTime = 300 ;
2777
2777
g_prefork_server -> m_iMaxReqProcessTime = 3600 ;
2778
+
2779
+ setsid ();
2780
+
2778
2781
return 0 ;
2779
2782
}
2780
2783
@@ -2830,6 +2833,11 @@ static lsapi_child_status * find_child_status( int pid )
2830
2833
{
2831
2834
if ( pStatus -> m_pid == pid )
2832
2835
{
2836
+ if (pid == 0 )
2837
+ {
2838
+ memset (pStatus , 0 , sizeof ( * pStatus ) );
2839
+ pStatus -> m_pid = -1 ;
2840
+ }
2833
2841
if ( pStatus + 1 > g_prefork_server -> m_pChildrenStatusCur )
2834
2842
g_prefork_server -> m_pChildrenStatusCur = pStatus + 1 ;
2835
2843
return pStatus ;
@@ -2928,7 +2936,10 @@ static void lsapi_sigchild( int signal )
2928
2936
static int lsapi_init_children_status (void )
2929
2937
{
2930
2938
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
2932
2943
+ g_prefork_server -> m_iExtraChildren ;
2933
2944
2934
2945
char * pBuf ;
@@ -2949,6 +2960,8 @@ static int lsapi_init_children_status(void)
2949
2960
s_accepting_workers = s_busy_workers + 1 ;
2950
2961
s_global_counter = s_accepting_workers + 1 ;
2951
2962
s_avail_pages = (size_t * )(s_global_counter + 1 );
2963
+
2964
+ setsid ();
2952
2965
return 0 ;
2953
2966
}
2954
2967
@@ -3118,8 +3131,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3118
3131
3119
3132
lsapi_init_children_status ();
3120
3133
3121
- setsid ();
3122
-
3123
3134
act .sa_flags = 0 ;
3124
3135
act .sa_handler = lsapi_sigchild ;
3125
3136
sigemptyset (& (act .sa_mask ));
@@ -3141,7 +3152,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3141
3152
perror ( "Can't set signals" );
3142
3153
return -1 ;
3143
3154
}
3144
- s_stop = 0 ;
3155
+
3145
3156
while ( !s_stop )
3146
3157
{
3147
3158
if (s_proc_group_timer_cb != NULL ) {
@@ -3219,8 +3230,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3219
3230
{
3220
3231
wait_secs = 0 ;
3221
3232
child_status = find_child_status ( 0 );
3222
- if ( child_status )
3223
- memset ( child_status , 0 , sizeof ( * child_status ) );
3224
3233
3225
3234
sigemptyset ( & mask );
3226
3235
sigaddset ( & mask , SIGCHLD );
@@ -3312,6 +3321,210 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3312
3321
}
3313
3322
3314
3323
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
+
3315
3528
void lsapi_perror ( const char * pMessage , int err_no )
3316
3529
{
3317
3530
lsapi_log ("%s, errno: %d (%s)\n" , pMessage , err_no ,
0 commit comments