Sitelet https://github.com/SSHGuard/sshguard/pull/21
Skip to content

Introduce small time library based on clock_gettime - #21

Open
mhrn83 wants to merge 1 commit into
SSHGuard:masterfrom
mhrn83:fix-use-not-adjustable-clock
Open

mhrn83 wants to merge 1 commit into
SSHGuard:masterfrom
mhrn83:fix-use-not-adjustable-clock

Conversation

@mhrn83

@mhrn83 mhrn83 commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Replace usage of time(NULL) with clock_gettime(). This provides elapsed time since boot and avoids issues caused by wall‑clock adjustments.

@mhrn83
mhrn83 force-pushed the fix-use-not-adjustable-clock branch from 14ade76 to 0cc0886 Compare April 30, 2026 18:13
@Partmedia

Copy link
Copy Markdown
Contributor

Thank you for your PR. Have wall clock adjustments been a problem for you? While it is true that CLOCK_BOOTTIME is better for this purpose, the fallback (CLOCK_MONOTONIC) does not increment during sleep. Point is, unless there is an actual problem with time(NULL) I don't want to fix something that's not broken.

@mhrn83

mhrn83 commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Have wall clock adjustments been a problem for you? While it is true that CLOCK_BOOTTIME is better for this purpose, the fallback (CLOCK_MONOTONIC) does not increment during sleep.

In my case, the system allowed users to adjust the system clock, so using a clock source immune to wall‑clock changes was a better approach. Since I was working on Linux, I knew CLOCK_BOOTTIME would be available. However, because it is Linux‑specific, I added CLOCK_MONOTONIC as a POSIX‑compliant fallback. While that fallback avoids problems caused by manual time adjustments, it indeed has the drawback you mentioned.

@mhrn83
mhrn83 force-pushed the fix-use-not-adjustable-clock branch from 0cc0886 to 913d868 Compare May 3, 2026 11:48
Replace usage of time(NULL) with clock_gettime().
This provides elapsed time since boot and avoids
issues caused by wall‑clock adjustments.
@mhrn83
mhrn83 force-pushed the fix-use-not-adjustable-clock branch from 913d868 to 3c135fe Compare May 3, 2026 11:55
@Partmedia

Copy link
Copy Markdown
Contributor

I'm still not entirely sure that this is a condition that SSHGuard can and should handle. On most systems, you have to be root to change time. If the user is root, what else can they change? Can you be more specific about why you need SSHGuard specifically to handle time changing?

@mhrn83

mhrn83 commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

I'm still not entirely sure that this is a condition that SSHGuard can and should handle. On most systems, you have to be root to change time. If the user is root, what else can they change? Can you be more specific about why you need SSHGuard specifically to handle time changing?

I’ve checked and found that the only configurable option is the timezone in the system I was working on, which does not affect time(NULL). However, I still believe that using clock_gettime() with a monotonic clockid_t is a standard systems‑programming best practice for measuring elapsed time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants