128
128
LSAPI_STATE_ACCEPTING ,
129
129
};
130
130
131
- typedef struct _lsapi_child_status
131
+ typedef struct lsapi_child_status
132
132
{
133
133
int m_pid ;
134
134
long m_tmStart ;
@@ -792,10 +792,10 @@ static int lsapi_load_lve_lib(void)
792
792
int uid = getuid ();
793
793
if ( uid )
794
794
{
795
- setreuid ( s_uid , uid );
795
+ if ( setreuid ( s_uid , uid )) {} ;
796
796
if ( !(* fp_lve_is_available )() )
797
797
s_enable_lve = 0 ;
798
- setreuid ( uid , s_uid );
798
+ if ( setreuid ( uid , s_uid )) {} ;
799
799
}
800
800
}
801
801
}
@@ -900,7 +900,7 @@ static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char
900
900
if ( pReq )
901
901
LSAPI_Write_Stderr_r ( pReq , achError , n );
902
902
else
903
- write ( STDERR_FILENO , achError , n );
903
+ if ( write ( STDERR_FILENO , achError , n )) {} ;
904
904
return 0 ;
905
905
}
906
906
@@ -2777,6 +2777,9 @@ int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork )
2777
2777
g_prefork_server -> m_iMaxIdleChildren = 1 ;
2778
2778
g_prefork_server -> m_iChildrenMaxIdleTime = 300 ;
2779
2779
g_prefork_server -> m_iMaxReqProcessTime = 3600 ;
2780
+
2781
+ setsid ();
2782
+
2780
2783
return 0 ;
2781
2784
}
2782
2785
@@ -2832,6 +2835,11 @@ static lsapi_child_status * find_child_status( int pid )
2832
2835
{
2833
2836
if ( pStatus -> m_pid == pid )
2834
2837
{
2838
+ if (pid == 0 )
2839
+ {
2840
+ memset (pStatus , 0 , sizeof ( * pStatus ) );
2841
+ pStatus -> m_pid = -1 ;
2842
+ }
2835
2843
if ( pStatus + 1 > g_prefork_server -> m_pChildrenStatusCur )
2836
2844
g_prefork_server -> m_pChildrenStatusCur = pStatus + 1 ;
2837
2845
return pStatus ;
@@ -2930,7 +2938,10 @@ static void lsapi_sigchild( int signal )
2930
2938
static int lsapi_init_children_status (void )
2931
2939
{
2932
2940
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
2934
2945
+ g_prefork_server -> m_iExtraChildren ;
2935
2946
2936
2947
char * pBuf ;
@@ -2951,6 +2962,8 @@ static int lsapi_init_children_status(void)
2951
2962
s_accepting_workers = s_busy_workers + 1 ;
2952
2963
s_global_counter = s_accepting_workers + 1 ;
2953
2964
s_avail_pages = (size_t * )(s_global_counter + 1 );
2965
+
2966
+ setsid ();
2954
2967
return 0 ;
2955
2968
}
2956
2969
@@ -3120,8 +3133,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3120
3133
3121
3134
lsapi_init_children_status ();
3122
3135
3123
- setsid ();
3124
-
3125
3136
act .sa_flags = 0 ;
3126
3137
act .sa_handler = lsapi_sigchild ;
3127
3138
sigemptyset (& (act .sa_mask ));
@@ -3143,7 +3154,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3143
3154
perror ( "Can't set signals" );
3144
3155
return -1 ;
3145
3156
}
3146
- s_stop = 0 ;
3157
+
3147
3158
while ( !s_stop )
3148
3159
{
3149
3160
if (s_proc_group_timer_cb != NULL ) {
@@ -3221,8 +3232,6 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3221
3232
{
3222
3233
wait_secs = 0 ;
3223
3234
child_status = find_child_status ( 0 );
3224
- if ( child_status )
3225
- memset ( child_status , 0 , sizeof ( * child_status ) );
3226
3235
3227
3236
sigemptyset ( & mask );
3228
3237
sigaddset ( & mask , SIGCHLD );
@@ -3314,6 +3323,210 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
3314
3323
}
3315
3324
3316
3325
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
+
3317
3530
void lsapi_perror ( const char * pMessage , int err_no )
3318
3531
{
3319
3532
lsapi_log ("%s, errno: %d (%s)\n" , pMessage , err_no ,
0 commit comments