1*4b9c6d91SCole Faust /* util.h
2*4b9c6d91SCole Faust * Copyright 2012 The ChromiumOS Authors
3*4b9c6d91SCole Faust * Use of this source code is governed by a BSD-style license that can be
4*4b9c6d91SCole Faust * found in the LICENSE file.
5*4b9c6d91SCole Faust *
6*4b9c6d91SCole Faust * Logging and other utility functions.
7*4b9c6d91SCole Faust */
8*4b9c6d91SCole Faust
9*4b9c6d91SCole Faust #ifndef _UTIL_H_
10*4b9c6d91SCole Faust #define _UTIL_H_
11*4b9c6d91SCole Faust
12*4b9c6d91SCole Faust #include <stdbool.h>
13*4b9c6d91SCole Faust #include <stdio.h>
14*4b9c6d91SCole Faust #include <stdlib.h>
15*4b9c6d91SCole Faust #include <string.h>
16*4b9c6d91SCole Faust #include <sys/types.h>
17*4b9c6d91SCole Faust #include <syslog.h>
18*4b9c6d91SCole Faust #include <unistd.h>
19*4b9c6d91SCole Faust
20*4b9c6d91SCole Faust #include "libsyscalls.h"
21*4b9c6d91SCole Faust
22*4b9c6d91SCole Faust #ifdef __cplusplus
23*4b9c6d91SCole Faust extern "C" {
24*4b9c6d91SCole Faust #endif
25*4b9c6d91SCole Faust
26*4b9c6d91SCole Faust /*
27*4b9c6d91SCole Faust * Silence compiler warnings for unused variables/functions.
28*4b9c6d91SCole Faust *
29*4b9c6d91SCole Faust * If the definition is actually used, the attribute should be removed, but if
30*4b9c6d91SCole Faust * it's forgotten or left in place, it doesn't cause a problem.
31*4b9c6d91SCole Faust *
32*4b9c6d91SCole Faust * If the definition is actually unused, the compiler is free to remove it from
33*4b9c6d91SCole Faust * the output so as to save size. If you want to make sure the definition is
34*4b9c6d91SCole Faust * kept (e.g. for ABI compatibility), look at the "used" attribute instead.
35*4b9c6d91SCole Faust */
36*4b9c6d91SCole Faust #define attribute_unused __attribute__((__unused__))
37*4b9c6d91SCole Faust
38*4b9c6d91SCole Faust /*
39*4b9c6d91SCole Faust * Mark the symbol as "weak" in the ELF output. This provides a fallback symbol
40*4b9c6d91SCole Faust * that may be overriden at link time. See this page for more details:
41*4b9c6d91SCole Faust * https://en.wikipedia.org/wiki/Weak_symbol
42*4b9c6d91SCole Faust */
43*4b9c6d91SCole Faust #define attribute_weak __attribute__((__weak__))
44*4b9c6d91SCole Faust
45*4b9c6d91SCole Faust /*
46*4b9c6d91SCole Faust * Mark the function as a printf-style function.
47*4b9c6d91SCole Faust * @format_idx The index in the function argument list where the format string
48*4b9c6d91SCole Faust * is passed (where the first argument is "1").
49*4b9c6d91SCole Faust * @check_idx The index in the function argument list where the first argument
50*4b9c6d91SCole Faust * used in the format string is passed.
51*4b9c6d91SCole Faust * Some examples:
52*4b9c6d91SCole Faust * foo([1] const char *format, [2] ...): format=1 check=2
53*4b9c6d91SCole Faust * foo([1] int, [2] const char *format, [3] ...): format=2 check=3
54*4b9c6d91SCole Faust * foo([1] const char *format, [2] const char *, [3] ...): format=1 check=3
55*4b9c6d91SCole Faust */
56*4b9c6d91SCole Faust #define attribute_printf(format_idx, check_idx) \
57*4b9c6d91SCole Faust __attribute__((__format__(__printf__, format_idx, check_idx)))
58*4b9c6d91SCole Faust
59*4b9c6d91SCole Faust #ifndef __cplusplus
60*4b9c6d91SCole Faust /* If writing C++, use std::unique_ptr with a destructor instead. */
61*4b9c6d91SCole Faust
62*4b9c6d91SCole Faust /*
63*4b9c6d91SCole Faust * Mark a local variable for automatic cleanup when exiting its scope.
64*4b9c6d91SCole Faust * See attribute_cleanup_fp as an example below.
65*4b9c6d91SCole Faust * Make sure any variable using this is always initialized to something.
66*4b9c6d91SCole Faust * @func The function to call on (a pointer to) the variable.
67*4b9c6d91SCole Faust */
68*4b9c6d91SCole Faust #define attribute_cleanup(func) \
69*4b9c6d91SCole Faust __attribute__((__cleanup__(func)))
70*4b9c6d91SCole Faust
71*4b9c6d91SCole Faust /*
72*4b9c6d91SCole Faust * Automatically close a FILE* when exiting its scope.
73*4b9c6d91SCole Faust * Make sure the pointer is always initialized.
74*4b9c6d91SCole Faust * Some examples:
75*4b9c6d91SCole Faust * attribute_cleanup_fp FILE *fp = fopen(...);
76*4b9c6d91SCole Faust * attribute_cleanup_fp FILE *fp = NULL;
77*4b9c6d91SCole Faust * ...
78*4b9c6d91SCole Faust * fp = fopen(...);
79*4b9c6d91SCole Faust *
80*4b9c6d91SCole Faust * NB: This will automatically close the underlying fd, so do not use this
81*4b9c6d91SCole Faust * with fdopen calls if the fd should be left open.
82*4b9c6d91SCole Faust */
83*4b9c6d91SCole Faust #define attribute_cleanup_fp attribute_cleanup(_cleanup_fp)
_cleanup_fp(FILE ** fp)84*4b9c6d91SCole Faust static inline void _cleanup_fp(FILE **fp)
85*4b9c6d91SCole Faust {
86*4b9c6d91SCole Faust if (*fp)
87*4b9c6d91SCole Faust fclose(*fp);
88*4b9c6d91SCole Faust }
89*4b9c6d91SCole Faust
90*4b9c6d91SCole Faust /*
91*4b9c6d91SCole Faust * Automatically close a fd when exiting its scope.
92*4b9c6d91SCole Faust * Make sure the fd is always initialized.
93*4b9c6d91SCole Faust * Some examples:
94*4b9c6d91SCole Faust * attribute_cleanup_fd int fd = open(...);
95*4b9c6d91SCole Faust * attribute_cleanup_fd int fd = -1;
96*4b9c6d91SCole Faust * ...
97*4b9c6d91SCole Faust * fd = open(...);
98*4b9c6d91SCole Faust *
99*4b9c6d91SCole Faust * NB: Be careful when using this with attribute_cleanup_fp and fdopen.
100*4b9c6d91SCole Faust */
101*4b9c6d91SCole Faust #define attribute_cleanup_fd attribute_cleanup(_cleanup_fd)
_cleanup_fd(int * fd)102*4b9c6d91SCole Faust static inline void _cleanup_fd(int *fd)
103*4b9c6d91SCole Faust {
104*4b9c6d91SCole Faust if (*fd >= 0)
105*4b9c6d91SCole Faust close(*fd);
106*4b9c6d91SCole Faust }
107*4b9c6d91SCole Faust
108*4b9c6d91SCole Faust /*
109*4b9c6d91SCole Faust * Automatically free a heap allocation when exiting its scope.
110*4b9c6d91SCole Faust * Make sure the pointer is always initialized.
111*4b9c6d91SCole Faust * Some examples:
112*4b9c6d91SCole Faust * attribute_cleanup_str char *s = strdup(...);
113*4b9c6d91SCole Faust * attribute_cleanup_str char *s = NULL;
114*4b9c6d91SCole Faust * ...
115*4b9c6d91SCole Faust * s = strdup(...);
116*4b9c6d91SCole Faust */
117*4b9c6d91SCole Faust #define attribute_cleanup_str attribute_cleanup(_cleanup_str)
_cleanup_str(char ** ptr)118*4b9c6d91SCole Faust static inline void _cleanup_str(char **ptr)
119*4b9c6d91SCole Faust {
120*4b9c6d91SCole Faust free(*ptr);
121*4b9c6d91SCole Faust }
122*4b9c6d91SCole Faust
123*4b9c6d91SCole Faust #endif /* __cplusplus */
124*4b9c6d91SCole Faust
125*4b9c6d91SCole Faust /* clang-format off */
126*4b9c6d91SCole Faust #define die(_msg, ...) \
127*4b9c6d91SCole Faust do_fatal_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
128*4b9c6d91SCole Faust
129*4b9c6d91SCole Faust #define pdie(_msg, ...) \
130*4b9c6d91SCole Faust die(_msg ": %m", ## __VA_ARGS__)
131*4b9c6d91SCole Faust
132*4b9c6d91SCole Faust #define warn(_msg, ...) \
133*4b9c6d91SCole Faust do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
134*4b9c6d91SCole Faust
135*4b9c6d91SCole Faust #define pwarn(_msg, ...) \
136*4b9c6d91SCole Faust warn(_msg ": %m", ## __VA_ARGS__)
137*4b9c6d91SCole Faust
138*4b9c6d91SCole Faust #define info(_msg, ...) \
139*4b9c6d91SCole Faust do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
140*4b9c6d91SCole Faust
141*4b9c6d91SCole Faust #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
142*4b9c6d91SCole Faust /* clang-format on */
143*4b9c6d91SCole Faust
144*4b9c6d91SCole Faust extern const char *const log_syscalls[];
145*4b9c6d91SCole Faust extern const size_t log_syscalls_len;
146*4b9c6d91SCole Faust
147*4b9c6d91SCole Faust enum logging_system_t {
148*4b9c6d91SCole Faust /* Log to syslog. This is the default. */
149*4b9c6d91SCole Faust LOG_TO_SYSLOG = 0,
150*4b9c6d91SCole Faust
151*4b9c6d91SCole Faust /* Log to a file descriptor. */
152*4b9c6d91SCole Faust LOG_TO_FD,
153*4b9c6d91SCole Faust };
154*4b9c6d91SCole Faust
155*4b9c6d91SCole Faust /*
156*4b9c6d91SCole Faust * Even though this function internally calls abort(2)/exit(2), it is
157*4b9c6d91SCole Faust * intentionally not marked with the noreturn attribute. When marked as
158*4b9c6d91SCole Faust * noreturn, clang coalesces several of the do_fatal_log() calls in methods that
159*4b9c6d91SCole Faust * have a large number of such calls (like minijail_enter()), making it
160*4b9c6d91SCole Faust * impossible for breakpad to correctly identify the line where it was called,
161*4b9c6d91SCole Faust * making the backtrace somewhat useless.
162*4b9c6d91SCole Faust */
163*4b9c6d91SCole Faust extern void do_fatal_log(int priority, const char *format, ...)
164*4b9c6d91SCole Faust attribute_printf(2, 3);
165*4b9c6d91SCole Faust
166*4b9c6d91SCole Faust extern void do_log(int priority, const char *format, ...)
167*4b9c6d91SCole Faust attribute_printf(2, 3);
168*4b9c6d91SCole Faust
is_android(void)169*4b9c6d91SCole Faust static inline int is_android(void)
170*4b9c6d91SCole Faust {
171*4b9c6d91SCole Faust #if defined(__ANDROID__)
172*4b9c6d91SCole Faust return 1;
173*4b9c6d91SCole Faust #else
174*4b9c6d91SCole Faust return 0;
175*4b9c6d91SCole Faust #endif
176*4b9c6d91SCole Faust }
177*4b9c6d91SCole Faust
compiled_with_asan(void)178*4b9c6d91SCole Faust static inline bool compiled_with_asan(void)
179*4b9c6d91SCole Faust {
180*4b9c6d91SCole Faust #if defined(__SANITIZE_ADDRESS__)
181*4b9c6d91SCole Faust /* For gcc. */
182*4b9c6d91SCole Faust return true;
183*4b9c6d91SCole Faust #elif defined(__has_feature)
184*4b9c6d91SCole Faust /* For clang. */
185*4b9c6d91SCole Faust return __has_feature(address_sanitizer) ||
186*4b9c6d91SCole Faust __has_feature(hwaddress_sanitizer);
187*4b9c6d91SCole Faust #else
188*4b9c6d91SCole Faust return false;
189*4b9c6d91SCole Faust #endif
190*4b9c6d91SCole Faust }
191*4b9c6d91SCole Faust
192*4b9c6d91SCole Faust void __asan_init(void) attribute_weak;
193*4b9c6d91SCole Faust void __hwasan_init(void) attribute_weak;
194*4b9c6d91SCole Faust
running_with_asan(void)195*4b9c6d91SCole Faust static inline bool running_with_asan(void)
196*4b9c6d91SCole Faust {
197*4b9c6d91SCole Faust /*
198*4b9c6d91SCole Faust * There are some configurations under which ASan needs a dynamic (as
199*4b9c6d91SCole Faust * opposed to compile-time) test. Some Android processes that start
200*4b9c6d91SCole Faust * before /data is mounted run with non-instrumented libminijail.so, so
201*4b9c6d91SCole Faust * the symbol-sniffing code must be present to make the right decision.
202*4b9c6d91SCole Faust */
203*4b9c6d91SCole Faust return compiled_with_asan() || &__asan_init != 0 || &__hwasan_init != 0;
204*4b9c6d91SCole Faust }
205*4b9c6d91SCole Faust
debug_logging_allowed(void)206*4b9c6d91SCole Faust static inline bool debug_logging_allowed(void)
207*4b9c6d91SCole Faust {
208*4b9c6d91SCole Faust #if defined(ALLOW_DEBUG_LOGGING)
209*4b9c6d91SCole Faust return true;
210*4b9c6d91SCole Faust #else
211*4b9c6d91SCole Faust return false;
212*4b9c6d91SCole Faust #endif
213*4b9c6d91SCole Faust }
214*4b9c6d91SCole Faust
seccomp_default_ret_log(void)215*4b9c6d91SCole Faust static inline bool seccomp_default_ret_log(void)
216*4b9c6d91SCole Faust {
217*4b9c6d91SCole Faust #if defined(SECCOMP_DEFAULT_RET_LOG)
218*4b9c6d91SCole Faust return true;
219*4b9c6d91SCole Faust #else
220*4b9c6d91SCole Faust return false;
221*4b9c6d91SCole Faust #endif
222*4b9c6d91SCole Faust }
223*4b9c6d91SCole Faust
block_symlinks_in_bindmount_paths(void)224*4b9c6d91SCole Faust static inline bool block_symlinks_in_bindmount_paths(void)
225*4b9c6d91SCole Faust {
226*4b9c6d91SCole Faust #if defined(BLOCK_SYMLINKS_IN_BINDMOUNT_PATHS)
227*4b9c6d91SCole Faust return true;
228*4b9c6d91SCole Faust #else
229*4b9c6d91SCole Faust return false;
230*4b9c6d91SCole Faust #endif
231*4b9c6d91SCole Faust }
232*4b9c6d91SCole Faust
block_symlinks_in_noninit_mountns_tmp(void)233*4b9c6d91SCole Faust static inline bool block_symlinks_in_noninit_mountns_tmp(void)
234*4b9c6d91SCole Faust {
235*4b9c6d91SCole Faust #if defined(BLOCK_SYMLINKS_IN_NONINIT_MOUNTNS_TMP)
236*4b9c6d91SCole Faust return true;
237*4b9c6d91SCole Faust #else
238*4b9c6d91SCole Faust return false;
239*4b9c6d91SCole Faust #endif
240*4b9c6d91SCole Faust }
241*4b9c6d91SCole Faust
get_num_syscalls(void)242*4b9c6d91SCole Faust static inline size_t get_num_syscalls(void)
243*4b9c6d91SCole Faust {
244*4b9c6d91SCole Faust return syscall_table_size;
245*4b9c6d91SCole Faust }
246*4b9c6d91SCole Faust
247*4b9c6d91SCole Faust int lookup_syscall(const char *name, size_t *ind);
248*4b9c6d91SCole Faust const char *lookup_syscall_name(int nr);
249*4b9c6d91SCole Faust
250*4b9c6d91SCole Faust long int parse_single_constant(char *constant_str, char **endptr);
251*4b9c6d91SCole Faust long int parse_constant(char *constant_str, char **endptr);
252*4b9c6d91SCole Faust int parse_size(size_t *size, const char *sizespec);
253*4b9c6d91SCole Faust
254*4b9c6d91SCole Faust char *strip(char *s);
255*4b9c6d91SCole Faust
256*4b9c6d91SCole Faust /*
257*4b9c6d91SCole Faust * streq: determine whether two strings are equal.
258*4b9c6d91SCole Faust */
streq(const char * s1,const char * s2)259*4b9c6d91SCole Faust static inline bool streq(const char *s1, const char *s2)
260*4b9c6d91SCole Faust {
261*4b9c6d91SCole Faust return strcmp(s1, s2) == 0;
262*4b9c6d91SCole Faust }
263*4b9c6d91SCole Faust
264*4b9c6d91SCole Faust /*
265*4b9c6d91SCole Faust * tokenize: locate the next token in @stringp using the @delim
266*4b9c6d91SCole Faust * @stringp A pointer to the string to scan for tokens
267*4b9c6d91SCole Faust * @delim The delimiter to split by
268*4b9c6d91SCole Faust *
269*4b9c6d91SCole Faust * Note that, unlike strtok, @delim is not a set of characters, but the full
270*4b9c6d91SCole Faust * delimiter. e.g. "a,;b,;c" with a delim of ",;" will yield ["a","b","c"].
271*4b9c6d91SCole Faust *
272*4b9c6d91SCole Faust * Note that, unlike strtok, this may return an empty token. e.g. "a,,b" with
273*4b9c6d91SCole Faust * strtok will yield ["a","b"], but this will yield ["a","","b"].
274*4b9c6d91SCole Faust */
275*4b9c6d91SCole Faust char *tokenize(char **stringp, const char *delim);
276*4b9c6d91SCole Faust
277*4b9c6d91SCole Faust char *path_join(const char *external_path, const char *internal_path);
278*4b9c6d91SCole Faust
279*4b9c6d91SCole Faust /*
280*4b9c6d91SCole Faust * path_is_parent: checks whether @parent is a parent of @child.
281*4b9c6d91SCole Faust * Note: this function does not evaluate '.' or '..' nor does it resolve
282*4b9c6d91SCole Faust * symlinks.
283*4b9c6d91SCole Faust */
284*4b9c6d91SCole Faust bool path_is_parent(const char *parent, const char *child);
285*4b9c6d91SCole Faust
286*4b9c6d91SCole Faust /*
287*4b9c6d91SCole Faust * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
288*4b9c6d91SCole Faust * @length Number of bytes to consume
289*4b9c6d91SCole Faust * @buf Buffer to consume from
290*4b9c6d91SCole Faust * @buflength Size of @buf
291*4b9c6d91SCole Faust *
292*4b9c6d91SCole Faust * Returns a pointer to the base of the bytes, or NULL for errors.
293*4b9c6d91SCole Faust */
294*4b9c6d91SCole Faust void *consumebytes(size_t length, char **buf, size_t *buflength);
295*4b9c6d91SCole Faust
296*4b9c6d91SCole Faust /*
297*4b9c6d91SCole Faust * consumestr: consumes a C string from a buffer @buf of length @length
298*4b9c6d91SCole Faust * @buf Buffer to consume
299*4b9c6d91SCole Faust * @length Length of buffer
300*4b9c6d91SCole Faust *
301*4b9c6d91SCole Faust * Returns a pointer to the base of the string, or NULL for errors.
302*4b9c6d91SCole Faust */
303*4b9c6d91SCole Faust char *consumestr(char **buf, size_t *buflength);
304*4b9c6d91SCole Faust
305*4b9c6d91SCole Faust /*
306*4b9c6d91SCole Faust * init_logging: initializes the module-wide logging.
307*4b9c6d91SCole Faust * @logger The logging system to use.
308*4b9c6d91SCole Faust * @fd The file descriptor to log into. Ignored unless
309*4b9c6d91SCole Faust * @logger = LOG_TO_FD.
310*4b9c6d91SCole Faust * @min_priority The minimum priority to display. Corresponds to syslog's
311*4b9c6d91SCole Faust * priority parameter. Ignored unless @logger = LOG_TO_FD.
312*4b9c6d91SCole Faust */
313*4b9c6d91SCole Faust void init_logging(enum logging_system_t logger, int fd, int min_priority);
314*4b9c6d91SCole Faust
315*4b9c6d91SCole Faust /*
316*4b9c6d91SCole Faust * minjail_free_env: Frees an environment array plus the environment strings it
317*4b9c6d91SCole Faust * points to. The environment and its constituent strings must have been
318*4b9c6d91SCole Faust * allocated (as opposed to pointing to static data), e.g. by using
319*4b9c6d91SCole Faust * minijail_copy_env() and minijail_setenv().
320*4b9c6d91SCole Faust *
321*4b9c6d91SCole Faust * @env The environment to free.
322*4b9c6d91SCole Faust */
323*4b9c6d91SCole Faust void minijail_free_env(char **env);
324*4b9c6d91SCole Faust
325*4b9c6d91SCole Faust /*
326*4b9c6d91SCole Faust * minjail_copy_env: Copy an environment array (such as passed to execve),
327*4b9c6d91SCole Faust * duplicating the environment strings and the array pointing at them.
328*4b9c6d91SCole Faust *
329*4b9c6d91SCole Faust * @env The environment to copy.
330*4b9c6d91SCole Faust *
331*4b9c6d91SCole Faust * Returns a pointer to the copied environment or NULL on memory allocation
332*4b9c6d91SCole Faust * failure.
333*4b9c6d91SCole Faust */
334*4b9c6d91SCole Faust char **minijail_copy_env(char *const *env);
335*4b9c6d91SCole Faust
336*4b9c6d91SCole Faust /*
337*4b9c6d91SCole Faust * minjail_setenv: Set an environment variable in @env. Semantics match the
338*4b9c6d91SCole Faust * standard setenv() function, but this operates on @env, not the global
339*4b9c6d91SCole Faust * environment. @env must be dynamically allocated (as opposed to pointing to
340*4b9c6d91SCole Faust * static data), e.g. via minijail_copy_env(). @name and @value get copied into
341*4b9c6d91SCole Faust * newly-allocated memory.
342*4b9c6d91SCole Faust *
343*4b9c6d91SCole Faust * @env Address of the environment to modify. Might be re-allocated to
344*4b9c6d91SCole Faust * make room for the new entry.
345*4b9c6d91SCole Faust * @name Name of the key to set.
346*4b9c6d91SCole Faust * @value The value to set.
347*4b9c6d91SCole Faust * @overwrite Whether to replace the existing value for @name. If non-zero and
348*4b9c6d91SCole Faust * the entry is already present, no changes will be made.
349*4b9c6d91SCole Faust *
350*4b9c6d91SCole Faust * Returns 0 and modifies *@env on success, returns an error code otherwise.
351*4b9c6d91SCole Faust */
352*4b9c6d91SCole Faust int minijail_setenv(char ***env, const char *name, const char *value,
353*4b9c6d91SCole Faust int overwrite);
354*4b9c6d91SCole Faust
355*4b9c6d91SCole Faust /*
356*4b9c6d91SCole Faust * getmultiline: This is like getline() but supports line wrapping with \.
357*4b9c6d91SCole Faust *
358*4b9c6d91SCole Faust * @lineptr Address of a buffer that a mutli-line is stored.
359*4b9c6d91SCole Faust * @n Number of bytes stored in *lineptr.
360*4b9c6d91SCole Faust * @stream Input stream to read from.
361*4b9c6d91SCole Faust *
362*4b9c6d91SCole Faust * Returns number of bytes read or -1 on failure to read (including EOF).
363*4b9c6d91SCole Faust */
364*4b9c6d91SCole Faust ssize_t getmultiline(char **lineptr, size_t *n, FILE *stream);
365*4b9c6d91SCole Faust
366*4b9c6d91SCole Faust /*
367*4b9c6d91SCole Faust * minjail_getenv: Get an environment variable from @envp. Semantics match the
368*4b9c6d91SCole Faust * standard getenv() function, but this operates on @envp, not the global
369*4b9c6d91SCole Faust * environment (usually referred to as `extern char **environ`).
370*4b9c6d91SCole Faust *
371*4b9c6d91SCole Faust * @env Address of the environment to read from.
372*4b9c6d91SCole Faust * @name Name of the key to get.
373*4b9c6d91SCole Faust *
374*4b9c6d91SCole Faust * Returns a pointer to the corresponding environment value. The caller must
375*4b9c6d91SCole Faust * take care not to modify the pointed value, as this points directly to memory
376*4b9c6d91SCole Faust * pointed to by @envp.
377*4b9c6d91SCole Faust * If the environment variable name is not found, returns NULL.
378*4b9c6d91SCole Faust */
379*4b9c6d91SCole Faust char *minijail_getenv(char **env, const char *name);
380*4b9c6d91SCole Faust
381*4b9c6d91SCole Faust /*
382*4b9c6d91SCole Faust * minjail_unsetenv: Clear the environment variable @name from the @envp array
383*4b9c6d91SCole Faust * of pointers to strings that have the KEY=VALUE format. If the operation is
384*4b9c6d91SCole Faust * successful, the array will contain one item less than before the call.
385*4b9c6d91SCole Faust * Only the first occurence is removed.
386*4b9c6d91SCole Faust *
387*4b9c6d91SCole Faust * @envp Address of the environment to clear the variable from.
388*4b9c6d91SCole Faust * @name Name of the variable to clear.
389*4b9c6d91SCole Faust *
390*4b9c6d91SCole Faust * Returns false and modifies *@envp on success, returns true otherwise.
391*4b9c6d91SCole Faust */
392*4b9c6d91SCole Faust bool minijail_unsetenv(char **envp, const char *name);
393*4b9c6d91SCole Faust
394*4b9c6d91SCole Faust #ifdef __cplusplus
395*4b9c6d91SCole Faust }; /* extern "C" */
396*4b9c6d91SCole Faust #endif
397*4b9c6d91SCole Faust
398*4b9c6d91SCole Faust #endif /* _UTIL_H_ */
399