]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/util.h
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / basic / util.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 2
c2f1db8f 3#pragma once
60918275 4
a7334b09
LP
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
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 18 Lesser General Public License for more details.
a7334b09 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
31885cd5 24#include <alloca.h>
370c860f 25#include <fcntl.h>
60918275 26#include <inttypes.h>
f6c2284a
LP
27#include <limits.h>
28#include <locale.h>
ec2002f8 29#include <stdarg.h>
60918275 30#include <stdbool.h>
f6c2284a 31#include <stddef.h>
80876c20 32#include <stdio.h>
f6c2284a
LP
33#include <stdlib.h>
34#include <sys/inotify.h>
2c35d880 35#include <sys/socket.h>
00dc5d76 36#include <sys/stat.h>
c6878637 37#include <sys/statfs.h>
f6c2284a
LP
38#include <sys/types.h>
39#include <time.h>
40#include <unistd.h>
60918275 41
f6c2284a 42#include "formats-util.h"
a838e6a1 43#include "macro.h"
dced1557 44#include "missing.h"
9a98c7a1 45#include "time-util.h"
871d7de4 46
44d8db9e 47/* What is interpreted as whitespace? */
4a72ff34 48#define WHITESPACE " \t\n\r"
e3e0314b
ZJS
49#define NEWLINE "\n\r"
50#define QUOTES "\"\'"
51#define COMMENTS "#;"
52#define GLOB_CHARS "*?["
44d8db9e 53
2e857429 54size_t page_size(void) _pure_;
37f85e66 55#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
56
60918275
LP
57static inline const char* yes_no(bool b) {
58 return b ? "yes" : "no";
59}
60
5232c42e
LS
61static inline const char* true_false(bool b) {
62 return b ? "true" : "false";
63}
64
769d324c
LP
65static inline const char* one_zero(bool b) {
66 return b ? "1" : "0";
67}
68
42856c10
LP
69bool fstype_is_network(const char *fstype);
70
919ce0b7 71noreturn void freeze(void);
3c14d26c 72
e801700e 73void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
83cc030f 74
a88c8750
TG
75bool plymouth_running(void);
76
44a6b1b6 77bool display_is_local(const char *display) _pure_;
4d6d6518
LP
78int socket_from_display(const char *display, char **path);
79
94959f0f
LP
80int block_get_whole_disk(dev_t d, dev_t *ret);
81
e23a0ce8 82#define NULSTR_FOREACH(i, l) \
c4e2ceae
LP
83 for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
84
5c0532d1
LP
85#define NULSTR_FOREACH_PAIR(i, j, l) \
86 for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
87
f8b69d1d 88int ioprio_class_to_string_alloc(int i, char **s);
1dccbe19
LP
89int ioprio_class_from_string(const char *s);
90
44a6b1b6
ZJS
91const char *sigchld_code_to_string(int i) _const_;
92int sigchld_code_from_string(const char *s) _pure_;
1dccbe19 93
f8b69d1d 94int sched_policy_to_string_alloc(int i, char **s);
1dccbe19
LP
95int sched_policy_from_string(const char *s);
96
9a0e6896
LP
97extern int saved_argc;
98extern char **saved_argv;
99
65457142
FC
100bool kexec_loaded(void);
101
44a6b1b6 102int prot_from_flags(int flags) _const_;
87d2c1ff 103
9bdc770c 104int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
6bb92a16 105
9be346c9 106bool in_initrd(void);
069cfc85 107
a9e12476
KS
108void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
109 int (*compar) (const void *, const void *, void *),
110 void *arg);
09017585 111
b5efdb8a
LP
112/**
113 * Normal qsort requires base to be nonnull. Here were require
114 * that only if nmemb > 0.
115 */
116static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
117 if (nmemb <= 1)
118 return;
f74e605f 119
b5efdb8a
LP
120 assert(base);
121 qsort(base, nmemb, size, compar);
6282c859 122}
66e35261 123
b5efdb8a
LP
124int on_ac_power(void);
125
7d50b32a
LP
126#define memzero(x,l) (memset((x), 0, (l)))
127#define zero(x) (memzero(&(x), sizeof(x)))
128
b5efdb8a
LP
129static inline void *mempset(void *s, int c, size_t n) {
130 memset(s, c, n);
131 return (uint8_t*)s + n;
132}
a1937e67 133
5c0d398d 134static inline void _reset_errno_(int *saved_errno) {
5c0aa72a
LP
135 errno = *saved_errno;
136}
137
2a371001 138#define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
5c0d398d 139
44dd2c6e
DH
140static inline int negative_errno(void) {
141 /* This helper should be used to shut up gcc if you know 'errno' is
142 * negative. Instead of "return -errno;", use "return negative_errno();"
143 * It will suppress bogus gcc warnings in case it assumes 'errno' might
144 * be 0 and thus the caller's error-handling might not be triggered. */
145 assert_return(errno > 0, -EINVAL);
146 return -errno;
147}
148
144e51ec 149static inline unsigned u64log2(uint64_t n) {
ec417ccc 150#if __SIZEOF_LONG_LONG__ == 8
693eb9a2 151 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
ec417ccc
LP
152#else
153#error "Wut?"
154#endif
155}
156
157static inline unsigned u32ctz(uint32_t n) {
158#if __SIZEOF_INT__ == 4
159 return __builtin_ctz(n);
160#else
161#error "Wut?"
162#endif
144e51ec 163}
79d860fe 164
7d328b54 165static inline unsigned log2i(int x) {
8fe90522
ZJS
166 assert(x > 0);
167
168 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
169}
170
b5de6d98
MS
171static inline unsigned log2u(unsigned x) {
172 assert(x > 0);
173
174 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
175}
176
177static inline unsigned log2u_round_up(unsigned x) {
178 assert(x > 0);
179
180 if (x == 1)
181 return 0;
182
183 return log2u(x - 1) + 1;
184}
185
44a6b1b6 186bool id128_is_valid(const char *s) _pure_;
d4ac85c6 187
bc9fd78c
LP
188int container_get_leader(const char *machine, pid_t *pid);
189
671c3419
RM
190int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
191int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
bf108e55 192
050f7277
LP
193#ifndef PERSONALITY_INVALID
194/* personality(7) documents that 0xffffffffUL is used for querying the
195 * current personality, hence let's use that here as error
196 * indicator. */
197#define PERSONALITY_INVALID 0xffffffffLU
198#endif
199
ac45f971
LP
200unsigned long personality_from_string(const char *p);
201const char *personality_to_string(unsigned long);
1c231f56
LP
202
203uint64_t physical_memory(void);
6db615c1 204
370c860f 205union file_handle_union {
c5220a94
MO
206 struct file_handle handle;
207 char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ];
370c860f 208};
2695c5c4 209#define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
c5220a94
MO
210
211int update_reboot_param_file(const char *param);
6d313367 212
f7c1ad4f
LP
213#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
214
215#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
0254e944
SL
216 for ((e) = &buffer.ev; \
217 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
f7c1ad4f 218 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
72648326 219
0254e944
SL
220union inotify_event_buffer {
221 struct inotify_event ev;
222 uint8_t raw[INOTIFY_EVENT_MAX];
223};
224
3f6fd1ba 225int version(void);
8dd4c05b
LP
226
227bool fdname_is_valid(const char *s);
257b0719
EV
228
229bool oom_score_adjust_is_valid(int oa);