diff --git a/HISTORY b/HISTORY index 40a12a0..d92d5c0 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ Version 1.12 2014-12-05 * bug fixed: must check the return value of vsnprintf + * can call sched_add_entries many times before schedule Version 1.11 2014-11-20 * remove usleep call in logger.c diff --git a/src/logger.c b/src/logger.c index 9bc89c3..860926a 100644 --- a/src/logger.c +++ b/src/logger.c @@ -800,9 +800,9 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... va_start(ap, format); len = vsnprintf(text, sizeof(text), format, ap); va_end(ap); - if (len > sizeof(text)) + if (len >= sizeof(text)) { - len = sizeof(text); + len = sizeof(text) - 1; } switch(priority) @@ -863,9 +863,9 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... va_start(ap, format); \ len = vsnprintf(text, sizeof(text), format, ap); \ va_end(ap); \ - if (len > sizeof(text)) \ + if (len >= sizeof(text)) \ { \ - len = sizeof(text); \ + len = sizeof(text) - 1; \ } \ } \ \ @@ -922,9 +922,9 @@ void logAccess(LogContext *pContext, struct timeval *tvStart, \ va_start(ap, format); len = vsnprintf(text, sizeof(text), format, ap); va_end(ap); - if (len > sizeof(text)) + if (len >= sizeof(text)) { - len = sizeof(text); + len = sizeof(text) - 1; } doLogEx(pContext, tvStart, NULL, text, len, false, true); } diff --git a/src/sched_thread.c b/src/sched_thread.c index ab82e86..808dcc7 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -408,6 +408,44 @@ static int sched_dup_array(const ScheduleArray *pSrcArray, \ return 0; } +static int sched_append_array(const ScheduleArray *pSrcArray, \ + ScheduleArray *pDestArray) +{ + int result; + int bytes; + ScheduleEntry *new_entries; + + if (pSrcArray->count == 0) + { + return 0; + } + + bytes = sizeof(ScheduleEntry) * (pDestArray->count + pSrcArray->count); + new_entries = (ScheduleEntry *)malloc(bytes); + if (new_entries == NULL) + { + result = errno != 0 ? errno : ENOMEM; + logError("file: "__FILE__", line: %d, " \ + "malloc %d bytes failed, " \ + "errno: %d, error info: %s", \ + __LINE__, bytes, result, STRERROR(result)); + return result; + } + + if (pDestArray->entries != NULL) + { + memcpy(new_entries, pDestArray->entries, + sizeof(ScheduleEntry) * pDestArray->count); + free(pDestArray->entries); + } + memcpy(new_entries + pDestArray->count, pSrcArray->entries, + sizeof(ScheduleEntry) * pSrcArray->count); + + pDestArray->entries = new_entries; + pDestArray->count += pSrcArray->count; + return 0; +} + int sched_add_entries(const ScheduleArray *pScheduleArray) { int result; @@ -419,14 +457,21 @@ int sched_add_entries(const ScheduleArray *pScheduleArray) return ENOENT; } - while (waiting_schedule_array.entries != NULL) - { - logDebug("file: "__FILE__", line: %d, " \ - "waiting for schedule array ready ...", __LINE__); - sleep(1); - } + if (waiting_schedule_array.entries != NULL) + { + if (g_schedule_flag) + { + while (waiting_schedule_array.entries != NULL) + { + logDebug("file: "__FILE__", line: %d, " \ + "waiting for schedule array ready ...", __LINE__); + sleep(1); + } + } + } - if ((result=sched_dup_array(pScheduleArray, &waiting_schedule_array))!=0) + if ((result=sched_append_array(pScheduleArray, + &waiting_schedule_array)) != 0) { return result; }