• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SubscribersFromCmdline()43 std::vector<cuttlefish::SharedFD> SubscribersFromCmdline() {
44   // Validate the parameter
45   std::string fd_list = FLAGS_subscriber_fds;
46   for (auto c: fd_list) {
47     if (c != ',' && (c < '0' || c > '9')) {
48       LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
49       std::exit(1);
50     }
51   }
52 
53   auto fds = android::base::Split(FLAGS_subscriber_fds, ",");
54   std::vector<cuttlefish::SharedFD> shared_fds;
55   for (auto& fd_str: fds) {
56     auto fd = std::stoi(fd_str);
57     auto shared_fd = cuttlefish::SharedFD::Dup(fd);
58     close(fd);
59     shared_fds.push_back(shared_fd);
60   }
61 
62   return shared_fds;
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66   cuttlefish::DefaultSubprocessLogging(argv);
67   google::ParseCommandLineFlags(&argc, &argv, true);
68 
69   auto config = cuttlefish::CuttlefishConfig::Get();
70 
71   CHECK(config) << "Could not open cuttlefish config";
72 
73   auto instance = config->ForDefaultInstance();
74 
75   auto subscriber_fds = SubscribersFromCmdline();
76 
77   // Disable default handling of SIGPIPE
78   struct sigaction new_action {
79   }, old_action{};
80   new_action.sa_handler = SIG_IGN;
81   sigaction(SIGPIPE, &new_action, &old_action);
82 
83   cuttlefish::SharedFD pipe;
84   if (FLAGS_log_pipe_fd < 0) {
85     auto log_name = instance.kernel_log_pipe_name();
86     pipe = cuttlefish::SharedFD::Open(log_name.c_str(), O_RDONLY);
87   } else {
88     pipe = cuttlefish::SharedFD::Dup(FLAGS_log_pipe_fd);
89     close(FLAGS_log_pipe_fd);
90   }
91 
92   if (!pipe->IsOpen()) {
93     LOG(ERROR) << "Error opening log pipe: " << pipe->StrError();
94     return 2;
95   }
96 
97   monitor::KernelLogServer klog{pipe, instance.PerInstancePath("kernel.log"),
98                                 config->deprecated_boot_completed()};
99 
100   for (auto subscriber_fd: subscriber_fds) {
101     if (subscriber_fd->IsOpen()) {
102       klog.SubscribeToEvents([subscriber_fd](Json::Value message) {
103         if (!monitor::WriteEvent(subscriber_fd, message)) {
104           if (subscriber_fd->GetErrno() != EPIPE) {
105             LOG(ERROR) << "Error while writing to pipe: "
106                        << subscriber_fd->StrError();
107           }
108           subscriber_fd->Close();
109           return monitor::SubscriptionAction::CancelSubscription;
110         }
111         return monitor::SubscriptionAction::ContinueSubscription;
112       });
113     } else {
114       LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError();
115       // Don't return here, we still need to write the logs to a file
116     }
117   }
118 
119   for (;;) {
120     cuttlefish::SharedFDSet fd_read;
121     fd_read.Zero();
122 
123     klog.BeforeSelect(&fd_read);
124 
125     int ret = cuttlefish::Select(&fd_read, nullptr, nullptr, nullptr);
126     if (ret <= 0) continue;
127 
128     klog.AfterSelect(fd_read);
129   }
130 
131   return 0;
132 }
133