static void daemon_select_loop (fd_set serverfds, fd_set sslfds, int maxfd) { fd_set rdfds; int nselect; for (;;) { if (reload) daemon_reload (); if (terminate) daemon_terminate (); rdfds = serverfds; errno = 0; nselect = select (maxfd + 1, &rdfds, NULL, NULL, NULL); if (nselect == -1) { switch (errno) { case EINTR: break; default: syslog(LOG_DEBUG, "select=%d err=%m", nselect); break; } } else { // nselect == 0 doesn't happen for (int fd = 0; fd <= maxfd && nselect > 0; fd++) { if (! FD_ISSET (fd, &rdfds)) continue; nselect--; daemon_accept (fd, serverfds, sslfds, maxfd); } } } }
static int _handleconn (int delay_time, int max_cost_time, int error) { if (error) { if (error == 1) { daemon_printf( "%d server temporarily unavailable\n", CODE_TEMPORARILY_UNAVAILABLE ); printf("daemon busy: temporarily unavailable\n"); } else { printf("daemon too busy: drop connection\n"); } net_close_fd(); return 0; } daemon_banner(); std::string buf; std::vector<std::string> *args; alarm(delay_time); while (true) { if (net_read_line(buf)) break; alarm(max_cost_time); printf("%s\n", buf.c_str()); args = args_new(buf.c_str()); if (args->size() == 0) { daemon_printf( "%d no command\n", CODE_SYNTAX_ERROR ); } else if ((*args)[0] == "client" && args->size()>=3) { daemon_client((*args)[1], (*args)[2]); } else if ((*args)[0] == "auth" && args->size()>=3) { daemon_auth((*args)[1], (*args)[2]); } else if ((*args)[0] == "lookup" && args->size()>=2) { if (args->size() >=3) { int wordcount = atoi((*args)[2].c_str()); if (wordcount < 1) { wordcount = 1; } else if (wordcount > 100) { wordcount = 100; } daemon_lookup((*args)[1], wordcount); } else { daemon_lookup((*args)[1], 30); } } else if ((*args)[0] == "query" && args->size()>=2) { daemon_query((*args)[1]); } else if ((*args)[0] == "selectquery" && args->size()>=2) { daemon_selectquery((*args)[1]); } else if ((*args)[0] == "smartquery" && args->size()>=3) { daemon_smartquery((*args)[1], (*args)[2]); } else if ((*args)[0] == "define" && args->size()>=2) { daemon_define((*args)[1]); } else if ((*args)[0] == "previous" && args->size()>=2) { if (args->size() >=3) { int wordcount = atoi((*args)[2].c_str()); if (wordcount < 1) { wordcount = 1; } else if (wordcount > 50) { wordcount = 50; } daemon_previous((*args)[1], wordcount); } else { daemon_previous((*args)[1], 15); } } else if ((*args)[0] == "next" && args->size()>=2) { if (args->size() >=3) { int wordcount = atoi((*args)[2].c_str()); if (wordcount < 1) { wordcount = 1; } else if (wordcount > 100) { wordcount = 100; } daemon_next((*args)[1], wordcount); } else { daemon_next((*args)[1], 30); } } else if ((*args)[0] == "quit" && args->size()>=1) { daemon_quit(); } else if ((*args)[0] == "register" && args->size()>=4) { daemon_register((*args)[1], (*args)[2], (*args)[3]); } else if ((*args)[0] == "change_password" && args->size()>=4) { daemon_change_password((*args)[1], (*args)[2], (*args)[3]); } else if ((*args)[0] == "setdictmask" && args->size()>=2) { daemon_setdictmask((*args)[1]); } else if ((*args)[0] == "getdictmask" && args->size()>=1) { daemon_getdictmask(); } else if ((*args)[0] == "setcollatefunc" && args->size()>=2) { daemon_setcollatefunc((*args)[1]); } else if ((*args)[0] == "getcollatefunc" && args->size()>=1) { daemon_getcollatefunc(); } else if ((*args)[0] == "setlanguage" && args->size()>=2) { daemon_setlanguage((*args)[1]); } else if ((*args)[0] == "getlanguage" && args->size()>=1) { daemon_getlanguage(); } else if ((*args)[0] == "setemail" && args->size()>=2) { daemon_setemail((*args)[1]); } else if ((*args)[0] == "getemail" && args->size()>=1) { daemon_getemail(); } else if ((*args)[0] == "getuserlevel" && args->size()>=1) { daemon_getuserlevel(); } else if ((*args)[0] == "maxdictcount" && args->size()>=1) { daemon_maxdictcount(); } else if ((*args)[0] == "dirinfo" && args->size()>=2) { daemon_dirinfo((*args)[1]); } else if ((*args)[0] == "dictinfo" && args->size()>=2) { daemon_dictinfo((*args)[1]); } else if ((*args)[0] == "userlevel" && args->size()>=4) { daemon_userlevel((*args)[1], (*args)[2], (*args)[3]); } else if ((*args)[0] == "fromto" && args->size() >= 1) { daemon_fromto(); } else if ((*args)[0] == "dictslist" && args->size()>=2) { daemon_dictslist((*args)[1]); } else if ((*args)[0] == "tmpdictmask" && args->size() >= 2) { daemon_LookupinTmpdictmask((*args)[1]); } else if ((*args)[0] == "getadinfo" && args->size()>=1) { daemon_getadinfo(); } else { daemon_printf( "%d unknown command\n", CODE_SYNTAX_ERROR ); } delete args; alarm(delay_time); } daemon_terminate( 0, "close" ); return 0; }
static void daemon_quit() { daemon_ok( CODE_GOODBYE, "bye"); daemon_terminate( 0, "quit" ); }