diff options
Diffstat (limited to 'src/service_common.c')
-rw-r--r-- | src/service_common.c | 105 |
1 files changed, 69 insertions, 36 deletions
diff --git a/src/service_common.c b/src/service_common.c index ea62e33..f09d60a 100644 --- a/src/service_common.c +++ b/src/service_common.c @@ -192,8 +192,9 @@ static void *client_packet_pump_main(void *data) case RECV_HEADER: ret = secure_socket_recv(tcb->fd, ptr, size - recv_offset, &pid); if (ret <= 0) { - if (ret == 0) + if (ret == 0) { ret = -ECANCELED; + } free(ptr); ptr = NULL; break; @@ -233,8 +234,9 @@ static void *client_packet_pump_main(void *data) case RECV_PAYLOAD: ret = secure_socket_recv(tcb->fd, ptr, size - recv_offset, &pid); if (ret <= 0) { - if (ret == 0) + if (ret == 0) { ret = -ECANCELED; + } free(ptr); ptr = NULL; break; @@ -318,8 +320,9 @@ static void *client_packet_pump_main(void *data) * \note * Emit a signal to collect this TCB from the SERVER THREAD. */ - if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &tcb, sizeof(tcb)) != sizeof(tcb)) + if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &tcb, sizeof(tcb)) != sizeof(tcb)) { ErrPrint("Unable to write pipe: %s\n", strerror(errno)); + } return (void *)ret; } @@ -384,14 +387,16 @@ static inline void tcb_teminate_all(struct service_context *svc_ctx) /*! * ASSERT(tcb->fd >= 0); */ - if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) + if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) { ErrPrint("write: %s\n", strerror(errno)); + } status = pthread_join(tcb->thid, &ret); - if (status != 0) + if (status != 0) { ErrPrint("Unable to join a thread: %s\n", strerror(status)); - else + } else { DbgPrint("Thread returns: %d\n", (int)ret); + } secure_socket_destroy_handle(tcb->fd); @@ -417,14 +422,16 @@ static inline void tcb_destroy(struct service_context *svc_ctx, struct tcb *tcb) * ASSERT(tcb->fd >= 0); * Close the connection, and then collecting the return value of thread */ - if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) + if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) { ErrPrint("write: %s\n", strerror(errno)); + } status = pthread_join(tcb->thid, &ret); - if (status != 0) + if (status != 0) { ErrPrint("Unable to join a thread: %s\n", strerror(status)); - else + } else { DbgPrint("Thread returns: %d\n", (int)ret); + } secure_socket_destroy_handle(tcb->fd); @@ -448,18 +455,21 @@ static inline int update_fdset(struct service_context *svc_ctx, fd_set *set) fd = svc_ctx->fd; FD_SET(svc_ctx->tcb_pipe[PIPE_READ], set); - if (svc_ctx->tcb_pipe[PIPE_READ] > fd) + if (svc_ctx->tcb_pipe[PIPE_READ] > fd) { fd = svc_ctx->tcb_pipe[PIPE_READ]; + } FD_SET(svc_ctx->evt_pipe[PIPE_READ], set); - if (svc_ctx->evt_pipe[PIPE_READ] > fd) + if (svc_ctx->evt_pipe[PIPE_READ] > fd) { fd = svc_ctx->evt_pipe[PIPE_READ]; + } EINA_LIST_FOREACH(svc_ctx->event_list, l, item) { if (item->type == SERVICE_EVENT_TIMER) { FD_SET(item->info.timer.fd, set); - if (fd < item->info.timer.fd) + if (fd < item->info.timer.fd) { fd = item->info.timer.fd; + } } } @@ -480,23 +490,27 @@ static inline void processing_timer_event(struct service_context *svc_ctx, fd_se EINA_LIST_FOREACH_SAFE(svc_ctx->event_list, l, n, item) { switch (item->type) { case SERVICE_EVENT_TIMER: - if (!FD_ISSET(item->info.timer.fd, set)) + if (!FD_ISSET(item->info.timer.fd, set)) { break; + } if (read(item->info.timer.fd, &expired_count, sizeof(expired_count)) == sizeof(expired_count)) { DbgPrint("Expired %d times\n", expired_count); - if (item->event_cb(svc_ctx, item->cbdata) >= 0) + if (item->event_cb(svc_ctx, item->cbdata) >= 0) { break; + } } else { ErrPrint("read: %s\n", strerror(errno)); } - if (!eina_list_data_find(svc_ctx->event_list, item)) + if (!eina_list_data_find(svc_ctx->event_list, item)) { break; + } svc_ctx->event_list = eina_list_remove(svc_ctx->event_list, item); - if (close(item->info.timer.fd) < 0) + if (close(item->info.timer.fd) < 0) { ErrPrint("close: %s\n", strerror(errno)); + } free(item); break; default: @@ -579,8 +593,9 @@ static void *server_main(void *data) * while processing svc_ctx->service_thread_main? */ ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data); - if (ret < 0) + if (ret < 0) { ErrPrint("Service thread returns: %d\n", ret); + } packet_destroy(packet_info->packet); free(packet_info); @@ -616,8 +631,9 @@ static void *server_main(void *data) lockfree_packet_list = NULL; CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock); EINA_LIST_FOREACH_SAFE(svc_ctx->packet_list, l, n, packet_info) { - if (packet_info->tcb != tcb) + if (packet_info->tcb != tcb) { continue; + } svc_ctx->packet_list = eina_list_remove(svc_ctx->packet_list, packet_info); lockfree_packet_list = eina_list_append(lockfree_packet_list, packet_info); @@ -628,8 +644,9 @@ static void *server_main(void *data) ret = read(svc_ctx->evt_pipe[PIPE_READ], &evt_ch, sizeof(evt_ch)); DbgPrint("Flushing filtered pipe: %d (%c)\n", ret, evt_ch); ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data); - if (ret < 0) + if (ret < 0) { ErrPrint("Service thread returns: %d\n", ret); + } packet_destroy(packet_info->packet); free(packet_info); } @@ -665,8 +682,9 @@ static void *server_main(void *data) ret = read(svc_ctx->evt_pipe[PIPE_READ], &evt_ch, sizeof(evt_ch)); DbgPrint("Flushing pipe: %d (%c)\n", ret, evt_ch); ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data); - if (ret < 0) + if (ret < 0) { ErrPrint("Service thread returns: %d\n", ret); + } packet_destroy(packet_info->packet); free(packet_info); } @@ -690,8 +708,9 @@ HAPI struct service_context *service_common_create(const char *addr, int (*servi return NULL; } - if (unlink(addr) < 0) + if (unlink(addr) < 0) { ErrPrint("[%s] - %s\n", addr, strerror(errno)); + } svc_ctx = calloc(1, sizeof(*svc_ctx)); if (!svc_ctx) { @@ -708,11 +727,13 @@ HAPI struct service_context *service_common_create(const char *addr, int (*servi svc_ctx->service_thread_main = service_thread_main; svc_ctx->service_thread_data = data; - if (fcntl(svc_ctx->fd, F_SETFD, FD_CLOEXEC) < 0) + if (fcntl(svc_ctx->fd, F_SETFD, FD_CLOEXEC) < 0) { ErrPrint("fcntl: %s\n", strerror(errno)); + } - if (fcntl(svc_ctx->fd, F_SETFL, O_NONBLOCK) < 0) + if (fcntl(svc_ctx->fd, F_SETFL, O_NONBLOCK) < 0) { ErrPrint("fcntl: %s\n", strerror(errno)); + } if (pipe2(svc_ctx->evt_pipe, O_NONBLOCK | O_CLOEXEC) < 0) { ErrPrint("pipe: %d\n", strerror(errno)); @@ -744,8 +765,9 @@ HAPI struct service_context *service_common_create(const char *addr, int (*servi if (status != 0) { ErrPrint("Unable to create a thread for shortcut service: %s\n", strerror(status)); status = pthread_mutex_destroy(&svc_ctx->packet_list_lock); - if (status != 0) + if (status != 0) { ErrPrint("Error: %s\n", strerror(status)); + } CLOSE_PIPE(svc_ctx->evt_pipe); CLOSE_PIPE(svc_ctx->tcb_pipe); secure_socket_destroy_handle(svc_ctx->fd); @@ -772,27 +794,31 @@ HAPI int service_common_destroy(struct service_context *svc_ctx) int status = 0; void *ret; - if (!svc_ctx) + if (!svc_ctx) { return -EINVAL; + } /*! * \note * Terminate server thread */ - if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &status, sizeof(status)) != sizeof(status)) + if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &status, sizeof(status)) != sizeof(status)) { ErrPrint("Failed to write: %s\n", strerror(errno)); + } status = pthread_join(svc_ctx->server_thid, &ret); - if (status != 0) + if (status != 0) { ErrPrint("Join: %s\n", strerror(status)); - else + } else { DbgPrint("Thread returns: %d\n", (int)ret); + } secure_socket_destroy_handle(svc_ctx->fd); status = pthread_mutex_destroy(&svc_ctx->packet_list_lock); - if (status != 0) + if (status != 0) { ErrPrint("Unable to destroy a mutex: %s\n", strerror(status)); + } CLOSE_PIPE(svc_ctx->evt_pipe); CLOSE_PIPE(svc_ctx->tcb_pipe); @@ -828,8 +854,9 @@ HAPI int tcb_is_valid(struct service_context *svc_ctx, struct tcb *tcb) */ HAPI int tcb_fd(struct tcb *tcb) { - if (!tcb) + if (!tcb) { return -EINVAL; + } return tcb->fd; } @@ -840,8 +867,9 @@ HAPI int tcb_fd(struct tcb *tcb) */ HAPI int tcb_client_type(struct tcb *tcb) { - if (!tcb) + if (!tcb) { return -EINVAL; + } return tcb->type; } @@ -852,8 +880,9 @@ HAPI int tcb_client_type(struct tcb *tcb) */ HAPI int tcb_client_type_set(struct tcb *tcb, enum tcb_type type) { - if (!tcb) + if (!tcb) { return -EINVAL; + } DbgPrint("TCB[%p] Client type is changed to %d from %d\n", tcb, type, tcb->type); tcb->type = type; @@ -866,8 +895,9 @@ HAPI int tcb_client_type_set(struct tcb *tcb, enum tcb_type type) */ HAPI struct service_context *tcb_svc_ctx(struct tcb *tcb) { - if (!tcb) + if (!tcb) { return NULL; + } return tcb->svc_ctx; } @@ -918,8 +948,9 @@ HAPI int service_common_multicast_packet(struct tcb *tcb, struct packet *packet, } ret = com_core_send(target->fd, (void *)packet_data(packet), packet_size(packet), DEFAULT_TIMEOUT); - if (ret < 0) + if (ret < 0) { ErrPrint("Failed to send packet: %d\n", ret); + } } DbgPrint("Finish to multicast packet\n"); return 0; @@ -955,8 +986,9 @@ HAPI struct service_event_item *service_common_add_timer(struct service_context if (timerfd_settime(item->info.timer.fd, 0, &spec, NULL) < 0) { ErrPrint("Error: %s\n", strerror(errno)); - if (close(item->info.timer.fd) < 0) + if (close(item->info.timer.fd) < 0) { ErrPrint("close: %s\n", strerror(errno)); + } free(item); return NULL; } @@ -981,8 +1013,9 @@ HAPI int service_common_del_timer(struct service_context *svc_ctx, struct servic svc_ctx->event_list = eina_list_remove(svc_ctx->event_list, item); - if (close(item->info.timer.fd) < 0) + if (close(item->info.timer.fd) < 0) { ErrPrint("close: %s\n", strerror(errno)); + } free(item); return 0; } |