162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com> 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any 562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1162306a36Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1362306a36Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1462306a36Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci#undef NDEBUG 1762306a36Sopenharmony_ci#include <assert.h> 1862306a36Sopenharmony_ci#include <errno.h> 1962306a36Sopenharmony_ci#include <string.h> 2062306a36Sopenharmony_ci#include <stdlib.h> 2162306a36Sopenharmony_ci#include <unistd.h> 2262306a36Sopenharmony_ci#include <time.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#include "proc.h" 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic uint64_t clock_boottime(void) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci struct timespec ts; 2962306a36Sopenharmony_ci int err; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci err = clock_gettime(CLOCK_BOOTTIME, &ts); 3262306a36Sopenharmony_ci assert(err >= 0); 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci return (ts.tv_sec * 100) + (ts.tv_nsec / 10000000); 3562306a36Sopenharmony_ci} 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cistatic uint64_t proc_uptime(int fd) 3862306a36Sopenharmony_ci{ 3962306a36Sopenharmony_ci uint64_t val1, val2; 4062306a36Sopenharmony_ci char buf[64], *p; 4162306a36Sopenharmony_ci ssize_t rv; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci /* save "p < end" checks */ 4462306a36Sopenharmony_ci memset(buf, 0, sizeof(buf)); 4562306a36Sopenharmony_ci rv = pread(fd, buf, sizeof(buf), 0); 4662306a36Sopenharmony_ci assert(0 <= rv && rv <= sizeof(buf)); 4762306a36Sopenharmony_ci buf[sizeof(buf) - 1] = '\0'; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci p = buf; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci val1 = xstrtoull(p, &p); 5262306a36Sopenharmony_ci assert(p[0] == '.'); 5362306a36Sopenharmony_ci assert('0' <= p[1] && p[1] <= '9'); 5462306a36Sopenharmony_ci assert('0' <= p[2] && p[2] <= '9'); 5562306a36Sopenharmony_ci assert(p[3] == ' '); 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci val2 = (p[1] - '0') * 10 + p[2] - '0'; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci return val1 * 100 + val2; 6062306a36Sopenharmony_ci} 61