Replaced some thread un-safe functions with their safe counterparts
This commit is contained in:
@@ -262,11 +262,13 @@ FILE * server_generate_directory_index(config_host *hconfig, const char* dirpath
|
||||
}
|
||||
|
||||
time_t file_mtime = (time_t)filestat->st_mtim.tv_sec;
|
||||
char* file_mod_time = ctime(&file_mtime);
|
||||
char* file_mod_time = calloc(32, sizeof(char));
|
||||
ctime_r(&file_mtime, file_mod_time);
|
||||
|
||||
utstring_printf(index, "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\r\n", uri,
|
||||
(filesize!=NULL)?filesize:"N/A",
|
||||
(file_mod_time!=NULL)?file_mod_time:"N/A");
|
||||
free(file_mod_time);
|
||||
free(filepath);
|
||||
free(filesize);
|
||||
free(uri);
|
||||
|
||||
@@ -71,7 +71,9 @@ void*log_loop(void* arg) {
|
||||
if (l->running == false) {
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "log[%s] read failed: %s. logger aborted\n", l->name, strerror(errno));
|
||||
char errnostr[64];
|
||||
strerror_r(errno, errnostr, 64);
|
||||
fprintf(stderr, "log[%s] read failed: %s. logger aborted\n", l->name, errnostr);
|
||||
log_stop(l);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ int main(int argc, char** argv) {
|
||||
log_register_add(log_new("stderr", stderr), true, LALL & ~(LINFO|LDEBUG));
|
||||
log_register_add(log_new("stdout", stdout), false, LDEBUG | LINFO);
|
||||
|
||||
|
||||
mime_init(NULL);
|
||||
config_server *config = config_server_new();
|
||||
if (config_read_ini("khttpd.ini", config) < 0) {
|
||||
|
||||
@@ -90,4 +90,11 @@ void queue_ping(queue *q) {
|
||||
QUEUE_LOCK(q);
|
||||
pthread_cond_broadcast(q->cond);
|
||||
QUEUE_UNLOCK(q);
|
||||
}
|
||||
size_t queue_count(queue *q) {
|
||||
size_t count;
|
||||
QUEUE_LOCK(q);
|
||||
count = q->count;
|
||||
QUEUE_UNLOCK(q);
|
||||
return count;
|
||||
}
|
||||
@@ -53,6 +53,7 @@ extern "C" {
|
||||
queue_item* queue_fetchone(queue *q, bool blocking);
|
||||
void queue_clear(queue *q);
|
||||
void queue_ping(queue *q);
|
||||
size_t queue_count(queue *q);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ void thread_pool_delete(thread_pool *pool) {
|
||||
void thread_pool_start(thread_pool *pool) {
|
||||
info("Starting thread pool %s", pool->name);
|
||||
pool->management_thread = thread_new(pool);
|
||||
thread_start(pool->management_thread, thread_mgt);
|
||||
thread_start(pool->management_thread, thread_pool_loop);
|
||||
}
|
||||
void thread_pool_stop(thread_pool *pool) {
|
||||
info("Stopping thread pool %s", pool->name);
|
||||
@@ -98,7 +98,7 @@ void thread_pool_remove_thread(thread_pool *pool, thread *th) {
|
||||
pool->thread_count--;
|
||||
}
|
||||
|
||||
void* thread_mgt(void* arg) {
|
||||
void* thread_pool_loop(void* arg) {
|
||||
thread* th = (thread*)arg;
|
||||
thread_pool *pool = th->pool;
|
||||
|
||||
@@ -122,18 +122,18 @@ void* thread_mgt(void* arg) {
|
||||
|
||||
int64_t last_queue_count = 0;
|
||||
while(pool->shutdown == false) {
|
||||
|
||||
int64_t queue_count_diff = pool->queue->count - last_queue_count;
|
||||
size_t queue_size = queue_count(pool->queue);
|
||||
int64_t queue_count_diff = queue_size - last_queue_count;
|
||||
int64_t watermark = pool->thread_count * pool->queue_factor;
|
||||
if (queue_count_diff > watermark && pool->thread_count < pool->max_threads) {
|
||||
//New thread
|
||||
thread *th = thread_new(pool);
|
||||
thread_pool_add_thread(pool, th);
|
||||
last_queue_count = pool->queue->count;
|
||||
last_queue_count = queue_size;
|
||||
} else if ((queue_count_diff*-1) > watermark && pool->thread_count > pool->min_threads) {
|
||||
//Remove thread (stop the first in the list)
|
||||
pool->threads->stop = true;
|
||||
last_queue_count = pool->queue->count;
|
||||
last_queue_count = queue_size;
|
||||
}
|
||||
|
||||
//cleanup finished threads
|
||||
@@ -156,7 +156,7 @@ void* thread_mgt(void* arg) {
|
||||
nanosleep(&loopdelay, NULL);
|
||||
}
|
||||
//Wait until queue is empty
|
||||
while(pool->queue->count > 0) {
|
||||
while(queue_count(pool->queue) > 0) {
|
||||
nanosleep(&loopdelay, NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ extern "C" {
|
||||
void thread_pool_add_thread(thread_pool *pool, thread *th);
|
||||
void thread_pool_remove_thread(thread_pool *pool, thread *th);
|
||||
|
||||
void* thread_mgt(void* th);
|
||||
void* thread_pool_loop(void* th);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -79,7 +79,8 @@ char** str_splitlines(char *str, size_t *line_count) {
|
||||
}
|
||||
|
||||
size_t i=0, linelen = 0;
|
||||
char *line = strtok(str, "\n");
|
||||
char* saveptr=NULL;
|
||||
char *line = strtok_r(str, "\n", &saveptr);
|
||||
while(line) {
|
||||
linelen = strlen(line);
|
||||
result[i] = calloc(linelen+1, sizeof(char));
|
||||
@@ -91,7 +92,7 @@ char** str_splitlines(char *str, size_t *line_count) {
|
||||
result[i][linelen-1] = '\0';
|
||||
result[i] = realloc(result[i], linelen);
|
||||
}
|
||||
line = strtok(NULL, "\n");
|
||||
line = strtok_r(NULL, "\n", &saveptr);
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user