Skip to content

Commit 08a80f5

Browse files
committed
rtpengine: handle notifications through IPC
This way we also have script routes in the notification process, preventing it from crashing when events are raised. Thanks go to Norm Brandinger for reporting it!
1 parent a2c0054 commit 08a80f5

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

modules/rtpengine/rtpengine.c

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
#include "rtpengine.h"
8383
#include "rtpengine_funcs.h"
8484
#include "bencode.h"
85+
#include "../../reactor_defs.h"
86+
#include "../../reactor_proc.h"
87+
#include "../../io_wait.h"
8588

8689
#if !defined(AF_LOCAL)
8790
#define AF_LOCAL AF_UNIX
@@ -376,6 +379,7 @@ struct rtpe_set_head **rtpe_set_list =0;
376379
struct rtpe_set **default_rtpe_set=0;
377380

378381
static str rtpengine_notify_sock;
382+
static short rtpengine_notify_port;
379383
static str rtpengine_notify_event_name = str_init("E_RTPENGINE_NOTIFICATION");
380384
static event_id_t rtpengine_notify_event = EVI_ERROR;
381385

@@ -738,7 +742,7 @@ static const dep_export_t deps = {
738742

739743
static const proc_export_t procs[] = {
740744
{"RTPEngine notification receiver", 0, 0, rtpengine_notify_process, 1,
741-
PROC_FLAG_INITCHILD},
745+
PROC_FLAG_INITCHILD|PROC_FLAG_HAS_IPC|PROC_FLAG_NEEDS_SCRIPT},
742746
{0,0,0,0,0,0}
743747
};
744748

@@ -4118,6 +4122,43 @@ static void rtpengine_raise_event(int sender, void *p)
41184122

41194123
#define RTPENGINE_DGRAM_BUF 35536
41204124

4125+
static int rtpengine_io_callback(int fd, void *fs, int was_timeout)
4126+
{
4127+
int ret;
4128+
char *p;
4129+
char buffer[RTPENGINE_DGRAM_BUF];
4130+
4131+
do
4132+
ret = read(fd, buffer, RTPENGINE_DGRAM_BUF);
4133+
while (ret == -1 && errno == EINTR);
4134+
if (ret < 0) {
4135+
LM_ERR("problem reading on socket %s:%u (%s:%d)\n",
4136+
rtpengine_notify_sock.s, rtpengine_notify_port, strerror(errno), errno);
4137+
return -1;
4138+
}
4139+
4140+
if (!evi_probe_event(rtpengine_notify_event)) {
4141+
LM_DBG("nothing to do - nobody is listening!\n");
4142+
return 0;
4143+
}
4144+
4145+
p = shm_malloc(ret + 1);
4146+
if (!p) {
4147+
/* coverity[string_null] - false positive CID #211356 */
4148+
LM_ERR("could not allocate %d for buffer %.*s\n", ret, ret, buffer);
4149+
return -1;
4150+
}
4151+
memcpy(p, buffer, ret);
4152+
p[ret] = '\0';
4153+
4154+
LM_INFO("dispatching buffer: %s\n", p);
4155+
if (ipc_dispatch_rpc(rtpengine_raise_event, p) < 0) {
4156+
LM_ERR("could not dispatch notification job!\n");
4157+
shm_free(p);
4158+
}
4159+
return 0;
4160+
}
4161+
41214162
static void rtpengine_notify_process(int rank)
41224163
{
41234164
int ret;
@@ -4126,7 +4167,6 @@ static void rtpengine_notify_process(int rank)
41264167
unsigned int port;
41274168
static int rtpengine_notify_fd;
41284169
union sockaddr_union ss;
4129-
char buffer[RTPENGINE_DGRAM_BUF];
41304170

41314171
p = q_memchr(rtpengine_notify_sock.s, ':', rtpengine_notify_sock.len);
41324172
if (!p) {
@@ -4173,37 +4213,17 @@ static void rtpengine_notify_process(int rank)
41734213
goto end;
41744214
}
41754215

4176-
for (;;) {
4177-
do
4178-
ret = read(rtpengine_notify_fd, buffer, RTPENGINE_DGRAM_BUF);
4179-
while (ret == -1 && errno == EINTR);
4180-
if (ret < 0) {
4181-
LM_ERR("problem reading on socket %s:%u (%s:%d)\n",
4182-
rtpengine_notify_sock.s, port, strerror(errno), errno);
4183-
goto end;
4184-
}
4185-
4186-
if (!evi_probe_event(rtpengine_notify_event)) {
4187-
LM_DBG("nothing to do - nobody is listening!\n");
4188-
continue;
4189-
}
4190-
4191-
p = shm_malloc(ret + 1);
4192-
if (!p) {
4193-
/* coverity[string_null] - false positive CID #211356 */
4194-
LM_ERR("could not allocate %d for buffer %.*s\n", ret, ret, buffer);
4195-
continue;
4196-
}
4197-
memcpy(p, buffer, ret);
4198-
p[ret] = '\0';
4216+
if (reactor_proc_init("RTPengine events") < 0) {
4217+
LM_ERR("failed to init the RTPengine events\n");
4218+
goto end;
4219+
}
41994220

4200-
LM_INFO("dispatching buffer: %s\n", p);
4201-
if (ipc_dispatch_rpc(rtpengine_raise_event, p) < 0) {
4202-
LM_ERR("could not dispatch notification job!\n");
4203-
shm_free(p);
4204-
}
4221+
if (reactor_proc_add_fd(rtpengine_notify_fd, rtpengine_io_callback, NULL) < 0) {
4222+
LM_CRIT("failed to add RTPengine listen socket to reactor\n");
4223+
goto end;
42054224
}
42064225

4226+
reactor_proc_loop();
42074227
end:
42084228
close(rtpengine_notify_fd);
42094229
}

0 commit comments

Comments
 (0)