From 6f9f9e9556db8d4ec5dc5f662a5fb13167dfa111 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 7 Jul 2024 18:02:12 -0700 Subject: [PATCH 1/2] avoid malloc with create_credentials_msg Not really needed. Signed-off-by: Rosen Penev --- ui/irqbalance-ui.c | 39 ++++++++++++++++++++------------------- ui/irqbalance-ui.h | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/ui/irqbalance-ui.c b/ui/irqbalance-ui.c index d281f47..f4cc48f 100644 --- a/ui/irqbalance-ui.c +++ b/ui/irqbalance-ui.c @@ -28,7 +28,7 @@ setup_t setup; GMainLoop *main_loop; static int default_bufsz = 8192; -struct msghdr * create_credentials_msg(void) +struct msghdr create_credentials_msg(void) { struct ucred credentials = { .pid = getpid(), @@ -36,12 +36,13 @@ struct msghdr * create_credentials_msg(void) .gid = getegid(), }; - struct msghdr *msg = g_malloc0(sizeof(struct msghdr)); - msg->msg_iovlen = 1; - msg->msg_control = g_malloc(CMSG_SPACE(sizeof(struct ucred))); - msg->msg_controllen = CMSG_SPACE(sizeof(struct ucred)); + struct msghdr msg = { + .msg_iovlen = 1, + .msg_control = g_malloc(CMSG_SPACE(sizeof(struct ucred))), + .msg_controllen = CMSG_SPACE(sizeof(struct ucred)), + }; - struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_CREDENTIALS; cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); @@ -90,16 +91,17 @@ void send_settings(char *data) return; } - struct msghdr *msg = create_credentials_msg(); - struct iovec iov; - iov.iov_base = (void *) data; - iov.iov_len = strlen(data); - msg->msg_iov = &iov; - sendmsg(socket_fd, msg, 0); + struct iovec iov = { + .iov_base = (void *) data, + .iov_len = strlen(data), + }; + + struct msghdr msg = create_credentials_msg(); + msg.msg_iov = &iov; + sendmsg(socket_fd, &msg, 0); close(socket_fd); - g_free(msg->msg_control); - g_free(msg); + g_free(msg.msg_control); } char * get_data(char *string) @@ -115,19 +117,18 @@ char * get_data(char *string) return NULL; } - struct msghdr *msg = create_credentials_msg(); + struct msghdr msg = create_credentials_msg(); struct iovec iov = { .iov_base = (void *) string, .iov_len = strlen(string), }; - msg->msg_iov = &iov; - sendmsg(socket_fd, msg, 0); + msg.msg_iov = &iov; + sendmsg(socket_fd, &msg, 0); char *data = g_malloc(default_bufsz); int len = recv(socket_fd, data, default_bufsz, MSG_TRUNC); close(socket_fd); - g_free(msg->msg_control); - g_free(msg); + g_free(msg.msg_control); if (len < 0) { g_free(data); return NULL; diff --git a/ui/irqbalance-ui.h b/ui/irqbalance-ui.h index 178be4b..4a2982d 100644 --- a/ui/irqbalance-ui.h +++ b/ui/irqbalance-ui.h @@ -72,7 +72,7 @@ typedef struct setup { /* Function prototypes */ -struct msghdr * create_credentials_msg(void); +struct msghdr create_credentials_msg(void); int init_connection(void); void send_settings(char *data); char * get_data(char *string); From da75aae4effd6bc276bb1b882d68617cd02aa8e0 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 7 Jul 2024 17:17:33 -0700 Subject: [PATCH 2/2] conver strncmp to g_str_has_prefix Avoids implicit bool conversions. Signed-off-by: Rosen Penev --- cputree.c | 2 +- irqbalance.c | 21 +++++++++------------ numa.c | 2 +- procinterrupts.c | 6 +++--- ui/irqbalance-ui.c | 30 +++++++++++++++--------------- ui/ui.c | 6 +++--- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/cputree.c b/cputree.c index 4820bdf..49b2f37 100644 --- a/cputree.c +++ b/cputree.c @@ -380,7 +380,7 @@ static void do_one_cpu(char *path) entry = readdir(dir); if (!entry) break; - if (strncmp(entry->d_name, "node", 4) == 0) { + if (g_str_has_prefix(entry->d_name, "node")) { char *end; int num; num = strtol(entry->d_name + 4, &end, 10); diff --git a/irqbalance.c b/irqbalance.c index 4f3b97d..491a912 100644 --- a/irqbalance.c +++ b/irqbalance.c @@ -154,7 +154,7 @@ static void parse_command_line(int argc, char **argv) add_cl_banned_module(optarg); break; case 'p': - if (!strncmp(optarg, "off", strlen(optarg))) + if (g_str_has_prefix(optarg, "off")) power_thresh = ULONG_MAX; else { power_thresh = strtoull(optarg, &endptr, 10); @@ -443,15 +443,14 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri goto out_close; } - if (!strncmp(buff, "stats", strlen("stats"))) { + if (g_str_has_prefix(buff, "stats")) { char *stats = NULL; for_each_object(numa_nodes, get_object_stat, &stats); send(sock, stats, strlen(stats), 0); free(stats); } - if (!strncmp(buff, "settings ", strlen("settings "))) { - if (!(strncmp(buff + strlen("settings "), "sleep ", - strlen("sleep ")))) { + if (g_str_has_prefix(buff, "settings ")) { + if (g_str_has_prefix(buff + strlen("settings "), "sleep ")) { char *sleep_string = malloc( sizeof(char) * (recv_size - strlen("settings sleep ") + 1)); @@ -465,8 +464,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri sleep_interval = new_iterval; } free(sleep_string); - } else if (!(strncmp(buff + strlen("settings "), "ban irqs ", - strlen("ban irqs ")))) { + } else if (g_str_has_prefix(buff + strlen("settings "), "ban irqs ")) { char *end; char *irq_string = malloc( sizeof(char) * (recv_size - strlen("settings ban irqs ") + 1)); @@ -479,7 +477,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri g_list_free_full(cl_banned_irqs, free); cl_banned_irqs = NULL; need_rescan = 1; - if (!strncmp(irq_string, "NONE", strlen("NONE"))) { + if (g_str_has_prefix(irq_string, "NONE")) { free(irq_string); goto out_close; } @@ -488,8 +486,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri add_cl_banned_irq(irq); } while((irq = strtoul(end, &end, 10))); free(irq_string); - } else if (!(strncmp(buff + strlen("settings "), "cpus ", - strlen("cpus")))) { + } else if (g_str_has_prefix(buff + strlen("settings "), "cpus ")) { banned_cpumask_from_ui = NULL; free(cpu_ban_string); cpu_ban_string = NULL; @@ -503,7 +500,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri recv_size - strlen("settings cpus ")); cpu_ban_string[recv_size - strlen("settings cpus ")] = '\0'; banned_cpumask_from_ui = strtok(cpu_ban_string, " "); - if (banned_cpumask_from_ui && !strncmp(banned_cpumask_from_ui, "NULL", strlen("NULL"))) { + if (banned_cpumask_from_ui && g_str_has_prefix(banned_cpumask_from_ui, "NULL")) { banned_cpumask_from_ui = NULL; free(cpu_ban_string); cpu_ban_string = NULL; @@ -511,7 +508,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri need_rescan = 1; } } - if (!strncmp(buff, "setup", strlen("setup"))) { + if (g_str_has_prefix(buff, "setup")) { char banned[512]; char *setup = calloc(strlen("SLEEP ") + 11 + 1, 1); char *newptr = NULL; diff --git a/numa.c b/numa.c index 5143ea0..a79f16e 100644 --- a/numa.c +++ b/numa.c @@ -84,7 +84,7 @@ void build_numa_node_list(void) if (!entry) break; if ((entry->d_type == DT_DIR) && - (strncmp(entry->d_name, "node", 4) == 0) && + g_str_has_prefix(entry->d_name, "node") && isdigit(entry->d_name[4])) { add_one_node(strtoul(&entry->d_name[4], NULL, 10)); } diff --git a/procinterrupts.c b/procinterrupts.c index e7ba653..4d04bf2 100644 --- a/procinterrupts.c +++ b/procinterrupts.c @@ -85,7 +85,7 @@ static int check_platform_device(char *name, struct irq_info *info) log(TO_ALL, LOG_DEBUG, "Checking entry %s\n", ent->d_name); for (i = 0; pdev_irq_info[i].d_name != NULL; i++) { - if (!strncmp(ent->d_name, pdev_irq_info[i].d_name, strlen(pdev_irq_info[i].d_name))) { + if (g_str_has_prefix(ent->d_name, pdev_irq_info[i].d_name)) { info->type = pdev_irq_info[i].type; info->class = pdev_irq_info[i].class; rc = 0; @@ -171,8 +171,8 @@ void init_irq_class_and_type(char *savedline, struct irq_info *info, int irq) * /proc/interrupts format defined, after of interrupt type * the reset string is mark the irq desc name. */ - if (strncmp(irq_name, "Level", strlen("Level")) == 0 || - strncmp(irq_name, "Edge", strlen("Edge")) == 0) + if (!g_str_has_prefix(irq_name, "Level") || + !g_str_has_prefix(irq_name, "Edge")) break; #endif } diff --git a/ui/irqbalance-ui.c b/ui/irqbalance-ui.c index f4cc48f..4a6832a 100644 --- a/ui/irqbalance-ui.c +++ b/ui/irqbalance-ui.c @@ -157,21 +157,21 @@ void parse_setup(char *setup_data) setup.banned_irqs = NULL; setup.banned_cpus = NULL; token = strtok_r(copy, " ", &ptr); - if(strncmp(token, "SLEEP", strlen("SLEEP")) != 0) goto out; + if(!g_str_has_prefix(token, "SLEEP")) goto out; setup.sleep = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); /* Parse banned IRQ data */ - while(!strncmp(token, "IRQ", strlen("IRQ"))) { + while(g_str_has_prefix(token, "IRQ")) { new_irq = g_malloc(sizeof(irq_t)); new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out; + if(!g_str_has_prefix(token, "LOAD")) goto out; new_irq->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "DIFF", strlen("DIFF")) != 0) goto out; + if(!g_str_has_prefix(token, "DIFF")) goto out; new_irq->diff = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(ptr, " ", &ptr); - if(strncmp(token, "CLASS", strlen("CLASS")) != 0) goto out; + if(!g_str_has_prefix(token, "CLASS")) goto out; new_irq->class = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); new_irq->is_banned = 1; new_irq->assigned_to = NULL; @@ -180,7 +180,7 @@ void parse_setup(char *setup_data) new_irq = NULL; } - if(strncmp(token, "BANNED", strlen("BANNED")) != 0) goto out; + if(!g_str_has_prefix(token, "BANNED")) goto out; token = strtok_r(NULL, " ", &ptr); for(i = strlen(token) - 1; i >= 0; i--) { if (token[i] == ',') @@ -285,7 +285,7 @@ void parse_into_tree(char *data) token = strtok_r(copy, " ", &ptr); while(token != NULL) { /* Parse node data */ - if(strncmp(token, "TYPE", strlen("TYPE")) != 0) { + if(!g_str_has_prefix(token, "TYPE")) { g_free(copy); goto out; } @@ -297,28 +297,28 @@ void parse_into_tree(char *data) parent = parent->parent; } token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "NUMBER", strlen("NUMBER")) != 0) goto out; + if(!g_str_has_prefix(token, "NUMBER")) goto out; new->number = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out; + if(!g_str_has_prefix(token, "LOAD")) goto out; new->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "SAVE_MODE", strlen("SAVE_MODE")) != 0) goto out; + if(!g_str_has_prefix(token, "SAVE_MODE")) goto out; new->is_powersave = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); /* Parse assigned IRQ data */ - while((token != NULL) && (!strncmp(token, "IRQ", strlen("IRQ")))) { + while(token && g_str_has_prefix(token, "IRQ")) { new_irq = g_malloc(sizeof(irq_t)); new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out; + if(!g_str_has_prefix(token, "LOAD")) goto out; new_irq->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "DIFF", strlen("DIFF")) != 0) goto out; + if(!g_str_has_prefix(token, "DIFF")) goto out; new_irq->diff = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); token = strtok_r(NULL, " ", &ptr); - if(strncmp(token, "CLASS", strlen("CLASS")) != 0) goto out; + if(!g_str_has_prefix(token, "CLASS")) goto out; new_irq->class = strtol(strtok_r(NULL, " ", &ptr), NULL, 10); new_irq->is_banned = 0; new->irqs = g_list_append(new->irqs, new_irq); @@ -326,7 +326,7 @@ void parse_into_tree(char *data) new_irq = NULL; } - if((token == NULL) || (strncmp(token, "IRQ", strlen("IRQ")) != 0)) { + if(!token || !g_str_has_prefix(token, "IRQ")) { new->parent = parent; if(parent == NULL) { tree = g_list_append(tree, new); diff --git a/ui/ui.c b/ui/ui.c index 752744a..8325bcd 100644 --- a/ui/ui.c +++ b/ui/ui.c @@ -336,9 +336,9 @@ void handle_cpu_banning(void) show_frame(); show_footer(); refresh(); - char settings_string[1024] = "settings cpus \0"; + char settings_string[1024] = "settings cpus "; for_each_cpu(all_cpus, get_new_cpu_ban_values, settings_string); - if(!strcmp("settings cpus \0", settings_string)) { + if(g_str_has_prefix(settings_string, "settings cpus ")) { strncpy(settings_string + strlen(settings_string), "NULL", 1024 - strlen(settings_string)); } @@ -654,7 +654,7 @@ void handle_irq_banning(void) refresh(); char settings_string[1024] = BAN_IRQS; for_each_irq(all_irqs, get_new_irq_ban_values, settings_string); - if(!strcmp(BAN_IRQS, settings_string)) { + if(g_str_has_prefix(settings_string, BAN_IRQS)) { strncpy(settings_string + strlen(settings_string), " NONE", 1024 - strlen(settings_string)); }