Skip to content

Commit

Permalink
avoid malloc with create_credentials_msg
Browse files Browse the repository at this point in the history
Not really needed.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Jul 8, 2024
1 parent 3eb6f9b commit 798d298
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
39 changes: 20 additions & 19 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
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

0 comments on commit 798d298

Please sign in to comment.