xref: /freebsd/lib/libc/include/libc_private.h (revision 0d4ad64077bcddcff5a170ee97273db95b9cab55)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * Private definitions for libc, libc_r and libpthread.
32  *
33  */
34 
35 #ifndef _LIBC_PRIVATE_H_
36 #define _LIBC_PRIVATE_H_
37 #include <sys/_types.h>
38 #include <sys/_pthreadtypes.h>
39 
40 #include <libsys.h>
41 
42 extern char **environ;
43 
44 /*
45  * The kernel doesn't expose PID_MAX to the user space. Save it here
46  * to allow to run a newer world on a pre-1400079 kernel.
47  */
48 #define	_PID_MAX	99999
49 
50 /*
51  * This global flag is non-zero when a process has created one
52  * or more threads. It is used to avoid calling locking functions
53  * when they are not required.
54  */
55 #ifndef __LIBC_ISTHREADED_DECLARED
56 #define __LIBC_ISTHREADED_DECLARED
57 extern int	__isthreaded;
58 #endif
59 
60 /*
61  * Elf_Auxinfo *__elf_aux_vector, the pointer to the ELF aux vector
62  * provided by kernel. Either set for us by rtld, or found at runtime
63  * on stack for static binaries.
64  *
65  * Type is void to avoid polluting whole libc with ELF types.
66  */
67 extern void	*__elf_aux_vector;
68 
69 /*
70  * libc should use libc_dlopen internally, which respects a global
71  * flag where loading of new shared objects can be restricted.
72  */
73 void *libc_dlopen(const char *, int);
74 
75 /*
76  * For dynamic linker.
77  */
78 void _rtld_error(const char *fmt, ...);
79 
80 /*
81  * File lock contention is difficult to diagnose without knowing
82  * where locks were set. Allow a debug library to be built which
83  * records the source file and line number of each lock call.
84  */
85 #ifdef	_FLOCK_DEBUG
86 #define _FLOCKFILE(x)	_flockfile_debug(x, __FILE__, __LINE__)
87 #else
88 #define _FLOCKFILE(x)	_flockfile(x)
89 #endif
90 
91 /*
92  * Macros for locking and unlocking FILEs. These test if the
93  * process is threaded to avoid locking when not required.
94  */
95 #define	FLOCKFILE(fp)		if (__isthreaded) _FLOCKFILE(fp)
96 #define	FUNLOCKFILE(fp)		if (__isthreaded) _funlockfile(fp)
97 
98 struct _spinlock;
99 extern struct _spinlock __stdio_thread_lock __hidden;
100 #define STDIO_THREAD_LOCK()				\
101 do {							\
102 	if (__isthreaded)				\
103 		_SPINLOCK(&__stdio_thread_lock);	\
104 } while (0)
105 #define STDIO_THREAD_UNLOCK()				\
106 do {							\
107 	if (__isthreaded)				\
108 		_SPINUNLOCK(&__stdio_thread_lock);	\
109 } while (0)
110 
111 void		__libc_spinlock_stub(struct _spinlock *);
112 void		__libc_spinunlock_stub(struct _spinlock *);
113 
114 /*
115  * Indexes into the pthread jump table.
116  *
117  * Warning! If you change this type, you must also change the threads
118  * libraries that reference it (libc_r, libpthread).
119  */
120 typedef enum {
121 	PJT_ATFORK,
122 	PJT_ATTR_DESTROY,
123 	PJT_ATTR_GETDETACHSTATE,
124 	PJT_ATTR_GETGUARDSIZE,
125 	PJT_ATTR_GETINHERITSCHED,
126 	PJT_ATTR_GETSCHEDPARAM,
127 	PJT_ATTR_GETSCHEDPOLICY,
128 	PJT_ATTR_GETSCOPE,
129 	PJT_ATTR_GETSTACKADDR,
130 	PJT_ATTR_GETSTACKSIZE,
131 	PJT_ATTR_INIT,
132 	PJT_ATTR_SETDETACHSTATE,
133 	PJT_ATTR_SETGUARDSIZE,
134 	PJT_ATTR_SETINHERITSCHED,
135 	PJT_ATTR_SETSCHEDPARAM,
136 	PJT_ATTR_SETSCHEDPOLICY,
137 	PJT_ATTR_SETSCOPE,
138 	PJT_ATTR_SETSTACKADDR,
139 	PJT_ATTR_SETSTACKSIZE,
140 	PJT_CANCEL,
141 	PJT_CLEANUP_POP,
142 	PJT_CLEANUP_PUSH,
143 	PJT_COND_BROADCAST,
144 	PJT_COND_DESTROY,
145 	PJT_COND_INIT,
146 	PJT_COND_SIGNAL,
147 	PJT_COND_TIMEDWAIT,
148 	PJT_COND_WAIT,
149 	PJT_DETACH,
150 	PJT_EQUAL,
151 	PJT_EXIT,
152 	PJT_GETSPECIFIC,
153 	PJT_JOIN,
154 	PJT_KEY_CREATE,
155 	PJT_KEY_DELETE,
156 	PJT_KILL,
157 	PJT_MAIN_NP,
158 	PJT_MUTEXATTR_DESTROY,
159 	PJT_MUTEXATTR_INIT,
160 	PJT_MUTEXATTR_SETTYPE,
161 	PJT_MUTEX_DESTROY,
162 	PJT_MUTEX_INIT,
163 	PJT_MUTEX_LOCK,
164 	PJT_MUTEX_TRYLOCK,
165 	PJT_MUTEX_UNLOCK,
166 	PJT_ONCE,
167 	PJT_RWLOCK_DESTROY,
168 	PJT_RWLOCK_INIT,
169 	PJT_RWLOCK_RDLOCK,
170 	PJT_RWLOCK_TRYRDLOCK,
171 	PJT_RWLOCK_TRYWRLOCK,
172 	PJT_RWLOCK_UNLOCK,
173 	PJT_RWLOCK_WRLOCK,
174 	PJT_SELF,
175 	PJT_SETCANCELSTATE,
176 	PJT_SETCANCELTYPE,
177 	PJT_SETSPECIFIC,
178 	PJT_SIGMASK,
179 	PJT_TESTCANCEL,
180 	PJT_CLEANUP_POP_IMP,
181 	PJT_CLEANUP_PUSH_IMP,
182 	PJT_CANCEL_ENTER,
183 	PJT_CANCEL_LEAVE,
184 	PJT_MUTEX_CONSISTENT,
185 	PJT_MUTEXATTR_GETROBUST,
186 	PJT_MUTEXATTR_SETROBUST,
187 	PJT_GETTHREADID_NP,
188 	PJT_ATTR_GET_NP,
189 	PJT_GETNAME_NP,
190 	PJT_MAX
191 } pjt_index_t;
192 
193 typedef int (*pthread_func_t)(void);
194 typedef pthread_func_t pthread_func_entry_t[2];
195 
196 extern pthread_func_entry_t __thr_jtable[];
197 
198 void	__set_error_selector(int *(*arg)(void));
199 int	_pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex,
200 	    void *(calloc_cb)(__size_t, __size_t));
201 
202 typedef int (*interpos_func_t)(void);
203 interpos_func_t *__libc_interposing_slot(int interposno);
204 extern interpos_func_t __libc_interposing[] __hidden;
205 interpos_func_t *__libsys_interposing_slot(int interposno);
206 
207 enum {
208 	INTERPOS_accept,
209 	INTERPOS_accept4,
210 	INTERPOS_aio_suspend,
211 	INTERPOS_close,
212 	INTERPOS_connect,
213 	INTERPOS_fcntl,
214 	INTERPOS_fsync,
215 	INTERPOS_fork,
216 	INTERPOS_msync,
217 	INTERPOS_nanosleep,
218 	INTERPOS_openat,
219 	INTERPOS_poll,
220 	INTERPOS_pselect,
221 	INTERPOS_recvfrom,
222 	INTERPOS_recvmsg,
223 	INTERPOS_select,
224 	INTERPOS_sendmsg,
225 	INTERPOS_sendto,
226 	INTERPOS_setcontext,
227 	INTERPOS_sigaction,
228 	INTERPOS_sigprocmask,
229 	INTERPOS_sigsuspend,
230 	INTERPOS_sigwait,
231 	INTERPOS_sigtimedwait,
232 	INTERPOS_sigwaitinfo,
233 	INTERPOS_swapcontext,
234 	INTERPOS_system,
235 	INTERPOS_tcdrain,
236 	INTERPOS_read,
237 	INTERPOS_readv,
238 	INTERPOS_wait4,
239 	INTERPOS_write,
240 	INTERPOS_writev,
241 	INTERPOS__pthread_mutex_init_calloc_cb,
242 	INTERPOS_spinlock,
243 	INTERPOS_spinunlock,
244 	INTERPOS_kevent,
245 	INTERPOS_wait6,
246 	INTERPOS_ppoll,
247 	INTERPOS_map_stacks_exec,
248 	INTERPOS_fdatasync,
249 	INTERPOS_clock_nanosleep,
250 	INTERPOS_distribute_static_tls,
251 	INTERPOS_pdfork,
252 	INTERPOS_MAX
253 };
254 
255 #define	_INTERPOS_SYS(type, idx, ...)				\
256     ((type *)*(__libc_interposing_slot(idx)))(__VA_ARGS__)
257 #define	INTERPOS_SYS(syscall, ...)				\
258     _INTERPOS_SYS(__sys_## syscall ##_t, INTERPOS_## syscall	\
259     __VA_OPT__(,) __VA_ARGS__)
260 
261 /*
262  * yplib internal interfaces
263  */
264 #ifdef YP
265 int _yp_check(char **);
266 #endif
267 
268 void __libc_start1(int, char *[], char *[],
269     void (*)(void), int (*)(int, char *[], char *[])) __dead2;
270 void __libc_start1_gcrt(int, char *[], char *[],
271     void (*)(void), int (*)(int, char *[], char *[]),
272     int *, int *) __dead2;
273 
274 /*
275  * Initialise TLS for static programs
276  */
277 void _init_tls(void);
278 
279 /*
280  * Provides pthread_once()-like functionality for both single-threaded
281  * and multi-threaded applications.
282  */
283 int _once(pthread_once_t *, void (*)(void));
284 
285 /*
286  * This is a pointer in the C run-time startup code. It is used
287  * by getprogname() and setprogname().
288  */
289 extern const char *__progname;
290 
291 /*
292  * This function is used by the threading libraries to notify malloc that a
293  * thread is exiting.
294  */
295 void _malloc_thread_cleanup(void);
296 
297 /*
298  * This function is used by the threading libraries to notify libc that a
299  * thread is exiting, so its thread-local dtors should be called.
300  */
301 void __cxa_thread_call_dtors(void);
302 int __cxa_thread_atexit_hidden(void (*dtor_func)(void *), void *obj,
303     void *dso_symbol) __hidden;
304 
305 /*
306  * These functions are used by the threading libraries in order to protect
307  * malloc across fork().
308  */
309 void _malloc_prefork(void);
310 void _malloc_postfork(void);
311 
312 void _malloc_first_thread(void);
313 
314 /*
315  * Function to clean up streams, called from abort() and exit().
316  */
317 extern void (*__cleanup)(void) __hidden;
318 
319 /*
320  * Get kern.osreldate to detect ABI revisions.  Explicitly
321  * ignores value of $OSVERSION and caches result.
322  */
323 int __getosreldate(void);
324 #include <sys/_types.h>
325 #include <sys/_sigset.h>
326 
327 struct aiocb;
328 struct fd_set;
329 struct iovec;
330 struct kevent;
331 struct msghdr;
332 struct pollfd;
333 struct rusage;
334 struct sigaction;
335 struct sockaddr;
336 struct stat;
337 struct statfs;
338 struct timespec;
339 struct timeval;
340 struct timezone;
341 struct __siginfo;
342 struct __ucontext;
343 struct __wrusage;
344 enum idtype;
345 
346 int		__libc_sigaction(int, const struct sigaction *,
347 		    struct sigaction *) __hidden;
348 int		__libc_sigprocmask(int, const __sigset_t *, __sigset_t *)
349 		    __hidden;
350 int		__libc_sigsuspend(const __sigset_t *) __hidden;
351 int		__libsys_sigwait(const __sigset_t *, int *) __hidden;
352 int		__libc_system(const char *);
353 int		__libc_tcdrain(int);
354 
355 int _elf_aux_info(int aux, void *buf, int buflen);
356 struct dl_phdr_info;
357 int __elf_phdr_match_addr(struct dl_phdr_info *, void *);
358 void __init_elf_aux_vector(void);
359 void __libc_map_stacks_exec(void);
360 void __libc_distribute_static_tls(__size_t, void *, __size_t, __size_t);
361 __uintptr_t __libc_static_tls_base(__size_t);
362 
363 void	_pthread_cancel_enter(int);
364 void	_pthread_cancel_leave(int);
365 
366 struct _pthread_cleanup_info;
367 void	___pthread_cleanup_push_imp(void (*)(void *), void *,
368 	    struct _pthread_cleanup_info *);
369 void	___pthread_cleanup_pop_imp(int);
370 
371 void __throw_constraint_handler_s(const char * restrict msg, int error);
372 
373 struct __nl_cat_d;
374 struct _xlocale;
375 struct __nl_cat_d *__catopen_l(const char *name, int type,
376 	    struct _xlocale *locale);
377 int __strerror_rl(int errnum, char *strerrbuf, size_t buflen,
378 	    struct _xlocale *locale);
379 
380 #endif /* _LIBC_PRIVATE_H_ */
381