int proxy_core(struct proxybind *header, int num_items, const char *chdir, char *user) { /* needed for IPC between parent and child in this two model process */ int thesockets[2]; pid_t pid; int status; struct conn_t state; struct proxybind *tmpbind; int *listen_array = calloc(num_items,sizeof(int)); /* int *conn_array = calloc(num_items,sizeof(int)); struct sockaddr_in *cliaddr_array = calloc(num_items,sizeof(struct sockaddr_in)); */ memset(&state,0,sizeof(struct conn_t)); state.connfd = -1; if (listen_array == NULL) { fprintf(stderr,"%s: Memory allocation error\n",__func__); exit(EXIT_FAILURE); } if (socketpair(AF_UNIX,SOCK_STREAM,0,thesockets)<0) { fprintf(stderr,"%s: Error during setting socketpair IPC\n",__func__); exit(EXIT_FAILURE); } tmpbind = header; int j; for (j = 0; j < num_items; j++) { listen_array[j] = create_server_socket(tmpbind->ipaddress,tmpbind->port); #ifdef DEBUG_CORE fprintf(stderr,"Opening socket on port %d\n",tmpbind->port); #endif tmpbind = tmpbind->next; } if ((pid = fork())<0) { fprintf(stderr,"%s: Fork failed before IPC\n",__func__); exit(EXIT_FAILURE); } if (pid) { /* parent 1) open file descriptor 2) send file descriptor to child. */ close(thesockets[0]); for (;;) { if (multiaccept (listen_array, num_items, &state)< 0) { fprintf(stderr,"Error"); //exit(EXIT_FAILURE); } if (state.connfd != -1) { fprintf(stderr,"Starting communication with client\n"); client_fsm(thesockets,state); memset(&state,0,sizeof(struct conn_t)); state.connfd = -1; } } while (waitpid(-1,&status,WNOHANG)<0) { fprintf(stderr,"%s: waitpid error\n",__func__); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { fprintf(stderr,"%s: program terminated\n",__func__); free(listen_array); exit(EXIT_FAILURE); } } else { /* fprintf(stderr,"%s: Chrooting to %s\n",__func__,chdir); if (jchroot(chdir)) { fprintf(stderr,"%s: cannot chroot to %s\n",__func__,chdir); kill(getppid(),SIGTERM); exit(EXIT_FAILURE); }*/ if (!drop_privs(user)) { fprintf(stderr,"%s: cannot drop privilegies to %s\n",__func__,user); kill(getppid(),SIGTERM); exit(EXIT_FAILURE); } #ifdef DEBUG_CORE fprintf(stderr,"Starting worker\n"); #endif close(thesockets[1]); main_worker(thesockets); } return 0; }
bool daemon_backend::start(int argc, char* argv[], view::i_view* pview_handler) { m_stop_singal_sent = false; if(pview_handler) m_pview = pview_handler; view::daemon_status_info dsi = AUTO_VAL_INIT(dsi); dsi.difficulty = "---"; dsi.text_state = "Initializing..."; pview_handler->update_daemon_status(dsi); //#ifdef WIN32 //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //#endif log_space::get_set_log_detalisation_level(true, LOG_LEVEL_0); LOG_PRINT_L0("Initing..."); TRY_ENTRY(); po::options_description desc_cmd_only("Command line options"); po::options_description desc_cmd_sett("Command line options and settings options"); command_line::add_arg(desc_cmd_only, command_line::arg_help); command_line::add_arg(desc_cmd_only, command_line::arg_version); command_line::add_arg(desc_cmd_only, command_line::arg_os_version); // tools::get_default_data_dir() can't be called during static initialization command_line::add_arg(desc_cmd_only, command_line::arg_data_dir, tools::get_default_data_dir()); command_line::add_arg(desc_cmd_only, command_line::arg_config_file); command_line::add_arg(desc_cmd_sett, command_line::arg_log_file); command_line::add_arg(desc_cmd_sett, command_line::arg_log_level); command_line::add_arg(desc_cmd_sett, command_line::arg_console); command_line::add_arg(desc_cmd_sett, command_line::arg_show_details); command_line::add_arg(desc_cmd_sett, arg_alloc_win_console); currency::core::init_options(desc_cmd_sett); currency::core_rpc_server::init_options(desc_cmd_sett); nodetool::node_server<currency::t_currency_protocol_handler<currency::core> >::init_options(desc_cmd_sett); currency::miner::init_options(desc_cmd_sett); po::options_description desc_options("Allowed options"); desc_options.add(desc_cmd_only).add(desc_cmd_sett); po::variables_map vm; bool r = command_line::handle_error_helper(desc_options, [&]() { po::store(po::parse_command_line(argc, argv, desc_options), vm); if (command_line::get_arg(vm, command_line::arg_help)) { std::cout << CURRENCY_NAME << " v" << PROJECT_VERSION_LONG << ENDL << ENDL; std::cout << desc_options << std::endl; return false; } m_data_dir = command_line::get_arg(vm, command_line::arg_data_dir); std::string config = command_line::get_arg(vm, command_line::arg_config_file); boost::filesystem::path data_dir_path(m_data_dir); boost::filesystem::path config_path(config); if (!config_path.has_parent_path()) { config_path = data_dir_path / config_path; } boost::system::error_code ec; if (boost::filesystem::exists(config_path, ec)) { po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_cmd_sett), vm); } po::notify(vm); return true; }); if (!r) return false; //set up logging options if(command_line::has_arg(vm, arg_alloc_win_console)) { log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL); } boost::filesystem::path log_file_path(command_line::get_arg(vm, command_line::arg_log_file)); if (log_file_path.empty()) log_file_path = log_space::log_singletone::get_default_log_file(); std::string log_dir; log_dir = log_file_path.has_parent_path() ? log_file_path.parent_path().string() : log_space::log_singletone::get_default_log_folder(); log_space::log_singletone::add_logger(LOGGER_FILE, log_file_path.filename().string().c_str(), log_dir.c_str()); LOG_PRINT_L0(CURRENCY_NAME << " v" << PROJECT_VERSION_LONG); LOG_PRINT("Module folder: " << argv[0], LOG_LEVEL_0); bool res = true; currency::checkpoints checkpoints; res = currency::create_checkpoints(checkpoints); CHECK_AND_ASSERT_MES(res, false, "Failed to initialize checkpoints"); m_ccore.set_checkpoints(std::move(checkpoints)); m_main_worker_thread = std::thread([this, vm](){main_worker(vm);}); return true; CATCH_ENTRY_L0("main", 1); }
/** Initialise the tweed library - must be called before any other * tweed calls */ int init_tweed(int workers_requested, int(*main_func)(struct generic_task_desc *,void*), void* main_args) { int i, err; if (workers_requested < 1) { fprintf(stderr, "Error initalizing tweed - requested less than 1 worker\n"); return -1; } num_workers = workers_requested; workers = (struct worker_desc *) malloc ( num_workers * sizeof(struct worker_desc)); // alloc task stack space for all workers, leave space for alignment task_stack_space = malloc (TWEED_TASK_STACK_SIZE * (num_workers + 1)); char * curr_stack_space = (char*)(((unsigned long)task_stack_space + TWEED_TASK_STACK_SIZE) & ~TWEED_TASK_STACK_MASK); // Initialize worker data-structures for (i=0; i<num_workers; i++) { init_worker(i, curr_stack_space); curr_stack_space += TWEED_TASK_STACK_SIZE; } // create dispatchers on all other cores required for num_workers for (i=1; i<num_workers; i++) { err = domain_new_dispatcher(i + disp_get_core_id(), domain_spanned_callback, (void*)(uintptr_t)i); if (err_is_fail(err)) { DEBUG_ERR(err, "domain_new_dispatcher failed"); printf("%d failed\n", i); } } // wait for all dispatchers to come up while (num_dispatchers < num_workers) { messages_wait_and_handle_next(); } num_dispatchers = 1; // reset // start work stealing threads on newly created domains for (i = 1; i < num_workers; i++) { struct worker_args * args = (struct worker_args *) malloc ( sizeof(struct worker_args)); args->id = i; args->origin = disp_get_core_id(); err = domain_thread_create_on(i + disp_get_core_id(), start_worker_thread, args); if (err_is_fail(err)) { DEBUG_ERR(err, "Failed to run a function on remote core"); } } // wait for all dispatchers to come up while (num_dispatchers < num_workers) { messages_wait_and_handle_next(); } // now start the main worker on the current dispatcher return main_worker(0, main_func, main_args); }