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