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/strings.h>
24 #include <gflags/gflags.h>
25 #include <android-base/logging.h>
26
27 #include <common/libs/fs/shared_fd.h>
28 #include <common/libs/fs/shared_select.h>
29 #include <host/libs/config/cuttlefish_config.h>
30 #include <host/libs/config/logging.h>
31 #include "host/commands/kernel_log_monitor/kernel_log_server.h"
32
33 DEFINE_int32(log_pipe_fd, -1,
34 "A file descriptor representing a (UNIX) socket from which to "
35 "read the logs. If -1 is given the socket is created according to "
36 "the instance configuration");
37 DEFINE_string(subscriber_fds, "",
38 "A comma separated list of file descriptors (most likely pipes) to"
39 " send boot events to.");
40
SubscribersFromCmdline()41 std::vector<cuttlefish::SharedFD> SubscribersFromCmdline() {
42 // Validate the parameter
43 std::string fd_list = FLAGS_subscriber_fds;
44 for (auto c: fd_list) {
45 if (c != ',' && (c < '0' || c > '9')) {
46 LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
47 std::exit(1);
48 }
49 }
50
51 auto fds = android::base::Split(FLAGS_subscriber_fds, ",");
52 std::vector<cuttlefish::SharedFD> shared_fds;
53 for (auto& fd_str: fds) {
54 auto fd = std::stoi(fd_str);
55 auto shared_fd = cuttlefish::SharedFD::Dup(fd);
56 close(fd);
57 shared_fds.push_back(shared_fd);
58 }
59
60 return shared_fds;
61 }
62
main(int argc,char ** argv)63 int main(int argc, char** argv) {
64 cuttlefish::DefaultSubprocessLogging(argv);
65 google::ParseCommandLineFlags(&argc, &argv, true);
66
67 auto config = cuttlefish::CuttlefishConfig::Get();
68
69 CHECK(config) << "Could not open cuttlefish config";
70
71 auto instance = config->ForDefaultInstance();
72
73 auto subscriber_fds = SubscribersFromCmdline();
74
75 // Disable default handling of SIGPIPE
76 struct sigaction new_action {
77 }, old_action{};
78 new_action.sa_handler = SIG_IGN;
79 sigaction(SIGPIPE, &new_action, &old_action);
80
81 cuttlefish::SharedFD pipe;
82 if (FLAGS_log_pipe_fd < 0) {
83 auto log_name = instance.kernel_log_pipe_name();
84 pipe = cuttlefish::SharedFD::Open(log_name.c_str(), O_RDONLY);
85 } else {
86 pipe = cuttlefish::SharedFD::Dup(FLAGS_log_pipe_fd);
87 close(FLAGS_log_pipe_fd);
88 }
89
90 if (!pipe->IsOpen()) {
91 LOG(ERROR) << "Error opening log pipe: " << pipe->StrError();
92 return 2;
93 }
94
95 monitor::KernelLogServer klog{pipe, instance.PerInstancePath("kernel.log"),
96 config->deprecated_boot_completed()};
97
98 for (auto subscriber_fd: subscriber_fds) {
99 if (subscriber_fd->IsOpen()) {
100 klog.SubscribeToBootEvents([subscriber_fd](monitor::BootEvent evt) {
101 int retval = subscriber_fd->Write(&evt, sizeof(evt));
102 if (retval < 0) {
103 if (subscriber_fd->GetErrno() != EPIPE) {
104 LOG(ERROR) << "Error while writing to pipe: "
105 << subscriber_fd->StrError();
106 }
107 subscriber_fd->Close();
108 return monitor::SubscriptionAction::CancelSubscription;
109 }
110 return monitor::SubscriptionAction::ContinueSubscription;
111 });
112 } else {
113 LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError();
114 // Don't return here, we still need to write the logs to a file
115 }
116 }
117
118 for (;;) {
119 cuttlefish::SharedFDSet fd_read;
120 fd_read.Zero();
121
122 klog.BeforeSelect(&fd_read);
123
124 int ret = cuttlefish::Select(&fd_read, nullptr, nullptr, nullptr);
125 if (ret <= 0) continue;
126
127 klog.AfterSelect(fd_read);
128 }
129
130 return 0;
131 }
132