void store_arguments(char* log_dir, int argc, char* argv[], char* envp[]) { char *fpath; FILE *arguments_file; int i, j, k; /* Create arguments file */ fpath = log_file_path(log_dir, "args"); arguments_file = fopen(fpath, "wb"); free(fpath); /* Store executable + arguments */ fwrite(&argc, sizeof(argc), 1, arguments_file); for(i = 0; i < argc; i++) { j = strlen(argv[i]); fwrite(&j, sizeof(j), 1, arguments_file); fwrite(argv[i], j, 1, arguments_file); } /* Store enviroment variables */ i = 0; while (envp[i]) { i++; } fwrite(&i, sizeof(i), 1, arguments_file); for (j = 0; j < i; j++) { k = strlen(envp[j]); fwrite(&k, sizeof(k), 1, arguments_file); fwrite(envp[j], k, 1, arguments_file); } fclose(arguments_file); }
static int get_log_seq_nonrecycle(int lvl) { char file_name[FILENAME_MAX]; struct tm tm; time_t now = time(0); localtime_r(&now, &tm); int seq; for (seq = 0; seq != MAX_LOG_CNT; ++seq) { log_file_path(lvl, seq, file_name, &tm); if (access(file_name, F_OK) == -1) { break; } } return (seq ? (seq - 1) : 0); }
static int open_fd(int lvl, const struct tm* tm) { int flag = O_WRONLY | O_CREAT | O_APPEND/* | O_LARGEFILE*/; char file_name[FILENAME_MAX]; log_file_path(lvl, fds_info[lvl].seq, file_name, tm); fds_info[lvl].opfd = open(file_name, flag, 0644); if (fds_info[lvl].opfd != -1) { fds_info[lvl].day = tm->tm_yday; flag = fcntl(fds_info[lvl].opfd, F_GETFD, 0); flag |= FD_CLOEXEC; fcntl(fds_info[lvl].opfd, F_SETFD, flag); // remove files only if logfile recyle is used if (max_log_files) { rm_files_by_seqno(lvl, fds_info[lvl].seq, tm); } } return fds_info[lvl].opfd; }
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); }
int main(int argc, char* argv[], char* envp[]) { int i, dev; pid_t app_pid; char *syscall_log, *sched_log, *fpath; syscall_log_entry_t *log_entry; FILE* syscall_log_file, *sched_log_file; callback_t cbdata = { .type = NO_DATA, .size = 0 }; /* Verify arguments */ if (argc < 3) { printf("Imitate Recorder\n"); printf("Usage: %s <log_path> <executable_path> <args>\n",argv[0]); return 0; } /* Create trace directory */ if (mkdir(argv[1], 0700) < 0) { perror("Creating log directory"); return -1; } store_arguments(argv[1], argc - PROG_ARGS, argv+PROG_ARGS, envp); dev = open("/dev/imitate0", O_RDWR); if (dev < 0) { perror("Opening imitate kernel device"); return -1; } if (ioctl(dev, IMITATE_MONITOR) < 0) { perror("Notifying imitate kernel driver of MONITOR"); goto error_after_dev; } if ((syscall_log = (char*) mmap(NULL, SYSCALL_BUFFER_SIZE, PROT_READ, MAP_SHARED, dev, 0)) == MAP_FAILED) { perror("Memory mapping system call log"); goto error_after_dev; } if ((sched_log = (char*) mmap(NULL, SCHED_BUFFER_SIZE, PROT_READ, MAP_SHARED, dev, 0)) == MAP_FAILED) { perror("Memory mapping schedule log"); goto error_after_dev; } app_pid = fork(); if (app_pid > 0) /* Parent */ { fpath = log_file_path(argv[1], "syscall"); syscall_log_file = fopen(fpath, "wb"); free(fpath); fpath = log_file_path(argv[1], "sched"); sched_log_file = fopen(fpath, "wb"); free(fpath); while (cbdata.type != APP_EXIT && cbdata.type != APP_KILLED) { if (ioctl(dev, IMITATE_MONITOR_CB, &cbdata) < 0) { perror("Requesting log data"); goto error_after_dev; } switch(cbdata.type) { case SYSCALL_DATA: fwrite(syscall_log, cbdata.size, 1, syscall_log_file); break; case SCHED_DATA: fwrite(sched_log, cbdata.size, 1, sched_log_file); break; case APP_KILLED: fprintf(stderr, "Recorded application killed by kernel driver.\n"); break; } } } else if (app_pid == 0) /* Child */ { if (execve("./patcher", argv+1, envp) < 0) { perror("Application execve()"); return -2; } } else /* Error */ { perror("Forking application process"); goto error_after_dev; } fclose(syscall_log_file); fclose(sched_log_file); waitpid(app_pid, &i, NULL); close(dev); return 0; error_after_dev: close(dev); return -1; }