laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-remsim/+/42665?usp=email )
Change subject: Fix: Prevent bankd from exiting upon SIGPIPE
......................................................................
Fix: Prevent bankd from exiting upon SIGPIPE
If a client disconnects from a worker thread while the worker is
responding to a to a request from that client, the socket is closed and
SIGPIPE is sent to the worker, causing the application to exit.
To prevent this, all workers have a dummy handler to prevent this. Now
the worker can handle the closing of the socket.
Change-Id: I13d6e9da48d12f93c00bd2d021789bde71aca7cf
---
M src/bankd/bankd_main.c
1 file changed, 9 insertions(+), 0 deletions(-)
Approvals:
Jenkins Builder: Verified
laforge: Looks good to me, approved
diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index fc8f480..5d46f97 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -55,6 +55,7 @@
#define SIGMAPADD SIGRTMIN+2
static void handle_sig_usr1(int sig);
+static void handle_sig_pipe(int sig);
static void handle_sig_mapdel(int sig);
static void handle_sig_mapadd(int sig);
@@ -439,6 +440,7 @@
g_bankd->main = pthread_self();
signal(SIGMAPDEL, handle_sig_mapdel);
signal(SIGMAPADD, handle_sig_mapadd);
+ signal(SIGPIPE, handle_sig_pipe);
signal(SIGUSR1, handle_sig_usr1);
LOGP(DMAIN, LOGL_INFO, "Reading PCSC slots...\n");
@@ -557,6 +559,13 @@
/* do nothing */
}
+/* signal handler for receiving SIGPIPE from worker thread */
+static void handle_sig_pipe(int sig)
+{
+ /* DO NOT LOG ANYTHING HERE, IT WILL DEADLOCK WITH THE osmo_log_tgt_mutex */
+ /* do nothing */
+}
+
static void handle_sig_usr1(int sig)
{
OSMO_ASSERT(sig == SIGUSR1);
--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/42665?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I13d6e9da48d12f93c00bd2d021789bde71aca7cf
Gerrit-Change-Number: 42665
Gerrit-PatchSet: 6
Gerrit-Owner: jolly <andreas(a)eversberg.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: lynxis lazus <lynxis(a)fe80.eu>
laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-remsim/+/42664?usp=email )
Change subject: Fix: Prevent race conditions when accessing slotmap in bankd
......................................................................
Fix: Prevent race conditions when accessing slotmap in bankd
If a client connects to a worker, the worker will check if there is an
existing mapping between a reader and this client. If it exists,
slotmap_by_client() will return a pointer. If the mapping is deleted at
this time by the server, the worker uses a pointer to a mapping entry
that has just been freed.
To prevent this, the worker locks the slot map and calls the new
function slotmap_by_client_nolock(). After it has finished working with
the returned pointer, it unlocks the slot map again. A possible delete
by the main thread would be delayed.
Change-Id: I3464726f37beb7c47b4e1f00c018ffa4f3948906
---
M src/bankd/bankd_main.c
M src/slotmap.c
M src/slotmap.h
3 files changed, 21 insertions(+), 9 deletions(-)
Approvals:
Jenkins Builder: Verified
lynxis lazus: Looks good to me, approved
diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index 479adf0..fc8f480 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -700,8 +700,10 @@
{
struct slot_mapping *slmap;
- slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
+ slotmaps_rdlock(worker->bankd->slotmaps);
+ slmap = slotmap_by_client_nolock(worker->bankd->slotmaps, &worker->client.clslot);
if (!slmap) {
+ slotmaps_unlock(worker->bankd->slotmaps);
LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
worker->client.clslot.client_id, worker->client.clslot.slot_nr);
/* check in 10s if the map has been installed meanwhile by main thread */
@@ -712,6 +714,7 @@
slmap->client.client_id, slmap->client.slot_nr,
slmap->bank.bank_id, slmap->bank.slot_nr);
worker->slot = slmap->bank;
+ slotmaps_unlock(worker->bankd->slotmaps);
worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
return worker_open_card(worker);
}
diff --git a/src/slotmap.c b/src/slotmap.c
index b1be0ab..1883cf5 100644
--- a/src/slotmap.c
+++ b/src/slotmap.c
@@ -53,20 +53,27 @@
return (map->bank.bank_id << 16) | map->bank.slot_nr;
}
+/* lookup of map by client:slot */
+struct slot_mapping *slotmap_by_client_nolock(struct slotmaps *maps, const struct client_slot *client)
+{
+ struct slot_mapping *map;
+
+ llist_for_each_entry(map, &maps->mappings, list) {
+ if (client_slot_equals(&map->client, client))
+ return map;
+ }
+ return NULL;
+}
+
/* thread-safe lookup of map by client:slot */
struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
{
struct slot_mapping *map;
slotmaps_rdlock(maps);
- llist_for_each_entry(map, &maps->mappings, list) {
- if (client_slot_equals(&map->client, client)) {
- slotmaps_unlock(maps);
- return map;
- }
- }
+ map = slotmap_by_client_nolock(maps, client);
slotmaps_unlock(maps);
- return NULL;
+ return map;
}
/* thread-safe lookup of map by bank:slot */
@@ -83,7 +90,6 @@
}
slotmaps_unlock(maps);
return NULL;
-
}
/* thread-safe creating of a new bank<->client map */
diff --git a/src/slotmap.h b/src/slotmap.h
index 3d07c8d..a14e1cf 100644
--- a/src/slotmap.h
+++ b/src/slotmap.h
@@ -70,6 +70,9 @@
uint32_t slotmap_get_id(const struct slot_mapping *map);
+/* lookup of map by client:slot */
+struct slot_mapping *slotmap_by_client_nolock(struct slotmaps *maps, const struct client_slot *client);
+
/* thread-safe lookup of map by client:slot */
struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client);
--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/42664?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I3464726f37beb7c47b4e1f00c018ffa4f3948906
Gerrit-Change-Number: 42664
Gerrit-PatchSet: 4
Gerrit-Owner: jolly <andreas(a)eversberg.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: lynxis lazus <lynxis(a)fe80.eu>
laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-remsim/+/42645?usp=email )
Change subject: Fix: Remove slot mapping at bankd when client disconnects
......................................................................
Fix: Remove slot mapping at bankd when client disconnects
If a client disconnects before removing the slot mapping, the worker
must undefine its 'bank_id' and 'slot_nr' and must change its state to
'UNMAPPED'.
send_signal_to_worker() searches for a worker that has a given bank and
slot. If a client re-connects to a different worker and if the bank and
slot would be still assigned to the old worker, the old worker could
receive signals when assigning or removing slot mapping. The new worker
would not be mapped.
Change-Id: I2fd03490e2506c55104309a0ef952389119023b8
---
M src/bankd/bankd_main.c
1 file changed, 9 insertions(+), 0 deletions(-)
Approvals:
Jenkins Builder: Verified
lynxis lazus: Looks good to me, approved
diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index 5e33b41..479adf0 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -1102,6 +1102,15 @@
close(g_worker->client.fd);
memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
g_worker->client.fd = -1;
+ if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
+ struct slot_mapping *slmap;
+ slmap = slotmap_by_client(g_worker->bankd->slotmaps, &g_worker->client.clslot);
+ if (slmap) {
+ g_worker->slot.bank_id = 0xffff;
+ g_worker->slot.slot_nr = 0xffff;
+ worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED, true);
+ }
+ }
g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
}
--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/42645?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I2fd03490e2506c55104309a0ef952389119023b8
Gerrit-Change-Number: 42645
Gerrit-PatchSet: 2
Gerrit-Owner: jolly <andreas(a)eversberg.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: lynxis lazus <lynxis(a)fe80.eu>