1 /*
2 * util.h - routeup/tlsdated utility functions
3 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef UTIL_H
9 #define UTIL_H
10
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #ifdef HAVE_PRCTL
17 #include <sys/prctl.h>
18 #ifndef PR_SET_NO_NEW_PRIVS
19 # define PR_SET_NO_NEW_PRIVS 38
20 #endif
21 #ifndef PR_GET_NO_NEW_PRIVS
22 # define PR_GET_NO_NEW_PRIVS 39
23 #endif
24 #endif
25
26 #include "src/rtc.h"
27
28 #ifdef TARGET_OS_HAIKU
29 #include <stdarg.h>
30 #endif
31
32 #define API __attribute__((visibility("default")))
33
34 extern const char *kTempSuffix;
35 #define IGNORE_EINTR(expr) ({ \
36 typeof(expr) _r; \
37 while ((_r = (expr)) == -1 && errno == EINTR); \
38 _r; \
39 })
40
41 extern int verbose;
42 extern int verbose_debug;
43 void initalize_syslog (void);
44 void terminate_syslog (void);
45 void die (const char *fmt, ...);
46 void verb (const char *fmt, ...);
47 extern void logat (int isverbose, const char *fmt, ...);
48
49 #define verb_debug debug
50 #define debug(fmt, ...) if (verbose_debug) logat(1, fmt, ## __VA_ARGS__)
51 #define info(fmt, ...) logat(0, fmt, ## __VA_ARGS__)
52 #define pinfo(fmt, ...) logat(1, fmt ": %s", ## __VA_ARGS__, strerror(errno))
53 #define error(fmt, ...) logat(0, fmt, ## __VA_ARGS__)
54 #define perror(fmt, ...) logat(0, fmt ": %s", ## __VA_ARGS__, strerror(errno))
55 #define fatal(fmt, ...) do { logat(0, fmt, ## __VA_ARGS__); exit(1); } while (0)
56 #define pfatal(fmt, ...) do { \
57 logat(0, fmt ": %s", ## __VA_ARGS__, strerror(errno)); \
58 exit(1); \
59 } while (0)
60
min(int x,int y)61 static inline int min (int x, int y)
62 {
63 return x < y ? x : y;
64 }
65
66 void drop_privs_to (const char *user, const char *group,
67 const char **supp_groups);
68 void no_new_privs (void);
69 const char *sync_type_str (int sync_type);
70
71 struct state;
72 enum event_id_t;
73 void trigger_event (struct state *state, enum event_id_t e, int sec);
74
75 struct platform {
76 int (*rtc_open)(struct rtc_handle *);
77 int (*rtc_write)(struct rtc_handle *, const struct timeval *tv);
78 int (*rtc_read)(struct rtc_handle *, struct timeval *tv);
79 int (*rtc_close)(struct rtc_handle *);
80
81 int (*file_open)(const char *path, int write, int cloexec);
82 int (*file_close)(int fd);
83 /* Atomic file write and read */
84 int (*file_write)(int fd, void *buf, size_t sz);
85 int (*file_read)(int fd, void *buf, size_t sz);
86
87 int (*time_get)(struct timeval *tv);
88
89 int (*pgrp_enter)(void);
90 int (*pgrp_kill)(void);
91
92 int (*process_signal)(pid_t pid, int sig);
93 int (*process_wait)(pid_t pid, int *status, int timeout);
94 };
95
96 extern struct platform *platform;
97
98 #endif /* !UTIL_H */
99