diff options
Diffstat (limited to 'accel-pptpd/triton')
-rw-r--r-- | accel-pptpd/triton/CMakeLists.txt | 1 | ||||
-rw-r--r-- | accel-pptpd/triton/conf_file.c | 154 | ||||
-rw-r--r-- | accel-pptpd/triton/log.c | 57 | ||||
-rw-r--r-- | accel-pptpd/triton/md.c | 111 | ||||
-rw-r--r-- | accel-pptpd/triton/spinlock.h | 3 | ||||
-rw-r--r-- | accel-pptpd/triton/timer.c | 94 | ||||
-rw-r--r-- | accel-pptpd/triton/triton.c | 117 | ||||
-rw-r--r-- | accel-pptpd/triton/triton_p.h | 2 |
8 files changed, 280 insertions, 259 deletions
diff --git a/accel-pptpd/triton/CMakeLists.txt b/accel-pptpd/triton/CMakeLists.txt index a531438c..c78703d1 100644 --- a/accel-pptpd/triton/CMakeLists.txt +++ b/accel-pptpd/triton/CMakeLists.txt @@ -8,5 +8,6 @@ SET(sources_c log.c ) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) +ADD_DEFINITIONS("-DUSE_SPINLOCK") ADD_LIBRARY(${target} STATIC ${sources_c}) diff --git a/accel-pptpd/triton/conf_file.c b/accel-pptpd/triton/conf_file.c index 026c4d3c..b0bc58b7 100644 --- a/accel-pptpd/triton/conf_file.c +++ b/accel-pptpd/triton/conf_file.c @@ -19,101 +19,88 @@ static char* skip_word(char *str); static struct conf_sect_t *find_sect(const char *name); static struct conf_sect_t *create_sect(const char *name); -static void sect_add_item(struct conf_sect_t *sect,const char *name,const char *val); -static struct conf_option_t *find_item(struct conf_sect_t *,const char *name); +static void sect_add_item(struct conf_sect_t *sect, const char *name, const char *val); +static struct conf_option_t *find_item(struct conf_sect_t *, const char *name); int conf_load(const char *fname) { char *buf,*str,*str2; char *path0,*path; - int cur_line=0; - static struct conf_sect_t *cur_sect=NULL; - FILE *f=fopen(fname,"r"); - if (!f) - { - perror("conf_file: open"); + int cur_line = 0; + static struct conf_sect_t *cur_sect = NULL; + + FILE *f = fopen(fname, "r"); + if (!f) { + perror("conf_file:open"); return -1; } - buf=(char*)malloc(1024); - path0=(char*)malloc(4096); - path=(char*)malloc(4096); + buf = malloc(1024); + path0 = malloc(4096); + path = malloc(4096); - getcwd(path0,1024); + getcwd(path0, 1024); - while(!feof(f)) - { - buf=fgets(buf,1024,f); - if (!buf) break; + while(!feof(f)) { + buf = fgets(buf, 1024, f); + if (!buf) + break; ++cur_line; - if (buf[strlen(buf)-1]=='\n') - buf[strlen(buf)-1]=0; + if (buf[strlen(buf) - 1] == '\n') + buf[strlen(buf) - 1] = 0; - str=skip_space(buf); - if (*str=='#' || *str==0) continue; - if (strncmp(str,"$include",8)==0) - { - str=skip_word(str); - str=skip_space(str); - /*if (*str=='.') - { - strcpy(path,path0); - strcat(path,str+1); - str=path; - }*/ + str = skip_space(buf); + if (*str == '#' || *str == 0) + continue; + if (strncmp(str, "$include", 8) == 0) { + str = skip_word(str); + str = skip_space(str); conf_load(str); continue; } - if (*str=='[') - { - for (str2=++str; *str2 && *str2!=']'; str2++); - if (*str2!=']') - { -//L1: - fprintf(stderr,"conf_file:%s:%i: sintax error\n",fname,cur_line); + if (*str == '[') { + for (str2 = ++str; *str2 && *str2 != ']'; str2++); + if (*str2 != ']') { + fprintf(stderr, "conf_file:%s:%i: sintax error\n", fname, cur_line); return -1; } - *str2=0; - cur_sect=find_sect(str); - if (!cur_sect) cur_sect=create_sect(str); + *str2 = 0; + cur_sect = find_sect(str); + if (!cur_sect) + cur_sect = create_sect(str); continue; } - if (!cur_sect) - { - fprintf(stderr,"conf_file:%s:%i: no section opened\n",fname,cur_line); + if (!cur_sect) { + fprintf(stderr, "conf_file:%s:%i: no section opened\n", fname, cur_line); return -1; } - str2=skip_word(str); - if (*str2==' ') - { - *str2=0; + str2 = skip_word(str); + if (*str2 == ' ') { + *str2 = 0; ++str2; } - str2=skip_space(str2); - if (*str2=='=' || *str2==',') - { - *str2=0; - str2=skip_space(str2+1); - if (*str2 && *(str2+1) && *str2=='$' && *(str2+1)=='{') - { + str2 = skip_space(str2); + if (*str2 == '=' || *str2 == ',') { + *str2 = 0; + str2 = skip_space(str2 + 1); + if (*str2 && *(str2 + 1) && *str2 == '$' && *(str2 + 1) == '{') { char *s; struct conf_option_t *opt; - for (s=str2+2; *s && *s!='}'; s++); - if (*s=='}') - { - *s=0; - str2+=2; + for (s = str2+2; *s && *s != '}'; s++); + if (*s == '}') { + *s = 0; + str2 += 2; } - opt=find_item(cur_sect,str2); - if (!opt) - { - fprintf(stderr,"conf_file:%s:%i: parent option not found\n",fname,cur_line); + opt = find_item(cur_sect, str2); + if (!opt) { + fprintf(stderr, "conf_file:%s:%i: parent option not found\n", fname, cur_line); return -1; } - str2=opt->val; + str2 = opt->val; } - }else str2=NULL; - sect_add_item(cur_sect,str,str2); + } else + str2 = NULL; + sect_add_item(cur_sect, str, str2); } free(buf); @@ -126,54 +113,51 @@ int conf_load(const char *fname) static char* skip_space(char *str) { - for (; *str && *str==' '; str++); + for (; *str && *str == ' '; str++); return str; } static char* skip_word(char *str) { - for (; *str && (*str!=' ' && *str!='='); str++); + for (; *str && (*str != ' ' && *str != '='); str++); return str; } static struct conf_sect_t *find_sect(const char *name) { struct sect_t *s; - list_for_each_entry(s,§ions,entry) - { - if (strcmp(s->sect->name,name)==0) return s->sect; - } + list_for_each_entry(s, §ions, entry) + if (strcmp(s->sect->name, name) == 0) return s->sect; return NULL; } static struct conf_sect_t *create_sect(const char *name) { - struct sect_t *s=(struct sect_t *)malloc(sizeof(struct sect_t)); + struct sect_t *s = malloc(sizeof(struct sect_t)); - s->sect=(struct conf_sect_t*)malloc(sizeof(struct conf_sect_t)); - s->sect->name=(char*)strdup(name); + s->sect = malloc(sizeof(struct conf_sect_t)); + s->sect->name = (char*)strdup(name); INIT_LIST_HEAD(&s->sect->items); - list_add_tail(&s->entry,§ions); + list_add_tail(&s->entry, §ions); return s->sect; } -static void sect_add_item(struct conf_sect_t *sect,const char *name,const char *val) +static void sect_add_item(struct conf_sect_t *sect, const char *name, const char *val) { - struct conf_option_t *opt=(struct conf_option_t *)malloc(sizeof(struct conf_option_t)); + struct conf_option_t *opt = malloc(sizeof(struct conf_option_t)); - opt->name=(char*)strdup(name); - opt->val=val?(char*)strdup(val):NULL; + opt->name = strdup(name); + opt->val = val ? strdup(val) : NULL; - list_add_tail(&opt->entry,§->items); + list_add_tail(&opt->entry, §->items); } -static struct conf_option_t *find_item(struct conf_sect_t *sect,const char *name) +static struct conf_option_t *find_item(struct conf_sect_t *sect, const char *name) { struct conf_option_t *opt; - list_for_each_entry(opt,§->items,entry) - { - if (strcmp(opt->name,name)==0) + list_for_each_entry(opt, §->items, entry) { + if (strcmp(opt->name, name) == 0) return opt; } diff --git a/accel-pptpd/triton/log.c b/accel-pptpd/triton/log.c index 39a9cb18..7ff90f87 100644 --- a/accel-pptpd/triton/log.c +++ b/accel-pptpd/triton/log.c @@ -1,29 +1,27 @@ #include <stdio.h> +#include <stdarg.h> #include "triton_p.h" static FILE *f_error; static FILE *f_debug; +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int log_init(void) { - char *log_error=conf_get_opt("core","log_error"); - char *log_debug=conf_get_opt("core","log_debug"); - - if (log_error) - { - f_error=fopen(log_error,"a"); - if (!f_error) - { + char *log_error = conf_get_opt("core","log_error"); + char *log_debug = conf_get_opt("core","log_debug"); + + if (log_error) { + f_error = fopen(log_error, "a"); + if (!f_error) { perror("log:log_error:open"); return -1; } } - if (log_debug) - { - f_debug=fopen(log_debug,"a"); - if (!f_debug) - { + if (log_debug) { + f_debug = fopen(log_debug, "a"); + if (!f_debug) { perror("log:log_debug:open"); return -1; } @@ -32,3 +30,36 @@ int log_init(void) return 0; } +static void do_log(FILE *f, const char *fmt, va_list ap) +{ + struct timeval tv; + struct tm tm; + char date[64]; + + gettimeofday(&tv, NULL); + localtime_r(&tv.tv_sec, &tm); + strftime(date, sizeof(date), "%F %H:%M:%S", &tm); + + pthread_mutex_lock(&lock); + fprintf(f, "[%s.%i]", date, (int)tv.tv_usec / 1000); + vfprintf(f, fmt,ap); + pthread_mutex_unlock(&lock); +} +void triton_log_error(const char *fmt,...) +{ + va_list ap; + if (!f_error) + return; + va_start(ap, fmt); + do_log(f_error, fmt, ap); +} + +void triton_log_debug(const char *fmt,...) +{ + va_list ap; + if (!f_debug) + return; + va_start(ap, fmt); + do_log(f_debug, fmt, ap); +} + diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 50e969c9..874e4948 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -4,6 +4,7 @@ #include <signal.h> #include <string.h> #include <errno.h> +#include <unistd.h> #include "triton_p.h" @@ -13,74 +14,75 @@ static int epoll_fd; static struct epoll_event *epoll_events; static pthread_t md_thr; -static void* md_thread(void *arg); +static void *md_thread(void *arg); int md_init(void) { - epoll_fd=epoll_create(1); - if (epoll_fd<0) - { - perror("epoll_create"); + epoll_fd = epoll_create(1); + if (epoll_fd < 0) { + perror("md:epoll_create"); return -1; } - epoll_events=malloc(max_events * sizeof(struct epoll_event)); - if (!epoll_events) - { - fprintf(stderr,"cann't allocate memory\n"); + epoll_events = malloc(max_events * sizeof(struct epoll_event)); + if (!epoll_events) { + fprintf(stderr,"md:cann't allocate memory\n"); return -1; } return 0; } -void md_run() +void md_run(void) { - pthread_create(&md_thr,NULL,md_thread,NULL); + if (pthread_create(&md_thr, NULL, md_thread, NULL)) { + triton_log_error("md:pthread_create: %s", strerror(errno)); + _exit(-1); + } } -void md_terminate() +void md_terminate(void) { pthread_cancel(md_thr); - pthread_join(md_thr,NULL); + pthread_join(md_thr, NULL); } -static void* md_thread(void *arg) +static void *md_thread(void *arg) { int i,n,r; struct triton_md_handler_t *h; - while(1) - { - n=epoll_wait(epoll_fd,epoll_events,max_events,-1); - if (n<0) - { - if (errno!=EINTR) - perror("epoll_wait"); - continue; + while(1) { + n = epoll_wait(epoll_fd, epoll_events, max_events, -1); + if (n < 0) { + if (errno == EINTR) + continue; + triton_log_error("md:epoll_wait: %s", strerror(errno)); + _exit(-1); } - for(i=0; i<n; i++) - { - h=(struct triton_md_handler_t*)epoll_events[i].data.ptr; + for(i = 0; i < n; i++) { + h = (struct triton_md_handler_t *)epoll_events[i].data.ptr; spin_lock(&h->ctx->lock); - h->trig_epoll_events=epoll_events[i].events; - list_add_tail(&h->entry2,&h->ctx->pending_handlers); - h->pending=1; + h->trig_epoll_events = epoll_events[i].events; + list_add_tail(&h->entry2, &h->ctx->pending_handlers); + h->pending = 1; r=triton_queue_ctx(h->ctx); spin_unlock(&h->ctx->lock); if (r) triton_thread_wakeup(h->ctx->thread); } } + + return NULL; } void triton_md_register_handler(struct triton_md_handler_t *h) { - h->epoll_event.data.ptr=h; + h->epoll_event.data.ptr = h; if (!h->ctx) - h->ctx=default_ctx; + h->ctx = default_ctx; spin_lock(&h->ctx->lock); - list_add_tail(&h->entry,&h->ctx->handlers); + list_add_tail(&h->entry, &h->ctx->handlers); spin_unlock(&h->ctx->lock); } void triton_md_unregister_handler(struct triton_md_handler_t *h) @@ -94,42 +96,47 @@ void triton_md_unregister_handler(struct triton_md_handler_t *h) int triton_md_enable_handler(struct triton_md_handler_t *h, int mode) { int r; - int events=h->epoll_event.events; + int events = h->epoll_event.events; - if (mode&MD_MODE_READ) - h->epoll_event.events|=EPOLLIN; - if (mode&MD_MODE_WRITE) - h->epoll_event.events|=EPOLLOUT; + if (mode & MD_MODE_READ) + h->epoll_event.events |= EPOLLIN; + if (mode & MD_MODE_WRITE) + h->epoll_event.events |= EPOLLOUT; - h->epoll_event.events|=EPOLLET; + h->epoll_event.events |= EPOLLET; if (events) - r=epoll_ctl(epoll_fd,EPOLL_CTL_MOD,h->fd,&h->epoll_event); + r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, h->fd, &h->epoll_event); else - r=epoll_ctl(epoll_fd,EPOLL_CTL_ADD,h->fd,&h->epoll_event); - + r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, h->fd, &h->epoll_event); + + if (r) + triton_log_error("md:epoll_ctl: %s",strerror(errno)); + return r; } int triton_md_disable_handler(struct triton_md_handler_t *h,int mode) { - int r; + int r=0; if (!h->epoll_event.events) return -1; - if (mode&MD_MODE_READ) - h->epoll_event.events&=~EPOLLIN; - if (mode&MD_MODE_WRITE) - h->epoll_event.events&=~EPOLLOUT; - - if (h->epoll_event.events&(EPOLLIN|EPOLLOUT)) - r=epoll_ctl(epoll_fd,EPOLL_CTL_MOD,h->fd,&h->epoll_event); - else - { - h->epoll_event.events=0; - r=epoll_ctl(epoll_fd,EPOLL_CTL_DEL,h->fd,NULL); + if (mode & MD_MODE_READ) + h->epoll_event.events &= ~EPOLLIN; + if (mode & MD_MODE_WRITE) + h->epoll_event.events &= ~EPOLLOUT; + + if (h->epoll_event.events & (EPOLLIN | EPOLLOUT)) + r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, h->fd, &h->epoll_event); + else { + h->epoll_event.events = 0; + r = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, h->fd, NULL); } + if (r) + triton_log_error("md:epoll_ctl: %s",strerror(errno)); + return r; } diff --git a/accel-pptpd/triton/spinlock.h b/accel-pptpd/triton/spinlock.h index 7da93f8c..b6d1656a 100644 --- a/accel-pptpd/triton/spinlock.h +++ b/accel-pptpd/triton/spinlock.h @@ -3,9 +3,10 @@ #ifdef USE_SPINLOCK typedef unsigned char spinlock_t; -#define spin_lock(l) {while(__sync_lock_test_and_set(l,1);} +#define spin_lock(l) {while(__sync_lock_test_and_set(l,1));} #define spin_unlock(l) __sync_lock_release(l) #define SPINLOCK_INITIALIZER 0 +#define spinlock_init(l) {*(l)=0;} #else #include <pthread.h> typedef pthread_mutex_t spinlock_t; diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index 3adbde5a..92187be7 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -9,7 +9,7 @@ #include "triton_p.h" -int max_events=128; +extern int max_events; static int epoll_fd; static struct epoll_event *epoll_events; @@ -18,17 +18,15 @@ static void *timer_thread(void *arg); int timer_init(void) { - epoll_fd=epoll_create(1); - if (epoll_fd<0) - { - perror("epoll_create"); + epoll_fd = epoll_create(1); + if (epoll_fd < 0) { + perror("timer:epoll_create"); return -1; } - epoll_events=malloc(max_events * sizeof(struct epoll_event)); - if (!epoll_events) - { - fprintf(stderr,"cann't allocate memory\n"); + epoll_events = malloc(max_events * sizeof(struct epoll_event)); + if (!epoll_events) { + fprintf(stderr,"timer:cann't allocate memory\n"); return -1; } @@ -37,13 +35,16 @@ int timer_init(void) void timer_run(void) { - pthread_create(&timer_thr,NULL,timer_thread,NULL); + if (pthread_create(&timer_thr, NULL, timer_thread, NULL)) { + triton_log_error("timer:pthread_create: %s",strerror(errno)); + _exit(-1); + } } void timer_terminate(void) { pthread_cancel(timer_thr); - pthread_join(timer_thr,NULL); + pthread_join(timer_thr, NULL); } void *timer_thread(void *arg) @@ -51,56 +52,53 @@ void *timer_thread(void *arg) int i,n,r; struct triton_timer_t *t; - while(1) - { - n=epoll_wait(epoll_fd,epoll_events,max_events,-1); - if (n<0) - { - if (errno!=EINTR) - perror("epoll_wait"); - continue; + while(1) { + n = epoll_wait(epoll_fd, epoll_events, max_events, -1); + if (n < 0) { + if (errno == EINTR) + continue; + triton_log_error("timer:epoll_wait: %s", strerror(errno)); + _exit(-1); } - for(i=0; i<n; i++) - { - t=(struct triton_timer_t*)epoll_events[i].data.ptr; + for(i = 0; i < n; i++) { + t = (struct triton_timer_t *)epoll_events[i].data.ptr; spin_lock(&t->ctx->lock); - list_add_tail(&t->entry2,&t->ctx->pending_timers); - t->pending=1; + list_add_tail(&t->entry2, &t->ctx->pending_timers); + t->pending = 1; r=triton_queue_ctx(t->ctx); spin_unlock(&t->ctx->lock); if (r) triton_thread_wakeup(t->ctx->thread); } } + + return NULL; } int triton_timer_add(struct triton_timer_t *t, int abs_time) { - t->epoll_event.data.ptr=t; - t->epoll_event.events=EPOLLIN|EPOLLET; + t->epoll_event.data.ptr = t; + t->epoll_event.events = EPOLLIN | EPOLLET; if (!t->ctx) - t->ctx=default_ctx; - t->fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK); - if (t->fd<0) - { - fprintf(stderr,"timer: timerfd_create failed: %s\n",strerror(errno)); + t->ctx = default_ctx; + t->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); + if (t->fd < 0) { + triton_log_error("timer:timerfd_create: %s" ,strerror(errno)); return -1; } - if (triton_timer_mod(t,abs_time)) - { + if (triton_timer_mod(t, abs_time)) { close(t->fd); return -1; } spin_lock(&t->ctx->lock); - list_add_tail(&t->entry,&t->ctx->timers); + list_add_tail(&t->entry, &t->ctx->timers); spin_unlock(&t->ctx->lock); - if (epoll_ctl(epoll_fd,EPOLL_CTL_ADD,t->fd,&t->epoll_event)) - { - fprintf(stderr,"timer: epoll_ctl failed: %s\n",strerror(errno)); + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, t->fd, &t->epoll_event)) { + triton_log_error("timer:epoll_ctl: %s", strerror(errno)); spin_lock(&t->ctx->lock); list_del(&t->entry); spin_unlock(&t->ctx->lock); @@ -112,20 +110,18 @@ int triton_timer_add(struct triton_timer_t *t, int abs_time) } int triton_timer_mod(struct triton_timer_t *t,int abs_time) { - struct itimerspec ts= - { - .it_value.tv_sec=t->expire_tv.tv_sec, - .it_value.tv_nsec=t->expire_tv.tv_usec*1000, - .it_interval.tv_sec=t->period/1000, - .it_interval.tv_nsec=t->period%1000*1000, + struct itimerspec ts = { + .it_value.tv_sec = t->expire_tv.tv_sec, + .it_value.tv_nsec = t->expire_tv.tv_usec * 1000, + .it_interval.tv_sec = t->period / 1000, + .it_interval.tv_nsec = t->period % 1000 * 1000, }; - if (t->expire_tv.tv_sec==0 && t->expire_tv.tv_usec==0) - ts.it_value=ts.it_interval; + if (t->expire_tv.tv_sec == 0 && t->expire_tv.tv_usec == 0) + ts.it_value = ts.it_interval; - if (timerfd_settime(t->fd,abs_time?TFD_TIMER_ABSTIME:0,&ts,NULL)) - { - fprintf(stderr,"timer: timerfd_settime failed: %s\n",strerror(errno)); + if (timerfd_settime(t->fd, abs_time ? TFD_TIMER_ABSTIME : 0, &ts, NULL)) { + triton_log_error("timer:timerfd_settime: %s", strerror(errno)); return -1; } @@ -133,7 +129,7 @@ int triton_timer_mod(struct triton_timer_t *t,int abs_time) } void triton_timer_del(struct triton_timer_t *t) { - epoll_ctl(epoll_fd,EPOLL_CTL_DEL,t->fd,&t->epoll_event); + epoll_ctl(epoll_fd, EPOLL_CTL_DEL, t->fd, &t->epoll_event); close(t->fd); spin_lock(&t->ctx->lock); list_del(&t->entry); diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index e4fa117c..2b160a91 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -3,18 +3,20 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <unistd.h> #include "triton_p.h" -int thread_count=64; +int thread_count = 64; +int max_events = 64; -static spinlock_t threads_lock=SPINLOCK_INITIALIZER; +static spinlock_t threads_lock = SPINLOCK_INITIALIZER; static LIST_HEAD(threads); static LIST_HEAD(sleep_threads); static LIST_HEAD(ctx_queue); -static spinlock_t ctx_list_lock=SPINLOCK_INITIALIZER; +static spinlock_t ctx_list_lock = SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_list); struct triton_ctx_t *default_ctx; @@ -22,7 +24,7 @@ static int terminate; void triton_thread_wakeup(struct triton_thread_t *thread) { - pthread_kill(thread->thread,SIGUSR1); + pthread_kill(thread->thread, SIGUSR1); } static void* triton_thread(struct triton_thread_t *thread) @@ -33,72 +35,65 @@ static void* triton_thread(struct triton_thread_t *thread) int sig; sigemptyset(&set); - sigaddset(&set,SIGUSR1); - sigaddset(&set,SIGQUIT); + sigaddset(&set, SIGUSR1); + sigaddset(&set, SIGQUIT); - while(1) - { - sigwait(&set,&sig); + while(1){ + sigwait(&set, &sig); cont: - if (thread->ctx->need_close) - { + if (thread->ctx->need_close) { thread->ctx->close(thread->ctx); - thread->ctx->need_close=0; + thread->ctx->need_close = 0; } - while (1) - { + while (1) { spin_lock(&thread->ctx->lock); - if (!list_empty(&thread->ctx->pending_timers)) - { - t=list_entry(thread->ctx->pending_timers.next,typeof(*t),entry2); + if (!list_empty(&thread->ctx->pending_timers)) { + t = list_entry(thread->ctx->pending_timers.next, typeof(*t), entry2); list_del(&t->entry2); spin_unlock(&thread->ctx->lock); if (t->expire(t)) continue; } - if (!list_empty(&thread->ctx->pending_handlers)) - { - h=list_entry(thread->ctx->pending_handlers.next,typeof(*h),entry2); + if (!list_empty(&thread->ctx->pending_handlers)) { + h = list_entry(thread->ctx->pending_handlers.next, typeof(*h), entry2); list_del(&h->entry2); - h->pending=0; + h->pending = 0; spin_unlock(&thread->ctx->lock); - if (h->trig_epoll_events&(EPOLLIN|EPOLLERR|EPOLLHUP)) + if (h->trig_epoll_events & (EPOLLIN | EPOLLERR | EPOLLHUP)) if (h->read) if (h->read(h)) continue; - if (h->trig_epoll_events&(EPOLLOUT)) + if (h->trig_epoll_events & EPOLLOUT) if (h->write) if (h->write(h)) continue; - h->trig_epoll_events=0; + h->trig_epoll_events = 0; continue; } - thread->ctx->thread=NULL; + thread->ctx->thread = NULL; spin_unlock(&thread->ctx->lock); if (thread->ctx->need_free) thread->ctx->free(thread->ctx); - thread->ctx=NULL; + thread->ctx = NULL; break; } spin_lock(&threads_lock); - if (!list_empty(&ctx_queue)) - { - thread->ctx=list_entry(ctx_queue.next,typeof(*thread->ctx),entry2); + if (!list_empty(&ctx_queue)) { + thread->ctx = list_entry(ctx_queue.next, typeof(*thread->ctx), entry2); list_del(&thread->ctx->entry2); spin_unlock(&threads_lock); spin_lock(&thread->ctx->lock); - thread->ctx->thread=thread; - thread->ctx->queued=0; + thread->ctx->thread = thread; + thread->ctx->queued = 0; spin_unlock(&thread->ctx->lock); goto cont; - }else - { + } else { if (!terminate) - list_add(&thread->entry2,&sleep_threads); + list_add(&thread->entry2, &sleep_threads); spin_unlock(&threads_lock); if (terminate) return NULL; @@ -108,10 +103,15 @@ cont: struct triton_thread_t *create_thread() { - struct triton_thread_t *thread=malloc(sizeof(*thread)); - - memset(thread,0,sizeof(*thread)); - pthread_create(&thread->thread,NULL,(void*(*)(void*))triton_thread,thread); + struct triton_thread_t *thread = malloc(sizeof(*thread)); + if (!thread) + return NULL; + + memset(thread, 0, sizeof(*thread)); + if (pthread_create(&thread->thread, NULL, (void*(*)(void*))triton_thread, thread)) { + triton_log_error("pthread_create: %s", strerror(errno)); + return NULL; + } return thread; } @@ -122,15 +122,14 @@ int triton_queue_ctx(struct triton_ctx_t *ctx) return 0; spin_lock(&threads_lock); - if (list_empty(&sleep_threads)) - { - list_add_tail(&ctx->entry2,&ctx_queue); + if (list_empty(&sleep_threads)) { + list_add_tail(&ctx->entry2, &ctx_queue); spin_unlock(&threads_lock); - ctx->queued=1; + ctx->queued = 1; return 0; } - ctx->thread=list_entry(sleep_threads.next,typeof(*ctx->thread),entry2); + ctx->thread = list_entry(sleep_threads.next, typeof(*ctx->thread), entry2); list_del(&ctx->thread->entry2); spin_unlock(&threads_lock); @@ -146,13 +145,13 @@ void triton_register_ctx(struct triton_ctx_t *ctx) INIT_LIST_HEAD(&ctx->pending_timers); spin_lock(&ctx_list_lock); - list_add_tail(&ctx->entry,&ctx_list); + list_add_tail(&ctx->entry, &ctx_list); spin_unlock(&ctx_list_lock); } void triton_unregister_ctx(struct triton_ctx_t *ctx) { - ctx->need_free=1; + ctx->need_free = 1; spin_lock(&ctx_list_lock); list_del(&ctx->entry); spin_unlock(&ctx_list_lock); @@ -161,8 +160,7 @@ void triton_unregister_ctx(struct triton_ctx_t *ctx) int triton_init(const char *conf_file) { default_ctx=malloc(sizeof(*default_ctx)); - if (!default_ctx) - { + if (!default_ctx) { fprintf(stderr,"cann't allocate memory\n"); return -1; } @@ -188,11 +186,13 @@ void triton_run() struct triton_thread_t *t; int i; - for(i=0;i<thread_count;i++) - { - t=create_thread(); - list_add_tail(&t->entry,&threads); - list_add_tail(&t->entry2,&sleep_threads); + for(i = 0; i < thread_count; i++) { + t = create_thread(); + if (!t) + _exit(-1); + + list_add_tail(&t->entry, &threads); + list_add_tail(&t->entry2, &sleep_threads); } md_run(); @@ -208,23 +208,22 @@ void triton_terminate() timer_terminate(); spin_lock(&ctx_list_lock); - list_for_each_entry(ctx,&ctx_list,entry) - { + list_for_each_entry(ctx, &ctx_list, entry) { spin_lock(&ctx->lock); - ctx->need_close=1; + ctx->need_close = 1; triton_queue_ctx(ctx); spin_unlock(&ctx->lock); } spin_unlock(&ctx_list_lock); spin_lock(&threads_lock); - terminate=1; + terminate = 1; spin_unlock(&threads_lock); - list_for_each_entry(t,&threads,entry) + list_for_each_entry(t, &threads, entry) triton_thread_wakeup(t); - list_for_each_entry(t,&threads,entry) - pthread_join(t->thread,NULL); + list_for_each_entry(t, &threads, entry) + pthread_join(t->thread, NULL); } diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h index e10c7a8e..385f3558 100644 --- a/accel-pptpd/triton/triton_p.h +++ b/accel-pptpd/triton/triton_p.h @@ -15,5 +15,7 @@ struct triton_ctx_t *default_ctx; int triton_queue_ctx(struct triton_ctx_t*); void triton_thread_wakeup(struct triton_thread_t*); int conf_load(const char *fname); +void triton_log_error(const char *fmt,...); +void triton_log_debug(const char *fmt,...); #endif |