Skip to content

Commit bce5306

Browse files
committed
Fix the sizing of the Async Reactors.
Adjust the size of the reactor accroding the limit of opens files. Closing OpenSIPS#765. (cherry picked from commit 2efdbf8)
1 parent bbaf821 commit bce5306

File tree

7 files changed

+27
-25
lines changed

7 files changed

+27
-25
lines changed

daemonize.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ int do_suid(const int uid, const int gid)
453453
* \param target target that should be reached
454454
* \return return 0 on success, -1 on error
455455
*/
456-
int increase_open_fds(unsigned int target)
456+
int set_open_fds_limit(void)
457457
{
458458
struct rlimit lim, orig;
459459

@@ -465,18 +465,23 @@ int increase_open_fds(unsigned int target)
465465
orig=lim;
466466
LM_DBG("current open file limits: %lu/%lu\n",
467467
(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
468-
if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
469-
/* nothing to do */
468+
if (open_files_limit<=0) {
469+
/* if not set from cfg, we just read the existing value */
470+
open_files_limit = lim.rlim_cur;
470471
goto done;
471-
else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
472-
lim.rlim_cur=target; /* increase soft limit to target */
473-
}else{
472+
}
473+
if ((lim.rlim_cur==RLIM_INFINITY) || (open_files_limit<=lim.rlim_cur))
474+
/* nothing to do (we do no reduce the limit) */
475+
goto done;
476+
if ((lim.rlim_max==RLIM_INFINITY) || (open_files_limit<=lim.rlim_max)) {
477+
lim.rlim_cur=open_files_limit; /* increase soft limit to target */
478+
} else {
474479
/* more than the hard limit */
475480
LM_INFO("trying to increase the open file limit"
476481
" past the hard limit (%ld -> %d)\n",
477-
(unsigned long)lim.rlim_max, target);
478-
lim.rlim_max=target;
479-
lim.rlim_cur=target;
482+
(unsigned long)lim.rlim_max, open_files_limit);
483+
lim.rlim_max=open_files_limit;
484+
lim.rlim_cur=open_files_limit;
480485
}
481486
LM_DBG("increasing open file limits to: %lu/%lu\n",
482487
(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
@@ -493,11 +498,14 @@ int increase_open_fds(unsigned int target)
493498
if (setrlimit(RLIMIT_NOFILE, &lim)==0){
494499
LM_CRIT("maximum number of file descriptors increased to"
495500
" %u\n",(unsigned)orig.rlim_max);
501+
open_files_limit = orig.rlim_max;
502+
goto done;
496503
}
497504
}
498505
goto error;
499506
}
500507
done:
508+
LM_DBG("open files limit set to %d\n",open_files_limit);
501509
return 0;
502510
error:
503511
return -1;

daemonize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
int daemonize(char* name, int * own_pgid);
3131
int do_suid(const int uid, const int gid);
32-
int increase_open_fds(unsigned int target);
32+
int set_open_fds_limit(void);
3333
int set_core_dump(int enable, unsigned int size);
3434

3535
int send_status_code(char val);

main.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,11 +1154,9 @@ int main(int argc, char** argv)
11541154

11551155
if (disable_core_dump) set_core_dump(0, 0);
11561156
else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
1157-
if (open_files_limit>0){
1158-
if(increase_open_fds(open_files_limit)<0){
1159-
LM_ERR("ERROR: error could not increase file limits\n");
1160-
goto error;
1161-
}
1157+
if(set_open_fds_limit()<0){
1158+
LM_ERR("ERROR: error could not increase file limits\n");
1159+
goto error;
11621160
}
11631161

11641162
/* print OpenSIPS version to log for history tracking */

net/net_tcp.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ int tcp_listen_backlog=DEFAULT_TCP_LISTEN_BACKLOG;
102102
/*!< by default choose the best method */
103103
enum poll_types tcp_poll_method=0;
104104
int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
105-
int tcp_max_fd_no=0;
106105
/* number of TCP workers */
107106
int tcp_children_no = CHILD_NO;
108107
/* Max number of seconds that we except a full SIP message
@@ -1454,7 +1453,7 @@ static void tcp_main_server(void)
14541453

14551454
/* we run in a separate, dedicated process, with its own reactor
14561455
* (reactors are per process) */
1457-
if (init_worker_reactor("TCP_main", tcp_max_fd_no, RCT_PRIO_MAX)<0)
1456+
if (init_worker_reactor("TCP_main", open_files_limit, RCT_PRIO_MAX)<0)
14581457
goto error;
14591458

14601459
/* now start watching all the fds*/
@@ -1683,9 +1682,6 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
16831682
if ( is_tcp_based_proto(n) )
16841683
for(si=protos[n].listeners; si ; si=si->next,r++ );
16851684

1686-
tcp_max_fd_no=counted_processes*2 + r - 1/*timer*/ + 3/*stdin/out/err*/;
1687-
tcp_max_fd_no+=tcp_max_connections;
1688-
16891685
if (register_tcp_load_stat( &load_p )!=0) {
16901686
LM_ERR("failed to init tcp load statistic\n");
16911687
goto error;
@@ -1739,7 +1735,7 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
17391735

17401736
report_conditional_status( (1), 0);
17411737

1742-
tcp_worker_proc( reader_fd[1], tcp_max_fd_no);
1738+
tcp_worker_proc( reader_fd[1] );
17431739
exit(-1);
17441740
}
17451741
}

net/net_tcp_proc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ static inline void tcp_receive_timeout(void)
284284

285285

286286

287-
void tcp_worker_proc( int unix_sock, int max_fd )
287+
void tcp_worker_proc( int unix_sock)
288288
{
289289
/* init reactor for TCP worker */
290290
tcpmain_sock=unix_sock; /* init com. socket */
291-
if ( init_worker_reactor( "TCP_worker", max_fd, RCT_PRIO_MAX)<0 ) {
291+
if ( init_worker_reactor("TCP_worker",open_files_limit,RCT_PRIO_MAX)<0 ) {
292292
goto error;
293293
}
294294

net/net_tcp_proc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
#define _NET_net_tcp_proc_h
2828

2929
/* Loop implementing a TCP worker */
30-
void tcp_worker_proc( int fd, int max_fd);
30+
void tcp_worker_proc( int fd);
3131

3232
#endif

net/net_udp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ inline static int handle_io(struct fd_map* fm, int idx,int event_type)
287287
int udp_rcv_loop( struct socket_info *si )
288288
{
289289
/* create the reactor for UDP proc */
290-
if ( init_worker_reactor( "UDP_worker", 100/*max_fd*/, RCT_PRIO_MAX)<0 ) {
290+
if ( init_worker_reactor("UDP_worker",open_files_limit,RCT_PRIO_MAX)<0 ) {
291291
LM_ERR("failed to init reactor\n");
292292
goto error;
293293
}

0 commit comments

Comments
 (0)