diff --git a/src/article_favor_display.c b/src/article_favor_display.c index 7d1fb27..85f92ec 100644 --- a/src/article_favor_display.c +++ b/src/article_favor_display.c @@ -179,7 +179,7 @@ static enum select_cmd_t article_favor_select(int total_page, int item_count, in case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); return EXIT_LIST; // exit list } continue; diff --git a/src/bbs_main.c b/src/bbs_main.c index 40be68d..e7c9c72 100644 --- a/src/bbs_main.c +++ b/src/bbs_main.c @@ -198,7 +198,7 @@ int bbs_center() case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); loop = 0; break; } diff --git a/src/bbs_net.c b/src/bbs_net.c index b91bc82..e3586d1 100644 --- a/src/bbs_net.c +++ b/src/bbs_net.c @@ -964,6 +964,7 @@ static int bbsnet_connect(int n) { if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { + log_debug("User input timeout\n"); break; } } @@ -1372,7 +1373,7 @@ static int bbsnet_connect(int n) if (sock != -1 && close(sock) == -1) { - log_error("Close socket failed\n"); + log_error("close(socket) error (%d)\n", errno); } if (res) @@ -1465,7 +1466,7 @@ int bbs_net() case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); goto cleanup; } continue; diff --git a/src/editor.c b/src/editor.c index 56ad07c..a04605f 100644 --- a/src/editor.c +++ b/src/editor.c @@ -971,7 +971,7 @@ int editor_display(EDITOR_DATA *p_editor_data) log_debug("KEY_NULL\n"); goto cleanup; case KEY_TIMEOUT: - log_error("User input timeout\n"); + log_debug("User input timeout\n"); goto cleanup; case Ctrl('W'): case Ctrl('X'): diff --git a/src/hash_dict.c b/src/hash_dict.c index 2338c97..3b8786e 100644 --- a/src/hash_dict.c +++ b/src/hash_dict.c @@ -34,13 +34,11 @@ static const unsigned int hash_dict_prime_list[] = { 50331653, }; -static const int hash_dict_prime_list_count = sizeof(hash_dict_prime_list) / sizeof(hash_dict_prime_list[0]); +static const unsigned int hash_dict_prime_list_count = sizeof(hash_dict_prime_list) / sizeof(hash_dict_prime_list[0]); HASH_DICT *hash_dict_create(int item_count_limit) { HASH_DICT *p_dict = NULL; - unsigned int i; - unsigned int j; if (item_count_limit <= 0) { @@ -56,7 +54,7 @@ HASH_DICT *hash_dict_create(int item_count_limit) } p_dict->prime_index = hash_dict_prime_list_count - 1; - for (i = 0; i < hash_dict_prime_list_count; i++) + for (unsigned int i = 0; i < hash_dict_prime_list_count; i++) { if (item_count_limit < hash_dict_prime_list[i]) { @@ -75,17 +73,14 @@ HASH_DICT *hash_dict_create(int item_count_limit) return NULL; } - for (i = 0; i < p_dict->bucket_count; i++) + for (unsigned int i = 0; i < p_dict->bucket_count; i++) { p_dict->buckets[i] = calloc(HASH_DICT_BUCKET_SIZE, sizeof(HASH_ITEM *)); if (p_dict->buckets[i] == NULL) { log_error("calloc(HASH_DICT_BUCKET_SIZE, HASH_ITEM) error at bucket %d\n", i); - for (j = i - 1; j >= 0; j--) - { - free(p_dict->buckets[j]); - } - free(p_dict); + p_dict->bucket_count = i; + hash_dict_destroy(p_dict); return NULL; } } @@ -99,17 +94,15 @@ void hash_dict_destroy(HASH_DICT *p_dict) { HASH_ITEM *p_item; HASH_ITEM *p_next; - unsigned int i; - unsigned int j; if (p_dict == NULL) { return; } - for (i = 0; i < p_dict->bucket_count; i++) + for (unsigned int i = 0; i < p_dict->bucket_count; i++) { - for (j = 0; j < HASH_DICT_BUCKET_SIZE; j++) + for (unsigned int j = 0; j < HASH_DICT_BUCKET_SIZE; j++) { p_item = p_dict->buckets[i][j]; while (p_item != NULL) diff --git a/src/io.c b/src/io.c index 89cdeea..6d05c01 100644 --- a/src/io.c +++ b/src/io.c @@ -88,14 +88,32 @@ int io_init(void) log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); if (close(stdin_epollfd) < 0) { - log_error("close(stdin_epollfd) error (%d)\n"); + log_error("close(stdin_epollfd) error (%d)\n", errno); } stdin_epollfd = -1; return -1; } - stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n", errno); + } + stdin_epollfd = -1; + return -1; + } + if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n", errno); + } + stdin_epollfd = -1; + return -1; + } } if (stdout_epollfd == -1) @@ -114,26 +132,60 @@ int io_init(void) log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); if (close(stdout_epollfd) < 0) { - log_error("close(stdout_epollfd) error (%d)\n"); + log_error("close(stdout_epollfd) error (%d)\n", errno); } stdout_epollfd = -1; return -1; } - stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n", errno); + } + stdout_epollfd = -1; + return -1; + } + if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n", errno); + } + stdout_epollfd = -1; + return -1; + } } #else if (stdin_flags == 0) { - stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + return -1; + } + if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + return -1; + } } if (stdout_flags == 0) { - stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + return -1; + } + if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + return -1; + } } #endif @@ -150,7 +202,7 @@ void io_cleanup(void) if (close(stdin_epollfd) < 0) { - log_error("close(stdin_epollfd) error (%d)\n"); + log_error("close(stdin_epollfd) error (%d)\n", errno); } stdin_epollfd = -1; } @@ -162,7 +214,7 @@ void io_cleanup(void) if (close(stdout_epollfd) < 0) { - log_error("close(stdout_epollfd) error (%d)\n"); + log_error("close(stdout_epollfd) error (%d)\n", errno); } stdout_epollfd = -1; } @@ -191,23 +243,27 @@ int prints(const char *format, ...) ret = vsnprintf(buf, sizeof(buf), format, args); va_end(args); - if (ret > 0) + if (ret >= 0) { - if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) + int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret); + + if (stdout_buf_len + written > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) + if (stdout_buf_len + written <= OUTPUT_BUF_SIZE) { - memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); - stdout_buf_len += ret; + memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written); + stdout_buf_len += written; + ret = written; } else { errno = EAGAIN; - ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); - log_error("Output buffer is full, additional %d is required\n", ret); + int need = stdout_buf_len + written - OUTPUT_BUF_SIZE; + log_error("Output buffer is full, additional %d bytes are required\n", need); + ret = -need; } } @@ -216,7 +272,7 @@ int prints(const char *format, ...) int outc(char c) { - int ret; + int ret = 0; if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) { @@ -1058,7 +1114,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char size_t i = 0; int skip_current = 0; - if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) + if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) { log_error("NULL pointer error\n"); return -1; @@ -1112,7 +1168,8 @@ int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char { if (errno == EINVAL) // Incomplete { - log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); + log_debug("iconv(inbytes=%zu, outbytes=%zu) error: EINVAL, in_buf[0]=%d\n", + in_bytes, out_bytes, (unsigned char)in_buf[0]); if (p_buf != in_buf) { *p_buf_len -= (int)(in_buf - p_buf); @@ -1125,14 +1182,14 @@ int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char } else if (errno == E2BIG) { - log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); + log_error("iconv(inbytes=%zu, outbytes=%zu) error: E2BIG\n", in_bytes, out_bytes); return -1; } else if (errno == EILSEQ) { if (in_bytes > out_bytes || out_bytes <= 0) { - log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); + log_error("iconv(inbytes=%zu, outbytes=%zu) error: EILSEQ\n", in_bytes, out_bytes); return -2; } @@ -1140,17 +1197,17 @@ int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char if (in_bytes == 0) { in_bytes = (size_t)(*p_buf_len - *p_buf_offset); - log_debug("Reset in_bytes from 0 to %d\n", in_bytes); + log_debug("Reset in_bytes from 0 to %zu\n", in_bytes); } - log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", - in_bytes, out_bytes, in_buf[0]); + log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: EILSEQ, in_buf[0]=%d\n", + in_bytes, out_bytes, (unsigned char)in_buf[0]); skip_current = 1; } else // something strange { - log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", - in_bytes, out_bytes, errno, in_buf[0]); + log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: %d, in_buf[0]=%d\n", + in_bytes, out_bytes, errno, (unsigned char)in_buf[0]); *p_buf_offset = (int)(in_buf - p_buf); *p_conv_len = (int)(conv_size - out_bytes); skip_current = 1; diff --git a/src/log.c b/src/log.c index 5c20cd2..006ff6c 100644 --- a/src/log.c +++ b/src/log.c @@ -27,8 +27,8 @@ enum _log_constant_t static char path_common_log[FILE_PATH_LEN]; static char path_error_log[FILE_PATH_LEN]; -static FILE *fp_common_log; -static FILE *fp_error_log; +static FILE *fp_common_log = NULL; +static FILE *fp_error_log = NULL; static int redir_common_log = 0; static int redir_error_log = 0; @@ -42,14 +42,16 @@ int log_begin(const char *common_log_file, const char *error_log_file) fp_common_log = fopen(path_common_log, "a"); if (fp_common_log == NULL) { - fprintf(stderr, "fopen(%s) error: %d\n", path_common_log, errno); + fprintf(stderr, "fopen(%s) error: %s\n", path_common_log, strerror(errno)); return -1; } fp_error_log = fopen(path_error_log, "a"); if (fp_error_log == NULL) { - fprintf(stderr, "fopen(%s) error: %d\n", path_error_log, errno); + fprintf(stderr, "fopen(%s) error: %s\n", path_error_log, strerror(errno)); + fclose(fp_common_log); + fp_common_log = NULL; return -2; } @@ -61,15 +63,24 @@ int log_begin(const char *common_log_file, const char *error_log_file) void log_end() { - fclose(fp_common_log); - fclose(fp_error_log); + if (fp_common_log) + { + fclose(fp_common_log); + fp_common_log = NULL; + } + if (fp_error_log) + { + fclose(fp_error_log); + fp_error_log = NULL; + } } -inline static void log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) +inline static int log_head(char *buf, size_t len, int log_level, const char *app_file, int app_line) { time_t t; struct tm gm_tm; char s_time[STR_LOG_TIME_MAX_LEN + 1]; + int ret; time(&t); gmtime_r(&t, &gm_tm); @@ -77,37 +88,66 @@ inline static void log_head(char *buf, size_t len, int log_level, const char *ap if (log_level == LOG_LEVEL_COMMON) { - snprintf(buf, len, "[%s] [%d] [INFO] ", s_time, getpid()); + ret = snprintf(buf, len, "[%s] [%d] [INFO] ", s_time, getpid()); } else if (log_level == LOG_LEVEL_ERROR) { - snprintf(buf, len, "[%s] [%d] [ERROR] [%s:%d] ", s_time, getpid(), app_file, app_line); + ret = snprintf(buf, len, "[%s] [%d] [ERROR] [%s:%d] ", s_time, getpid(), app_file, app_line); } else // if (log_level == LOG_LEVEL_DEBUG) { - snprintf(buf, len, "[%s] [%d] [DEBUG] [%s:%d] ", s_time, getpid(), app_file, app_line); + ret = snprintf(buf, len, "[%s] [%d] [DEBUG] [%s:%d] ", s_time, getpid(), app_file, app_line); } + + return ret; } int log_printf(enum log_level_t log_level, const char *app_file, int app_line, const char *format, ...) { va_list args; - int retval; char buf[LINE_BUFFER_LEN]; FILE *fp_log; + int offset; + int ret; fp_log = (log_level == LOG_LEVEL_COMMON ? fp_common_log : fp_error_log); - log_head(buf, sizeof(buf), log_level, app_file, app_line); - strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf))); + offset = log_head(buf, sizeof(buf), log_level, app_file, app_line); va_start(args, format); - retval = vfprintf(fp_log, buf, args); + ret = vsnprintf(buf + offset, sizeof(buf) - (size_t)offset, format, args); va_end(args); - fflush(fp_log); + if (ret < 0) + { + // Encoding error + return -1; + } + else if (offset + ret >= sizeof(buf)) + { + buf[sizeof(buf) - 2] = '\n'; // Add newline for truncated messages + buf[sizeof(buf) - 1] = '\0'; // Ensure null termination + if (fputs(buf, fp_log) == EOF) + { + return -3; // Write error + } + ret = -2; // Indicate truncation + } + else + { + if (fputs(buf, fp_log) == EOF) + { + return -3; // Write error + } + ret = offset + ret; + } + + if (fflush(fp_log) == EOF) + { + return -4; // Flush error + } - return retval; + return ret; } int log_common_redir(int fd) @@ -124,30 +164,43 @@ int log_error_redir(int fd) int log_restart(void) { - FILE *fp; + FILE *fp_common = NULL; + FILE *fp_error = NULL; if (!redir_common_log) { - fp = fopen(path_common_log, "a"); - if (fp == NULL) + fp_common = fopen(path_common_log, "a"); + if (fp_common == NULL) { - log_error("fopen(%s) error: %d\n", path_common_log, errno); + log_error("fopen(%s) error: %s\n", path_common_log, strerror(errno)); return -1; } - fclose(fp_common_log); - fp_common_log = fp; } if (!redir_error_log) { - fp = fopen(path_error_log, "a"); - if (fp == NULL) + fp_error = fopen(path_error_log, "a"); + if (fp_error == NULL) { - log_error("fopen(%s) error: %d\n", path_error_log, errno); + log_error("fopen(%s) error: %s\n", path_error_log, strerror(errno)); + if (fp_common) + { + fclose(fp_common); + } return -2; } + } + + // Apply new file pointers + if (fp_common) + { + fclose(fp_common_log); + fp_common_log = fp_common; + } + if (fp_error) + { fclose(fp_error_log); - fp_error_log = fp; + fp_error_log = fp_error; } return 0; diff --git a/src/menu_proc.c b/src/menu_proc.c index fd568de..14f2da4 100644 --- a/src/menu_proc.c +++ b/src/menu_proc.c @@ -320,7 +320,7 @@ int show_top10_menu(void *param) case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); loop = 0; break; } diff --git a/src/net_server.c b/src/net_server.c index 269a1e3..b66bdfa 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -789,6 +789,8 @@ int net_server(const char *hostaddr, in_port_t port[]) sd_notify(0, "RELOADING=1"); #endif + log_common("Reload configuration\n"); + // Restart log if (log_restart() < 0) { diff --git a/src/screen.c b/src/screen.c index 1bb7ce1..1fedf65 100644 --- a/src/screen.c +++ b/src/screen.c @@ -719,7 +719,7 @@ int display_data(const void *p_data, long display_line_total, const long *p_line log_debug("KEY_NULL\n"); goto cleanup; case KEY_TIMEOUT: - log_error("User input timeout\n"); + log_debug("User input timeout\n"); goto cleanup; case KEY_HOME: if (line_current - output_current_row < 0) // Reach begin diff --git a/src/section_list_display.c b/src/section_list_display.c index 39f8d0c..2c5205d 100644 --- a/src/section_list_display.c +++ b/src/section_list_display.c @@ -286,7 +286,7 @@ static enum select_cmd_t section_list_select(int total_page, int item_count, int case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); return EXIT_SECTION; } continue; @@ -1421,7 +1421,7 @@ int section_list_ex_dir_display(SECTION_LIST *p_section) case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); loop = 0; break; } diff --git a/src/user_list_display.c b/src/user_list_display.c index 3ede9a5..655ba73 100644 --- a/src/user_list_display.c +++ b/src/user_list_display.c @@ -236,7 +236,7 @@ static enum select_cmd_t user_list_select(int total_page, int item_count, int *p case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { - log_error("User input timeout\n"); + log_debug("User input timeout\n"); return EXIT_LIST; // exit list } continue;