Skip to content

Commit

Permalink
Merge pull request #13 from nickeldan/bug/fix_alternative_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeldan authored Mar 2, 2023
2 parents ba694e3 + c65f7b1 commit 56d902b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ format:
find . -path ./packages -prune -o -name '*.[hc]' -print0 | xargs -0 -n 1 clang-format -i

install: /usr/local/lib/$(notdir $(SCR_SHARED_LIBRARY)) $(foreach file,$(SCR_ONLY_HEADER_FILES),/usr/local/include/scrutiny/$(notdir $(file)))
ldconfig

/usr/local/lib/$(notdir $(SCR_SHARED_LIBRARY)): $(SCR_SHARED_LIBRARY)
cp $< $@
ldconfig

/usr/local/include/scrutiny/%.h: include/scrutiny/%.h
@mkdir -p $(@D)
Expand Down
17 changes: 11 additions & 6 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ waitForProcess(pid_t child, unsigned int timeout, int *status, bool *timed_out)

#else // no pidfd_open

#include <stdbool.h>
#include <time.h>
#ifdef CLOCK_MONOTONIC_COARSE
#define SCR_CLOCK_TYPE CLOCK_MONOTONIC_COARSE
#else
#define SCR_CLOCK_TYPE CLOCK_MONOTONIC
#endif

#define ONE_TENTH_SECOND 10000000

static bool
caughtSignal(void)
Expand All @@ -163,11 +168,11 @@ caughtSignal(void)
void
waitForProcess(pid_t child, unsigned int timeout, int *status, bool *timed_out)
{
struct timespec start_time, lapse = {.tv_nsec = 10000000};
struct timespec start_time, lapse = {.tv_nsec = ONE_TENTH_SECOND};

*timed_out = false;
if (timeout > 0) {
clock_gettime(CLOCK_MOOTONIC_COARSE, &start_time);
clock_gettime(SCR_CLOCK_TYPE, &start_time);
}

while (1) {
Expand All @@ -179,11 +184,11 @@ waitForProcess(pid_t child, unsigned int timeout, int *status, bool *timed_out)
return;
}

nanosleep(&spec, NULL);
nanosleep(&lapse, NULL);
if (timeout > 0 && !*timed_out) {
struct timespec now;

clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
clock_gettime(SCR_CLOCK_TYPE, &now);
if (now.tv_sec - start_time.tv_sec >= timeout) {
*timed_out = true;
kill(child, SIGKILL);
Expand Down

0 comments on commit 56d902b

Please sign in to comment.