1 /*
2 * Copyright (C) 2023 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 "host/commands/process_restarter/parser.h"
18
19 #include <iostream>
20 #include <string>
21 #include <vector>
22
23 #include "common/libs/utils/flag_parser.h"
24 #include "common/libs/utils/result.h"
25
26 namespace cuttlefish {
27
28 static constexpr char kIgnoreSigtstpHelp[] =
29 "Ignore the sigtstp. This is useful when the managed processes are crosvm"
30 "Crosvm has its own logic to be suspended.";
31 static constexpr char kWhenDumpedHelp[] = "restart when the process crashed";
32 static constexpr char kWhenKilledHelp[] = "restart when the process was killed";
33 static constexpr char kWhenExitedWithFailureHelp[] =
34 "restart when the process exited with a code !=0";
35 static constexpr char kWhenExitedWithCodeHelp[] =
36 "restart when the process exited with a specific code";
37 static constexpr char kHelp[] = R"(
38 This program launches and automatically restarts the input command
39 following the selected restart conditions.
40 Example usage:
41
42 ./process_restarter -when_dumped -- my_program --arg1 --arg2)";
43
ConsumeAndParse(std::vector<std::string> & args)44 Result<Parser> Parser::ConsumeAndParse(std::vector<std::string>& args) {
45 Parser parser;
46 std::vector<Flag> flags;
47 flags.push_back(GflagsCompatFlag("ignore_sigtstp", parser.ignore_sigtstp)
48 .Help(kIgnoreSigtstpHelp));
49 flags.push_back(GflagsCompatFlag("when_dumped", parser.when_dumped)
50 .Help(kWhenDumpedHelp));
51 flags.push_back(GflagsCompatFlag("when_killed", parser.when_killed)
52 .Help(kWhenKilledHelp));
53 flags.push_back(GflagsCompatFlag("when_exited_with_failure",
54 parser.when_exited_with_failure)
55 .Help(kWhenExitedWithFailureHelp));
56 flags.push_back(
57 GflagsCompatFlag("when_exited_with_code", parser.when_exited_with_code)
58 .Help(kWhenExitedWithCodeHelp));
59 flags.push_back(
60 GflagsCompatFlag("first_time_argument", parser.first_time_argument)
61 .Help(
62 "add an argument to the first invocation, but not to restarts"));
63 flags.push_back(HelpFlag(flags, kHelp));
64 bool matched_help_xml = false;
65 flags.push_back(HelpXmlFlag(flags, std::cout, matched_help_xml, ""));
66 flags.push_back(UnexpectedArgumentGuard());
67 constexpr const bool recognize_end_of_option_mark = true;
68 CF_EXPECT(ConsumeFlags(flags, args, recognize_end_of_option_mark));
69 return parser;
70 }
71
72 } // namespace cuttlefish
73