1 | // |
2 | // pkill.cpp |
3 | // |
4 | // Process killer utility application. |
5 | // |
6 | // Copyright (c) 2010, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | |
13 | #include "Poco/Util/Application.h" |
14 | #include "Poco/Util/Option.h" |
15 | #include "Poco/Util/OptionSet.h" |
16 | #include "Poco/Util/HelpFormatter.h" |
17 | #include "Poco/Process.h" |
18 | #include "Poco/NumberParser.h" |
19 | #include <iostream> |
20 | |
21 | |
22 | using Poco::Util::Application; |
23 | using Poco::Util::Option; |
24 | using Poco::Util::OptionSet; |
25 | using Poco::Util::HelpFormatter; |
26 | using Poco::Util::OptionCallback; |
27 | |
28 | |
29 | class ProcessKillerApp: public Application |
30 | { |
31 | public: |
32 | ProcessKillerApp(): |
33 | _helpRequested(false), |
34 | _friendly(false) |
35 | { |
36 | } |
37 | |
38 | protected: |
39 | void defineOptions(OptionSet& options) |
40 | { |
41 | Application::defineOptions(options); |
42 | |
43 | options.addOption( |
44 | Option("help" , "h" , "Display help information on command line arguments." ) |
45 | .required(false) |
46 | .repeatable(false) |
47 | .callback(OptionCallback<ProcessKillerApp>(this, &ProcessKillerApp::handleHelp))); |
48 | |
49 | options.addOption( |
50 | Option("friendly" , "f" , "Kindly ask application to shut down." ) |
51 | .required(false) |
52 | .repeatable(false) |
53 | .callback(OptionCallback<ProcessKillerApp>(this, &ProcessKillerApp::handleFriendly))); |
54 | } |
55 | |
56 | void handleHelp(const std::string& name, const std::string& value) |
57 | { |
58 | _helpRequested = true; |
59 | stopOptionsProcessing(); |
60 | } |
61 | |
62 | void handleFriendly(const std::string& name, const std::string& value) |
63 | { |
64 | _friendly = true; |
65 | } |
66 | |
67 | void displayHelp() |
68 | { |
69 | HelpFormatter helpFormatter(options()); |
70 | helpFormatter.setCommand(commandName()); |
71 | helpFormatter.setUsage("[options] <pid> ..." ); |
72 | helpFormatter.setHeader("A utility application to kill processes." ); |
73 | helpFormatter.setFooter("Note that the friendly option only works with applications using Poco::Util::ServerApplication::waitForTerminationRequest()." ); |
74 | helpFormatter.format(std::cout); |
75 | } |
76 | |
77 | int main(const std::vector<std::string>& args) |
78 | { |
79 | if (_helpRequested || args.empty()) |
80 | { |
81 | displayHelp(); |
82 | } |
83 | else |
84 | { |
85 | for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it) |
86 | { |
87 | Poco::Process::PID pid = Poco::NumberParser::parseUnsigned(*it); |
88 | if (_friendly) |
89 | Poco::Process::requestTermination(pid); |
90 | else |
91 | Poco::Process::kill(pid); |
92 | } |
93 | } |
94 | return Application::EXIT_OK; |
95 | } |
96 | |
97 | private: |
98 | bool _helpRequested; |
99 | bool _friendly; |
100 | }; |
101 | |
102 | |
103 | POCO_APP_MAIN(ProcessKillerApp) |
104 | |