2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-24 20:27:22 -05:00
|
|
|
* Copyright (c) 2022, Alex Major
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-26 13:35:09 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-24 20:27:22 -05:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-05-14 16:32:57 +02:00
|
|
|
#include <errno.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2021-01-23 08:53:32 +01:00
|
|
|
#include <time.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <unistd.h>
|
2018-10-25 13:53:49 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static bool volatile g_interrupted;
|
2020-08-10 23:48:37 +02:00
|
|
|
static void handle_sigint(int)
|
2018-11-07 21:19:47 +01:00
|
|
|
{
|
2020-09-08 10:19:07 -04:00
|
|
|
g_interrupted = true;
|
2018-11-07 21:19:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:27:22 -05:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-11-07 21:19:47 +01:00
|
|
|
{
|
2021-01-23 08:53:32 +01:00
|
|
|
double secs;
|
2020-05-26 13:35:09 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(secs, "Number of seconds to sleep for", "num-seconds");
|
2022-01-24 20:27:22 -05:00
|
|
|
args_parser.parse(arguments);
|
2020-05-26 13:35:09 +03:00
|
|
|
|
2018-11-07 21:19:47 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(struct sigaction));
|
|
|
|
sa.sa_handler = handle_sigint;
|
|
|
|
sigaction(SIGINT, &sa, nullptr);
|
2020-05-26 13:52:42 +03:00
|
|
|
|
2022-04-03 16:16:06 -07:00
|
|
|
TRY(Core::System::pledge("stdio sigaction"));
|
2020-05-26 13:52:42 +03:00
|
|
|
|
2021-01-23 08:53:32 +01:00
|
|
|
double whole_seconds = static_cast<time_t>(secs);
|
|
|
|
double fraction = secs - whole_seconds;
|
|
|
|
|
|
|
|
timespec requested_sleep {
|
|
|
|
.tv_sec = static_cast<time_t>(whole_seconds),
|
|
|
|
.tv_nsec = static_cast<long>(fraction * (double)1000000000),
|
|
|
|
};
|
|
|
|
|
|
|
|
timespec remaining_sleep {};
|
|
|
|
|
2021-03-30 02:29:07 +04:30
|
|
|
sleep_again:
|
2021-01-23 08:53:32 +01:00
|
|
|
if (clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, &remaining_sleep) < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
perror("clock_nanosleep");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining_sleep.tv_sec || remaining_sleep.tv_nsec) {
|
2021-03-30 02:29:07 +04:30
|
|
|
if (!g_interrupted) {
|
|
|
|
// If not interrupted with SIGINT, just go back to sleep.
|
|
|
|
requested_sleep = remaining_sleep;
|
|
|
|
goto sleep_again;
|
|
|
|
}
|
2021-01-23 08:53:32 +01:00
|
|
|
outln("Sleep interrupted with {}.{} seconds remaining.", remaining_sleep.tv_sec, remaining_sleep.tv_nsec);
|
2018-11-07 21:19:47 +01:00
|
|
|
}
|
2020-09-08 10:19:07 -04:00
|
|
|
|
2022-01-24 20:27:22 -05:00
|
|
|
TRY(Core::System::signal(SIGINT, SIG_DFL));
|
2020-09-10 16:40:32 +03:00
|
|
|
if (g_interrupted)
|
2020-09-08 10:19:07 -04:00
|
|
|
raise(SIGINT);
|
|
|
|
|
2018-10-25 13:53:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|