int main(int argc, char * * argv) { #ifdef USE_CHECK_ALLOC check_set_file(stderr); #endif eventlog_set(stderr); if (config_init(argc, argv)<0) { return 1; } log_info(D2CS_VERSION); if (init()<0) { log_error("failed to init"); return 1; } else { log_info("server initialized"); } if (server_process()<0) { log_error("failed to run server"); return 1; } cleanup(); config_cleanup(); #ifdef USE_CHECK_ALLOC check_cleanup(); if (memlog_fp) fclose(memlog_fp); #endif return 0; }
static int run_server (int argc, char *argv[]) { int ret; initialize_subsystems(); fserve_initialize(); ret = server_init (argc, argv); if (ret == 0) server_process(); shutdown_subsystems(); return ret; }
int main (int argc, char *argv[]) { initialize_subsystems(); if (server_init (argc, argv) == 0) server_process(); shutdown_subsystems(); if (pidfile) { remove (pidfile); free (pidfile); } return 0; }
void server_run() { int i; int last_max_pollfd = 0; struct pollfd *my_pollfd = NULL; for( ; ; ) { int max_pollfd = server_max_pollfd; if(max_pollfd == 0 && server_pollfd == NULL) { break; } if(last_max_pollfd != max_pollfd) { struct pollfd *new_pollfd = realloc(my_pollfd, sizeof(struct pollfd) * max_pollfd); if(new_pollfd == NULL) { fprintf(stderr, "server_run : out of memory\n"); break; } my_pollfd = new_pollfd; last_max_pollfd = max_pollfd; } for(i = 0; i < max_pollfd; ++i) { my_pollfd[i].fd = server_pollfd[i].fd; my_pollfd[i].events = server_pollfd[i].events; my_pollfd[i].revents = 0; } switch(i = poll(my_pollfd, max_pollfd, -1)) { case -1: if(errno = EINTR) { continue; } fprintf(stderr, "server_run : poll failed\n"); break; case 0: continue; default: server_process(my_pollfd, i); continue; } free(my_pollfd); } }
void server_loop(int remotefd, int localfd) { fd_set set; int max; while (1) { struct timeval tv = {60, 0}; FD_ZERO(&set); FD_SET(remotefd, &set); FD_SET(localfd, &set); max = remotefd > localfd ? remotefd : localfd; if (connfd != -1) { FD_SET(connfd, &set); if (connfd > max) max = connfd; } max = select(max + 1, &set, NULL, NULL, &tv); if (max > 0) server_process(max, &set, remotefd, localfd); } }
//***************************************************************************************** // // Function : main // Description : main program, // //***************************************************************************************** int main (void) { // change your mac address here avr_mac.byte[0] = 'A'; avr_mac.byte[1] = 'V'; avr_mac.byte[2] = 'R'; avr_mac.byte[3] = 'P'; avr_mac.byte[4] = 'O'; avr_mac.byte[5] = 'R'; // read avr and server ip from eeprom eeprom_read_block ( &avr_ip, ee_avr_ip, 4 ); eeprom_read_block ( &server_ip, ee_server_ip, 4 ); // setup port as input and enable pull-up SW_DDR &= ~ ( _BV( SW_MENU ) | _BV( SW_EXIT ) | _BV( SW_UP ) | _BV( SW_DW ) ); SW_PORT |= _BV( SW_MENU ) | _BV( SW_EXIT ) | _BV( SW_UP ) | _BV( SW_DW ); SFIOR &= ~_BV( PUD ); // setup lcd backlight as output LCD_BL_DDR |= _BV( LCD_BL_PIN ); // lcd backlight on LCD_BL_PORT |= _BV( LCD_BL_PIN ); // setup clock for timer1 TCCR1B = 0x01; // clk/1 no prescaling // initial adc, lcd, and menu adc_init(); lcd_init (); menu_init (); // set LED1, LED2 as output */ LED_DDR |= _BV( LED_PIN1_DDR ) | _BV( LED_PIN2_DDR ); // set LED pin to "1" ( LED1,LED2 off) LED_PORT |= _BV( LED_PIN1 ) | _BV( LED_PIN2 ); // initial enc28j60 enc28j60_init( (BYTE*)&avr_mac ); // loop forever for(;;) { // wait until timer1 overflow while ( (TIFR & _BV ( TOV1 )) == 0 ); TIFR |= _BV(TOV1); TCNT1 = 1536; // Timer1 overflow every 1/16MHz * ( 65536 - 1536 ) = 4ms, 250Hz // general time base, generate by timer1 // overflow every 1/250 seconds time_base (); // read temparature adc_read_temp(); // server process response for arp, icmp, http server_process (); // send temparature to web server unsing http protocol // disable by default. client_process (); // lcd user interface menu // setup IP address, countdown timer menu_process (); // display AVR ethernet status // temparature, AVR ip, server ip, countdown time standby_display (); } return 0; }
int main(int argc, const char* argv[]) { pid_t server_child = 0; int fd; int res = 0, status; struct sigaction sigchildAction; tio_param sParam [] = { {"D:", "DURATION", "Duration"}, { "m:", "PORTSPEED", "Prot speed"}, { "s:", "SENDPACKSLENGTH", "Send pack length"}, { "d", "SERVERMODE", "Server mode" }, { "l", "CLIENTMODE", "Client mode" }, { "L", "CLIENTSERVERMODE", "Client/Server mode" }, {NULL, NULL, NULL} }; sigchildAction.sa_handler = termination_signal; sigchildAction.sa_flags = SA_NOCLDSTOP; sigemptyset(&(sigchildAction.sa_mask)); sigaddset(&(sigchildAction.sa_mask),SIGTERM); if (sigaction(SIGTERM, &sigchildAction, NULL)) { perror("Signal SIGTERM registration failed"); return -1; } if (sigaction(SIGINT, &sigchildAction, NULL)) { perror("Signal SIGINT registration failed"); return -1; } // Инициализация tio и разбор входных параметров командной строки tioInit( "alpha", "RS232 test", sParam, argc, argv); if (write_configuration(&config)) { fputs("Congiguration read error\n", stderr); return -1; } fd = open_serial_port( tio_argv[0], tioGetDefL( "PORTSPEED", 115200 )); if (fd < 0) { return -1; } config.outputDevice = fd; if ( tioGetL( "CLIENTSERVERMODE" ) > 0 ) { server_child = fork(); } if ((server_child == 0) && (tioGetL( "CLIENTSERVERMODE" ) || tioGetL( "SERVERMODE" ) ) ) { if (server_process(&config)) { return -1; } } else if ( tioGetL( "CLIENTSERVERMODE" ) || tioGetL( "CLIENTMODE" )) res = client_process(&config); else { DEBUGMSG("Undefined target action"); return -1; } if (server_child != 0) waitpid(server_child, &status, WNOHANG); // Завершение работы библиотеки tio tioFinish(0); return (int)(res || status); /*return 0;*/ }
/** * main entry point of the application. * * parses command line arguments, then sets up IPC, and then spawns worker * processes to accept and service new connections until application * termination. * * @function main * * @date 2016-02-14 * * @revision none * * @designer Eric Tsang * * @programmer Eric Tsang * * @note none * * @signature int main (int argc, char* argv[]) * * @param argc number of command line arguments. * @param argv array of c-style strings. * * @return exit code of the application. */ int main (int argc, char* argv[]) { // file descriptor to a server socket int serverSocket; // port for server socket to listen on int listeningPort; // number of worker process to create to server connections int numWorkerProcesses; // parse command line arguments { char option; int portInitialized = false; int numWorkerProcessesInitialized = false; while ((option = getopt(argc,argv,"p:n:")) != -1) { switch (option) { case 'p': { char* parsedCursor = optarg; listeningPort = (int) strtol(optarg,&parsedCursor,10); if (parsedCursor == optarg) { fprintf(stderr,"invalid argument for option -%c\n",option); } else { portInitialized = true; } break; } case 'n': { char* parsedCursor = optarg; numWorkerProcesses = (int) strtol(optarg,&parsedCursor,10); if (parsedCursor == optarg) { fprintf(stderr,"invalid argument for option -%c\n",option); } else { numWorkerProcessesInitialized = true; } break; } case '?': { if (isprint(optopt)) { fprintf(stderr,"unknown option \"-%c\".\n",optopt); } else { fprintf(stderr,"unknown option character \"%x\".\n",optopt); } } default: { fatal_error(""); } } } // print usage and abort if not all required arguments were provided if (!portInitialized && !numWorkerProcessesInitialized) { fprintf(stderr,"usage: %s [-p server listening port] [-n number of worker processes]\n",argv[0]); return EX_USAGE; } } // create server socket serverSocket = make_tcp_server_socket(listeningPort,true).fd; if (serverSocket == -1) { fatal_error("socket"); } // start the worker processes for(register int i = 0; i < numWorkerProcesses; ++i) { // if this is worker process, run worker process code if (fork() == 0) { return child_process(serverSocket); } } return server_process(numWorkerProcesses); }
extern int main(int argc, char * * argv) #endif { int a; char *pidfile; xalloc_install(); if ((a = cmdline_load(argc, argv)) != 1) return a; #ifdef DO_DAEMONIZE if ((a = fork_bnetd(cmdline_get_foreground())) != 0) return a < 0 ? a : 0; /* dont return code != 0 when things are OK! */ #endif eventlog_set(stderr); /* errors to eventlog from here on... */ if (prefs_load(cmdline_get_preffile())<0) { eventlog(eventlog_level_fatal,__FUNCTION__,"could not parse configuration file (exiting)"); return -1; } /* Start logging to log file */ if (eventlog_startup() == -1) return -1; /* eventlog goes to log file from here on... */ /* Give up root privileges */ /* Hakan: That's way too late to give up root privileges... Have to look for a better place */ if (give_up_root_privileges(prefs_get_effective_user(),prefs_get_effective_group())<0) { eventlog(eventlog_level_fatal,__FUNCTION__,"could not give up privileges (exiting)"); return -1; } /* Write the pidfile */ pidfile = write_to_pidfile(); if (cmdline_get_hexfile()) { if (!(hexstrm = fopen(cmdline_get_hexfile(),"w"))) eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing the hexdump (fopen: %s)",cmdline_get_hexfile(),pstrerror(errno)); else fprintf(hexstrm,"# dump generated by "PVPGN_SOFTWARE" version "PVPGN_VERSION"\n"); } /* Run the pre server stuff */ a = pre_server_startup(); /* now process connections and network traffic */ if (a == 0) { if (server_process() < 0) eventlog(eventlog_level_fatal,__FUNCTION__,"failed to initialize network (exiting)"); } // run post server stuff and exit post_server_shutdown(a); // Close hexfile if (hexstrm) { fprintf(hexstrm,"# end of dump\n"); if (fclose(hexstrm)<0) eventlog(eventlog_level_error,__FUNCTION__,"could not close hexdump file \"%s\" after writing (fclose: %s)",cmdline_get_hexfile(),pstrerror(errno)); } // Delete pidfile if (pidfile) { if (remove(pidfile)<0) eventlog(eventlog_level_error,__FUNCTION__,"could not remove pid file \"%s\" (remove: %s)",pidfile,pstrerror(errno)); xfree((void *)pidfile); /* avoid warning */ } if (a == 0) eventlog(eventlog_level_info,__FUNCTION__,"server has shut down"); prefs_unload(); eventlog_close(); cmdline_unload(); xalloc_deinstall(); if (a == 0) return 0; return -1; }
extern int main(int argc, char * argv[]) { int sockfd; if (argc<1 || !argv || !argv[0]) { fprintf(stderr,"bad arguments\n"); return STATUS_FAILURE; } getprefs(argc,argv); if (!prefs.debug) eventlog_del_level("debug"); if (prefs.logfile) { eventlog_set(stderr); if (eventlog_open(prefs.logfile)<0) { eventlog(eventlog_level_fatal,__FUNCTION__,"could not use file \"%s\" for the eventlog (exiting)",prefs.logfile); return STATUS_FAILURE; } } #ifdef DO_DAEMONIZE if (!prefs.foreground) { switch (fork()) { case -1: eventlog(eventlog_level_error,__FUNCTION__,"could not fork (fork: %s)\n",pstrerror(errno)); return STATUS_FAILURE; case 0: /* child */ break; default: /* parent */ return STATUS_SUCCESS; } close(STDINFD); close(STDOUTFD); close(STDERRFD); # ifdef HAVE_SETPGID if (setpgid(0,0)<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgid: %s)\n",pstrerror(errno)); return STATUS_FAILURE; } # else # ifdef HAVE_SETPGRP # ifdef SETPGRP_VOID if (setpgrp()<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgrp: %s)\n",pstrerror(errno)); return STATUS_FAILURE; } # else if (setpgrp(0,0)<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgrp: %s)\n",pstrerror(errno)); return STATUS_FAILURE; } # endif # else # ifdef HAVE_SETSID if (setsid()<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setsid: %s)\n",pstrerror(errno)); return STATUS_FAILURE; } # else # error "One of setpgid(), setpgrp(), or setsid() is required" # endif # endif # endif } #endif if (prefs.pidfile) { #ifdef HAVE_GETPID FILE * fp; if (!(fp = fopen(prefs.pidfile,"w"))) { eventlog(eventlog_level_error,__FUNCTION__,"unable to open pid file \"%s\" for writing (fopen: %s)",prefs.pidfile,pstrerror(errno)); prefs.pidfile = NULL; } else { fprintf(fp,"%u",(unsigned int)getpid()); if (fclose(fp)<0) eventlog(eventlog_level_error,__FUNCTION__,"could not close pid file \"%s\" after writing (fclose: %s)",prefs.pidfile,pstrerror(errno)); } #else eventlog(eventlog_level_warn,__FUNCTION__,"no getpid() system call, do not use the -P or the --pidfile option"); prefs.pidfile = NULL; #endif } #ifdef HAVE_GETPID eventlog(eventlog_level_info,__FUNCTION__,"bntrackd version "PVPGN_VERSION" process %u",(unsigned int)getpid()); #else eventlog(eventlog_level_info,__FUNCTION__,"bntrackd version "PVPGN_VERSION); #endif if (psock_init()<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not initialize socket functions"); return STATUS_FAILURE; } /* create the socket */ if ((sockfd = psock_socket(PSOCK_PF_INET,PSOCK_SOCK_DGRAM,PSOCK_IPPROTO_UDP))<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not create UDP listen socket (psock_socket: %s)\n",pstrerror(psock_errno())); return STATUS_FAILURE; } { struct sockaddr_in servaddr; /* bind the socket to correct port and interface */ memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = PSOCK_AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(prefs.port); if (psock_bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0) { eventlog(eventlog_level_error,__FUNCTION__,"could not bind to UDP port %hu (psock_bind: %s)\n",prefs.port,pstrerror(psock_errno())); return STATUS_FAILURE; } } if (server_process(sockfd)<0) return STATUS_FAILURE; return STATUS_SUCCESS; }
// ---------------------------------------- server_start ------------------------------------------- void server_start() { int master_sockfd, current_sockfd; socklen_t client_len; struct sockaddr_in serv_addr, client_addr; char buffer[256]; int nread; // Create master socket: if ((master_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("Server: Cannot create master socket."); exit(-1); } // create socket structure bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); // bind the socket to the local port if (bind(master_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("Server: Error on binding"); exit(1); } // listen listen(master_sockfd, 5); client_len = sizeof(client_addr); printf("Server listening on port %d\n", portno); // master loop while(TRUE) { // block until a client connects if ((current_sockfd = accept(master_sockfd, (struct sockaddr *) &client_addr, &client_len)) < 0) { perror("Server: Error on accept()"); exit(1); } nread = read(current_sockfd, buffer, 255); buffer[nread] = '\0'; if (nread > 0) { // Quit command received? if (buffer[0] == 'q') { write(current_sockfd, QUITTING, sizeof(QUITTING)); close(current_sockfd); close(master_sockfd); printf("Server quitting...\n"); exit(0); } // myui4 key received? if (buffer[0] == 'u') { fd_write = current_sockfd; server_process(current_sockfd, buffer, nread); } else { // unrecognized command write(current_sockfd, HUH, sizeof(HUH)); close(current_sockfd); printf("Server: unrecognized command received\n"); } } else { // zero length message? write(current_sockfd, EMPTY_MSG, sizeof(EMPTY_MSG)); close(current_sockfd); printf("Server: empty message received\n"); } } }
void server_loop(fd_type remotefd, local_fd_type localfd) { fd_set set; int max; vector_t v; vector_functor_t f = { vector_dummy_dup, NULL }; this.remotefd = remotefd; this.localfd = localfd; vector_init(&v, f); while (1) { struct timeval tv = {0, 1}; active_vector_iterator_t iter; FD_ZERO(&set); FD_SET(remotefd, &set); #ifdef WIN32 max = remotefd; #else FD_SET(localfd, &set); max = remotefd > localfd ? remotefd : localfd; #endif if (!this.use_udp) { iter = active_vector_begin(&this.clients); while (!active_vector_is_end(iter)) { int fd = ((client_t*)iter.data)->fd; FD_SET(fd, &set); if (fd > max) max = fd; iter = active_vector_next(iter); } } max = select(max + 1, &set, NULL, NULL, &tv); server_process(max, &set); iter = active_vector_begin(&this.clients); while (!active_vector_is_end(iter)) { client_t* client = (client_t*)iter.data; if (IS_CLIENT_STATUS_CHECKLOGIN(client->status)) { if ((time(NULL) - client->keepalive) > LOGIN_TIMEOUT) vector_push_back(&v, (void*)(long)active_vector_iterator_idx(iter), sizeof(active_vector_iterator_idx(iter))); } else if (IS_CLIENT_STATUS_NORMAL(client->status)) { if ((time(NULL) - ((client_t*)iter.data)->keepalive) > KEEPALIVE_LIMIT) vector_push_back(&v, (void*)(long)active_vector_iterator_idx(iter), sizeof(active_vector_iterator_idx(iter))); } iter = active_vector_next(iter); } remove_clients(&v, "login or keepalive timeouted"); } vector_free(&v); }