int main(int argc, char *argv[]) { int soc; /* 引数にポート番号が指定されているか? */ if (argc <= 1) { (void) fprintf(stderr,"server port\n"); return (EX_USAGE); } /* サーバソケットの準備 */ if ((soc = server_socket(argv[1])) == -1) { (void) fprintf(stderr,"server_socket(%s):error\n", argv[1]); return (EX_UNAVAILABLE); } (void) fprintf(stdout, "ready for accept\n"); /* アクセプトループ */ accept_loop(soc); /* for (;;) { */ /* (void) fprintf(stdout, "just sleep\n"); */ /* sleep(5); */ /* } */ /* ソケットクローズ */ (void) close(soc); return (EX_OK); }
int main(int argc, char *argv[]){ int soc; if(argc <= 1){ fprintf(stderr, "server port\n"); return(EX_USAGE); } if(argc >= 3 && argv[2][0] == 'n'){ fprintf(stderr, "Nonblocking mode\n"); g_mode = 'n'; } else { g_mode = 'b'; } if((soc = server_socket(argv[1])) == -1){ fprintf(stderr, "server_socket(%s):error\n", argv[1]); return(EX_UNAVAILABLE); } fprintf(stderr, "ready for accept\n"); accept_loop(soc); close(soc); return(EX_OK); }
int main(int argc, char *argv[]) { int server, port = PORT, opt_idx = 0, c; static struct option long_options[] = { { "debug", no_argument, &debug, 1 }, { "port", required_argument, 0, 'p' } }; while ((c = getopt_long(argc, argv, "p:", long_options, &opt_idx)) != -1) { switch (c) { case 'p': port = atoi(optarg); break; case '?': break; } } if ((server = create_server(port)) == -1) { return -1; } if (!debug) { daemon(0, 0); } accept_loop(server, handle_client); return 0; }
static int server(int offset) { struct sockaddr_in saddr_in; struct sockaddr addr; unsigned int len; int sk, opt; bind_to_cpu(offset); if (nice(-20) < 0) perror("nice"); sk = socket(PF_INET, SOCK_STREAM, 0); if (sk < 0) return error("socket"); opt = 1; if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) return error("setsockopt"); saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); saddr_in.sin_port = htons(net_port + offset); if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) return error("bind"); if (listen(sk, 1) < 0) return error("listen"); len = sizeof(addr); if (getsockname(sk, &addr, &len) < 0) return error("getsockname"); return accept_loop(sk); }
void serve(int sockfd) { if (listen(sockfd, 0) == -1) { perror("Cannot listen"); exit(1); } accept_loop(sockfd); }
int start_server() { int soc; if ((soc = server_socket(PORT)) == -1) { (void) fprintf(stderr, "server_socket(%s):error\n", PORT); return (EX_UNAVAILABLE); } (void) fprintf(stderr, "ready for accept\n"); accept_loop(soc); (void) close(soc); return 0; }
/* and in here we also loop for exosip events*/ void acceptor_process(dp_config *cfg) { int i,k; unsigned int sock; LOG(L_INFO,"INFO:Acceptor process starting up...\n"); listening_socks = pkg_malloc((cfg->acceptors_cnt+1)*sizeof(int)); if (!listening_socks){ LOG_NO_MEM("pkg",(cfg->acceptors_cnt+1)*sizeof(int)); goto done; } memset(listening_socks,0,(cfg->acceptors_cnt+1)*sizeof(int)); k=0; for(i=0;i<cfg->acceptors_cnt;i++) if (create_socket(cfg->acceptors[i].port,cfg->acceptors[i].bind,&sock)){ listening_socks[k++]=sock; } LOG(L_INFO,"INFO:... Acceptor opened sockets. Entering accept loop ...\n"); accept_loop(); for(i=0;listening_socks[i];i++) close(listening_socks[i]); if (listening_socks) pkg_free(listening_socks); #ifdef CDP_FOR_SER #else #ifdef PKG_MALLOC #ifdef PKG_MALLOC LOG(memlog, "Acceptor Memory status (pkg):\n"); //pkg_status(); #ifdef pkg_sums pkg_sums(); #endif #endif #endif dp_del_pid(getpid()); #endif done: LOG(L_INFO,"INFO:... Acceptor process finished\n"); exit(0); }
int main(int argc, char *argv[]){ int soc; if(argc <= 2){ fprintf(stderr, "server1 address port\n"); return(EX_USAGE); } if((soc = server_socket_by_hostname(argv[1], argv[2])) == -1){ fprintf(stderr, "server_socket(%s, %s):error\n", argv[1], argv[2]); return(EX_UNAVAILABLE); } fprintf(stderr, "ready for accept\n"); accept_loop(soc); close(soc); return(EX_OK); }
int main(int argc, char *argv[], char *envp[]) { struct sigaction sa; int soc; /* 引数にポート番号が指定されているか? */ if (argc <= 1) { (void) fprintf(stderr,"re-exec port\n"); return (EX_USAGE); } if (argc == 3) { (void) fprintf(stderr,"skip daemonize\n"); } else { /* デーモン化 */ (void) daemonize(0, 0); } /* コマンドライン引数、環境変数のアドレスをグローバルに保持 */ argc_ = &argc; argv_ = &argv; envp_ = &envp; /* SIGHUPのシグナルハンドラを指定 */ (void) sigaction(SIGHUP, (struct sigaction *) NULL, &sa); sa.sa_handler = sig_hangup_handler; sa.sa_flags = SA_NODEFER; (void) sigaction(SIGHUP, &sa, (struct sigaction *) NULL); (void) fprintf(stderr, "sigaction():end\n"); /* サーバソケットの準備 */ if ((soc = server_socket(argv[1])) == -1) { (void) fprintf(stderr,"server_socket(%s):error\n", argv[1]); return (EX_UNAVAILABLE); } (void) fprintf(stderr, "ready for accept\n"); /* アクセプトループ */ accept_loop(soc); /* ソケットクローズ */ (void) close(soc); return (EX_OK); }
int main(int argc, char **argv) { int ret; init_vars(); init_args(argc, argv); ret = init_coroutine_env(); if (ret != 0) { fprintf(stderr, "init async_coro failed\n"); exit(-1); } if (accept_loop() != RET_SUCCESS) { fprintf(stderr, "accept_loop() failed\n"); exit(-1); } return 0; }
int main(int argc, char ** argv) { char *server, *service; struct addrinfo *addr_info, *p, hints; int ret, i; int sockets[128]; int numsockets = 0; setProgName(argv[0]); /* For our infoPrint/errorPrint functions */ if (argc <= 1) { errorPrint("Simple Echo-Server"); errorPrint("Usage: %s [BINDADDR] [SERVICE]", argv[0]); errorPrint("Example: %s localhost 8080", argv[0]); exit(1); } if (argc == 3) { server = argv[1]; service = argv[2]; } else { server = NULL; service = argv[1]; } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE | AI_V4MAPPED; /* RTFM: getaddrinfo */ ret = getaddrinfo(server, service, &hints, &addr_info); if (ret) { errorPrint("Error in getaddrinfo: %s", gai_strerror(ret)); exit(1); } p = addr_info; while (p) { int s; char dst[INET6_ADDRSTRLEN]; char service[INET6_ADDRSTRLEN]; int on = 1; /* Create socket for found family */ s = socket(p->ai_family, p->ai_socktype, 0); if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) { errorPrint("Error in setsockopt: %s", strerror(errno)); /* maybe not so fatal, continue */ } if (p->ai_family == AF_INET6) { if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) { errorPrint("Error in setsockopt: %s", strerror(errno)); } } /* RTFM: getnameinfo */ getnameinfo(p->ai_addr, p->ai_addrlen, dst, sizeof(dst), service, sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV); infoPrint("Trying %s:%s ... ",dst, service); /* Try to bind socket */ if (bind(s, p->ai_addr, p->ai_addrlen) == 0) { if (listen(s, 1) < 0) { errorPrint("listen failed: %s", strerror(errno)); close(s); } else { infoPrint("bind successful"); sockets[numsockets++] = s; } } else { errorPrint("bind failed: %s", strerror(errno)); close(s); } p = p->ai_next; } freeaddrinfo(addr_info); infoPrint("Waiting for connections..."); accept_loop(sockets, numsockets); for (i=0; i<numsockets; i++) { close(sockets[i]); } return 0; }
bool w_start_listener(const char *path) { pthread_mutexattr_t mattr; #ifndef _WIN32 struct sigaction sa; sigset_t sigset; #endif void *ignored; #ifdef HAVE_LIBGIMLI_H volatile struct gimli_heartbeat *hb = NULL; #endif struct timeval tv; int n_clients = 0; listener_thread = pthread_self(); pthread_mutexattr_init(&mattr); pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&w_client_lock, &mattr); pthread_mutexattr_destroy(&mattr); #ifdef HAVE_LIBGIMLI_H hb = gimli_heartbeat_attach(); #endif #if defined(HAVE_KQUEUE) || defined(HAVE_FSEVENTS) { struct rlimit limit; # ifndef __OpenBSD__ int mib[2] = { CTL_KERN, # ifdef KERN_MAXFILESPERPROC KERN_MAXFILESPERPROC # else KERN_MAXFILES # endif }; # endif int maxperproc; getrlimit(RLIMIT_NOFILE, &limit); # ifndef __OpenBSD__ size_t len; len = sizeof(maxperproc); sysctl(mib, 2, &maxperproc, &len, NULL, 0); w_log(W_LOG_ERR, "file limit is %" PRIu64 " kern.maxfilesperproc=%i\n", limit.rlim_cur, maxperproc); # else maxperproc = limit.rlim_max; w_log(W_LOG_ERR, "openfiles-cur is %" PRIu64 " openfiles-max=%i\n", limit.rlim_cur, maxperproc); # endif if (limit.rlim_cur != RLIM_INFINITY && maxperproc > 0 && limit.rlim_cur < (rlim_t)maxperproc) { limit.rlim_cur = maxperproc; if (setrlimit(RLIMIT_NOFILE, &limit)) { w_log(W_LOG_ERR, "failed to raise limit to %" PRIu64 " (%s).\n", limit.rlim_cur, strerror(errno)); } else { w_log(W_LOG_ERR, "raised file limit to %" PRIu64 "\n", limit.rlim_cur); } } getrlimit(RLIMIT_NOFILE, &limit); #ifndef HAVE_FSEVENTS if (limit.rlim_cur < 10240) { w_log(W_LOG_ERR, "Your file descriptor limit is very low (%" PRIu64 "), " "please consult the watchman docs on raising the limits\n", limit.rlim_cur); } #endif } #endif proc_pid = (int)getpid(); if (gettimeofday(&tv, NULL) == -1) { w_log(W_LOG_ERR, "gettimeofday failed: %s\n", strerror(errno)); return false; } proc_start_time = (uint64_t)tv.tv_sec; #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); /* allow SIGUSR1 and SIGCHLD to wake up a blocked thread, without restarting * syscalls */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = wakeme; sa.sa_flags = 0; sigaction(SIGUSR1, &sa, NULL); sigaction(SIGCHLD, &sa, NULL); // Block SIGCHLD everywhere sigemptyset(&sigset); sigaddset(&sigset, SIGCHLD); sigprocmask(SIG_BLOCK, &sigset, NULL); listener_fd = get_listener_socket(path); if (listener_fd == -1) { return false; } w_set_cloexec(listener_fd); #endif if (pthread_create(&reaper_thread, NULL, child_reaper, NULL)) { w_log(W_LOG_FATAL, "pthread_create(reaper): %s\n", strerror(errno)); return false; } if (!clients) { clients = w_ht_new(2, &client_hash_funcs); } w_state_load(); #ifdef HAVE_LIBGIMLI_H if (hb) { gimli_heartbeat_set(hb, GIMLI_HB_RUNNING); } else { w_setup_signal_handlers(); } #else w_setup_signal_handlers(); #endif w_set_nonblock(listener_fd); // Now run the dispatch #ifndef _WIN32 accept_loop(); #else named_pipe_accept_loop(path); #endif #ifndef _WIN32 /* close out some resources to persuade valgrind to run clean */ close(listener_fd); listener_fd = -1; #endif // Wait for clients, waking any sleeping clients up in the process do { w_ht_iter_t iter; pthread_mutex_lock(&w_client_lock); n_clients = w_ht_size(clients); if (w_ht_first(clients, &iter)) do { struct watchman_client *client = w_ht_val_ptr(iter.value); w_event_set(client->ping); } while (w_ht_next(clients, &iter)); pthread_mutex_unlock(&w_client_lock); w_log(W_LOG_ERR, "waiting for %d clients to terminate\n", n_clients); usleep(2000); } while (n_clients > 0); w_root_free_watched_roots(); pthread_join(reaper_thread, &ignored); cfg_shutdown(); return true; }
void main(void) { svr_fd = SetupSocket(); accept_loop(svr_fd); }
bool w_start_listener(const char *path) { #ifndef _WIN32 struct sigaction sa; sigset_t sigset; #endif void *ignored; listener_thread = pthread_self(); #ifdef HAVE_LIBGIMLI_H hb = gimli_heartbeat_attach(); #endif #if defined(HAVE_KQUEUE) || defined(HAVE_FSEVENTS) { struct rlimit limit; # ifndef __OpenBSD__ int mib[2] = { CTL_KERN, # ifdef KERN_MAXFILESPERPROC KERN_MAXFILESPERPROC # else KERN_MAXFILES # endif }; # endif int maxperproc; getrlimit(RLIMIT_NOFILE, &limit); # ifndef __OpenBSD__ { size_t len; len = sizeof(maxperproc); sysctl(mib, 2, &maxperproc, &len, NULL, 0); w_log(W_LOG_ERR, "file limit is %" PRIu64 " kern.maxfilesperproc=%i\n", limit.rlim_cur, maxperproc); } # else maxperproc = limit.rlim_max; w_log(W_LOG_ERR, "openfiles-cur is %" PRIu64 " openfiles-max=%i\n", limit.rlim_cur, maxperproc); # endif if (limit.rlim_cur != RLIM_INFINITY && maxperproc > 0 && limit.rlim_cur < (rlim_t)maxperproc) { limit.rlim_cur = maxperproc; if (setrlimit(RLIMIT_NOFILE, &limit)) { w_log(W_LOG_ERR, "failed to raise limit to %" PRIu64 " (%s).\n", limit.rlim_cur, strerror(errno)); } else { w_log(W_LOG_ERR, "raised file limit to %" PRIu64 "\n", limit.rlim_cur); } } getrlimit(RLIMIT_NOFILE, &limit); #ifndef HAVE_FSEVENTS if (limit.rlim_cur < 10240) { w_log(W_LOG_ERR, "Your file descriptor limit is very low (%" PRIu64 "), " "please consult the watchman docs on raising the limits\n", limit.rlim_cur); } #endif } #endif #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); /* allow SIGUSR1 and SIGCHLD to wake up a blocked thread, without restarting * syscalls */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = wakeme; sa.sa_flags = 0; sigaction(SIGUSR1, &sa, NULL); sigaction(SIGCHLD, &sa, NULL); // Block SIGCHLD everywhere sigemptyset(&sigset); sigaddset(&sigset, SIGCHLD); sigprocmask(SIG_BLOCK, &sigset, NULL); listener_fd = get_listener_socket(path); if (listener_fd == -1) { return false; } w_set_cloexec(listener_fd); #endif if (!clients) { clients = w_ht_new(2, NULL); } #ifdef HAVE_LIBGIMLI_H if (hb) { gimli_heartbeat_set(hb, GIMLI_HB_RUNNING); } else { w_setup_signal_handlers(); } #else w_setup_signal_handlers(); #endif w_set_nonblock(listener_fd); // Now run the dispatch #ifndef _WIN32 accept_loop(); #else named_pipe_accept_loop(path); #endif #ifndef _WIN32 /* close out some resources to persuade valgrind to run clean */ close(listener_fd); listener_fd = -1; #endif // Wait for clients, waking any sleeping clients up in the process { int interval = 2000; int last_count = 0, n_clients = 0; const int max_interval = 1000000; // 1 second do { w_ht_iter_t iter; pthread_mutex_lock(&w_client_lock); n_clients = w_ht_size(clients); if (w_ht_first(clients, &iter)) do { struct watchman_client *client = w_ht_val_ptr(iter.value); w_event_set(client->ping); #ifndef _WIN32 // If we've been waiting around for a while, interrupt // the client thread; it may be blocked on a write if (interval >= max_interval) { pthread_kill(client->thread_handle, SIGUSR1); } #endif } while (w_ht_next(clients, &iter)); pthread_mutex_unlock(&w_client_lock); if (n_clients != last_count) { w_log(W_LOG_ERR, "waiting for %d clients to terminate\n", n_clients); } usleep(interval); interval = MIN(interval * 2, max_interval); } while (n_clients > 0); } pthread_join(reaper_thread, &ignored); cfg_shutdown(); return true; }
int main (int argc, char ** argv) { int sockFd = SetupSock(65535, AF_INET, "eth0"); signal(SIGCHLD, SIG_IGN); accept_loop(sockFd, ff); }