1 /* 2 * Copyright (C) 2026 Daniel Golle <daniel@makrotopia.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2.1 6 * as published by the Free Software Foundation 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #ifndef __UPTIME_H 15 #define __UPTIME_H 16 17 #include <fcntl.h> 18 #include <stdbool.h> 19 #include <stdint.h> 20 #include <string.h> 21 #include <time.h> 22 #include <unistd.h> 23 24 static inline uint64_t uptime_get(void) 25 { 26 struct timespec ts; 27 28 clock_gettime(CLOCK_BOOTTIME, &ts); 29 30 return ts.tv_sec; 31 } 32 33 static inline bool boot_id_get(char *buf, size_t len) 34 { 35 ssize_t n; 36 int fd; 37 38 fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY | O_CLOEXEC); 39 if (fd < 0) 40 return false; 41 42 n = read(fd, buf, len - 1); 43 close(fd); 44 if (n <= 0) 45 return false; 46 47 buf[n] = '\0'; 48 buf[strcspn(buf, "\n")] = '\0'; 49 50 return true; 51 } 52 53 #endif 54
This page was automatically generated by LXR 0.3.1. • OpenWrt