1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <signal.h>
18 #include <unistd.h>
19
20 #include <string>
21 #include <vector>
22
23 #include <android-base/logging.h>
24 #include <android-base/strings.h>
25 #include <gflags/gflags.h>
26 #include <json/json.h>
27
28 #include <common/libs/fs/shared_fd.h>
29 #include <common/libs/fs/shared_select.h>
30 #include <host/libs/config/cuttlefish_config.h>
31 #include <host/libs/config/logging.h>
32 #include "host/commands/kernel_log_monitor/kernel_log_server.h"
33 #include "host/commands/kernel_log_monitor/utils.h"
34
35 DEFINE_int32(log_pipe_fd, -1,
36 "A file descriptor representing a (UNIX) socket from which to "
37 "read the logs. If -1 is given the socket is created according to "
38 "the instance configuration");
39 DEFINE_string(subscriber_fds, "",
40 "A comma separated list of file descriptors (most likely pipes) to"
41 " send kernel log events to.");
42
43 namespace cuttlefish::monitor {
44 namespace {
45
SubscribersFromCmdline()46 std::vector<SharedFD> SubscribersFromCmdline() {
47 // Validate the parameter
48 std::string fd_list = FLAGS_subscriber_fds;
49 for (auto c: fd_list) {
50 if (c != ',' && (c < '0' || c > '9')) {
51 LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
52 std::exit(1);
53 }
54 }
55
56 auto fds = android::base::Split(FLAGS_subscriber_fds, ",");
57 std::vector<SharedFD> shared_fds;
58 for (auto& fd_str: fds) {
59 auto fd = std::stoi(fd_str);
60 auto shared_fd = SharedFD::Dup(fd);
61 close(fd);
62 shared_fds.push_back(shared_fd);
63 }
64
65 return shared_fds;
66 }
67
KernelLogMonitorMain(int argc,char ** argv)68 int KernelLogMonitorMain(int argc, char** argv) {
69 DefaultSubprocessLogging(argv);
70 google::ParseCommandLineFlags(&argc, &argv, true);
71
72 auto config = CuttlefishConfig::Get();
73
74 CHECK(config) << "Could not open cuttlefish config";
75
76 auto instance = config->ForDefaultInstance();
77
78 auto subscriber_fds = SubscribersFromCmdline();
79
80 // Disable default handling of SIGPIPE
81 struct sigaction new_action {
82 }, old_action{};
83 new_action.sa_handler = SIG_IGN;
84 sigaction(SIGPIPE, &new_action, &old_action);
85
86 SharedFD pipe;
87 if (FLAGS_log_pipe_fd < 0) {
88 auto log_name = instance.kernel_log_pipe_name();
89 pipe = SharedFD::Open(log_name.c_str(), O_RDONLY);
90 } else {
91 pipe = SharedFD::Dup(FLAGS_log_pipe_fd);
92 close(FLAGS_log_pipe_fd);
93 }
94
95 if (!pipe->IsOpen()) {
96 LOG(ERROR) << "Error opening log pipe: " << pipe->StrError();
97 return 2;
98 }
99
100 KernelLogServer klog{pipe, instance.PerInstanceLogPath("kernel.log")};
101
102 for (auto subscriber_fd: subscriber_fds) {
103 if (subscriber_fd->IsOpen()) {
104 klog.SubscribeToEvents([subscriber_fd](Json::Value message) {
105 if (!WriteEvent(subscriber_fd, message)) {
106 if (subscriber_fd->GetErrno() != EPIPE) {
107 LOG(ERROR) << "Error while writing to pipe: "
108 << subscriber_fd->StrError();
109 }
110 subscriber_fd->Close();
111 return SubscriptionAction::CancelSubscription;
112 }
113 return SubscriptionAction::ContinueSubscription;
114 });
115 } else {
116 LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError();
117 // Don't return here, we still need to write the logs to a file
118 }
119 }
120
121 for (;;) {
122 SharedFDSet fd_read;
123 fd_read.Zero();
124
125 klog.BeforeSelect(&fd_read);
126
127 int ret = Select(&fd_read, nullptr, nullptr, nullptr);
128 if (ret <= 0) {
129 continue;
130 }
131
132 klog.AfterSelect(fd_read);
133 }
134
135 return 0;
136 }
137
138 } // namespace
139 } // namespace cuttlefish::monitor
140
main(int argc,char ** argv)141 int main(int argc, char** argv) {
142 return cuttlefish::monitor::KernelLogMonitorMain(argc, argv);
143 }
144