Skip to content

Commit

Permalink
Added freebsd support for timestamp (persist)
Browse files Browse the repository at this point in the history
  • Loading branch information
leath-dub committed Jun 8, 2024
1 parent b96106b commit 9721832
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ int main(void) {
persistmethod() {
[ -z "$WITHOUT_TIMESTAMP" ] && {
printf '#define USE_TIMESTAMP\n' >>$CONFIG_H
printf '#ifdef __freebsd__\n' >>$CONFIG_H
printf '# define TIMESTAMP_DIR "/var/run/doas"\n' >>$CONFIG_H
printf '#endif\n' >>$CONFIG_H
printf 'SRCS += timestamp.c\n' >>$CONFIG_MK
printf 'timestamp\n'
return 0
Expand Down
27 changes: 25 additions & 2 deletions timestamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@

#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#ifndef __freebsd__
# include <sys/vfs.h>
#endif


#if !defined(timespecisset) || \
!defined(timespeccmp) || \
Expand Down Expand Up @@ -95,7 +98,22 @@
# endif
#endif

#ifdef __linux__
#if defined(__freebsd__)
#include <sys/types.h>
#include <sys/user.h>
#include <libutil.h>
#include <stdio.h>

static int
proc_info(pid_t pid, int *ttynr, unsigned long long *starttime)
{
struct kinfo_proc *ki = kinfo_getproc(pid);
*ttynr = ki->ki_tdev;
*starttime = ki->ki_start.tv_usec;
free(ki);
return 0;
}
#elif defined(__linux__)
/* Use tty_nr from /proc/self/stat instead of using
* ttyname(3), stdin, stdout and stderr are user
* controllable and would allow to reuse timestamps
Expand Down Expand Up @@ -221,8 +239,13 @@ timestamp_check(int fd, int secs)

if (fstat(fd, &st) == -1)
err(1, "fstat");
#ifdef __freebsd__
if (st.st_uid != 0 || st.st_gid != 0 || st.st_mode != (S_IFREG | 0000))
errx(1, "timestamp uid, gid or mode wrong");
#else
if (st.st_uid != 0 || st.st_gid != getgid() || st.st_mode != (S_IFREG | 0000))
errx(1, "timestamp uid, gid or mode wrong");
#endif

/* this timestamp was created but never set, invalid but no error */
if (!timespecisset(&st.st_atim) || !timespecisset(&st.st_mtim))
Expand Down

0 comments on commit 9721832

Please sign in to comment.