David 'Digit' Turner | f9077a8 | 2014-02-10 23:10:47 +0100 | [diff] [blame] | 1 | // Copyright (C) 2011 The Android Open Source Project |
| 2 | // |
| 3 | // This software is licensed under the terms of the GNU General Public |
| 4 | // License version 2, as published by the Free Software Foundation, and |
| 5 | // may be copied, distributed, and modified under those terms. |
| 6 | // |
| 7 | // This program is distributed in the hope that it will be useful, |
| 8 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | // GNU General Public License for more details. |
| 11 | |
| 12 | #include "android/log-rotate.h" |
| 13 | #include "net/net.h" |
| 14 | #include "slirp-android/libslirp.h" |
| 15 | #include "sysemu/sysemu.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #ifdef _WIN32 |
| 20 | |
| 21 | void qemu_log_rotation_init(void) { |
| 22 | // Nothing to do on Win32 for now. |
| 23 | } |
| 24 | |
| 25 | void qemu_log_rotation_poll(void) { |
| 26 | // Nothing to do on Win32 for now. |
| 27 | } |
| 28 | |
| 29 | #else // !_WIN32 |
| 30 | |
| 31 | #include <sys/signal.h> |
| 32 | |
| 33 | static int rotate_logs_requested = 0; |
| 34 | |
| 35 | static void rotate_qemu_logs_handler(int signum) { |
| 36 | rotate_logs_requested = 1; |
| 37 | } |
| 38 | |
| 39 | void qemu_log_rotation_init(void) { |
| 40 | // Install SIGUSR1 signal handler. |
| 41 | struct sigaction act; |
| 42 | sigfillset(&act.sa_mask); |
| 43 | act.sa_flags = 0; |
| 44 | act.sa_handler = rotate_qemu_logs_handler; |
| 45 | if (sigaction(SIGUSR1, &act, NULL) == -1) { |
| 46 | fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n"); |
| 47 | exit(-1); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | // Clears the passed qemu log when the rotate_logs_requested |
| 53 | // is set. We need to clear the logs between the tasks that do not |
| 54 | // require restarting Qemu. |
| 55 | static FILE* rotate_log(FILE* old_log_fd, const char* filename) { |
| 56 | FILE* new_log_fd = NULL; |
| 57 | if (old_log_fd) { |
| 58 | if (fclose(old_log_fd) == -1) { |
| 59 | fprintf(stderr, "Cannot close old_log fd\n"); |
| 60 | exit(errno); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!filename) { |
| 65 | fprintf(stderr, "The log filename to be rotated is not provided"); |
| 66 | exit(-1); |
| 67 | } |
| 68 | |
| 69 | new_log_fd = fopen(filename , "wb+"); |
| 70 | if (new_log_fd == NULL) { |
| 71 | fprintf(stderr, "Cannot open the log file: %s for write.\n", |
| 72 | filename); |
| 73 | exit(1); |
| 74 | } |
| 75 | |
| 76 | return new_log_fd; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void qemu_log_rotation_poll(void) { |
| 81 | if (!rotate_logs_requested) |
| 82 | return; |
David 'Digit' Turner | c005246 | 2014-02-25 18:39:29 +0100 | [diff] [blame] | 83 | |
David 'Digit' Turner | f9077a8 | 2014-02-10 23:10:47 +0100 | [diff] [blame] | 84 | FILE* new_dns_log_fd = rotate_log(get_slirp_dns_log_fd(), |
| 85 | dns_log_filename); |
| 86 | FILE* new_drop_log_fd = rotate_log(get_slirp_drop_log_fd(), |
| 87 | drop_log_filename); |
| 88 | slirp_dns_log_fd(new_dns_log_fd); |
| 89 | slirp_drop_log_fd(new_drop_log_fd); |
| 90 | rotate_logs_requested = 0; |
| 91 | } |
| 92 | |
| 93 | #endif // !_WIN32 |