Annotation of solidite/utils/kill.cpp, revision 1.1.1.1
1.1 user 1: /* Copyright (c) 2026 The Solidite Developers
2: *
3: * This program is free software: you can redistribute it and/or modify it under
4: * the terms of the GNU Affero General Public License as published by the Free
5: * Software Foundation, either version 3 of the License, or (at your option)
6: * any later version.
7: *
8: * This program is distributed in the hope that it will be useful, but WITHOUT
9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11: * details.
12: *
13: * You should have received a copy of the GNU Affero General Public License
14: * along with this program. If not, see <https://www.gnu.org/licenses/>. */
15:
16: #include <bitset>
17: #include <iostream>
18: #include <vector>
19:
20: #include <cstddef>
21: #include <cstdint>
22: #include <cstring>
23:
24: #include <libintl.h>
25: #include <signal.h>
26:
27: /* Questionably IEEE Std 1003.1-2024 and XSI conformant
28: * needs testing */
29:
30: constexpr char SIGNALS_LIST[] = "SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM"
31: " SIGTERM\n";
32:
33: int main(int argc, char* argv[]) {
34: // locale work
35: std::locale::global(std::locale(""));
36: std::cerr.imbue(std::locale());
37:
38: std::bitset<2> options = 0b00;
39: // options[0]=skip next arg from being processed
40: // options[1]=stop processing arguments (-- encountered)
41: std::vector<std::size_t> pids;
42: int signal = 15;
43:
44: for (int i = 1; i < argc; ++i) {
45: if (options.test(0) == 1) {
46: options.reset(0);
47: continue;
48: } else if (std::strcmp(argv[i], "--") == 0 && options.test(1) == 0) {
49: options.set(1);
50: continue;
51: } else if (options.test(1) == 1) {
52: pids.push_back(std::atoi(argv[i]));
53: continue;
54: }
55:
56: if (std::strcmp(argv[i], "-l") == 0 && argv[i + 1] == nullptr) {
57: std::cout << SIGNALS_LIST;
58: return 0;
59: } else if (std::strcmp(argv[i], "-l") == 0) {
60: switch (std::atoi(argv[i + 1])) {
61: case 0:
62: std::cout << "0\n";
63: options.set(0);
64: break;
65: case 1:
66: std::cout << "HUP\n";
67: options.set(0);
68: break;
69: case 2:
70: std::cout << "INT\n";
71: options.set(0);
72: break;
73: case 3:
74: std::cout << "QUIT\n";
75: options.set(0);
76: break;
77: case 6:
78: std::cout << "ABRT\n";
79: options.set(0);
80: break;
81: case 9:
82: std::cout << "KILL\n";
83: options.set(0);
84: break;
85: case 14:
86: std::cout << "ALRM\n";
87: options.set(0);
88: break;
89: case 15:
90: std::cout << "TERM\n";
91: options.set(0);
92: break;
93: default:
94: std::cerr << "bad signal\n";
95: options.set(0);
96: break;
97: }
98: } else if (std::strcmp(argv[i], "-s") == 0) {
99: if (argv[i + 1] == nullptr) {
100: std::cerr << "signal name required after -s\n";
101: continue;
102: }
103: if (std::strcmp(argv[i + 1], "0") == 0) {
104: signal = 0;
105: } else if (std::strcmp(argv[i + 1], "HUP") == 0) {
106: signal = 1;
107: } else if (std::strcmp(argv[i + 1], "INT") == 0) {
108: signal = 2;
109: } else if (std::strcmp(argv[i + 1], "QUIT") == 0) {
110: signal = 3;
111: } else if (std::strcmp(argv[i + 1], "ABRT") == 0) {
112: signal = 6;
113: } else if (std::strcmp(argv[i + 1], "KILL") == 0) {
114: signal = 9;
115: } else if (std::strcmp(argv[i + 1], "ALRM") == 0) {
116: signal = 14;
117: } else if (std::strcmp(argv[i + 1], "TERM") == 0) {
118: signal = 15;
119: } else {
120: std::cerr << "bad signal name\n";
121: }
122: options.set(0);
123: continue;
124: } else if (std::strcmp(argv[i], "-0") == 0) {
125: signal = 0;
126: } else if (std::strcmp(argv[i], "-1") == 0) {
127: signal = 1;
128: } else if (std::strcmp(argv[i], "-2") == 0) {
129: signal = 2;
130: } else if (std::strcmp(argv[i], "-3") == 0) {
131: signal = 3;
132: } else if (std::strcmp(argv[i], "-6") == 0) {
133: signal = 6;
134: } else if (std::strcmp(argv[i], "-9") == 0) {
135: signal = 9;
136: } else if (std::strcmp(argv[i], "-14") == 0) {
137: signal = 14;
138: } else if (std::strcmp(argv[i], "-15") == 0) {
139: signal = 15;
140: } else if (std::strcmp(argv[i], "-UP") == 0) {
141: signal = 1;
142: } else if (std::strcmp(argv[i], "-INT") == 0) {
143: signal = 2;
144: } else if (std::strcmp(argv[i], "-QUIT") == 0) {
145: signal = 3;
146: } else if (std::strcmp(argv[i], "-ABRT") == 0) {
147: signal = 6;
148: } else if (std::strcmp(argv[i], "-KILL") == 0) {
149: signal = 9;
150: } else if (std::strcmp(argv[i], "-ALRM") == 0) {
151: signal = 14;
152: } else if (std::strcmp(argv[i], "-TERM") == 0) {
153: signal = 15;
154: } else {
155: pids.push_back(std::atoi(argv[i]));
156: }
157: }
158: for (std::size_t pid : pids) {
159: kill(pid, signal);
160: }
161: return 0;
162: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>