Skip to content

Commit

Permalink
+ + 2b6f14c
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Oct 4, 2024
1 parent e01e696 commit 23ae7dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
56 changes: 28 additions & 28 deletions modules/rtp.io/rtp_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ static struct opt_save {

#define howmany(x, y) (sizeof(x) / sizeof(y))

struct rtp_io_desc *rpi_descp;
static struct rtpp_env argv0 = {.cp = "rtpproxy"};

static struct rtp_io_desc rpi_desc = {
.env = {.len = 1, .first = &argv0, .last = &argv0},
};

struct rtp_io_desc *rpi_descp = &rpi_desc;

#define ENV_ADD(x, elabel, ...) { \
struct rtpp_env *_e = rtp_io_env_asprintf((x), ##__VA_ARGS__); \
Expand All @@ -118,30 +124,24 @@ struct rtp_io_desc *rpi_descp;
rtp_io_env_append(&rpi_descp->env, _e); \
}

int
rio_init(void)
static int
rio_socks_init(int nsocks)
{
int nsocks;
size_t asize;

nsocks = count_child_processes();

if (nsocks <= 1)
goto e0;
struct rtp_io_socks *socks;

LM_DBG("allocating %s(%d)\n", exports.name, nsocks);

asize = sizeof(struct rtp_io_desc) + (nsocks * sizeof(int) * 2);
rpi_descp = malloc(asize);
if (rpi_descp == NULL)
asize = sizeof(struct rtp_io_socks) + (nsocks * sizeof(int) * 2);
socks = malloc(asize);
if (socks == NULL)
goto e0;

memset(rpi_descp, '\0', asize);
ENV_ADD("rtpproxy", e1);
memset(socks, '\0', asize);
rpi_descp->socks = socks;
return 0;
e1:
free(rpi_descp);
rpi_descp = NULL;
free(socks);
e0:
return -1;
}
Expand All @@ -165,17 +165,15 @@ static int mod_init(void)

LM_DBG("initializing %s(%d)\n", exports.name, nsocks);

if (rpi_descp == NULL) {
if (rio_init() != 0)
goto e0;
}
if (rio_socks_init(nsocks) != 0)
goto e0;

for (int i = 0; i < howmany(argv_stat, *argv_stat); i++) {
ENV_ADD(argv_stat[i], e1);
}

for (int i = 0; i < nsocks; i++) {
int *fdp = &rpi_descp->rtpp_socks_holder[i * 2];
int *fdp = &rpi_descp->socks->holder[i * 2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdp) < 0)
goto e1;
ENV_ADD("-s", e1);
Expand All @@ -196,7 +194,7 @@ static int mod_init(void)
if (rpi_descp->rtpp_cfsp == NULL)
goto e1;

rpi_descp->nsocks = nsocks;
rpi_descp->socks->n = nsocks;

return 0;
e1:
Expand All @@ -211,10 +209,12 @@ void mod_destroy(void)

LM_DBG("cleaning up %s...\n", exports.name);
rtpp_shutdown(rpi_descp->rtpp_cfsp);
for (struct rtpp_env *e = rpi_descp->env.first; e != NULL; e = enext) {
for (const struct rtpp_env *e = rpi_descp->env.first; e != NULL; e = enext) {
enext = e->next;
free(e->_cp);
free(e);
if (e == &argv0)
continue;
free((void *)e);
}
free(rpi_descp);
}
Expand All @@ -227,8 +227,8 @@ child_init(int rank)

assert(rank >= 0);

for (int i = 0; i < rpi_descp->nsocks; i++) {
int *fdp = &rpi_descp->rtpp_socks_holder[i * 2];
for (int i = 0; i < rpi_descp->socks->n; i++) {
int *fdp = &rpi_descp->socks->holder[i * 2];
if (close(fdp[0]) < 0)
return (-1);
fdp[0] = -1;
Expand All @@ -245,9 +245,9 @@ child_init(int rank)
static int rtp_io_getchildsock(int rank)
{

if (rank < 1 || rank > rpi_descp->nsocks)
if (rank < 1 || rank > rpi_descp->socks->n)
return (-1);

int *fdp = &rpi_descp->rtpp_socks_holder[(rank - 1) * 2];
int *fdp = &rpi_descp->socks->holder[(rank - 1) * 2];
return (fdp[1]);
}
17 changes: 11 additions & 6 deletions modules/rtp.io/rtp_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ struct rtpp_env {

struct rtpp_env_hd {
int len;
struct rtpp_env *first;
struct rtpp_env *last;
const struct rtpp_env *first;
union {
const struct rtpp_env *last;
struct rtpp_env *_last;
};
};

struct rtp_io_socks {
int n;
int holder[];
};

struct rtp_io_desc {
int nsocks;
struct rtpp_cfg *rtpp_cfsp;
struct rtpp_env_hd env;
int rtpp_socks_holder[];
struct rtp_io_socks *socks;
};

extern struct rtp_io_desc *rpi_descp;

int rio_init(void);
5 changes: 0 additions & 5 deletions modules/rtp.io/rtp_io_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <string.h>

#include "../../sr_module.h"
#include "../../pt.h"

#include "rtp_io.h"
#include "rtp_io_util.h"
Expand All @@ -19,10 +18,6 @@ rio_set_rtpp_args(modparam_t type, void *val)
if (p == NULL || *p == '\0') {
return 0;
}
if (rpi_descp == NULL) {
if (rio_init() != 0)
return -1;
}
do {
char *ep = strchr(p, ' ');
if (ep != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions modules/rtp.io/rtp_io_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rtp_io_env_append(struct rtpp_env_hd *ecp, struct rtpp_env *ep)
if (ecp->first == NULL) {
ecp->last = ecp->first = ep;
} else {
ecp->last->next = ep;
ecp->_last->next = ep;
ecp->last = ep;
}
ecp->len += 1;
Expand All @@ -55,7 +55,7 @@ const char *const *
rtp_io_env_gen_argv(struct rtpp_env_hd *ecp, int *lenp)
{
const char **rval;
struct rtpp_env *ep;
const struct rtpp_env *ep;

size_t asize = ecp->len * sizeof(rval[0]);
rval = malloc(asize);
Expand Down

0 comments on commit 23ae7dc

Please sign in to comment.