src/rss/info.c \
src/rss/init.c \
src/session.c \
- src/setup.c \
src/torrent/add.c \
src/torrent/file.c \
src/torrent/free.c \
inc/pqueue.h \
inc/rss.h \
inc/session.h \
- inc/setup.h \
inc/torrent.h \
inc/tree.h \
inc/usage.h \
#define __INIT_H_
#include<getopt.h>
+#include<signal.h>
#include<default.h>
-#include<setup.h>
-#include<shutdown.h>
+#include<log.h>
+#include<session.h>
#include<usage.h>
extern struct option long_options[];
size_t next;
};
-int log_entries_init();
void log_message(enum log_level,FILE*,const char*,...);
void *log_poll(void*);
-void log_shutdown();
+int logging_setup();
#endif
extern struct session session;
-void session_clean();
struct torrent *session_find_torrent(uint8_t*,size_t);
int session_init();
+int session_setup();
int session_torrent_add(struct torrent*);
#endif
+++ /dev/null
-#ifndef __SETUP_H_
-#define __SETUP_H_
-
-#include<log.h>
-#include<session.h>
-
-int setup_logging();
-int setup_session();
-
-#endif
{0,0,0,0}
};
+static int signal_handler();
+
int init(int argc, char **argv) {
int c;
+
+ if(signal_handler()<0) { return -1; }
- if(setup_session()<0) { return -1; }
+ if(session_setup()<0) { return -1; }
if(defaults()<0) { return -1; }
if(opt_load_from_env()<0) { return -1; }
- if(setup_logging()<0) { return -1; }
+ if(logging_setup()<0) { return -1; }
while(1) {
int option_index = 0;
return 1;
}
+
+static void handle_interrupt() {
+ /*
+ * call exit explicitly will call functions registered
+ * with atexit and do appropriate cleanup.
+ */
+ exit(EXIT_FAILURE);
+}
+
+static int signal_handler() {
+ struct sigaction action;
+
+ action.sa_handler = &handle_interrupt;
+ action.sa_flags = SA_RESETHAND;
+ if(sigaction(SIGINT,&action,NULL)!=0) {
+ perror("sigaction");
+ return -1;
+ }
+
+ return 1;
+}
#include<log.h>
+pthread_t logging_thread;
+pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
+
static struct log_helper helper;
static struct log_entry *log_dequeue();
static void log_enqueue(struct log_entry*);
static void log_entries_clean(void*);
+static int log_entries_init();
static void log_flush(void*);
static void log_print();
static void log_print_prefix(FILE*);
+static void log_shutdown();
static struct log_entry *log_dequeue() {
// requires logging_mutex to be locked
pthread_mutex_unlock(&logging_mutex);
}
-int log_entries_init() {
+static void log_entries_clean(void *p) {
+ free(helper.p);
+}
+
+static int log_entries_init() {
helper.start = NULL;
helper.end = NULL;
helper.next = 0;
return 1;
}
-static void log_entries_clean(void *p) {
- free(helper.p);
-}
-
static void log_flush(void *p) {
pthread_mutex_lock(&logging_mutex);
return NULL;
}
-void log_shutdown() {
+static void log_shutdown() {
int ret_cancel, ret_join;
void *res;
log_msg(LOG_MESSAGE_SHUTDOWN_SUCCESS);
}
}
+
+int logging_setup() {
+ if(log_entries_init()<0) { return -1; }
+
+ if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) {
+ perror("pthread_create");
+ return -1;
+ }
+
+ if(0!=atexit(&log_shutdown)) {
+ perror("atexit");
+ return -1;
+ }
+
+ return 1;
+}
struct session session;
+static void session_clean();
static int session_torrent_add_by_infohash(struct torrent*);
static int session_torrent_add_by_path(struct torrent*);
static int session_torrent_resize(struct hash_map**,size_t);
-void session_clean() {
+static void session_clean() {
struct torrent *p;
for(size_t i=0;i<session.torrents.paths->size;i++) {
return 1;
}
+int session_setup() {
+ if(session_init(&session)<0) { return -1; }
+
+ if(0!=atexit(&session_clean)) {
+ perror("atexit");
+ return -1;
+ }
+
+ return 1;
+}
+
int session_torrent_add(struct torrent *p) {
int i;
+++ /dev/null
-#include<setup.h>
-
-pthread_t logging_thread;
-pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-int setup_logging() {
- if(log_entries_init()<0) { return -1; }
- if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) {
- perror("pthread_create");
- return -1;
- }
-
- if(0!=atexit(&log_shutdown)) {
- perror("atexit");
- return -1;
- }
-
- return 1;
-}
-
-int setup_session() {
- if(session_init(&session)<0) { return -1; }
-
- if(0!=atexit(&session_clean)) {
- perror("atexit");
- return -1;
- }
-
- return 1;
-}