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