OLD | NEW |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A simple daemon to detect, mount, and eject removable storage devices. | 5 // A simple daemon to detect, mount, and eject removable storage devices. |
6 | 6 |
| 7 #include "cros-disks-server-impl.h" |
| 8 #include "disk-manager.h" |
| 9 #include <base/basictypes.h> |
7 #include <base/file_util.h> | 10 #include <base/file_util.h> |
8 #include <base/logging.h> | 11 #include <base/logging.h> |
9 #include <base/string_util.h> | 12 #include <base/string_util.h> |
10 #include <dbus-c++/glib-integration.h> | 13 #include <dbus-c++/glib-integration.h> |
11 #include <dbus-c++/util.h> | 14 #include <dbus-c++/util.h> |
12 #include <gflags/gflags.h> | 15 #include <gflags/gflags.h> |
13 #include <glib-object.h> | 16 #include <glib-object.h> |
14 #include <glib.h> | 17 #include <glib.h> |
| 18 #include <libudev.h> |
15 #include <metrics/metrics_library.h> | 19 #include <metrics/metrics_library.h> |
16 | 20 |
| 21 |
| 22 using cros_disks::CrosDisksServer; |
| 23 using cros_disks::DiskManager; |
| 24 |
| 25 DEFINE_bool(foreground, false, |
| 26 "Don't daemon()ize; run in foreground."); |
| 27 |
| 28 // TODO(rtc): gflags string defines require the use of |
| 29 // -fno-strict-aliasing for some reason. Verify that disabling this check |
| 30 // is sane. |
| 31 DEFINE_string(log_dir, "/var/log/cros-disks", "log directory"); |
| 32 |
| 33 void SetupLogging() { |
| 34 logging::InitLogging(FLAGS_log_dir.c_str(), |
| 35 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, |
| 36 logging::DONT_LOCK_LOG_FILE, |
| 37 logging::APPEND_TO_OLD_LOG_FILE); |
| 38 } |
| 39 |
| 40 // This callback will be invoked once udev has data about |
| 41 // new, changed, or removed devices. |
| 42 gboolean UdevCallback(GIOChannel* source, |
| 43 GIOCondition condition, |
| 44 gpointer data) { |
| 45 DiskManager* mgr = static_cast<DiskManager*>(data); |
| 46 mgr->ProcessUdevChanges(); |
| 47 return true; |
| 48 } |
| 49 |
17 int main(int argc, char** argv) { | 50 int main(int argc, char** argv) { |
18 ::g_type_init(); | 51 ::g_type_init(); |
19 g_thread_init(NULL); | 52 g_thread_init(NULL); |
20 google::ParseCommandLineFlags(&argc, &argv, true); | 53 google::ParseCommandLineFlags(&argc, &argv, true); |
21 | 54 |
| 55 if(!FLAGS_foreground) { |
| 56 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 57 // SetupLogging(); |
| 58 } |
| 59 |
22 LOG(INFO) << "Creating a GMainLoop"; | 60 LOG(INFO) << "Creating a GMainLoop"; |
23 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 61 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 62 CHECK(loop) << "Failed to create a GMainLoop"; |
| 63 |
| 64 LOG(INFO) << "Creating the dbus dispatcher"; |
| 65 DBus::Glib::BusDispatcher* dispatcher = |
| 66 new(std::nothrow) DBus::Glib::BusDispatcher(); |
| 67 CHECK(dispatcher) << "Failed to create a dbus-dispatcher"; |
| 68 DBus::default_dispatcher = dispatcher; |
| 69 dispatcher->attach(NULL); |
| 70 |
| 71 LOG(INFO) << "creating server"; |
| 72 DBus::Connection server_conn = DBus::Connection::SystemBus(); |
| 73 server_conn.request_name("org.chromium.CrosDisks"); |
| 74 CrosDisksServer* server = new(std::nothrow) CrosDisksServer(server_conn); |
| 75 CHECK(server) << "Failed to create the cros-disks server"; |
24 | 76 |
25 LOG(INFO) << "Initializing the metrics library"; | 77 LOG(INFO) << "Initializing the metrics library"; |
26 MetricsLibrary metrics_lib; | 78 MetricsLibrary metrics_lib; |
27 metrics_lib.Init(); | 79 metrics_lib.Init(); |
28 | 80 |
29 // Run the main loop until exit time: | 81 |
30 // TODO(rtc): daemonize this | 82 DiskManager manager; |
| 83 manager.EnumerateDisks(); |
| 84 |
| 85 // Setup a monitor |
| 86 g_io_add_watch_full(g_io_channel_unix_new(manager.udev_monitor_fd()), |
| 87 G_PRIORITY_HIGH_IDLE, |
| 88 GIOCondition(G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_NVAL), |
| 89 UdevCallback, |
| 90 &manager, |
| 91 NULL); |
31 g_main_loop_run(loop); | 92 g_main_loop_run(loop); |
32 | 93 |
| 94 LOG(INFO) << "Cleaining up and exiting"; |
| 95 g_main_loop_unref(loop); |
| 96 delete server; |
| 97 delete dispatcher; |
| 98 |
33 return 0; | 99 return 0; |
34 } | 100 } |
OLD | NEW |