diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mds-clipboard.c | 47 | ||||
-rw-r--r-- | src/mds-clipboard.h | 7 | ||||
-rw-r--r-- | src/mds-kkbd.c | 33 |
3 files changed, 79 insertions, 8 deletions
diff --git a/src/mds-clipboard.c b/src/mds-clipboard.c index ef30f49..a726c52 100644 --- a/src/mds-clipboard.c +++ b/src/mds-clipboard.c @@ -80,7 +80,12 @@ static size_t clipboard_used[CLIPBOARD_LEVELS] = { 0, 0, 0 }; /** * The entries in each clipstack */ -static clipitem_t* clipboard[CLIPBOARD_LEVELS]; /* TODO: removed expired on danger */ +static clipitem_t* clipboard[CLIPBOARD_LEVELS]; + +/** + * Whether the server should free memory as soon as possible + */ +static int danger = 0; @@ -333,19 +338,38 @@ int __attribute__((const)) reexec_failure_recover(void) /** + * This function is called when a signal that + * signals that the system is running out of memory + * has been received + * + * @param signo The signal that has been received + */ +void received_danger(int signo) +{ + (void) signo; + danger = 1; +} + + +/** * Perform the server's mission * * @return Non-zero on error */ int master_loop(void) { - int rc = 1; + int rc = 1, r; size_t i, j; while (!reexecing && !terminating) { - int r = mds_message_read(&received, socket_fd); - if (r == 0) + if (danger) + { + danger = 0; + clipboard_danger(); + } + + if (r = mds_message_read(&received, socket_fd), r == 0) { if (r = handle_message(), r == 0) continue; @@ -641,6 +665,21 @@ static int clipboard_purge(int level, const char* client_id) /** + * Remove expired entries + * + * @return Zero on success, -1 on error + */ +int clipboard_danger(void) +{ + int i; + for (i = 0; i < CLIPBOARD_LEVELS; i++) + if (clipboard_purge(i, NULL)) + return -1; + return 0; +} + + +/** * Remove entries in the clipboard added by a client * * @param recv_client_id The ID of the client diff --git a/src/mds-clipboard.h b/src/mds-clipboard.h index 965d107..6bfde98 100644 --- a/src/mds-clipboard.h +++ b/src/mds-clipboard.h @@ -106,6 +106,13 @@ int full_send(const char* message, size_t length); int handle_message(void); /** + * Remove expired entries + * + * @return Zero on success, -1 on error + */ +int clipboard_danger(void); + +/** * Remove entries in the clipboard added by a client * * @param recv_client_id The ID of the client diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c index 5dd43a0..2e1986f 100644 --- a/src/mds-kkbd.c +++ b/src/mds-kkbd.c @@ -160,7 +160,7 @@ static char key_send_buffer[111]; /** * Message buffer for the main thread */ -static char* send_buffer = NULL; /* TODO free on danger */ +static char* send_buffer = NULL; /** * The size of `send_buffer` @@ -187,6 +187,11 @@ static pthread_mutex_t send_mutex; */ static pthread_mutex_t mapping_mutex; +/** + * Whether the server should free memory as soon as possible + */ +static int danger = 0; + /** @@ -411,21 +416,41 @@ int __attribute__((const)) reexec_failure_recover(void) /** + * This function is called when a signal that + * signals that the system is running out of memory + * has been received + * + * @param signo The signal that has been received + */ +void received_danger(int signo) +{ + (void) signo; + danger = 1; +} + + +/** * Perform the server's mission * * @return Non-zero on error */ int master_loop(void) { - int rc = 1, joined = 0; + int rc = 1, joined = 0, r; void* kbd_ret; fail_if ((errno = pthread_create(&kbd_thread, NULL, keyboard_loop, NULL))); while (!reexecing && !terminating) { - int r = mds_message_read(&received, socket_fd); - if (r == 0) + if (danger) + { + danger = 0; + free(send_buffer), send_buffer = NULL; + send_buffer_size = 0; + } + + if (r = mds_message_read(&received, socket_fd), r == 0) { if (r = handle_message(), r == 0) continue; |