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 * $FreeBSD$ 32 * 33 * Private definitions for libc, libc_r and libpthread. 34 * 35 */ 36 37 #ifndef _LIBC_PRIVATE_H_ 38 #define _LIBC_PRIVATE_H_ 39 #include <sys/_types.h> 40 #include <sys/_pthreadtypes.h> 41 42 /* 43 * The kernel doesn't expose PID_MAX to the user space. Save it here 44 * to allow to run a newer world on a pre-1400079 kernel. 45 */ 46 #define _PID_MAX 99999 47 48 /* 49 * This global flag is non-zero when a process has created one 50 * or more threads. It is used to avoid calling locking functions 51 * when they are not required. 52 */ 53 #ifndef __LIBC_ISTHREADED_DECLARED 54 #define __LIBC_ISTHREADED_DECLARED 55 extern int __isthreaded; 56 #endif 57 58 /* 59 * Elf_Auxinfo *__elf_aux_vector, the pointer to the ELF aux vector 60 * provided by kernel. Either set for us by rtld, or found at runtime 61 * on stack for static binaries. 62 * 63 * Type is void to avoid polluting whole libc with ELF types. 64 */ 65 extern void *__elf_aux_vector; 66 67 /* 68 * libc should use libc_dlopen internally, which respects a global 69 * flag where loading of new shared objects can be restricted. 70 */ 71 void *libc_dlopen(const char *, int); 72 73 /* 74 * For dynamic linker. 75 */ 76 void _rtld_error(const char *fmt, ...); 77 78 /* 79 * File lock contention is difficult to diagnose without knowing 80 * where locks were set. Allow a debug library to be built which 81 * records the source file and line number of each lock call. 82 */ 83 #ifdef _FLOCK_DEBUG 84 #define _FLOCKFILE(x) _flockfile_debug(x, __FILE__, __LINE__) 85 #else 86 #define _FLOCKFILE(x) _flockfile(x) 87 #endif 88 89 /* 90 * Macros for locking and unlocking FILEs. These test if the 91 * process is threaded to avoid locking when not required. 92 */ 93 #define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp) 94 #define FUNLOCKFILE(fp) if (__isthreaded) _funlockfile(fp) 95 96 struct _spinlock; 97 extern struct _spinlock __stdio_thread_lock __hidden; 98 #define STDIO_THREAD_LOCK() \ 99 do { \ 100 if (__isthreaded) \ 101 _SPINLOCK(&__stdio_thread_lock); \ 102 } while (0) 103 #define STDIO_THREAD_UNLOCK() \ 104 do { \ 105 if (__isthreaded) \ 106 _SPINUNLOCK(&__stdio_thread_lock); \ 107 } while (0) 108 109 void __libc_spinlock_stub(struct _spinlock *); 110 void __libc_spinunlock_stub(struct _spinlock *); 111 112 /* 113 * Indexes into the pthread jump table. 114 * 115 * Warning! If you change this type, you must also change the threads 116 * libraries that reference it (libc_r, libpthread). 117 */ 118 typedef enum { 119 PJT_ATFORK, 120 PJT_ATTR_DESTROY, 121 PJT_ATTR_GETDETACHSTATE, 122 PJT_ATTR_GETGUARDSIZE, 123 PJT_ATTR_GETINHERITSCHED, 124 PJT_ATTR_GETSCHEDPARAM, 125 PJT_ATTR_GETSCHEDPOLICY, 126 PJT_ATTR_GETSCOPE, 127 PJT_ATTR_GETSTACKADDR, 128 PJT_ATTR_GETSTACKSIZE, 129 PJT_ATTR_INIT, 130 PJT_ATTR_SETDETACHSTATE, 131 PJT_ATTR_SETGUARDSIZE, 132 PJT_ATTR_SETINHERITSCHED, 133 PJT_ATTR_SETSCHEDPARAM, 134 PJT_ATTR_SETSCHEDPOLICY, 135 PJT_ATTR_SETSCOPE, 136 PJT_ATTR_SETSTACKADDR, 137 PJT_ATTR_SETSTACKSIZE, 138 PJT_CANCEL, 139 PJT_CLEANUP_POP, 140 PJT_CLEANUP_PUSH, 141 PJT_COND_BROADCAST, 142 PJT_COND_DESTROY, 143 PJT_COND_INIT, 144 PJT_COND_SIGNAL, 145 PJT_COND_TIMEDWAIT, 146 PJT_COND_WAIT, 147 PJT_DETACH, 148 PJT_EQUAL, 149 PJT_EXIT, 150 PJT_GETSPECIFIC, 151 PJT_JOIN, 152 PJT_KEY_CREATE, 153 PJT_KEY_DELETE, 154 PJT_KILL, 155 PJT_MAIN_NP, 156 PJT_MUTEXATTR_DESTROY, 157 PJT_MUTEXATTR_INIT, 158 PJT_MUTEXATTR_SETTYPE, 159 PJT_MUTEX_DESTROY, 160 PJT_MUTEX_INIT, 161 PJT_MUTEX_LOCK, 162 PJT_MUTEX_TRYLOCK, 163 PJT_MUTEX_UNLOCK, 164 PJT_ONCE, 165 PJT_RWLOCK_DESTROY, 166 PJT_RWLOCK_INIT, 167 PJT_RWLOCK_RDLOCK, 168 PJT_RWLOCK_TRYRDLOCK, 169 PJT_RWLOCK_TRYWRLOCK, 170 PJT_RWLOCK_UNLOCK, 171 PJT_RWLOCK_WRLOCK, 172 PJT_SELF, 173 PJT_SETCANCELSTATE, 174 PJT_SETCANCELTYPE, 175 PJT_SETSPECIFIC, 176 PJT_SIGMASK, 177 PJT_TESTCANCEL, 178 PJT_CLEANUP_POP_IMP, 179 PJT_CLEANUP_PUSH_IMP, 180 PJT_CANCEL_ENTER, 181 PJT_CANCEL_LEAVE, 182 PJT_MUTEX_CONSISTENT, 183 PJT_MUTEXATTR_GETROBUST, 184 PJT_MUTEXATTR_SETROBUST, 185 PJT_GETTHREADID_NP, 186 PJT_ATTR_GET_NP, 187 PJT_MAX 188 } pjt_index_t; 189 190 typedef int (*pthread_func_t)(void); 191 typedef pthread_func_t pthread_func_entry_t[2]; 192 193 extern pthread_func_entry_t __thr_jtable[]; 194 195 void __set_error_selector(int *(*arg)(void)); 196 int _pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex, 197 void *(calloc_cb)(__size_t, __size_t)); 198 199 typedef int (*interpos_func_t)(void); 200 interpos_func_t *__libc_interposing_slot(int interposno); 201 extern interpos_func_t __libc_interposing[] __hidden; 202 203 enum { 204 INTERPOS_accept, 205 INTERPOS_accept4, 206 INTERPOS_aio_suspend, 207 INTERPOS_close, 208 INTERPOS_connect, 209 INTERPOS_fcntl, 210 INTERPOS_fsync, 211 INTERPOS_fork, 212 INTERPOS_msync, 213 INTERPOS_nanosleep, 214 INTERPOS_openat, 215 INTERPOS_poll, 216 INTERPOS_pselect, 217 INTERPOS_recvfrom, 218 INTERPOS_recvmsg, 219 INTERPOS_select, 220 INTERPOS_sendmsg, 221 INTERPOS_sendto, 222 INTERPOS_setcontext, 223 INTERPOS_sigaction, 224 INTERPOS_sigprocmask, 225 INTERPOS_sigsuspend, 226 INTERPOS_sigwait, 227 INTERPOS_sigtimedwait, 228 INTERPOS_sigwaitinfo, 229 INTERPOS_swapcontext, 230 INTERPOS_system, 231 INTERPOS_tcdrain, 232 INTERPOS_read, 233 INTERPOS_readv, 234 INTERPOS_wait4, 235 INTERPOS_write, 236 INTERPOS_writev, 237 INTERPOS__pthread_mutex_init_calloc_cb, 238 INTERPOS_spinlock, 239 INTERPOS_spinunlock, 240 INTERPOS_kevent, 241 INTERPOS_wait6, 242 INTERPOS_ppoll, 243 INTERPOS_map_stacks_exec, 244 INTERPOS_fdatasync, 245 INTERPOS_clock_nanosleep, 246 INTERPOS_distribute_static_tls, 247 INTERPOS_pdfork, 248 INTERPOS_MAX 249 }; 250 251 /* 252 * yplib internal interfaces 253 */ 254 #ifdef YP 255 int _yp_check(char **); 256 #endif 257 258 void __libc_start1(int, char *[], char *[], 259 void (*)(void), int (*)(int, char *[], char *[])) __dead2; 260 void __libc_start1_gcrt(int, char *[], char *[], 261 void (*)(void), int (*)(int, char *[], char *[]), 262 int *, int *) __dead2; 263 264 /* 265 * Initialise TLS for static programs 266 */ 267 void _init_tls(void); 268 269 /* 270 * Provides pthread_once()-like functionality for both single-threaded 271 * and multi-threaded applications. 272 */ 273 int _once(pthread_once_t *, void (*)(void)); 274 275 /* 276 * This is a pointer in the C run-time startup code. It is used 277 * by getprogname() and setprogname(). 278 */ 279 extern const char *__progname; 280 281 /* 282 * This function is used by the threading libraries to notify malloc that a 283 * thread is exiting. 284 */ 285 void _malloc_thread_cleanup(void); 286 287 /* 288 * This function is used by the threading libraries to notify libc that a 289 * thread is exiting, so its thread-local dtors should be called. 290 */ 291 void __cxa_thread_call_dtors(void); 292 int __cxa_thread_atexit_hidden(void (*dtor_func)(void *), void *obj, 293 void *dso_symbol) __hidden; 294 295 /* 296 * These functions are used by the threading libraries in order to protect 297 * malloc across fork(). 298 */ 299 void _malloc_prefork(void); 300 void _malloc_postfork(void); 301 302 void _malloc_first_thread(void); 303 304 /* 305 * Function to clean up streams, called from abort() and exit(). 306 */ 307 extern void (*__cleanup)(void) __hidden; 308 309 /* 310 * Get kern.osreldate to detect ABI revisions. Explicitly 311 * ignores value of $OSVERSION and caches result. 312 */ 313 int __getosreldate(void); 314 #include <sys/_types.h> 315 #include <sys/_sigset.h> 316 317 struct aiocb; 318 struct fd_set; 319 struct iovec; 320 struct kevent; 321 struct msghdr; 322 struct pollfd; 323 struct rusage; 324 struct sigaction; 325 struct sockaddr; 326 struct stat; 327 struct statfs; 328 struct timespec; 329 struct timeval; 330 struct timezone; 331 struct __siginfo; 332 struct __ucontext; 333 struct __wrusage; 334 enum idtype; 335 int __sys_aio_suspend(const struct aiocb * const[], int, 336 const struct timespec *); 337 int __sys_accept(int, struct sockaddr *, __socklen_t *); 338 int __sys_accept4(int, struct sockaddr *, __socklen_t *, int); 339 int __sys_clock_gettime(__clockid_t, struct timespec *ts); 340 int __sys_clock_nanosleep(__clockid_t, int, 341 const struct timespec *, struct timespec *); 342 int __sys_close(int); 343 int __sys_close_range(unsigned, unsigned, int); 344 int __sys_connect(int, const struct sockaddr *, __socklen_t); 345 int __sys_fcntl(int, int, ...); 346 int __sys_fdatasync(int); 347 int __sys_fstat(int fd, struct stat *); 348 int __sys_fstatfs(int fd, struct statfs *); 349 int __sys_fstatat(int, const char *, struct stat *, int); 350 int __sys_fsync(int); 351 __pid_t __sys_fork(void); 352 int __sys_ftruncate(int, __off_t); 353 __ssize_t __sys_getdirentries(int, char *, __size_t, __off_t *); 354 int __sys_getfsstat(struct statfs *, long, int); 355 int __sys_gettimeofday(struct timeval *, struct timezone *); 356 int __sys_kevent(int, const struct kevent *, int, struct kevent *, 357 int, const struct timespec *); 358 __off_t __sys_lseek(int, __off_t, int); 359 void *__sys_mmap(void *, __size_t, int, int, int, __off_t); 360 int __sys_msync(void *, __size_t, int); 361 int __sys_nanosleep(const struct timespec *, struct timespec *); 362 int __sys_open(const char *, int, ...); 363 int __sys_openat(int, const char *, int, ...); 364 int __sys_pdfork(int *, int); 365 int __sys_pselect(int, struct fd_set *, struct fd_set *, 366 struct fd_set *, const struct timespec *, 367 const __sigset_t *); 368 int __sys_ptrace(int, __pid_t, char *, int); 369 int __sys_poll(struct pollfd *, unsigned, int); 370 int __sys_ppoll(struct pollfd *, unsigned, const struct timespec *, 371 const __sigset_t *); 372 __ssize_t __sys_pread(int, void *, __size_t, __off_t); 373 __ssize_t __sys_pwrite(int, const void *, __size_t, __off_t); 374 __ssize_t __sys_read(int, void *, __size_t); 375 __ssize_t __sys_readv(int, const struct iovec *, int); 376 __ssize_t __sys_recv(int, void *, __size_t, int); 377 __ssize_t __sys_recvfrom(int, void *, __size_t, int, struct sockaddr *, 378 __socklen_t *); 379 __ssize_t __sys_recvmsg(int, struct msghdr *, int); 380 int __sys_sched_getcpu(void); 381 int __sys_select(int, struct fd_set *, struct fd_set *, 382 struct fd_set *, struct timeval *); 383 __ssize_t __sys_sendmsg(int, const struct msghdr *, int); 384 __ssize_t __sys_sendto(int, const void *, __size_t, int, 385 const struct sockaddr *, __socklen_t); 386 int __sys_setcontext(const struct __ucontext *); 387 int __sys_sigaction(int, const struct sigaction *, 388 struct sigaction *); 389 int __sys_sigprocmask(int, const __sigset_t *, __sigset_t *); 390 int __sys_sigsuspend(const __sigset_t *); 391 int __sys_sigtimedwait(const __sigset_t *, struct __siginfo *, 392 const struct timespec *); 393 int __sys_sigwait(const __sigset_t *, int *); 394 int __sys_sigwaitinfo(const __sigset_t *, struct __siginfo *); 395 int __sys___specialfd(int, const void *, __size_t); 396 int __sys_statfs(const char *, struct statfs *); 397 int __sys_swapcontext(struct __ucontext *, 398 const struct __ucontext *); 399 int __sys_thr_kill(long, int); 400 int __sys_thr_self(long *); 401 int __sys_truncate(const char *, __off_t); 402 __pid_t __sys_wait4(__pid_t, int *, int, struct rusage *); 403 __pid_t __sys_wait6(enum idtype, __id_t, int *, int, 404 struct __wrusage *, struct __siginfo *); 405 __ssize_t __sys_write(int, const void *, __size_t); 406 __ssize_t __sys_writev(int, const struct iovec *, int); 407 int __sys_shm_open2(const char *, int, __mode_t, int, const char *); 408 409 int __libc_sigaction(int, const struct sigaction *, 410 struct sigaction *) __hidden; 411 int __libc_sigprocmask(int, const __sigset_t *, __sigset_t *) 412 __hidden; 413 int __libc_sigsuspend(const __sigset_t *) __hidden; 414 int __libc_sigwait(const __sigset_t * __restrict, 415 int * restrict sig); 416 int __libc_system(const char *); 417 int __libc_tcdrain(int); 418 int __fcntl_compat(int fd, int cmd, ...); 419 420 int __sys_futimens(int fd, const struct timespec *times) __hidden; 421 int __sys_utimensat(int fd, const char *path, 422 const struct timespec *times, int flag) __hidden; 423 424 /* execve() with PATH processing to implement posix_spawnp() */ 425 int _execvpe(const char *, char * const *, char * const *); 426 427 int _elf_aux_info(int aux, void *buf, int buflen); 428 struct dl_phdr_info; 429 int __elf_phdr_match_addr(struct dl_phdr_info *, void *); 430 void __init_elf_aux_vector(void); 431 void __libc_map_stacks_exec(void); 432 void __libc_distribute_static_tls(__size_t, void *, __size_t, __size_t); 433 __uintptr_t __libc_static_tls_base(__size_t); 434 435 void _pthread_cancel_enter(int); 436 void _pthread_cancel_leave(int); 437 438 struct _pthread_cleanup_info; 439 void ___pthread_cleanup_push_imp(void (*)(void *), void *, 440 struct _pthread_cleanup_info *); 441 void ___pthread_cleanup_pop_imp(int); 442 443 void __throw_constraint_handler_s(const char * restrict msg, int error); 444 445 struct __nl_cat_d; 446 struct _xlocale; 447 struct __nl_cat_d *__catopen_l(const char *name, int type, 448 struct _xlocale *locale); 449 450 #endif /* _LIBC_PRIVATE_H_ */ 451