From 2709b3ea09627a55b23d400557c58ca2e70d958d Mon Sep 17 00:00:00 2001 From: Nikita Kalyazin Date: Sun, 30 Jun 2013 16:54:38 +0400 Subject: [PATCH] [FIX] add insert buffer modules + refactor --- daemon/buffer.c | 40 ++++++++++++++++++++++++++++++++++++++-- daemon/buffer.h | 4 ++-- daemon/da_protocol.c | 8 -------- daemon/da_protocol.h | 2 -- daemon/main.c | 8 ++++++++ 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/daemon/buffer.c b/daemon/buffer.c index c945ec6..961f1e4 100644 --- a/daemon/buffer.c +++ b/daemon/buffer.c @@ -9,7 +9,7 @@ static int buf_fd = 0; -int open_buf(void) +static int open_buf(void) { buf_fd = creat(BUF_FILENAME, 0644); if (buf_fd == -1) { @@ -21,11 +21,47 @@ int open_buf(void) return 0; } -void close_buf(void) +static void close_buf(void) { close(buf_fd); } +static int insert_buf_modules(void) +{ + system("insmod /opt/swap/sdk/swap_buffer.ko"); + system("insmod /opt/swap/sdk/swap_driver.ko"); + + return 0; +} + +static void remove_buf_modules(void) +{ + system("rmmod /opt/swap/sdk/swap_driver.ko"); + system("rmmod /opt/swap/sdk/swap_buffer.ko"); +} + +int init_buf(void) +{ + if (insert_buf_modules() != 0) { + LOGE("Cannot insert buffer modules\n"); + return 1; + } + + if (open_buf() != 0) { + LOGE("Cannot open buffer\n"); + remove_buf_modules(); + return 1; + } + + return 0; +} + +void exit_buf(void) +{ + close_buf(); + remove_buf_modules(); +} + int write_to_buf(struct msg_data_t *msg) { if (write(buf_fd, msg, MSG_DATA_HDR_LEN + msg->len) == -1) { diff --git a/daemon/buffer.h b/daemon/buffer.h index ffb157f..7fd7423 100644 --- a/daemon/buffer.h +++ b/daemon/buffer.h @@ -5,8 +5,8 @@ #define BUF_FILENAME "/tmp/daemon_events" -int open_buf(void); -void close_buf(void); +int init_buf(void); +void exit_buf(void); int write_to_buf(struct msg_data_t *msg); #endif /* _BUFFER_ */ diff --git a/daemon/da_protocol.c b/daemon/da_protocol.c index 076e150..4300bab 100644 --- a/daemon/da_protocol.c +++ b/daemon/da_protocol.c @@ -1005,13 +1005,6 @@ int host_message_handler(struct msg_t *msg) return 1; } - // prepare buffer to write - if (open_buf() != 0) { - LOGE("Cannot open buffer\n"); - sendACKToHost(msg->id, ERR_UNKNOWN, 0, 0); - return 1; - } - // TODO: launch translator thread // TODO: kill app @@ -1042,7 +1035,6 @@ int host_message_handler(struct msg_t *msg) case NMSG_STOP: terminate_all(); sendACKToHost(msg->id,ERR_NO, 0, 0); - close_buf(); reset_prof_session(&prof_session); break; case NMSG_CONFIG: diff --git a/daemon/da_protocol.h b/daemon/da_protocol.h index d518a71..210440e 100644 --- a/daemon/da_protocol.h +++ b/daemon/da_protocol.h @@ -337,8 +337,6 @@ struct msg_data_t *pack_system_info(struct system_info_t *sys_info); int write_to_buf(struct msg_data_t *msg); void free_msg_data(struct msg_data_t *msg); void free_msg_payload(struct msg_t *msg); -int open_buf(void); -void close_buf(void); void free_sys_info(struct system_info_t *sys_info); int start_replay(); void reset_msg(struct msg_t *msg); diff --git a/daemon/main.c b/daemon/main.c index 1bf3af4..16a5fdf 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -43,6 +43,7 @@ #include "daemon.h" #include "da_protocol.h" #include "sys_stat.h" +#include "buffer.h" #define SINGLETON_LOCKFILE "/tmp/lockfile.da" #define PORTFILE "/tmp/port.da" @@ -239,6 +240,11 @@ static int initializeManager() int i; sigset_t newsigmask; + if (init_buf() != 0) { + LOGE("Cannot init buffer\n"); + return -1; + } + atexit(_close_server_socket); if(initialize_system_info() < 0) { @@ -308,6 +314,8 @@ static int finalizeManager() if(manager.host.data_socket != -1) close(manager.host.data_socket); + exit_buf(); + return 0; } -- 2.7.4