Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use g_str_has_prefix and no malloc #319

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cputree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 9 additions & 12 deletions irqbalance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));

Expand All @@ -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));
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -503,15 +500,15 @@ 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;
}
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;
Expand Down
2 changes: 1 addition & 1 deletion numa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions procinterrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down
69 changes: 35 additions & 34 deletions ui/irqbalance-ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ 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(),
.uid = geteuid(),
.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));
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -156,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;
Expand All @@ -179,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] == ',')
Expand Down Expand Up @@ -284,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;
}
Expand All @@ -296,36 +297,36 @@ 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);
token = strtok_r(NULL, " ", &ptr);
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);
Expand Down
2 changes: 1 addition & 1 deletion ui/irqbalance-ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down