Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 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> |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 22 | |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 23 | #include <android-base/strings.h> |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 24 | #include <gflags/gflags.h> |
| 25 | #include <glog/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> |
Jorge E. Moreira | 66e9a1a | 2018-07-26 17:17:47 -0700 | [diff] [blame] | 30 | #include "host/commands/kernel_log_monitor/kernel_log_server.h" |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 31 | |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 32 | DEFINE_int32(log_pipe_fd, -1, |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 33 | "A file descriptor representing a (UNIX) socket from which to " |
| 34 | "read the logs. If -1 is given the socket is created according to " |
| 35 | "the instance configuration"); |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 36 | DEFINE_string(subscriber_fds, "", |
| 37 | "A comma separated list of file descriptors (most likely pipes) to" |
| 38 | " send boot events to."); |
| 39 | |
| 40 | std::vector<cvd::SharedFD> SubscribersFromCmdline() { |
| 41 | // Validate the parameter |
| 42 | std::string fd_list = FLAGS_subscriber_fds; |
| 43 | for (auto c: fd_list) { |
| 44 | if (c != ',' && (c < '0' || c > '9')) { |
| 45 | LOG(ERROR) << "Invalid file descriptor list: " << fd_list; |
| 46 | std::exit(1); |
| 47 | } |
| 48 | } |
| 49 | |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 50 | auto fds = android::base::Split(FLAGS_subscriber_fds, ","); |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 51 | std::vector<cvd::SharedFD> shared_fds; |
| 52 | for (auto& fd_str: fds) { |
| 53 | auto fd = std::stoi(fd_str); |
| 54 | auto shared_fd = cvd::SharedFD::Dup(fd); |
| 55 | close(fd); |
| 56 | shared_fds.push_back(shared_fd); |
| 57 | } |
| 58 | |
| 59 | return shared_fds; |
| 60 | } |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 61 | |
| 62 | int main(int argc, char** argv) { |
| 63 | ::android::base::InitLogging(argv, android::base::StderrLogger); |
| 64 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 65 | |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 66 | auto subscriber_fds = SubscribersFromCmdline(); |
| 67 | |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 68 | // Disable default handling of SIGPIPE |
| 69 | struct sigaction new_action { |
| 70 | }, old_action{}; |
| 71 | new_action.sa_handler = SIG_IGN; |
| 72 | sigaction(SIGPIPE, &new_action, &old_action); |
| 73 | |
| 74 | auto config = vsoc::CuttlefishConfig::Get(); |
| 75 | if (!config) { |
| 76 | LOG(ERROR) << "Unable to get config object"; |
| 77 | return 1; |
| 78 | } |
Cody Schuffelen | eb242c4 | 2019-12-13 15:16:50 -0800 | [diff] [blame] | 79 | auto instance = config->ForDefaultInstance(); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 80 | |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 81 | cvd::SharedFD pipe; |
| 82 | if (FLAGS_log_pipe_fd < 0) { |
Cody Schuffelen | eb242c4 | 2019-12-13 15:16:50 -0800 | [diff] [blame] | 83 | auto log_name = instance.kernel_log_pipe_name(); |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 84 | pipe = cvd::SharedFD::Open(log_name.c_str(), O_RDONLY); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 85 | } else { |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 86 | pipe = cvd::SharedFD::Dup(FLAGS_log_pipe_fd); |
| 87 | close(FLAGS_log_pipe_fd); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 90 | if (!pipe->IsOpen()) { |
| 91 | LOG(ERROR) << "Error opening log pipe: " << pipe->StrError(); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 92 | return 2; |
| 93 | } |
| 94 | |
Cody Schuffelen | eb242c4 | 2019-12-13 15:16:50 -0800 | [diff] [blame] | 95 | monitor::KernelLogServer klog{pipe, instance.PerInstancePath("kernel.log"), |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 96 | config->deprecated_boot_completed()}; |
| 97 | |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 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)); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 102 | if (retval < 0) { |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 103 | if (subscriber_fd->GetErrno() != EPIPE) { |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 104 | LOG(ERROR) << "Error while writing to pipe: " |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 105 | << subscriber_fd->StrError(); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 106 | } |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 107 | subscriber_fd->Close(); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 108 | return monitor::SubscriptionAction::CancelSubscription; |
| 109 | } |
| 110 | return monitor::SubscriptionAction::ContinueSubscription; |
| 111 | }); |
| 112 | } else { |
Jorge E. Moreira | a57b7ba | 2019-05-02 17:01:44 -0700 | [diff] [blame] | 113 | LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError(); |
Jorge E. Moreira | 81afca1 | 2018-07-26 16:48:49 -0700 | [diff] [blame] | 114 | // Don't return here, we still need to write the logs to a file |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (;;) { |
| 119 | cvd::SharedFDSet fd_read; |
| 120 | fd_read.Zero(); |
| 121 | |
| 122 | klog.BeforeSelect(&fd_read); |
| 123 | |
| 124 | int ret = cvd::Select(&fd_read, nullptr, nullptr, nullptr); |
| 125 | if (ret <= 0) continue; |
| 126 | |
| 127 | klog.AfterSelect(fd_read); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |