]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
util-lib: move inotify-related definitions to fs-util.[ch]
[thirdparty/systemd.git] / src / basic / util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <alloca.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/inotify.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/statfs.h>
38 #include <sys/types.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "formats-util.h"
43 #include "macro.h"
44 #include "missing.h"
45 #include "time-util.h"
46
47 /* What is interpreted as whitespace? */
48 #define WHITESPACE " \t\n\r"
49 #define NEWLINE "\n\r"
50 #define QUOTES "\"\'"
51 #define COMMENTS "#;"
52 #define GLOB_CHARS "*?["
53
54 size_t page_size(void) _pure_;
55 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
56
57 static inline const char* yes_no(bool b) {
58 return b ? "yes" : "no";
59 }
60
61 static inline const char* true_false(bool b) {
62 return b ? "true" : "false";
63 }
64
65 static inline const char* one_zero(bool b) {
66 return b ? "1" : "0";
67 }
68
69 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
70
71 bool plymouth_running(void);
72
73 bool display_is_local(const char *display) _pure_;
74 int socket_from_display(const char *display, char **path);
75
76 int block_get_whole_disk(dev_t d, dev_t *ret);
77
78 #define NULSTR_FOREACH(i, l) \
79 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
80
81 #define NULSTR_FOREACH_PAIR(i, j, l) \
82 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
83
84 extern int saved_argc;
85 extern char **saved_argv;
86
87 bool kexec_loaded(void);
88
89 int prot_from_flags(int flags) _const_;
90
91 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
92
93 bool in_initrd(void);
94
95 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
96 int (*compar) (const void *, const void *, void *),
97 void *arg);
98
99 /**
100 * Normal qsort requires base to be nonnull. Here were require
101 * that only if nmemb > 0.
102 */
103 static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
104 if (nmemb <= 1)
105 return;
106
107 assert(base);
108 qsort(base, nmemb, size, compar);
109 }
110
111 int on_ac_power(void);
112
113 #define memzero(x,l) (memset((x), 0, (l)))
114 #define zero(x) (memzero(&(x), sizeof(x)))
115
116 static inline void *mempset(void *s, int c, size_t n) {
117 memset(s, c, n);
118 return (uint8_t*)s + n;
119 }
120
121 static inline void _reset_errno_(int *saved_errno) {
122 errno = *saved_errno;
123 }
124
125 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
126
127 static inline int negative_errno(void) {
128 /* This helper should be used to shut up gcc if you know 'errno' is
129 * negative. Instead of "return -errno;", use "return negative_errno();"
130 * It will suppress bogus gcc warnings in case it assumes 'errno' might
131 * be 0 and thus the caller's error-handling might not be triggered. */
132 assert_return(errno > 0, -EINVAL);
133 return -errno;
134 }
135
136 static inline unsigned u64log2(uint64_t n) {
137 #if __SIZEOF_LONG_LONG__ == 8
138 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
139 #else
140 #error "Wut?"
141 #endif
142 }
143
144 static inline unsigned u32ctz(uint32_t n) {
145 #if __SIZEOF_INT__ == 4
146 return __builtin_ctz(n);
147 #else
148 #error "Wut?"
149 #endif
150 }
151
152 static inline unsigned log2i(int x) {
153 assert(x > 0);
154
155 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
156 }
157
158 static inline unsigned log2u(unsigned x) {
159 assert(x > 0);
160
161 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
162 }
163
164 static inline unsigned log2u_round_up(unsigned x) {
165 assert(x > 0);
166
167 if (x == 1)
168 return 0;
169
170 return log2u(x - 1) + 1;
171 }
172
173 bool id128_is_valid(const char *s) _pure_;
174
175 int container_get_leader(const char *machine, pid_t *pid);
176
177 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
178 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
179
180 uint64_t physical_memory(void);
181
182 int update_reboot_param_file(const char *param);
183
184 int version(void);