aboutsummaryrefslogtreecommitdiff
path: root/host/libs/process_monitor/process_monitor.cc
blob: a70021d9cd46495c5fdc13b96a7b22c5557c29a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "host/libs/process_monitor/process_monitor.h"

#ifdef __linux__
#include <sys/prctl.h>
#endif

#include <sys/types.h>
#include <sys/wait.h>

#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>

#include <algorithm>
#include <atomic>
#include <cstdint>
#include <future>
#include <memory>
#include <string>
#include <thread>
#include <vector>

#include <android-base/file.h>
#include <android-base/logging.h>

#include "common/libs/fs/shared_buf.h"
#include "common/libs/fs/shared_select.h"
#include "common/libs/utils/result.h"
#include "common/libs/utils/subprocess.h"
#include "host/libs/command_util/runner/defs.h"
#include "host/libs/command_util/runner/proto_utils.h"
#include "host/libs/command_util/util.h"
#include "host/libs/config/cuttlefish_config.h"
#include "host/libs/config/known_paths.h"
#include "host/libs/process_monitor/process_monitor_channel.h"

namespace cuttlefish {

namespace {

void LogSubprocessExit(const std::string& name, pid_t pid, int wstatus) {
  LOG(INFO) << "Detected unexpected exit of monitored subprocess " << name;
  if (WIFEXITED(wstatus)) {
    LOG(INFO) << "Subprocess " << name << " (" << pid
              << ") has exited with exit code " << WEXITSTATUS(wstatus);
  } else if (WIFSIGNALED(wstatus)) {
    int sig_num = WTERMSIG(wstatus);
    LOG(ERROR) << "Subprocess " << name << " (" << pid
               << ") was interrupted by a signal '" << strsignal(sig_num)
               << "' (" << sig_num << ")";
  } else {
    LOG(INFO) << "subprocess " << name << " (" << pid
              << ") has exited for unknown reasons";
  }
}

void LogSubprocessExit(const std::string& name, const siginfo_t& infop) {
  LOG(INFO) << "Detected unexpected exit of monitored subprocess " << name;
  if (infop.si_code == CLD_EXITED) {
    LOG(INFO) << "Subprocess " << name << " (" << infop.si_pid
              << ") has exited with exit code " << infop.si_status;
  } else if (infop.si_code == CLD_KILLED) {
    LOG(ERROR) << "Subprocess " << name << " (" << infop.si_pid
               << ") was interrupted by a signal '"
               << strsignal(infop.si_status) << "' (" << infop.si_status << ")";
  } else {
    LOG(INFO) << "subprocess " << name << " (" << infop.si_pid
              << ") has exited for unknown reasons (code = " << infop.si_code
              << ", status = " << infop.si_status << ")";
  }
}

Result<void> StartSubprocesses(std::vector<MonitorEntry>& entries) {
  LOG(DEBUG) << "Starting monitored subprocesses";
  for (auto& monitored : entries) {
    LOG(INFO) << monitored.cmd->GetShortName();
    auto options = SubprocessOptions().InGroup(true);
    monitored.proc.reset(new Subprocess(monitored.cmd->Start(options)));
    CF_EXPECT(monitored.proc->Started(), "Failed to start subprocess");
  }
  return {};
}

Result<void> MonitorLoop(const std::atomic_bool& running,
                         std::mutex& properties_mutex,
                         const bool restart_subprocesses,
                         std::vector<MonitorEntry>& monitored) {
  while (running.load()) {
    int wstatus;
    pid_t pid = wait(&wstatus);
    int error_num = errno;
    CF_EXPECT(pid != -1, "Wait failed: " << strerror(error_num));
    if (!WIFSIGNALED(wstatus) && !WIFEXITED(wstatus)) {
      LOG(DEBUG) << "Unexpected status from wait: " << wstatus << " for pid "
                 << pid;
      continue;
    }
    if (!running.load()) {  // Avoid extra restarts near the end
      break;
    }
    auto matches = [pid](const auto& it) { return it.proc->pid() == pid; };
    std::unique_lock lock(properties_mutex);
    auto it = std::find_if(monitored.begin(), monitored.end(), matches);
    if (it == monitored.end()) {
      LogSubprocessExit("(unknown)", pid, wstatus);
    } else {
      LogSubprocessExit(it->cmd->GetShortName(), it->proc->pid(), wstatus);
      if (restart_subprocesses) {
        auto options = SubprocessOptions().InGroup(true);
        // in the future, cmd->Start might not run exec()
        it->proc.reset(new Subprocess(it->cmd->Start(options)));
      } else {
        bool is_critical = it->is_critical;
        monitored.erase(it);
        if (running.load() && is_critical) {
          LOG(ERROR) << "Stopping all monitored processes due to unexpected "
                        "exit of critical process";
          Command stop_cmd(StopCvdBinary());
          stop_cmd.Start();
        }
      }
    }
  }
  return {};
}

Result<void> StopSubprocesses(std::vector<MonitorEntry>& monitored) {
  LOG(DEBUG) << "Stopping monitored subprocesses";
  auto stop = [](const auto& it) {
    auto stop_result = it.proc->Stop();
    if (stop_result == StopperResult::kStopFailure) {
      LOG(WARNING) << "Error in stopping \"" << it.cmd->GetShortName() << "\"";
      return false;
    }
    siginfo_t infop;
    auto success = it.proc->Wait(&infop, WEXITED);
    if (success < 0) {
      LOG(WARNING) << "Failed to wait for process " << it.cmd->GetShortName();
      return false;
    }
    if (stop_result == StopperResult::kStopCrash) {
      LogSubprocessExit(it.cmd->GetShortName(), infop);
    }
    return true;
  };
  // Processes were started in the order they appear in the vector, stop them in
  // reverse order for symmetry.
  size_t stopped = std::count_if(monitored.rbegin(), monitored.rend(), stop);
  CF_EXPECT(stopped == monitored.size(), "Didn't stop all subprocesses");
  return {};
}

Result<void> SuspendResumeImpl(std::vector<MonitorEntry>& monitor_entries,
                               std::mutex& properties_mutex,
                               const SharedFD& channel_to_secure_env,
                               const bool is_suspend,
                               SharedFD child_monitor_socket) {
  std::lock_guard lock(properties_mutex);
  auto secure_env_itr = std::find_if(
      monitor_entries.begin(), monitor_entries.end(), [](MonitorEntry& entry) {
        auto prog_name = android::base::Basename(entry.cmd->Executable());
        return (prog_name == "secure_env");
      });
  if (secure_env_itr != monitor_entries.end()) {
    CF_EXPECT(channel_to_secure_env->IsOpen(),
              "channel to secure_env is not open.");
    const ExtendedActionType extended_type =
        (is_suspend ? ExtendedActionType::kSuspend
                    : ExtendedActionType::kResume);
    auto serialized_request = CF_EXPECT(
        (is_suspend ? SerializeSuspendRequest() : SerializeResumeRequest()),
        "Failed to serialize request.");
    CF_EXPECT(WriteLauncherActionWithData(
        channel_to_secure_env, LauncherAction::kExtended, extended_type,
        std::move(serialized_request)));
    const std::string failed_command = (is_suspend ? "suspend" : "resume");
    CF_EXPECT(ReadLauncherResponse(channel_to_secure_env),
              "secure_env refused to " + failed_command);
  }

  for (const auto& entry : monitor_entries) {
    if (!entry.cmd) {
      LOG(ERROR) << "Monitor Entry has a nullptr for cmd.";
      continue;
    }
    if (!entry.proc) {
      LOG(ERROR) << "Monitor Entry has a nullptr for proc.";
      continue;
    }
    auto prog_name = android::base::Basename(entry.cmd->Executable());
    auto process_restart_bin =
        android::base::Basename(ProcessRestarterBinary());
    if (prog_name == "log_tee") {
      // Don't stop log_tee, we want to continue processing logs while
      // suspended.
      continue;
    }
    if (prog_name == "wmediumd") {
      // wmediumd should be running while openWRT is saved using the
      // guest snapshot logic
      continue;
    }
    if (prog_name == "secure_env") {
      // secure_env was handled above in a customized way
      continue;
    }

    if (process_restart_bin == prog_name) {
      if (is_suspend) {
        CF_EXPECT(entry.proc->SendSignal(SIGTSTP));
      } else {
        CF_EXPECT(entry.proc->SendSignal(SIGCONT));
      }
      continue;
    }
    if (is_suspend) {
      CF_EXPECT(entry.proc->SendSignalToGroup(SIGTSTP));
    } else {
      CF_EXPECT(entry.proc->SendSignalToGroup(SIGCONT));
    }
  }
  using process_monitor_impl::ChildToParentResponse;
  using process_monitor_impl::ChildToParentResponseType;
  ChildToParentResponse response(ChildToParentResponseType::kSuccess);
  CF_EXPECT(response.Write(child_monitor_socket));
  return {};
}

}  // namespace

Result<void> ProcessMonitor::ReadMonitorSocketLoop(std::atomic_bool& running) {
  LOG(DEBUG) << "Waiting for a `stop` message from the parent";
  while (running.load()) {
    using process_monitor_impl::ParentToChildMessage;
    auto message = CF_EXPECT(ParentToChildMessage::Read(child_monitor_socket_));
    if (message.Stop()) {
      running.store(false);
      // Wake up the wait() loop by giving it an exited child process
      if (fork() == 0) {
        std::exit(0);
      }
      // will break the for-loop as running is now false
      continue;
    }
    using process_monitor_impl::ParentToChildMessageType;
    if (message.Type() == ParentToChildMessageType::kHostSuspend) {
      CF_EXPECT(SuspendHostProcessesImpl());
      continue;
    }
    if (message.Type() == ParentToChildMessageType::kHostResume) {
      CF_EXPECT(ResumeHostProcessesImpl());
      continue;
    }
  }
  return {};
}

Result<void> ProcessMonitor::SuspendHostProcessesImpl() {
  CF_EXPECT(SuspendResumeImpl(properties_.entries_, properties_mutex_,
                              channel_to_secure_env_, /* is_suspend */ true,
                              child_monitor_socket_),
            "Failed suspend");
  return {};
}

Result<void> ProcessMonitor::ResumeHostProcessesImpl() {
  CF_EXPECT(SuspendResumeImpl(properties_.entries_, properties_mutex_,
                              channel_to_secure_env_, /* is_suspend */ false,
                              child_monitor_socket_),
            "Failed resume");
  return {};
}

ProcessMonitor::Properties& ProcessMonitor::Properties::RestartSubprocesses(
    bool r) & {
  restart_subprocesses_ = r;
  return *this;
}

ProcessMonitor::Properties ProcessMonitor::Properties::RestartSubprocesses(
    bool r) && {
  return std::move(RestartSubprocesses(r));
}

ProcessMonitor::Properties& ProcessMonitor::Properties::AddCommand(
    MonitorCommand cmd) & {
  entries_.emplace_back(std::move(cmd.command), cmd.is_critical);
  return *this;
}

ProcessMonitor::Properties ProcessMonitor::Properties::AddCommand(
    MonitorCommand cmd) && {
  return std::move(AddCommand(std::move(cmd)));
}

ProcessMonitor::ProcessMonitor(ProcessMonitor::Properties&& properties,
                               const SharedFD& secure_env_fd)
    : properties_(std::move(properties)),
      channel_to_secure_env_(secure_env_fd),
      monitor_(-1) {}

Result<void> ProcessMonitor::StopMonitoredProcesses() {
  CF_EXPECT(monitor_ != -1, "The monitor process has already exited.");
  CF_EXPECT(parent_monitor_socket_->IsOpen(),
            "The monitor socket is already closed");
  using process_monitor_impl::ParentToChildMessage;
  using process_monitor_impl::ParentToChildMessageType;
  ParentToChildMessage message(ParentToChildMessageType::kStop);
  CF_EXPECT(message.Write(parent_monitor_socket_));

  pid_t last_monitor = monitor_;
  monitor_ = -1;
  parent_monitor_socket_->Close();
  int wstatus;
  CF_EXPECT(waitpid(last_monitor, &wstatus, 0) == last_monitor,
            "Failed to wait for monitor process");
  CF_EXPECT(!WIFSIGNALED(wstatus), "Monitor process exited due to a signal");
  CF_EXPECT(WIFEXITED(wstatus), "Monitor process exited for unknown reasons");
  CF_EXPECT(WEXITSTATUS(wstatus) == 0,
            "Monitor process exited with code " << WEXITSTATUS(wstatus));
  return {};
}

Result<void> ProcessMonitor::SuspendMonitoredProcesses() {
  CF_EXPECT(monitor_ != -1, "The monitor process has already exited.");
  CF_EXPECT(parent_monitor_socket_->IsOpen(),
            "The monitor socket is already closed");
  using process_monitor_impl::ParentToChildMessage;
  using process_monitor_impl::ParentToChildMessageType;
  ParentToChildMessage message(ParentToChildMessageType::kHostSuspend);
  CF_EXPECT(message.Write(parent_monitor_socket_));
  using process_monitor_impl::ChildToParentResponse;
  auto response =
      CF_EXPECT(ChildToParentResponse::Read(parent_monitor_socket_));
  CF_EXPECT(response.Success(),
            "On kHostSuspend, the child run_cvd returned kFailure.");
  return {};
}

Result<void> ProcessMonitor::ResumeMonitoredProcesses() {
  CF_EXPECT(monitor_ != -1, "The monitor process has already exited.");
  CF_EXPECT(parent_monitor_socket_->IsOpen(),
            "The monitor socket is already closed");
  using process_monitor_impl::ParentToChildMessage;
  using process_monitor_impl::ParentToChildMessageType;
  ParentToChildMessage message(ParentToChildMessageType::kHostResume);
  CF_EXPECT(message.Write(parent_monitor_socket_));
  using process_monitor_impl::ChildToParentResponse;
  auto response =
      CF_EXPECT(ChildToParentResponse::Read(parent_monitor_socket_));
  CF_EXPECT(response.Success(),
            "On kHostResume, the child run_cvd returned kFailure.");
  return {};
}

Result<void> ProcessMonitor::StartAndMonitorProcesses() {
  CF_EXPECT(monitor_ == -1, "The monitor process was already started");
  CF_EXPECT(!parent_monitor_socket_->IsOpen(),
            "Parent monitor socket was already opened");
  SharedFD parent_sock;
  SharedFD child_sock;
  SharedFD::SocketPair(AF_UNIX, SOCK_STREAM, 0, &parent_sock, &child_sock);
  monitor_ = fork();
  if (monitor_ == 0) {
    child_monitor_socket_ = std::move(child_sock);
    parent_sock->Close();
    auto monitor_result = MonitorRoutine();
    if (!monitor_result.ok()) {
      LOG(ERROR) << "Monitoring processes failed:\n"
                 << monitor_result.error().FormatForEnv();
    }
    std::exit(monitor_result.ok() ? 0 : 1);
  } else {
    parent_monitor_socket_ = std::move(parent_sock);
    child_sock->Close();
    return {};
  }
}

Result<void> ProcessMonitor::MonitorRoutine() {
#ifdef __linux__
  // Make this process a subreaper to reliably catch subprocess exits.
  // See https://man7.org/linux/man-pages/man2/prctl.2.html
  prctl(PR_SET_CHILD_SUBREAPER, 1);
  prctl(PR_SET_PDEATHSIG, SIGHUP);  // Die when parent dies
#endif

  LOG(DEBUG) << "Monitoring subprocesses";
  CF_EXPECT(StartSubprocesses(properties_.entries_));

  std::atomic_bool running(true);

  auto read_monitor_socket_loop =
      [this](std::atomic_bool& running) -> Result<void> {
    CF_EXPECT(this->ReadMonitorSocketLoop(running));
    return {};
  };
  auto parent_comms = std::async(std::launch::async, read_monitor_socket_loop,
                                 std::ref(running));

  CF_EXPECT(MonitorLoop(running, properties_mutex_,
                        properties_.restart_subprocesses_,
                        properties_.entries_));
  CF_EXPECT(parent_comms.get(), "Should have exited if monitoring stopped");

  CF_EXPECT(StopSubprocesses(properties_.entries_));
  LOG(DEBUG) << "Done monitoring subprocesses";
  return {};
}

}  // namespace cuttlefish