Android-cuttlefish cvd tool
subprocess.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 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#pragma once
17
18#include <sys/types.h>
19#include <sys/wait.h>
20
21#include <atomic>
22#include <functional>
23
25
26namespace cuttlefish {
27
28enum class StopperResult {
29 kFailure, /* Failed to stop the subprocess. */
30 kCrash, /* Attempted to stop the subprocess cleanly, but that failed. */
31 kSuccess, /* The subprocess exited in the expected way. */
32};
33
34class Subprocess;
36// Kills a process by sending it the SIGKILL signal.
38/* Creates a `SubprocessStopper` that first tries `nice_stopper` then falls back
39 * to `KillSubprocess` if that fails. */
42
43// Keeps track of a running (sub)process. Allows to wait for its completion.
44// It's an error to wait twice for the same subprocess.
46 public:
48 : pid_(pid), started_(pid > 0), stopper_(stopper) {}
49 // The default implementation won't do because we need to reset the pid of the
50 // moved object.
52 ~Subprocess() = default;
54 // Waits for the subprocess to complete. Returns zero if completed
55 // successfully, non-zero otherwise.
56 int Wait();
57 // Same as waitid(2)
59 // Whether the command started successfully. It only says whether the call to
60 // fork() succeeded or not, it says nothing about exec or successful
61 // completion of the command, that's what Wait is for.
62 bool Started() const { return started_; }
63 pid_t pid() const { return pid_; }
64 StopperResult Stop() { return stopper_(this); }
65
66 Result<void> SendSignal(int signal);
68
69 private:
70 // Copy is disabled to avoid waiting twice for the same pid (the first wait
71 // frees the pid, which allows the kernel to reuse it so we may end up waiting
72 // for the wrong process)
73 Subprocess(const Subprocess&) = delete;
74 Subprocess& operator=(const Subprocess&) = delete;
75 std::atomic<pid_t> pid_ = -1;
76 bool started_ = false;
78};
79
80} // namespace cuttlefish
Definition: subprocess.h:45
bool started_
Definition: subprocess.h:76
std::atomic< pid_t > pid_
Definition: subprocess.h:75
SubprocessStopper stopper_
Definition: subprocess.h:77
Subprocess & operator=(const Subprocess &)=delete
Result< void > SendSignalToGroup(int signal)
Definition: subprocess.cc:132
Result< void > SendSignal(int signal)
Definition: subprocess.cc:127
Subprocess(pid_t pid, SubprocessStopper stopper=KillSubprocess)
Definition: subprocess.h:47
Subprocess & operator=(Subprocess &&)
Definition: subprocess.cc:47
Subprocess(const Subprocess &)=delete
bool Started() const
Definition: subprocess.h:62
int Wait()
Definition: subprocess.cc:57
StopperResult Stop()
Definition: subprocess.h:64
pid_t pid() const
Definition: subprocess.h:63
int options
Definition: f2fscrypt.c:124
Definition: alloc_driver.h:20
StopperResult KillSubprocess(Subprocess *subprocess)
Definition: subprocess.cc:137
StopperResult
Definition: subprocess.h:28
std::function< StopperResult(Subprocess *)> SubprocessStopper
Definition: subprocess.h:35
SubprocessStopper KillSubprocessFallback(std::function< StopperResult()> nice)
Definition: subprocess.cc:160
@ kSuccess
Definition: channel_monitor.h:30
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30