int main(int argc, char **argv) { char *arg; int i, ret; int level = YLOG_LOG; int number = 1; unsigned int wait_between_log = 0; while ((ret = options("f:p:v:l:m:n:r:w:Hh", argv, argc, &arg)) != -2) { switch (ret) { case 'r': yaz_log_init_max_size(atoi(arg)); break; case 'f': yaz_log_time_format(arg); break; case 'p': yaz_log_init_prefix(arg); break; case 'v': yaz_log_init_level(yaz_log_mask_str(arg)); break; case 'l': if (!strcmp(arg, "@")) yaz_log_init_file(0); else yaz_log_init_file(arg); break; case 'n': number = atoi(arg); break; case 'm': level = yaz_log_module_level(arg); break; case 'w': wait_between_log = atoi(arg); break; case 'H': yaz_log_set_handler(hook_func, 0); break; case 0: for (i = 0; i<number; i++) { yaz_log(level, "%d %s", i, arg); #if HAVE_UNISTD_H if (wait_between_log) sleep(wait_between_log); #endif } break; case 'h': default: fprintf(stderr, "tstlog [-f logformat] [-v level] [-l file] " "[-p prefix] [-m module] [-w sec] [-r max] [-n num] [-H] msg ..\n"); exit(1); } } exit(0); }
/* UNIX listener */ static void listener(IOCHAN h, int event) { COMSTACK line = (COMSTACK) iochan_getdata(h); int res; if (event == EVENT_INPUT) { COMSTACK new_line; if ((res = cs_listen_check(line, 0, 0, control_block.check_ip, control_block.daemon_name)) < 0) { yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed"); return; } else if (res == 1) { yaz_log(YLOG_WARN, "cs_listen incomplete"); return; } new_line = cs_accept(line); if (!new_line) { yaz_log(YLOG_FATAL, "Accept failed."); iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */ return; } yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line)); no_sessions++; if (control_block.dynamic) { if ((res = fork()) < 0) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork"); iochan_destroy(h); return; } else if (res == 0) /* child */ { char nbuf[100]; IOCHAN pp; for (pp = pListener; pp; pp = iochan_getnext(pp)) { COMSTACK l = (COMSTACK)iochan_getdata(pp); cs_close(l); iochan_destroy(pp); } sprintf(nbuf, "%s(%d)", me, no_sessions); yaz_log_init_prefix(nbuf); /* ensure that bend_stop is not called when each child exits - only for the main process .. */ control_block.bend_stop = 0; } else /* parent */ { cs_close(new_line); return; } } if (control_block.threads) { #if YAZ_POSIX_THREADS pthread_t child_thread; pthread_create(&child_thread, 0, new_session, new_line); pthread_detach(child_thread); #elif YAZ_GNU_THREADS pth_attr_t attr; pth_t child_thread; attr = pth_attr_new(); pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE); pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 32*1024); pth_attr_set(attr, PTH_ATTR_NAME, "session"); yaz_log(YLOG_DEBUG, "pth_spawn begin"); child_thread = pth_spawn(attr, new_session, new_line); yaz_log(YLOG_DEBUG, "pth_spawn finish"); pth_attr_destroy(attr); #else new_session(new_line); #endif } else new_session(new_line); } else if (event == EVENT_TIMEOUT) { yaz_log(log_server, "Shutting down listener."); iochan_destroy(h); } else { yaz_log(YLOG_FATAL, "Bad event on listener."); iochan_destroy(h); } }