int tun_start(struct tundev *tun) { uv_loop_t *loop = uv_default_loop(); struct tundev_context *ctx = tun->contexts; tcp_client_start(ctx, loop); uv_poll_init(loop, &ctx->watcher, ctx->tunfd); uv_poll_start(&ctx->watcher, UV_READABLE, poll_cb); #ifndef ANDROID signal_install(loop, signal_cb, tun); #endif uv_run(loop, UV_RUN_DEFAULT); loop_close(loop); return 0; }
int main(int argc, char *argv[]) { int ret; struct rlimit rlim; char *conffile; if (argc == 2) { conffile = argv[1]; }else{ //conffile = "etc/memlink.conf"; usage(); return 0; } //DINFO("%s\n", MEMLINK_VERSION); //DINFO("config file: %s\n", conffile); myconfig_create(conffile); DNOTE("====== %s ======\n", MEMLINK_VERSION); //DNOTE("config file: %s\n", conffile); //DNOTE("data dir: %s\n", g_cf->datadir); if (g_cf->max_core) { struct rlimit rlim_new; if (getrlimit(RLIMIT_CORE, &rlim) == 0) { rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { /* failed. try raising just to the old max */ rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; (void)setrlimit(RLIMIT_CORE, &rlim_new); } } if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { DERROR("failed to ensure corefile creation\n"); MEMLINK_EXIT; } } if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { DERROR("failed to getrlimit number of files\n"); MEMLINK_EXIT; } else { int maxfiles = g_cf->max_conn + 20; if (rlim.rlim_cur < maxfiles) rlim.rlim_cur = maxfiles; if (rlim.rlim_max < rlim.rlim_cur) rlim.rlim_max = rlim.rlim_cur; if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) { DERROR("failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n"); MEMLINK_EXIT; } } if (change_group_user(g_cf->user) < 0) { DERROR("change group user error!\n"); MEMLINK_EXIT; } signal_install(); if (g_cf->is_daemon) { ret = daemonize(g_cf->max_core, 0); if (ret == -1) { char errbuf[1024]; strerror_r(errno, errbuf, 1024); DERROR("daemon error! %s\n", errbuf); MEMLINK_EXIT; } } logfile_create(g_cf->log_name, g_cf->log_level); if (g_cf->log_error_name[0] != 0) { logfile_error_separate(g_log, g_cf->log_error_name); } if (g_cf->log_rotate_type == LOG_ROTATE_SIZE) { logfile_rotate_size(g_log, g_cf->log_size, g_cf->log_count); }else if (g_cf->log_rotate_type == LOG_ROTATE_TIME){ logfile_rotate_time(g_log, g_cf->log_time, g_cf->log_count); } DINFO("logfile ok!\n"); myconfig_print(g_cf); if (g_cf->sync_mode == MODE_MASTER_BACKUP) { master(argv[0], conffile); } else if (g_cf->sync_mode == MODE_MASTER_SLAVE) { if (g_cf->role == ROLE_MASTER) { master(argv[0], conffile); }else{ slave(argv[0], conffile); } } else { DERROR("sync_mode is error\n"); MEMLINK_EXIT; } return 0; }
static int mainthread_scheduler(void) { int i, res, sig; thread_t *thread_from; thread_t *thread_to; pthread_mutex_t mutex; pthread_mutexattr_t mutexattr; sigset_t sigmask, oldmask; /* save the main thread id */ mainthread_pid = pthread_self(); TRACE("pid <%08x> mainthread\n", (unsigned int)(mainthread_pid)); /* 屏蔽suspend信号和resume信号 */ sigemptyset(&sigmask); sigaddset(&sigmask, MSG_SUSPEND); sigaddset(&sigmask, MSG_RESUME); pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); sigemptyset(&sigmask); sigaddset(&sigmask, SIGALRM); /* install signal handler of system tick */ signal_install(SIGALRM, mthread_signal_tick); /* install signal handler used to suspend/resume threads */ signal_install(MSG_SUSPEND, thread_suspend_signal_handler); signal_install(MSG_RESUME, thread_resume_signal_handler); /* create a mutex and condition val, used to indicate interrupts occrue */ ptr_int_mutex = &mutex; pthread_mutexattr_init(&mutexattr); pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP); pthread_mutex_init(ptr_int_mutex, &mutexattr); /* start timer */ start_sys_timer(); thread_to = (thread_t *) rt_interrupt_to_thread; thread_resume(thread_to); for (;;) { #if 1 if (sigwait(&sigmask, &sig) != 0) { printf("mthread: sigwait get unexpected sig %d\n", sig); } #else pause(); #endif TRACE("mthread:got sig %d\n", sig); /* signal mask sigalrm 屏蔽SIGALRM信号 */ pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); // if (systick_signal_flag != 0) if (pthread_mutex_trylock(ptr_int_mutex) == 0) { tick_interrupt_isr(); // systick_signal_flag = 0; pthread_mutex_unlock(ptr_int_mutex); } else { TRACE("try lock failed.\n"); } /* 开启SIGALRM信号 */ pthread_sigmask(SIG_UNBLOCK, &sigmask, &oldmask); } return 0; }
/** Clean up the signal handler. * * \see signal_install, sigaction(3) */ static void signal_cleanup (void) { logdebug("Restoring default signal handlers\n"); signal_install((sh_t)SIG_DFL); }
/** Set up the signal handler. * * \see signal_install, sighandler, sigaction(3) */ static void signal_setup (void) { logdebug("Installing signal handlers\n"); signal_install(sighandler); }