mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:17:35 +00:00
killall: Port to LibMain and LibCore
This commit is contained in:
parent
681b643093
commit
a65e1fa828
2 changed files with 15 additions and 14 deletions
|
@ -120,6 +120,7 @@ target_link_libraries(js LibJS LibLine LibMain)
|
||||||
link_with_unicode_data(js)
|
link_with_unicode_data(js)
|
||||||
target_link_libraries(keymap LibKeyboard LibMain)
|
target_link_libraries(keymap LibKeyboard LibMain)
|
||||||
target_link_libraries(kill LibMain)
|
target_link_libraries(kill LibMain)
|
||||||
|
target_link_libraries(killall LibCore LibMain)
|
||||||
target_link_libraries(less LibMain)
|
target_link_libraries(less LibMain)
|
||||||
target_link_libraries(ln LibMain)
|
target_link_libraries(ln LibMain)
|
||||||
target_link_libraries(logout LibMain)
|
target_link_libraries(logout LibMain)
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Zachary Penn <zack@sysdevs.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibCore/ProcessStatisticsReader.h>
|
#include <LibCore/ProcessStatisticsReader.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static void print_usage_and_exit()
|
static void print_usage_and_exit()
|
||||||
|
@ -17,7 +19,7 @@ static void print_usage_and_exit()
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int kill_all(const String& process_name, const unsigned signum)
|
static ErrorOr<int> kill_all(const String& process_name, const unsigned signum)
|
||||||
{
|
{
|
||||||
auto all_processes = Core::ProcessStatisticsReader::get_all();
|
auto all_processes = Core::ProcessStatisticsReader::get_all();
|
||||||
if (!all_processes.has_value())
|
if (!all_processes.has_value())
|
||||||
|
@ -25,46 +27,44 @@ static int kill_all(const String& process_name, const unsigned signum)
|
||||||
|
|
||||||
for (auto& process : all_processes.value().processes) {
|
for (auto& process : all_processes.value().processes) {
|
||||||
if (process.name == process_name) {
|
if (process.name == process_name) {
|
||||||
int ret = kill(process.pid, signum);
|
TRY(Core::System::kill(process.pid, signum));
|
||||||
if (ret < 0)
|
|
||||||
perror("kill");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
unsigned signum = SIGTERM;
|
unsigned signum = SIGTERM;
|
||||||
int name_argi = 1;
|
int name_argi = 1;
|
||||||
|
|
||||||
if (argc != 2 && argc != 3)
|
if (arguments.argc != 2 && arguments.argc != 3)
|
||||||
print_usage_and_exit();
|
print_usage_and_exit();
|
||||||
|
|
||||||
if (argc == 3) {
|
if (arguments.argc == 3) {
|
||||||
name_argi = 2;
|
name_argi = 2;
|
||||||
|
|
||||||
if (argv[1][0] != '-')
|
if (arguments.argv[1][0] != '-')
|
||||||
print_usage_and_exit();
|
print_usage_and_exit();
|
||||||
|
|
||||||
Optional<unsigned> number;
|
Optional<unsigned> number;
|
||||||
|
|
||||||
if (isalpha(argv[1][1])) {
|
if (isalpha(arguments.argv[1][1])) {
|
||||||
int value = getsignalbyname(&argv[1][1]);
|
int value = getsignalbyname(&arguments.argv[1][1]);
|
||||||
if (value >= 0 && value < NSIG)
|
if (value >= 0 && value < NSIG)
|
||||||
number = value;
|
number = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!number.has_value())
|
if (!number.has_value())
|
||||||
number = String(&argv[1][1]).to_uint();
|
number = String(&arguments.argv[1][1]).to_uint();
|
||||||
|
|
||||||
if (!number.has_value()) {
|
if (!number.has_value()) {
|
||||||
warnln("'{}' is not a valid signal name or number", &argv[1][1]);
|
warnln("'{}' is not a valid signal name or number", &arguments.argv[1][1]);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
signum = number.value();
|
signum = number.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
return kill_all(argv[name_argi], signum);
|
return kill_all(arguments.strings[name_argi], signum);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue