2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-01-21 20:56:25 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2015-01-21 20:56:25 +00:00
|
|
|
|
2014-07-31 00:35:19 +00:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/config.h>
|
|
|
|
#include <osquery/core.h>
|
|
|
|
#include <osquery/events.h>
|
2015-01-21 21:36:55 +00:00
|
|
|
#include <osquery/logger.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/scheduler.h>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2015-01-26 08:02:02 +00:00
|
|
|
#include "osquery/core/watcher.h"
|
|
|
|
|
|
|
|
const std::string kWatcherWorkerName = "osqueryd-worker";
|
|
|
|
|
2015-01-05 03:27:04 +00:00
|
|
|
#ifndef __APPLE__
|
|
|
|
namespace osquery {
|
|
|
|
DEFINE_osquery_flag(bool, daemonize, false, "Run as daemon (osqueryd only).");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 20:56:25 +00:00
|
|
|
namespace osquery {
|
|
|
|
DEFINE_osquery_flag(bool,
|
|
|
|
config_check,
|
|
|
|
false,
|
|
|
|
"Check the format and accessibility of the daemon");
|
2015-01-26 08:02:02 +00:00
|
|
|
|
|
|
|
DEFINE_osquery_flag(bool,
|
|
|
|
disable_watchdog,
|
|
|
|
false,
|
|
|
|
"Do not use a userland watchdog process.");
|
2015-01-21 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 11:06:21 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
2014-11-09 04:27:28 +00:00
|
|
|
osquery::initOsquery(argc, argv, osquery::OSQUERY_TOOL_DAEMON);
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2015-01-21 20:56:25 +00:00
|
|
|
if (osquery::FLAGS_config_check) {
|
|
|
|
auto s = osquery::Config::checkConfig();
|
|
|
|
if (!s.ok()) {
|
|
|
|
std::cerr << "Error reading config: " << s.toString() << "\n";
|
|
|
|
}
|
|
|
|
return s.getCode();
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:27:04 +00:00
|
|
|
#ifndef __APPLE__
|
|
|
|
// OSX uses launchd to daemonize.
|
|
|
|
if (osquery::FLAGS_daemonize) {
|
|
|
|
if (daemon(0, 0) == -1) {
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-18 02:42:36 +00:00
|
|
|
auto pid_status = osquery::createPidFile();
|
|
|
|
if (!pid_status.ok()) {
|
2015-01-07 23:22:50 +00:00
|
|
|
LOG(ERROR) << "Could not start osqueryd: " << pid_status.toString();
|
2014-12-08 18:40:10 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
2014-11-18 02:42:36 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 18:55:28 +00:00
|
|
|
try {
|
|
|
|
osquery::DBHandle::getInstance();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
LOG(ERROR) << "osqueryd failed to start: " << e.what();
|
2015-01-05 03:27:04 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
2014-10-27 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 08:02:02 +00:00
|
|
|
if (!osquery::FLAGS_disable_watchdog) {
|
|
|
|
// When a watcher is used, the current watcher will fork into a worker.
|
|
|
|
osquery::initWorkerWatcher(kWatcherWorkerName, argc, argv);
|
|
|
|
}
|
|
|
|
|
2014-08-21 21:35:51 +00:00
|
|
|
LOG(INFO) << "Listing all plugins";
|
|
|
|
|
|
|
|
LOG(INFO) << "Logger plugins:";
|
2015-01-31 08:25:51 +00:00
|
|
|
for (const auto& name : osquery::Registry::names("logger")) {
|
2015-01-30 18:44:25 +00:00
|
|
|
LOG(INFO) << " - " << name;
|
2014-08-21 21:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Config plugins:";
|
2015-01-31 08:25:51 +00:00
|
|
|
for (const auto& name : osquery::Registry::names("config")) {
|
2015-01-30 18:44:25 +00:00
|
|
|
LOG(INFO) << " - " << name;
|
2014-08-21 21:35:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 04:27:28 +00:00
|
|
|
LOG(INFO) << "Event Publishers:";
|
2015-02-03 22:27:17 +00:00
|
|
|
for (const auto& name : osquery::Registry::names("event_publisher")) {
|
2015-01-30 18:44:25 +00:00
|
|
|
LOG(INFO) << " - " << name;
|
2014-09-24 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 04:27:28 +00:00
|
|
|
LOG(INFO) << "Event Subscribers:";
|
2015-02-03 22:27:17 +00:00
|
|
|
for (const auto& name : osquery::Registry::names("event_subscriber")) {
|
2015-01-30 18:44:25 +00:00
|
|
|
LOG(INFO) << " - " << name;
|
2014-09-24 18:25:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 08:35:44 +00:00
|
|
|
// Start event threads.
|
2014-09-23 01:35:12 +00:00
|
|
|
osquery::EventFactory::delay();
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-09-23 01:35:12 +00:00
|
|
|
boost::thread scheduler_thread(osquery::initializeScheduler);
|
2014-07-31 00:35:19 +00:00
|
|
|
scheduler_thread.join();
|
|
|
|
|
2015-02-01 08:35:44 +00:00
|
|
|
// Finally shutdown.
|
|
|
|
osquery::shutdownOsquery();
|
2014-09-23 01:35:12 +00:00
|
|
|
|
2014-07-31 00:35:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|