/** *@brief 处理客户端请求包 *@return 0 数据包不完整,需要继续读取 1 数据包完整,处理完数据包 */ static int try_read_cli(conn *c) { gettimeofday(&c->timestamp, NULL); // DEBUG_LOGGER(dsmplog, "start to read data on socket[%d]", c->sfd); char *el, *cont; assert(c != NULL); assert(c->rcurr <= (c->rbuf + c->rsize)); if (c->rbytes == 0) return 0; //el = memchr(c->rcurr, '\n', c->rbytes); //if (!el) // return 0; //cont = el + 1; //if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { // el--; //} //*el = '\0'; //assert(cont <= (c->rcurr + c->rbytes)); process_server(c, c->rbuf); //c->rbytes -= (cont - c->rcurr); c->rbytes = 0; //c->rcurr = cont; //assert(c->rcurr <= (c->rbuf + c->rsize)); return 1; }
int main(int argc, char** argv) { try { std::string config_file( (argc>1)?argv[1]:DEFAULT_CONFIG_FILE ); dictionary * ini = NULL; if ( (ini=iniparser_load(config_file.c_str())) == NULL ) { Log::crit("cannot parse the config file: %s\n", config_file.c_str()); throw GenericError("cannot parse the config file %s", config_file.c_str()); } int use_ipc = iniparser_getboolean(ini, "INTERMEDIATE_BROKER:use_ipc", -1); if(use_ipc) { Log::notice("No se lanzan los message_broker, use_ipc=true en archivo de configuracion %s", config_file.c_str()); return 0; } Log::notice("Launch message_broker_server, read configuration from %s", config_file.c_str()); strcpy(working_dir, iniparser_getstring(ini, "INTERMEDIATE_BROKER:working_dir", NULL)); strcpy(port, iniparser_getstring(ini, "INTERMEDIATE_BROKER:port", NULL)); strcpy(log_path, iniparser_getstring(ini, "INTERMEDIATE_BROKER:log_path", NULL)); strcpy(persist_path, iniparser_getstring(ini, "INTERMEDIATE_BROKER:persist_path", NULL)); Process process_server("./../local_broker/message_broker_server", args_message_broker); sleep(1); ApiComunicacionAeropuerto api_comm( ApiConfiguracion::get_wkdir(config_file.c_str()).c_str(), config_file.c_str(), true); Log::notice("Done, waiting for a SIGINT or a SIGTERM signal."); wait_signal(); Log::notice("Signal recieved. Shutdown..."); process_server.send_signal(SIGTERM); process_server.send_signal(SIGKILL); } catch (const std::exception &e) { Log::crit("%s", e.what()); } catch (...) { Log::crit("Critical error. Unknow exception at the end of the 'main' function."); } return 0; }
int message_demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp) { extern int process_server (mach_msg_header_t *, mach_msg_header_t *); extern int notify_server (mach_msg_header_t *, mach_msg_header_t *); extern int proc_exc_server (mach_msg_header_t *, mach_msg_header_t *); int status; mutex_lock (&global_lock); status = (process_server (inp, outp) || notify_server (inp, outp) || ports_interrupt_server (inp, outp) || proc_exc_server (inp, outp)); mutex_unlock (&global_lock); return status; }
int main (int argc, char *argv []) { int argn; /* Argument number */ Bool args_ok = TRUE, /* Were the arguments okay? */ quiet_mode = FALSE, /* -q means suppress messages */ background = FALSE; /* -s means run in background */ char *workdir, /* Working directory */ *port, /* Value for listen port */ **argparm; /* Argument parameter to pick-up */ /* First off, switch to user's id */ set_uid_user (); /* These are the arguments we may get on the command line */ workdir = NULL; port = NULL; argparm = NULL; /* Argument parameter to pick-up */ for (argn = 1; argn < argc; argn++) { /* If argparm is set, we have to collect an argument parameter */ if (argparm) { if (*argv [argn] != '-') /* Parameter can't start with '-' */ { *argparm = strdupl (argv [argn]); argparm = NULL; } else { args_ok = FALSE; break; } } else if (*argv [argn] == '-') { switch (argv [argn][1]) { /* These switches take a parameter */ case 'w': argparm = &workdir; break; case 'p': argparm = &port; break; /* These switches have an immediate effect */ case 'q': quiet_mode = TRUE; break; case 's': background = TRUE; break; case 'S': background = FALSE; break; case 't': smtsock_trace (TRUE); break; case 'v': coprintf ("Sysman %s", SYSMAN_VERSION); coprintf (PRODUCT); coprintf (BUILDMODEL); coprintf (COPYRIGHT); coprintf ("Built on: %s", BUILDDATE); exit (EXIT_SUCCESS); case 'h': coprintf ("Sysman %s", SYSMAN_VERSION); coprintf (COPYRIGHT); coprintf (USAGE); exit (EXIT_SUCCESS); /* Anything else is an error */ default: args_ok = FALSE; } } else { args_ok = FALSE; break; } } /* If there was a missing parameter or an argument error, quit */ if (argparm) { puts ("Argument missing - type 'sysman -h' for help"); exit (EXIT_FAILURE); } else if (!args_ok) { puts ("Invalid arguments - type 'sysman -h' for help"); exit (EXIT_FAILURE); } /* Set server working directory if necessary */ if (workdir && set_curdir (workdir)) { printf ("Can't work in '%s' - %s\n", workdir, strerror (errno)); exit (EXIT_FAILURE); } /* Handle the remaining arguments we got */ if (!port) port = SYSMAN_DEFAULT_PORT; if (quiet_mode) { fclose (stdout); /* Kill standard output */ fclose (stderr); /* and standard error */ } else { puts ("Sysman " SYSMAN_VERSION); puts (COPYRIGHT); } if (background) { const char *background_args [] = { "-s", NULL }; puts ("Moving into the background"); if (process_server (NULL, NULL, argc, argv, background_args) != 0) { puts ("Backgrounding failed. Giving up."); exit (EXIT_FAILURE); } } smt_init (); /* Initialise SMT kernel */ if (sysmana_init (port) == 0) /* Initialise SYSMAN agent */ smt_exec_full (); /* Run until completed */ else printf ("Initialisation error\n"); smt_term (); /* Shut-down SMT kernel */ mem_assert (); return (EXIT_SUCCESS); }
int server( int listen_port, const char* host_ip, int host_port ) { #if !defined(HTTP_PROXY) && !defined(SERVER) assert( NULL != host_ip ); #endif int sockfd = 0; if( (sockfd=socket(AF_INET,SOCK_STREAM,0) ) == -1 ) { SERVER_DEBUG( "server socket error:%s\n", strerror(errno) ); return -1; } SERVER_DEBUG( "server socket successfully\n" ); if( setsockopt_wrapper( sockfd ) < 0 ) { SERVER_DEBUG( "setsockopt_wrapper error:%s\n", strerror(errno) ); close( sockfd ); return -1; } struct sockaddr_in server_addr; memset( &server_addr, 0, sizeof(server_addr) ); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons( listen_port ); if( bind( sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr) ) == - 1 ) { SERVER_DEBUG( "server bind error:%s\n", strerror(errno) ); close( sockfd ); return -1; } SERVER_DEBUG( "server bind successfully\n" ); if( listen( sockfd, BACK_LOG_DEFAULT ) == -1 ) { SERVER_DEBUG( "server listen error:%s\n", strerror(errno) ); close( sockfd ); return -1; } SERVER_DEBUG( "server listen successfully port = %d\n", listen_port ); #if defined(EPOLL) return epoll_server( sockfd ); #endif parent = getpid(); struct pthread_vargs *list = NULL; while(1) { if( exit_flag ) { break; } int sockfd_new = handle_new_connection( sockfd ); if( sockfd_new < 0 ) { SERVER_DEBUG( "handle_new_connection\n" ); break; } #if !defined(MULTI_PTHREAD) pid_t child = fork(); if( child < 0 ) { SERVER_DEBUG( "fork error:%s\n", strerror(errno) ); close( sockfd_new ); break; }else if( child == 0 ){ SERVER_DEBUG( "child process\n" ); close( sockfd ); #if defined(PROXY) if( process_proxy( sockfd_new, host_ip, host_port ) < 0 ) { SERVER_DEBUG( "process_proxy error:%s\n", strerror(errno) ); close( sockfd_new ); break; } #elif defined(HTTP_PROXY) if( process_http_proxy( sockfd_new ) < 0 ) { SERVER_DEBUG( "process_http_proxy error\n" ); close( sockfd_new ); break; } #elif defined(SERVER) if( process_server( sockfd_new ) < 0 ) { SERVER_DEBUG( "process_server error:%s\n", strerror(errno) ); close( sockfd_new ); break; } #endif } else { int status; SERVER_DEBUG( "waitpid\n" ); close( sockfd_new ); waitpid( -1, &status, WNOHANG ); } #elif defined(MULTI_PTHREAD) struct pthread_vargs *pv = (struct pthread_vargs *)malloc(sizeof(struct pthread_vargs)); if( NULL == pv ) { SERVER_DEBUG( "malloc error:%s\n", strerror(errno) ); close( sockfd_new ); break; } memset( pv, 0, sizeof(struct pthread_vargs) ); if( NULL == list ) { list = pv; list->next = NULL; } else { struct pthread_vargs *iter = list; while( NULL != iter->next ) { iter = iter->next; } iter->next = pv; pv->next = NULL; iter = NULL; } pv->sockfd = sockfd_new; if( pthread_create( &pv->pid, NULL, pthread_http_proxy, pv ) < 0 ) { SERVER_DEBUG( "pthread_create error\n" ); close( sockfd_new ); break; } #endif } #if defined(MULTI_PTHREAD) (void)pthread_join_wrapper( list ); pthread_vargs_cleanup( list ); #endif close( sockfd ); SERVER_DEBUG( "server close\n" ); return 0; }
int main (int argc, char *argv []) { int argn; /* Argument number */ Bool args_ok = TRUE, /* Were the arguments okay? */ quiet_mode = FALSE; /* -q means suppress messages */ char *workdir, /* Working directory */ *rootdir, /* Default root directory */ *cgidir, /* CGI program directory */ *ftproot, /* Default FTP root directory */ *portbase, /* Value for IP portbase */ *background, /* -s means run in background */ **argparm; /* Argument parameter to pick-up */ /* First off, switch to user's id */ set_uid_user (); /* These are the arguments we may get on the command line */ workdir = NULL; rootdir = NULL; cgidir = NULL; portbase = NULL; background = NULL; ftproot = NULL; argparm = NULL; /* Argument parameter to pick-up */ for (argn = 1; argn < argc; argn++) { /* If argparm is set, we have to collect an argument parameter */ if (argparm) { if (*argv [argn] != '-') /* Parameter can't start with '-' */ { *argparm = strdupl (argv [argn]); argparm = NULL; } else { args_ok = FALSE; break; } } else if (*argv [argn] == '-') { switch (argv [argn][1]) { /* These switches take a parameter */ case 'w': argparm = &workdir; break; case 'r': argparm = &rootdir; break; case 'c': argparm = &cgidir; break; case 'b': argparm = &portbase; break; case 'f': argparm = &ftproot; break; /* These switches have an immediate effect */ case 'q': quiet_mode = TRUE; break; case 's': background = "1"; break; case 'S': background = "0"; break; case 't': smtsock_trace (TRUE); break; case 'v': coprintf (PRODUCT); coprintf (BUILDMODEL); coprintf (COPYRIGHT); coprintf ("Built on: %s", BUILDDATE); exit (EXIT_SUCCESS); case 'h': coprintf (SERVER_NAME); coprintf (COPYRIGHT); coprintf (USAGE); exit (EXIT_SUCCESS); /* Anything else is an error */ default: args_ok = FALSE; } } else { args_ok = FALSE; break; } } /* If there was a missing parameter or an argument error, quit */ if (argparm) { puts ("Argument missing - type 'xitami -h' for help"); exit (EXIT_FAILURE); } else if (!args_ok) { puts ("Invalid arguments - type 'xitami -h' for help"); exit (EXIT_FAILURE); } /* Set server working directory if necessary */ if (workdir && set_curdir (workdir)) { printf ("Can't work in '%s' - %s\n", workdir, strerror (errno)); exit (EXIT_FAILURE); } /* Load configuration data, if any, into the config_table */ config = ini_dyn_load (NULL, "xitami.cfg"); ini_dyn_load (config, CONFIG ("server:defaults")); /* Initialise arguments, taking defaults from the config_table */ if (!rootdir) rootdir = CONFIG ("server:webpages"); if (!cgidir) cgidir = CONFIG ("server:cgi-bin"); if (!portbase) portbase = CONFIG ("server:portbase"); if (!background) background = CONFIG ("server:background"); if (!ftproot) ftproot = CONFIG ("ftp:root"); /* Now, handle the remaining arguments we got */ ip_portbase = atoi (portbase); if (quiet_mode) { fclose (stdout); /* Kill standard output */ fclose (stderr); /* and standard error */ } else { puts (SERVER_NAME); puts (COPYRIGHT); } if (*background == '1') { const char *background_args [] = { "-s", NULL }; puts ("Moving into the background"); if (process_server (NULL, NULL, argc, argv, background_args) != 0) { puts ("Backgrounding failed. Giving up."); exit (EXIT_FAILURE); } } /* Initialise the SMT kernel */ smt_init (); server_name = "Xitami"; /* Load the agents we want to use */ if (*CONFIG ("lrwp:enabled") == '1') xilrwp_init (); /* LRWP service agent */ if (*CONFIG ("security:admin") == '1') xiadmin_init (); /* Administration agent */ if (*CONFIG ("server:supervisor") == '1') xisuper_init (); /* Supervisor agent */ xierror_init (); /* Error-simulation agent */ xiredir_init (); /* Redirection agent */ xiddns_init (); /* Dynamic DNS registration */ xiimap_init (); /* Image mapping agent */ xixlog_init (); /* Extended logging agent */ xixssi_init (); /* Internal SSI processor */ xixxml_init (); /* Internal XML processor */ smthttp_init (rootdir, cgidir); /* HTTP agent, required */ smtftpc_init (ftproot); /* FTP service agent */ smtpipe_init (CONFIG ("server:pipedef")); /* Transfer pipe agent */ smt_exec_full (); /* Run SMT until completed */ smt_term (); /* Deallocate configuration symbol table */ sym_delete_table (config); /* Check that all memory was cleanly released */ mem_assert (); return (EXIT_SUCCESS); }
/* ---------------------------------------------------------------------[<]- Function: service_begin Synopsis: depending on arguments (from the command line): -i: install service (windows) -u: remove service (windows) -d: runs service in console mode (windows) -h: basic help information (windows) -d: runs service in background (UNIX / Linux) if no arguments, the service is actually started. NOTE: with Windows, the working directory is set to the one where the binary program stands. Returns: 0 is everything is OK, negative error code otherwise ---------------------------------------------------------------------[>]-*/ int service_begin ( int argc, char **argv, SMT_AGENTS_INIT_FCT *init_fct, SMT_AGENTS_TERM_FCT *term_fct, const char *appl_version) { int action; int rc = 0; #if (defined(WIN32)) static char buffer [LINE_MAX]; char *p_char; SERVICE_TABLE_ENTRY dispatch_table [] = { { NULL, (LPSERVICE_MAIN_FUNCTION) service_main }, { NULL, NULL } }; /* Change to the correct working directory, where config file stands */ GetModuleFileName (NULL, buffer, LINE_MAX); if ((p_char = strrchr (buffer, '\\')) != NULL) *p_char = '\0'; SetCurrentDirectory (buffer); #endif rc = init_resources (argv[0], appl_version, init_fct, term_fct); if (rc != 0) return (1); ASSERT (application_config); /* init_resources post condition */ if (load_service_config (application_config) != 0) { free_resources (); return (1); } ASSERT (service_trace_file); /* load_service_config postcondition */ console_set_mode (CONSOLE_DATETIME); console_capture (service_trace_file, 'a'); #if (defined(WIN32)) dispatch_table [0].lpServiceName = service_name; win_version = get_windows_version (); #endif action = parse_command_line (argc, argv); if (action == ACTION_HELP) { puts (USAGE); } #if (defined(WIN32)) else if (action == ACTION_INSTALL) { if (win_version == WINDOWS_95) set_win95_service (TRUE); else install_service (); } else if (action == ACTION_UNINSTALL) { if (win_version == WINDOWS_95) set_win95_service (FALSE); else remove_service (); } else if (action == ACTION_CONSOLE) { console_mode = TRUE; console_service (argc, argv); } else if (action == ACTION_NOARG) { console_send (NULL, FALSE); if (win_version == WINDOWS_95) { hide_window (); console_mode = TRUE; console_service (argc, argv); } else if (win_version == WINDOWS_NT_3X || win_version == WINDOWS_NT_4 || win_version == WINDOWS_2000) { log_printf ("%s: initialising service ...", application_name); if (!StartServiceCtrlDispatcher (dispatch_table)) add_to_message_log ("StartServiceCtrlDispatcher failed"); } } #elif (defined(__UNIX__)) else if (action == ACTION_BACKGROUND) { const char *background_args [] = { "-s", NULL }; log_printf ("Moving into the background"); if (process_server (NULL, NULL, argc, argv, background_args) != 0) { log_printf ("Backgrounding failed. Giving up."); rc = -1; } else action = ACTION_NOARG; } if (action == ACTION_NOARG) { rc = smt_init (); if (!rc && (init_fct != NULL)) rc = (*init_fct)(application_config); if (!rc) smt_exec_full (); if (term_fct != NULL) (*term_fct)(); smt_term (); } #endif else if (action == ACTION_ERROR) puts (USAGE); free_resources (); return rc; }