]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/libpreopen/lib/internal.h
Remove more unsupported headers. (#123)
[wasi-libc.git] / libc-bottom-half / libpreopen / lib / internal.h
1 /**
2 * @file internal.h
3 * @brief Declarations of internal data structures and functions
4 *
5 * @cond internal
6 */
7
8 /*-
9 * Copyright (c) 2016-2017 Stanley Uche Godfrey
10 * Copyright (c) 2016-2018 Jonathan Anderson
11 * All rights reserved.
12 *
13 * This software was developed at Memorial University under the
14 * NSERC Discovery program (RGPIN-2015-06048).
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #ifndef LIBPO_INTERNAL_H
39 #define LIBPO_INTERNAL_H
40
41 #ifdef __wasilibc_unmodified_upstream
42 #include <sys/cdefs.h>
43 #endif
44
45 #ifdef __wasilibc_unmodified_upstream
46 #ifdef WITH_CAPSICUM
47 #include <sys/capsicum.h>
48 #endif
49 #else
50 // We do Capsicum-style rights-checking, though we use the WASI API directly
51 // rather than the Capsicum API.
52 #define WITH_CAPSICUM
53 #endif
54
55 #include <assert.h>
56 #include <stdbool.h>
57
58 #include "libpreopen.h"
59
60 /**
61 * An entry in a po_map.
62 *
63 * @internal
64 */
65 struct po_map_entry {
66 /**
67 * The name this file or directory is mapped to.
68 *
69 * This name should look like a path, but it does not necessarily need
70 * match to match the path it was originally obtained from.
71 */
72 const char *name;
73
74 /** File descriptor (which may be a directory) */
75 int fd;
76
77 #ifdef WITH_CAPSICUM
78 /** Capability rights associated with the file descriptor */
79 #ifdef __wasilibc_unmodified_upstream
80 cap_rights_t rights;
81 #else
82 __wasi_rights_t rights_base;
83 __wasi_rights_t rights_inheriting;
84 #endif
85 #endif
86 };
87
88 // Documented in external header file
89 struct po_map {
90 //! @internal
91 int refcount;
92 struct po_map_entry *entries;
93 size_t capacity;
94 size_t length;
95 };
96
97
98 /**
99 * Is a directory a prefix of a given path?
100 *
101 * @param dir a directory path, e.g., `/foo/bar`
102 * @param dirlen the length of @b dir
103 * @param path a path that may have @b dir as a prefix,
104 * e.g., `/foo/bar/baz`
105 *
106 * @internal
107 */
108 #ifdef __wasilibc_unmodified_upstream
109 #else
110 static
111 #endif
112 bool po_isprefix(const char *dir, size_t dirlen, const char *path);
113
114
115 /**
116 * Check that a @ref po_map is valid (assert out if it's not).
117 *
118 * @internal
119 */
120 #ifdef NDEBUG
121 #define po_map_assertvalid(...)
122 #else
123 #ifdef __wasilibc_unmodified_upstream
124 #else
125 static
126 #endif
127 void po_map_assertvalid(const struct po_map *);
128 #endif
129
130 /**
131 * Enlarge a @ref po_map's capacity.
132 *
133 * This results in new memory being allocated and existing entries being copied.
134 * If the allocation fails, the function will return NULL but the original
135 * map will remain valid.
136 *
137 * @internal
138 */
139 #ifdef __wasilibc_unmodified_upstream
140 #else
141 static
142 #endif
143 struct po_map* po_map_enlarge(struct po_map *map);
144
145 #ifdef __wasilibc_unmodified_upstream
146 /**
147 * Store an error message in the global "last error message" buffer.
148 *
149 * @internal
150 */
151 void po_errormessage(const char *msg);
152 #endif
153
154 /**
155 * Set the default map used by the libpreopen libc wrappers.
156 *
157 * If there is an existing default map, it will be freed before it is replaced.
158 * Passing NULL to this function will thus clear the default map.
159 */
160 #ifdef __wasilibc_unmodified_upstream
161 void po_set_libc_map(struct po_map *);
162 #endif
163
164 #endif /* LIBPO_INTERNAL_H */
165
166 /** @endcond */