bool ServerZMQ::_onUpdate() { if (s_interrupted) { return false; } int result; const TString& subscriberId = this->getSubscribeId(); result = s_sendmore(m_socket, subscriberId.c_str()); if (result == -1) { DMSG_LOGGER("Error occurred during send subscribe id"); return false; } const TString& message = getMessageString(); //TString msg = message; //std::replace(msg.begin(), msg.end(), '{', ')'); //DMSG_LOGGER(message.c_str()); result = s_send(m_socket, message.c_str()); if (result == -1) { zmqlog("Error occurred during send message "); DMSG_LOGGER("Error sending %s ", message.c_str()); return false; } //sleep for 1 to 2 seconds int sleeptime = (rand() % 1000) + 1000; s_sleep(sleeptime); return true; }
bool Pack::checkDiscovery() { stats_.total++; auto current = getUnixTime(); if ((current - discovery_cache_.first) < FLAGS_pack_refresh_interval) { stats_.hits++; return discovery_cache_.second; } stats_.misses++; discovery_cache_.first = current; discovery_cache_.second = true; for (const auto& q : discovery_queries_) { auto sql = SQL(q); if (!sql.ok()) { LOG(WARNING) << "Discovery query failed (" << q << "): " << sql.getMessageString(); discovery_cache_.second = false; break; } if (sql.rows().size() == 0) { discovery_cache_.second = false; break; } } return discovery_cache_.second; }
void launchQuery(const std::string& name, const ScheduledQuery& query) { // Execute the scheduled query and create a named query object. VLOG(1) << "Executing query: " << query.query; auto sql = (FLAGS_enable_monitor) ? monitor(name, query) : SQL(query.query); if (!sql.ok()) { LOG(ERROR) << "Error executing query (" << query.query << "): " << sql.getMessageString(); return; } // Fill in a host identifier fields based on configuration or availability. std::string ident; auto status = getHostIdentifier(ident); if (!status.ok() || ident.empty()) { ident = "<unknown>"; } // A query log item contains an optional set of differential results or // a copy of the most-recent execution alongside some query metadata. QueryLogItem item; item.name = name; item.identifier = ident; item.time = osquery::getUnixTime(); item.calendar_time = osquery::getAsciiTime(); if (query.options.count("snapshot") && query.options.at("snapshot")) { // This is a snapshot query, emit results with a differential or state. item.snapshot_results = std::move(sql.rows()); logSnapshotQuery(item); return; } // Create a database-backed set of query results. auto dbQuery = Query(name, query); DiffResults diff_results; // Add this execution's set of results to the database-tracked named query. // We can then ask for a differential from the last time this named query // was executed by exact matching each row. status = dbQuery.addNewResults(sql.rows(), diff_results); if (!status.ok()) { LOG(ERROR) << "Error adding new results to database: " << status.what(); return; } if (diff_results.added.size() == 0 && diff_results.removed.size() == 0) { // No diff results or events to emit. return; } VLOG(1) << "Found results for query (" << name << ") for host: " << ident; item.results = diff_results; status = logQueryLogItem(item); if (!status.ok()) { LOG(ERROR) << "Error logging the results of query (" << query.query << "): " << status.toString(); } }
std::vector<fs::path> getHomeDirectories() { auto sql = SQL("SELECT DISTINCT directory FROM users WHERE directory != '/var/empty';"); std::vector<fs::path> results; if (sql.ok()) { for (const auto& row: sql.rows()) { results.push_back(row.at("directory")); } } else { LOG(ERROR) << "Error executing query to return users: " << sql.getMessageString(); } return results; }
Status checkStalePid(const std::string& content) { int pid; try { pid = boost::lexical_cast<int>(content); } catch (const boost::bad_lexical_cast& e) { if (FLAGS_force) { return Status(0, "Force loading and not parsing pidfile"); } else { return Status(1, "Could not parse pidfile"); } } PlatformProcess target(pid); int status = 0; // The pid is running, check if it is an osqueryd process by name. std::stringstream query_text; query_text << "SELECT name FROM processes WHERE pid = " << pid << " AND name LIKE 'osqueryd%';"; auto q = SQL(query_text.str()); if (!q.ok()) { return Status(1, "Error querying processes: " + q.getMessageString()); } if (q.rows().size() > 0) { // If the process really is osqueryd, return an "error" status. if (FLAGS_force) { // The caller may choose to abort the existing daemon with --force. // Do not use SIGQUIT as it will cause a crash on OS X. status = target.kill() ? 0 : -1; sleepFor(1000); return Status(status, "Tried to force remove the existing osqueryd"); } return Status(1, "osqueryd (" + content + ") is already running"); } else { LOG(INFO) << "Found stale process for osqueryd (" << content << ") removing pidfile"; } return Status(0, "OK"); }
inline void launchQuery(const std::string& name, const ScheduledQuery& query) { // Execute the scheduled query and create a named query object. LOG(INFO) << "Executing scheduled query " << name << ": " << query.query; runDecorators(DECORATE_ALWAYS); auto sql = monitor(name, query); if (!sql.ok()) { LOG(ERROR) << "Error executing scheduled query " << name << ": " << sql.getMessageString(); return; } // Fill in a host identifier fields based on configuration or availability. std::string ident = getHostIdentifier(); // A query log item contains an optional set of differential results or // a copy of the most-recent execution alongside some query metadata. QueryLogItem item; item.name = name; item.identifier = ident; item.time = osquery::getUnixTime(); item.epoch = FLAGS_schedule_epoch; item.calendar_time = osquery::getAsciiTime(); getDecorations(item.decorations); if (query.options.count("snapshot") && query.options.at("snapshot")) { // This is a snapshot query, emit results with a differential or state. item.snapshot_results = std::move(sql.rows()); logSnapshotQuery(item); return; } // Create a database-backed set of query results. auto dbQuery = Query(name, query); // Comparisons and stores must include escaped data. sql.escapeResults(); Status status; DiffResults diff_results; // Add this execution's set of results to the database-tracked named query. // We can then ask for a differential from the last time this named query // was executed by exact matching each row. if (!FLAGS_events_optimize || !sql.eventBased()) { status = dbQuery.addNewResults( sql.rows(), item.epoch, item.counter, diff_results); if (!status.ok()) { std::string line = "Error adding new results to database: " + status.what(); LOG(ERROR) << line; // If the database is not available then the daemon cannot continue. Initializer::requestShutdown(EXIT_CATASTROPHIC, line); } } else { diff_results.added = std::move(sql.rows()); } if (diff_results.added.empty() && diff_results.removed.empty()) { // No diff results or events to emit. return; } VLOG(1) << "Found results for query: " << name; item.results = diff_results; if (query.options.count("removed") && !query.options.at("removed")) { item.results.removed.clear(); } status = logQueryLogItem(item); if (!status.ok()) { // If log directory is not available, then the daemon shouldn't continue. std::string error = "Error logging the results of query: " + name + ": " + status.toString(); LOG(ERROR) << error; Initializer::requestShutdown(EXIT_CATASTROPHIC, error); } }
bool ONScripterLabel::executeSystemYesNo( int caller, int file_no ) { current_font = &menu_font; text_info.fill( 0, 0, 0, 0 ); dirty_rect.fill( screen_width, screen_height ); char name[64] = {'\0'}; if ( caller == SYSTEM_SAVE ){ SaveFileInfo save_file_info; searchSaveFile( save_file_info, file_no ); sprintf( name, getMessageString(MESSAGE_SAVE_CONFIRM), save_item_name, save_file_info.sjis_no ); } else if ( caller == SYSTEM_LOAD ){ SaveFileInfo save_file_info; searchSaveFile( save_file_info, file_no ); sprintf( name, getMessageString(MESSAGE_LOAD_CONFIRM), save_item_name, save_file_info.sjis_no ); } else if ( caller == SYSTEM_RESET ) strcpy( name, getMessageString(MESSAGE_RESET_CONFIRM) ); else if ( caller == SYSTEM_END ) strcpy( name, getMessageString(MESSAGE_END_CONFIRM) ); menu_font.num_xy[0] = strlen(name)/2; menu_font.num_xy[1] = 3; menu_font.top_xy[0] = (ContractPos(screen_width) - menu_font.num_xy[0] * menu_font.pitch_xy[0]) / 2; menu_font.top_xy[1] = (ContractPos(screen_height) - menu_font.num_xy[1] * menu_font.pitch_xy[1]) / 2; menu_font.setXY(0, 0); //Mion: fixed the menu title bug noted in the past by Seung Park: // the menu title must be drawn close to last during refresh, // not in the textwindow, since there could be sprites above the // window if windowback is used. if (system_menu_title){ dirty_rect.add( system_menu_title->pos ); delete system_menu_title; system_menu_title = NULL; } system_menu_title = getSentence( name, &menu_font, 1, false ); flush( refreshMode() ); int offset1 = strlen(name)/5; int offset2 = strlen(name)/2 - offset1; strcpy( name, getMessageString(MESSAGE_YES) ); menu_font.setXY(offset1-2, 2); ButtonLink *button = getSelectableSentence( name, &menu_font, false ); root_button_link.insert( button ); button->no = 1; strcpy( name, getMessageString(MESSAGE_NO) ); menu_font.setXY(offset2, 2); button = getSelectableSentence( name, &menu_font, false ); root_button_link.insert( button ); button->no = 2; flush( refreshMode() ); refreshMouseOverButton(); event_mode = WAIT_BUTTON_MODE; do waitEventSub(-1); while (current_button_state.button == 0); deleteButtonLink(); if (system_menu_title){ dirty_rect.add( system_menu_title->pos ); delete system_menu_title; system_menu_title = NULL; } if ( current_button_state.button == 1 ){ // yes is selected if ( menuselectvoice_file_name[MENUSELECTVOICE_YES] ) playSound(menuselectvoice_file_name[MENUSELECTVOICE_YES], SOUND_WAVE|SOUND_OGG, false, MIX_WAVE_CHANNEL); return true; } else{ if ( menuselectvoice_file_name[MENUSELECTVOICE_NO] ) playSound(menuselectvoice_file_name[MENUSELECTVOICE_NO], SOUND_WAVE|SOUND_OGG, false, MIX_WAVE_CHANNEL); return false; } }
void ONScripterLabel::executeSystemSave() { current_font = &menu_font; text_info.fill( 0, 0, 0, 0 ); menu_font.num_xy[0] = (strlen(save_item_name)+1)/2+2+13; menu_font.num_xy[1] = num_save_file+2; menu_font.top_xy[0] = (ContractPos(screen_width) - menu_font.num_xy[0] * menu_font.pitch_xy[0]) / 2; menu_font.top_xy[1] = (ContractPos(screen_height) - menu_font.num_xy[1] * menu_font.pitch_xy[1]) / 2; menu_font.setXY((menu_font.num_xy[0] - (strlen( save_menu_name )+1) / 2 ) / 2, 0); //Mion: fixed the menu title bug noted in the past by Seung Park: // the menu title must be drawn close to last during refresh, // not in the textwindow, since there could be sprites above the // window if windowback is used. if (system_menu_title){ dirty_rect.add( system_menu_title->pos ); delete system_menu_title; system_menu_title = NULL; } system_menu_title = getSentence( save_menu_name, &menu_font, 1, false ); menu_font.newLine(); flush( refreshMode() ); bool nofile_flag; int slen = strlen(save_item_name); char *buffer = new char[ slen + (slen % 2) + 30 + 3 ]; for ( unsigned int i=1 ; i<=num_save_file ; i++ ){ SaveFileInfo save_file_info; searchSaveFile( save_file_info, i ); menu_font.setXY( (menu_font.num_xy[0] - ((strlen( save_item_name )+1) / 2 + 15) ) / 2 ); if ( save_file_info.valid ){ sprintf( buffer, getMessageString(MESSAGE_SAVE_EXIST), save_item_name, save_file_info.sjis_no, save_file_info.sjis_month, save_file_info.sjis_day, save_file_info.sjis_hour, save_file_info.sjis_minute ); nofile_flag = false; } else{ sprintf( buffer, getMessageString(MESSAGE_SAVE_EMPTY), save_item_name, save_file_info.sjis_no ); nofile_flag = true; } ButtonLink *button = getSelectableSentence( buffer, &menu_font, false, nofile_flag ); root_button_link.insert( button ); button->no = i; flush( refreshMode() ); } delete[] buffer; refreshMouseOverButton(); event_mode = WAIT_BUTTON_MODE; do waitEventSub(-1); while (current_button_state.button == 0); deleteButtonLink(); if (system_menu_title){ dirty_rect.add( system_menu_title->pos ); delete system_menu_title; system_menu_title = NULL; } if ( current_button_state.button > 0 ){ int file_no = current_button_state.button; if (executeSystemYesNo( SYSTEM_SAVE, file_no )){ saveSaveFile( file_no ); leaveSystemCall(); } return; } leaveSystemCall(); }