bankd: Log worker number; Add formal state + state transition function

Change-Id: Ib18e3ad79657e9423a1c1ac75438abcd2e4fdbbd
This commit is contained in:
Harald Welte 2018-08-15 23:36:46 +02:00
parent 77911b0091
commit 8d85829821
2 changed files with 45 additions and 2 deletions

View File

@ -64,6 +64,21 @@ int bankd_slotmap_add(struct bankd *bankd, const struct bank_slot *bank,
/* thread-safe removal of a bank<->client map */
void bankd_slotmap_del(struct bankd *bankd, struct bankd_slot_mapping *map);
enum bankd_worker_state {
/* just started*/
BW_ST_INIT,
/* blocking in the accept() call on the server socket fd */
BW_ST_ACCEPTING,
/* TCP established, but peer not yet identified itself */
BW_ST_CONN_WAIT_ID,
/* TCP established, client has identified itself, no mapping */
BW_ST_CONN_CLIENT,
/* TCP established, client has identified itself, mapping exists */
BW_ST_CONN_CLIENT_MAPPED,
/* TCP established, client identified, mapping exists, card opened */
BW_ST_CONN_CLIENT_MAPPED_CARD,
};
/* bankd worker instance; one per card/slot, includes thread */
struct bankd_worker {
@ -72,6 +87,11 @@ struct bankd_worker {
/* back-pointer to bankd */
struct bankd *bankd;
/* thread number */
unsigned int num;
/* worker thread state */
enum bankd_worker_state state;
/* slot number we are representing */
struct bank_slot slot;

View File

@ -37,7 +37,7 @@ static void bankd_init(struct bankd *bankd)
}
/* create + start a new bankd_worker thread */
static struct bankd_worker *bankd_create_worker(struct bankd *bankd)
static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
{
struct bankd_worker *worker;
int rc;
@ -47,6 +47,7 @@ static struct bankd_worker *bankd_create_worker(struct bankd *bankd)
return NULL;
worker->bankd = bankd;
worker->num = i;
/* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
@ -75,7 +76,7 @@ int main(int argc, char **argv)
for (i = 0; i < 10; i++) {
struct bankd_worker *w;
w = bankd_create_worker(bankd);
w = bankd_create_worker(bankd, i);
if (!w)
exit(21);
}
@ -103,6 +104,24 @@ if (rv != SCARD_S_SUCCESS) { \
printf(text ": OK\n\n"); \
}
#define LOGW(w, fmt, args...) \
printf("[%u] " fmt, (w)->num, args)
struct value_string worker_state_names[] = {
{ BW_ST_INIT, "INIT" },
{ BW_ST_ACCEPTING, "ACCEPTING" },
{ BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
{ BW_ST_CONN_CLIENT, "CONN_CLIENT" },
{ BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
{ BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
{ 0, NULL }
};
static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
{
LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
worker->state = new_state;
}
static void worker_cleanup(void *arg)
{
@ -237,6 +256,8 @@ static void *worker_main(void *arg)
void *top_ctx;
int rc;
worker_set_state(worker, BW_ST_INIT);
/* not permitted in multithreaded environment */
talloc_disable_null_tracking();
top_ctx = talloc_named_const(NULL, 0, "top");
@ -250,6 +271,7 @@ static void *worker_main(void *arg)
while (1) {
worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
worker_set_state(worker, BW_ST_ACCEPTING);
/* first wait for an incoming TCP connection */
rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
&worker->client.peer_addr_len);
@ -257,6 +279,7 @@ static void *worker_main(void *arg)
continue;
}
worker->client.fd = rc;
worker_set_state(worker, BW_ST_CONN_WAIT_ID);
/* run the main worker transceive loop body until there was some error */
while (1) {