pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-bts/+/41662?usp=email )
Change subject: pcu_sock: Use osmo_io ......................................................................
pcu_sock: Use osmo_io
Change-Id: Ib708e0790568e368554da264a08d8804c8f95583 --- M src/common/pcu_sock.c 1 file changed, 37 insertions(+), 18 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-bts refs/changes/62/41662/1
diff --git a/src/common/pcu_sock.c b/src/common/pcu_sock.c index 3f1689a..4a1cd1f 100644 --- a/src/common/pcu_sock.c +++ b/src/common/pcu_sock.c @@ -1216,21 +1216,50 @@ return 0; }
+static struct pcu_sock_state *pcu_sock_state_alloc(struct gsm_bts_sm *bts_sm, int qlength_max) +{ + struct pcu_sock_state *state; + state = talloc_zero(bts_sm, struct pcu_sock_state); + OSMO_ASSERT(state); + + osmo_wqueue_init(&state->upqueue, qlength_max); + state->upqueue.read_cb = pcu_sock_read; + state->upqueue.write_cb = pcu_sock_write; + state->upqueue.bfd.fd = -1; + + state->listen_bfd.fd = -1; + + return state; +} + +static void pcu_sock_state_free(struct pcu_sock_state *state) +{ + if (!state) + return; + + if (state->upqueue.bfd.fd > 0) + pcu_sock_close(state); + + if (state->listen_bfd.fd > 0) { + close(state->listen_bfd.fd); + osmo_fd_unregister(&state->listen_bfd); + state->listen_bfd.fd = -1; + } + talloc_free(state); +} + int pcu_sock_init(const char *path, int qlength_max) { struct pcu_sock_state *state; struct osmo_fd *bfd; int rc;
+ state = pcu_sock_state_alloc(g_bts_sm, qlength_max); + state = talloc_zero(g_bts_sm, struct pcu_sock_state); if (!state) return -ENOMEM;
- osmo_wqueue_init(&state->upqueue, qlength_max); - state->upqueue.read_cb = pcu_sock_read; - state->upqueue.write_cb = pcu_sock_write; - state->upqueue.bfd.fd = -1; - bfd = &state->listen_bfd;
rc = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path, OSMO_SOCK_F_BIND); @@ -1252,9 +1281,8 @@ return rc; }
- osmo_signal_register_handler(SS_GLOBAL, pcu_if_signal_cb, NULL); - g_bts_sm->gprs.pcu_state = state; + osmo_signal_register_handler(SS_GLOBAL, pcu_if_signal_cb, NULL);
LOGP(DPCU, LOGL_INFO, "Started listening on PCU socket (PCU IF v%u): %s\n", PCU_IF_VERSION, path);
@@ -1263,20 +1291,11 @@
void pcu_sock_exit(void) { - struct pcu_sock_state *state = g_bts_sm->gprs.pcu_state; - struct osmo_fd *bfd, *conn_bfd; - - if (!state) + if (!g_bts_sm->gprs.pcu_state) return;
osmo_signal_unregister_handler(SS_GLOBAL, pcu_if_signal_cb, NULL); - conn_bfd = &state->upqueue.bfd; - if (conn_bfd->fd > 0) - pcu_sock_close(state); - bfd = &state->listen_bfd; - close(bfd->fd); - osmo_fd_unregister(bfd); - talloc_free(state); + pcu_sock_state_free(g_bts_sm->gprs.pcu_state); g_bts_sm->gprs.pcu_state = NULL; }