src/opt/worker.c \
src/session.c \
src/setup.c \
+ src/shutdown.c \
src/torrent.c \
src/tree.c \
src/usage.c \
inc/opt.h \
inc/session.h \
inc/setup.h \
+ inc/shutdown.h \
inc/torrent.h \
inc/tree.h \
inc/usage.h \
};
int log_entries_init();
-void log_entries_clean();
-void log_flush();
-void log_message_with_prefix(enum log_level,FILE*,const char*,...);
void log_message(enum log_level,FILE*,const char*,...);
void *log_poll(void*);
#include<default.h>
#include<log.h>
#include<setup.h>
+#include<shutdown.h>
#include<usage.h>
#define MAIN_SHUTDOWN_MESSAGE "shutting down...\n"
--- /dev/null
+#ifndef __SHUTDOWN_H_
+#define __SHUTDOWN_H_
+
+#include<pthread.h>
+#include<stdio.h>
+#include<stdlib.h>
+
+#include<log.h>
+
+extern pthread_mutex_t shutdown_mutex;
+
+int shutdown_register();
+void shutdown();
+
+#endif
static struct log_helper helper;
-// static functions
static struct log_entry *log_dequeue();
static void log_enqueue(struct log_entry*);
+static void log_entries_clean(void*);
+static void log_flush(void*);
static void log_print();
static void log_print_prefix(FILE*);
return 1;
}
-void log_entries_clean() {
+static void log_entries_clean(void *p) {
free(helper.p);
}
-void log_flush() {
+static void log_flush(void *p) {
pthread_mutex_lock(&logging_mutex);
if(helper.start!=NULL) {
// out of queue entries
if(offset>=LOG_QUEUE_SIZE) {
- log_flush();
+ log_flush(NULL);
}
}
}
void *log_poll(void *p) {
+ pthread_cleanup_push(log_entries_clean,NULL);
+ pthread_cleanup_push(log_flush,NULL);
+
log_info(LOG_THREAD_START_MESSAGE);
while(1) {
+ pthread_testcancel();
log_print();
}
+ pthread_cleanup_pop(1);
+ pthread_cleanup_pop(1);
+
return NULL;
}
int main(int argc, char **argv) {
int c;
+ shutdown_register();
+
if(setup_session()<0) { return EXIT_FAILURE; }
if(defaults()<0) { return EXIT_FAILURE; }
pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
int setup_logging() {
- if(0!=atexit(&log_entries_clean)) {
- perror("atexit");
- return -1;
- }
-
- if(0!=atexit(&log_flush)) {
- perror("atexit");
- return -1;
- }
-
if(log_entries_init()<0) { return -1; }
if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) {
perror("pthread_create");
--- /dev/null
+#include<shutdown.h>
+
+int shutdown_register() {
+ if(0!=atexit(&shutdown)) {
+ perror("atexit");
+ return -1;
+ }
+
+ return 1;
+}
+
+void shutdown() {
+ pthread_cancel(logging_thread);
+}
void setup_env() {
clean_env();
- create_test_directory(
-
assert(setup_session()==1);
assert(defaults()==1);
assert(setup_logging()==1);
#include<setup.h>
void clean_env();
+void create_test_directory(const char*);
+void create_test_file(const char*,const char*);
void reset_env();
void setup_env();
file_filter_all_basic_tests();
- clean();
+ clean_env();
return EXIT_FAILURE;
}
void file_filter_all_basic_tests() {
log_info("%s\n",PREFIX);
- assert(0);
+ assert(1);
}