diff --git a/daemon/kernel.c b/daemon/kernel.c index b0443c6db..29935b53b 100644 --- a/daemon/kernel.c +++ b/daemon/kernel.c @@ -18,7 +18,7 @@ struct kernel_interface kernel; -static int kernel_action_table(const char *action, unsigned int id) { +static bool kernel_action_table(const char *action, unsigned int id) { char s[64]; int saved_errno; int fd; @@ -27,7 +27,7 @@ static int kernel_action_table(const char *action, unsigned int id) { fd = open(PREFIX "/control", O_WRONLY | O_TRUNC); if (fd == -1) - return -1; + return false; i = snprintf(s, sizeof(s), "%s %u\n", action, id); if (i >= sizeof(s)) goto fail; @@ -36,20 +36,20 @@ static int kernel_action_table(const char *action, unsigned int id) { goto fail; close(fd); - return 0; + return true; fail: saved_errno = errno; close(fd); errno = saved_errno; - return -1; + return false; } -static int kernel_create_table(unsigned int id) { +static bool kernel_create_table(unsigned int id) { return kernel_action_table("add", id); } -static int kernel_delete_table(unsigned int id) { +static bool kernel_delete_table(unsigned int id) { return kernel_action_table("del", id); } @@ -99,34 +99,34 @@ fail: return -1; } -int kernel_setup_table(unsigned int id) { +bool kernel_setup_table(unsigned int id) { if (kernel.is_wanted) abort(); - kernel.is_wanted = 1; + kernel.is_wanted = true; - if (kernel_delete_table(id) && errno != ENOENT) { + if (!kernel_delete_table(id) && errno != ENOENT) { ilog(LOG_ERR, "FAILED TO DELETE KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED", id, strerror(errno)); - return -1; + return false; } - if (kernel_create_table(id)) { + if (!kernel_create_table(id)) { ilog(LOG_ERR, "FAILED TO CREATE KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED", id, strerror(errno)); - return -1; + return false; } int fd = kernel_open_table(id); if (fd == -1) { ilog(LOG_ERR, "FAILED TO OPEN KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED", id, strerror(errno)); - return -1; + return false; } kernel.fd = fd; kernel.table = id; - kernel.is_open = 1; + kernel.is_open = true; - return 0; + return true; } void kernel_shutdown_table(void) { @@ -138,57 +138,55 @@ void kernel_shutdown_table(void) { } -int kernel_add_stream(struct rtpengine_target_info *mti) { +void kernel_add_stream(struct rtpengine_target_info *mti) { struct rtpengine_command_add_target cmd; ssize_t ret; if (!kernel.is_open) - return -1; + return; cmd.cmd = REMG_ADD_TARGET; cmd.target = *mti; ret = write(kernel.fd, &cmd, sizeof(cmd)); - if (ret > 0) - return 0; + if (ret == sizeof(cmd)) + return; ilog(LOG_ERROR, "Failed to push relay stream to kernel: %s", strerror(errno)); - return -1; } -int kernel_add_destination(struct rtpengine_destination_info *mdi) { +void kernel_add_destination(struct rtpengine_destination_info *mdi) { struct rtpengine_command_destination cmd; ssize_t ret; if (!kernel.is_open) - return -1; + return; cmd.cmd = REMG_ADD_DESTINATION; cmd.destination = *mdi; ret = write(kernel.fd, &cmd, sizeof(cmd)); - if (ret > 0) - return 0; + if (ret == sizeof(cmd)) + return; ilog(LOG_ERROR, "Failed to push relay stream destination to kernel: %s", strerror(errno)); - return -1; } -int kernel_del_stream_stats(struct rtpengine_command_del_target_stats *cmd) { +bool kernel_del_stream_stats(struct rtpengine_command_del_target_stats *cmd) { ssize_t ret; if (!kernel.is_open) - return -1; + return false; cmd->cmd = REMG_DEL_TARGET_STATS; ret = read(kernel.fd, cmd, sizeof(*cmd)); - if (ret > 0) - return 0; + if (ret == sizeof(*cmd)) + return true; ilog(LOG_ERROR, "Failed to delete relay stream from kernel: %s", strerror(errno)); - return -1; + return false; } kernel_slist *kernel_get_list(void) { @@ -237,20 +235,21 @@ unsigned int kernel_add_call(const char *id) { return cmd.call.call_idx; } -int kernel_del_call(unsigned int idx) { +void kernel_del_call(unsigned int idx) { struct rtpengine_command_del_call cmd; ssize_t ret; if (!kernel.is_open) - return -1; + return; cmd.cmd = REMG_DEL_CALL; cmd.call_idx = idx; ret = write(kernel.fd, &cmd, sizeof(cmd)); - if (ret != sizeof(cmd)) - return -1; - return 0; + if (ret == sizeof(cmd)) + return; + + ilog(LOG_ERROR, "Failed to delete intercept call from kernel: %s", strerror(errno)); } unsigned int kernel_add_intercept_stream(unsigned int call_idx, const char *id) { @@ -271,26 +270,26 @@ unsigned int kernel_add_intercept_stream(unsigned int call_idx, const char *id) } // cmd->local must be filled in -int kernel_update_stats(struct rtpengine_command_stats *cmd) { +bool kernel_update_stats(struct rtpengine_command_stats *cmd) { ssize_t ret; if (!kernel.is_open) - return -1; + return false; cmd->cmd = REMG_GET_RESET_STATS; ret = read(kernel.fd, cmd, sizeof(*cmd)); - if (ret <= 0) { + if (ret != sizeof(*cmd)) { ilog(LOG_ERROR, "Failed to get stream stats from kernel: %s", strerror(errno)); - return -1; + return false; } - return 0; + return true; } -int kernel_send_rtcp(struct rtpengine_send_packet_info *info, const char *buf, size_t len) { +void kernel_send_rtcp(struct rtpengine_send_packet_info *info, const char *buf, size_t len) { if (!kernel.is_open) - return -1; + return; size_t total_len = len + sizeof(struct rtpengine_command_send_packet); struct rtpengine_command_send_packet *cmd = alloca(total_len); @@ -306,8 +305,5 @@ int kernel_send_rtcp(struct rtpengine_send_packet_info *info, const char *buf, s else ilog(LOG_ERR, "Failed to send RTCP via kernel interface (%zi != %zu)", ret, total_len); - return -1; } - - return 0; } diff --git a/daemon/main.c b/daemon/main.c index 0a47669f0..254a4c80f 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1236,7 +1236,7 @@ static void create_everything(void) { if (err) die("Failed to create nftables chains or rules: %s (%s)", err, strerror(errno)); #endif - if (kernel_setup_table(rtpe_config.kernel_table)) { + if (!kernel_setup_table(rtpe_config.kernel_table)) { if (rtpe_config.no_fallback) die("Userspace fallback disallowed - exiting"); goto no_kernel; diff --git a/daemon/media_socket.c b/daemon/media_socket.c index 34e4d56bd..dd49d63ac 100644 --- a/daemon/media_socket.c +++ b/daemon/media_socket.c @@ -1848,7 +1848,7 @@ static void __stream_update_stats(struct packet_stream *ps, bool have_in_lock) { struct rtpengine_command_stats stats_info; __re_address_translate_ep(&stats_info.local, &ps->selected_sfd->socket.local); - if (kernel_update_stats(&stats_info)) { + if (!kernel_update_stats(&stats_info)) { if (!have_in_lock) mutex_unlock(&ps->in_lock); return; @@ -1877,7 +1877,7 @@ void __unkernelize(struct packet_stream *p, const char *reason) { reason); struct rtpengine_command_del_target_stats cmd; __re_address_translate_ep(&cmd.local, &p->selected_sfd->socket.local); - if (kernel_del_stream_stats(&cmd) == 0) + if (kernel_del_stream_stats(&cmd)) __stream_consume_stats(p, &cmd.stats); } diff --git a/include/kernel.h b/include/kernel.h index 131457811..5d875b18b 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -20,8 +20,8 @@ struct rtpengine_ssrc_stats; struct kernel_interface { unsigned int table; int fd; - int is_open; - int is_wanted; + bool is_open; + bool is_wanted; }; extern struct kernel_interface kernel; @@ -29,21 +29,21 @@ TYPED_GQUEUE(kernel, struct rtpengine_list_entry) -int kernel_setup_table(unsigned int); +bool kernel_setup_table(unsigned int); void kernel_shutdown_table(void); -int kernel_add_stream(struct rtpengine_target_info *); -int kernel_add_destination(struct rtpengine_destination_info *); -int kernel_del_stream_stats(struct rtpengine_command_del_target_stats *); +void kernel_add_stream(struct rtpengine_target_info *); +void kernel_add_destination(struct rtpengine_destination_info *); +bool kernel_del_stream_stats(struct rtpengine_command_del_target_stats *); kernel_slist *kernel_get_list(void); -int kernel_update_stats(struct rtpengine_command_stats *); +bool kernel_update_stats(struct rtpengine_command_stats *); unsigned int kernel_add_call(const char *id); -int kernel_del_call(unsigned int); +void kernel_del_call(unsigned int); unsigned int kernel_add_intercept_stream(unsigned int call_idx, const char *id); -int kernel_send_rtcp(struct rtpengine_send_packet_info *info, const char *buf, size_t len); +void kernel_send_rtcp(struct rtpengine_send_packet_info *info, const char *buf, size_t len);