Merge pull request #74 from Irqbalance/file-socket
irqbalance: Add support for file-based socket for IPC
This commit is contained in:
commit
808c7198ac
32
irqbalance.c
32
irqbalance.c
|
@ -45,6 +45,7 @@
|
||||||
|
|
||||||
volatile int keep_going = 1;
|
volatile int keep_going = 1;
|
||||||
int socket_fd;
|
int socket_fd;
|
||||||
|
char socket_name[64];
|
||||||
int one_shot_mode;
|
int one_shot_mode;
|
||||||
int debug_mode;
|
int debug_mode;
|
||||||
int foreground_mode;
|
int foreground_mode;
|
||||||
|
@ -456,7 +457,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_socket(char *socket_name)
|
int init_socket()
|
||||||
{
|
{
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||||
|
@ -467,13 +468,25 @@ int init_socket(char *socket_name)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* First try to create a file-based socket in tmpfs. If that doesn't
|
||||||
|
* succeed, fall back to an abstract socket (non file-based).
|
||||||
|
*/
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
strncpy(addr.sun_path, socket_name, strlen(addr.sun_path));
|
snprintf(socket_name, 64, "%s/%s%d.sock", SOCKET_TMPFS, SOCKET_PATH, getpid());
|
||||||
if (bind(socket_fd, (struct sockaddr *)&addr,
|
strncpy(addr.sun_path, socket_name, strlen(socket_name));
|
||||||
sizeof(sa_family_t) + strlen(socket_name) + 1) < 0) {
|
if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
log(TO_ALL, LOG_WARNING, "Daemon couldn't be bound to the socket.\n");
|
log(TO_ALL, LOG_WARNING, "Daemon couldn't be bound to the file-based socket.\n");
|
||||||
return 1;
|
|
||||||
|
/* Try binding to abstract */
|
||||||
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
log(TO_ALL, LOG_WARNING, "Daemon couldn't be bound to the abstract socket, bailing out.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int optval = 1;
|
int optval = 1;
|
||||||
if (setsockopt(socket_fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0) {
|
if (setsockopt(socket_fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0) {
|
||||||
log(TO_ALL, LOG_WARNING, "Unable to set socket options.\n");
|
log(TO_ALL, LOG_WARNING, "Unable to set socket options.\n");
|
||||||
|
@ -598,10 +611,7 @@ int main(int argc, char** argv)
|
||||||
parse_proc_interrupts();
|
parse_proc_interrupts();
|
||||||
parse_proc_stat();
|
parse_proc_stat();
|
||||||
|
|
||||||
char socket_name[64];
|
if (init_socket()) {
|
||||||
snprintf(socket_name, 64, "%s%d.sock", SOCKET_PATH, getpid());
|
|
||||||
|
|
||||||
if (init_socket(socket_name)) {
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
main_loop = g_main_loop_new(NULL, FALSE);
|
main_loop = g_main_loop_new(NULL, FALSE);
|
||||||
|
@ -619,6 +629,8 @@ int main(int argc, char** argv)
|
||||||
unlink(pidfile);
|
unlink(pidfile);
|
||||||
/* Remove socket */
|
/* Remove socket */
|
||||||
close(socket_fd);
|
close(socket_fd);
|
||||||
|
if (socket_name[0])
|
||||||
|
unlink(socket_name);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,6 +158,7 @@ extern unsigned int log_mask;
|
||||||
#endif /* HAVE_LIBSYSTEMD */
|
#endif /* HAVE_LIBSYSTEMD */
|
||||||
|
|
||||||
#define SOCKET_PATH "irqbalance"
|
#define SOCKET_PATH "irqbalance"
|
||||||
|
#define SOCKET_TMPFS "/var/run"
|
||||||
|
|
||||||
#endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */
|
#endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue