libc_private.h revision 281454
1228753Smm/*
2228753Smm * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
3232153Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm * 3. Neither the name of the author nor the names of any co-contributors
14228753Smm *    may be used to endorse or promote products derived from this software
15228753Smm *    without specific prior written permission.
16228753Smm *
17228753Smm * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20228753Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27228753Smm * SUCH DAMAGE.
28228763Smm *
29228753Smm * $FreeBSD: stable/10/lib/libc/include/libc_private.h 281454 2015-04-12 06:52:43Z kib $
30228753Smm *
31228753Smm * Private definitions for libc, libc_r and libpthread.
32228753Smm *
33228753Smm */
34228753Smm
35228753Smm#ifndef _LIBC_PRIVATE_H_
36228753Smm#define _LIBC_PRIVATE_H_
37228753Smm#include <sys/_types.h>
38228753Smm#include <sys/_pthreadtypes.h>
39228753Smm
40228753Smm/*
41228753Smm * This global flag is non-zero when a process has created one
42228753Smm * or more threads. It is used to avoid calling locking functions
43228753Smm * when they are not required.
44232153Smm */
45228753Smmextern int	__isthreaded;
46228753Smm
47358090Smm/*
48228753Smm * Elf_Auxinfo *__elf_aux_vector, the pointer to the ELF aux vector
49228753Smm * provided by kernel. Either set for us by rtld, or found at runtime
50228753Smm * on stack for static binaries.
51228753Smm *
52232153Smm * Type is void to avoid polluting whole libc with ELF types.
53232153Smm */
54232153Smmextern void	*__elf_aux_vector;
55232153Smm
56228753Smm/*
57228753Smm * libc should use libc_dlopen internally, which respects a global
58228753Smm * flag where loading of new shared objects can be restricted.
59228753Smm */
60228753Smmvoid *libc_dlopen(const char *, int);
61228753Smm
62228753Smm/*
63228753Smm * For dynamic linker.
64228753Smm */
65228753Smmvoid _rtld_error(const char *fmt, ...);
66228753Smm
67228753Smm/*
68228753Smm * File lock contention is difficult to diagnose without knowing
69228753Smm * where locks were set. Allow a debug library to be built which
70228753Smm * records the source file and line number of each lock call.
71228753Smm */
72228753Smm#ifdef	_FLOCK_DEBUG
73228753Smm#define _FLOCKFILE(x)	_flockfile_debug(x, __FILE__, __LINE__)
74228753Smm#else
75228753Smm#define _FLOCKFILE(x)	_flockfile(x)
76228753Smm#endif
77228753Smm
78228753Smm/*
79228753Smm * Macros for locking and unlocking FILEs. These test if the
80228753Smm * process is threaded to avoid locking when not required.
81228753Smm */
82228753Smm#define	FLOCKFILE(fp)		if (__isthreaded) _FLOCKFILE(fp)
83228753Smm#define	FUNLOCKFILE(fp)		if (__isthreaded) _funlockfile(fp)
84228753Smm
85228753Smmstruct _spinlock;
86228753Smmextern struct _spinlock __stdio_thread_lock __hidden;
87228753Smm#define STDIO_THREAD_LOCK()				\
88228753Smmdo {							\
89228753Smm	if (__isthreaded)				\
90228753Smm		_SPINLOCK(&__stdio_thread_lock);	\
91228753Smm} while (0)
92228753Smm#define STDIO_THREAD_UNLOCK()				\
93228753Smmdo {							\
94228753Smm	if (__isthreaded)				\
95228753Smm		_SPINUNLOCK(&__stdio_thread_lock);	\
96228753Smm} while (0)
97228753Smm
98228753Smmvoid		__libc_spinlock_stub(struct _spinlock *);
99228753Smmvoid		__libc_spinunlock_stub(struct _spinlock *);
100228753Smm
101228753Smm/*
102228753Smm * Indexes into the pthread jump table.
103228753Smm *
104228753Smm * Warning! If you change this type, you must also change the threads
105228753Smm * libraries that reference it (libc_r, libpthread).
106228753Smm */
107228753Smmtypedef enum {
108228753Smm	PJT_ATFORK,
109228753Smm	PJT_ATTR_DESTROY,
110228753Smm	PJT_ATTR_GETDETACHSTATE,
111228753Smm	PJT_ATTR_GETGUARDSIZE,
112228753Smm	PJT_ATTR_GETINHERITSCHED,
113228753Smm	PJT_ATTR_GETSCHEDPARAM,
114228753Smm	PJT_ATTR_GETSCHEDPOLICY,
115228753Smm	PJT_ATTR_GETSCOPE,
116228753Smm	PJT_ATTR_GETSTACKADDR,
117228753Smm	PJT_ATTR_GETSTACKSIZE,
118311042Smm	PJT_ATTR_INIT,
119228753Smm	PJT_ATTR_SETDETACHSTATE,
120311042Smm	PJT_ATTR_SETGUARDSIZE,
121228753Smm	PJT_ATTR_SETINHERITSCHED,
122228753Smm	PJT_ATTR_SETSCHEDPARAM,
123228753Smm	PJT_ATTR_SETSCHEDPOLICY,
124228753Smm	PJT_ATTR_SETSCOPE,
125228753Smm	PJT_ATTR_SETSTACKADDR,
126228753Smm	PJT_ATTR_SETSTACKSIZE,
127228753Smm	PJT_CANCEL,
128228753Smm	PJT_CLEANUP_POP,
129228753Smm	PJT_CLEANUP_PUSH,
130228753Smm	PJT_COND_BROADCAST,
131228753Smm	PJT_COND_DESTROY,
132228753Smm	PJT_COND_INIT,
133228753Smm	PJT_COND_SIGNAL,
134228753Smm	PJT_COND_TIMEDWAIT,
135228753Smm	PJT_COND_WAIT,
136228753Smm	PJT_DETACH,
137228753Smm	PJT_EQUAL,
138228753Smm	PJT_EXIT,
139228753Smm	PJT_GETSPECIFIC,
140228753Smm	PJT_JOIN,
141228753Smm	PJT_KEY_CREATE,
142228753Smm	PJT_KEY_DELETE,
143228753Smm	PJT_KILL,
144228753Smm	PJT_MAIN_NP,
145228753Smm	PJT_MUTEXATTR_DESTROY,
146228753Smm	PJT_MUTEXATTR_INIT,
147228753Smm	PJT_MUTEXATTR_SETTYPE,
148228753Smm	PJT_MUTEX_DESTROY,
149228753Smm	PJT_MUTEX_INIT,
150228753Smm	PJT_MUTEX_LOCK,
151228753Smm	PJT_MUTEX_TRYLOCK,
152228753Smm	PJT_MUTEX_UNLOCK,
153232153Smm	PJT_ONCE,
154232153Smm	PJT_RWLOCK_DESTROY,
155228753Smm	PJT_RWLOCK_INIT,
156228753Smm	PJT_RWLOCK_RDLOCK,
157228753Smm	PJT_RWLOCK_TRYRDLOCK,
158232153Smm	PJT_RWLOCK_TRYWRLOCK,
159232153Smm	PJT_RWLOCK_UNLOCK,
160228753Smm	PJT_RWLOCK_WRLOCK,
161228753Smm	PJT_SELF,
162228753Smm	PJT_SETCANCELSTATE,
163228753Smm	PJT_SETCANCELTYPE,
164228753Smm	PJT_SETSPECIFIC,
165228753Smm	PJT_SIGMASK,
166228753Smm	PJT_TESTCANCEL,
167228753Smm	PJT_CLEANUP_POP_IMP,
168228753Smm	PJT_CLEANUP_PUSH_IMP,
169228753Smm	PJT_CANCEL_ENTER,
170228753Smm	PJT_CANCEL_LEAVE,
171228753Smm	PJT_MAX
172228753Smm} pjt_index_t;
173232153Smm
174232153Smmtypedef int (*pthread_func_t)(void);
175232153Smmtypedef pthread_func_t pthread_func_entry_t[2];
176228753Smm
177232153Smmextern pthread_func_entry_t __thr_jtable[];
178232153Smm
179228753Smmvoid	__set_error_selector(int *(*arg)(void));
180228753Smmint	_pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex,
181228753Smm	    void *(calloc_cb)(__size_t, __size_t));
182232153Smm
183232153Smmtypedef int (*interpos_func_t)(void);
184232153Smminterpos_func_t *__libc_interposing_slot(int interposno);
185228753Smmextern interpos_func_t __libc_interposing[] __hidden;
186228753Smm
187228753Smmenum {
188311042Smm	INTERPOS_accept,
189228753Smm	INTERPOS_accept4,
190232153Smm	INTERPOS_aio_suspend,
191232153Smm	INTERPOS_close,
192228753Smm	INTERPOS_connect,
193228753Smm	INTERPOS_fcntl,
194228753Smm	INTERPOS_fsync,
195228753Smm	INTERPOS_fork,
196232153Smm	INTERPOS_msync,
197228753Smm	INTERPOS_nanosleep,
198228753Smm	INTERPOS_openat,
199232153Smm	INTERPOS_poll,
200232153Smm	INTERPOS_pselect,
201228753Smm	INTERPOS_recvfrom,
202228753Smm	INTERPOS_recvmsg,
203228753Smm	INTERPOS_select,
204228753Smm	INTERPOS_sendmsg,
205228753Smm	INTERPOS_sendto,
206228753Smm	INTERPOS_setcontext,
207228753Smm	INTERPOS_sigaction,
208232153Smm	INTERPOS_sigprocmask,
209232153Smm	INTERPOS_sigsuspend,
210232153Smm	INTERPOS_sigwait,
211232153Smm	INTERPOS_sigtimedwait,
212232153Smm	INTERPOS_sigwaitinfo,
213232153Smm	INTERPOS_swapcontext,
214232153Smm	INTERPOS_system,
215232153Smm	INTERPOS_tcdrain,
216232153Smm	INTERPOS_read,
217232153Smm	INTERPOS_readv,
218232153Smm	INTERPOS_wait4,
219232153Smm	INTERPOS_write,
220232153Smm	INTERPOS_writev,
221232153Smm	INTERPOS__pthread_mutex_init_calloc_cb,
222232153Smm	INTERPOS_spinlock,
223232153Smm	INTERPOS_spinunlock,
224232153Smm	INTERPOS_kevent,
225232153Smm	INTERPOS_MAX
226232153Smm};
227232153Smm
228232153Smm/*
229232153Smm * yplib internal interfaces
230232153Smm */
231232153Smm#ifdef YP
232232153Smmint _yp_check(char **);
233232153Smm#endif
234232153Smm
235232153Smm/*
236232153Smm * Initialise TLS for static programs
237228753Smm */
238228753Smmvoid _init_tls(void);
239228753Smm
240228753Smm/*
241228753Smm * Provides pthread_once()-like functionality for both single-threaded
242232153Smm * and multi-threaded applications.
243232153Smm */
244228753Smmint _once(pthread_once_t *, void (*)(void));
245228753Smm
246228753Smm/*
247232153Smm * Set the TLS thread pointer
248232153Smm */
249232153Smmvoid _set_tp(void *tp);
250232153Smm
251232153Smm/*
252232153Smm * This is a pointer in the C run-time startup code. It is used
253232153Smm * by getprogname() and setprogname().
254232153Smm */
255232153Smmextern const char *__progname;
256232153Smm
257232153Smm/*
258232153Smm * This function is used by the threading libraries to notify malloc that a
259232153Smm * thread is exiting.
260232153Smm */
261232153Smmvoid _malloc_thread_cleanup(void);
262232153Smm
263232153Smm/*
264232153Smm * These functions are used by the threading libraries in order to protect
265228753Smm * malloc across fork().
266228753Smm */
267228753Smmvoid _malloc_prefork(void);
268228753Smmvoid _malloc_postfork(void);
269228753Smm
270228753Smmvoid _malloc_first_thread(void);
271228753Smm
272228753Smm/*
273232153Smm * Function to clean up streams, called from abort() and exit().
274228753Smm */
275228753Smmextern void (*__cleanup)(void) __hidden;
276228753Smm
277228753Smm/*
278232153Smm * Get kern.osreldate to detect ABI revisions.  Explicitly
279232153Smm * ignores value of $OSVERSION and caches result.  Prototypes
280232153Smm * for the wrapped "new" pad-less syscalls are here for now.
281232153Smm */
282232153Smmint __getosreldate(void);
283232153Smm#include <sys/_types.h>
284232153Smm#include <sys/_sigset.h>
285232153Smm
286232153Smm/* With pad */
287232153Smm__off_t	__sys_freebsd6_lseek(int, int, __off_t, int);
288232153Smmint	__sys_freebsd6_ftruncate(int, int, __off_t);
289228753Smmint	__sys_freebsd6_truncate(const char *, int, __off_t);
290232153Smm__ssize_t __sys_freebsd6_pread(int, void *, __size_t, int, __off_t);
291232153Smm__ssize_t __sys_freebsd6_pwrite(int, const void *, __size_t, int, __off_t);
292228753Smmvoid *	__sys_freebsd6_mmap(void *, __size_t, int, int, int, int, __off_t);
293228753Smm
294232153Smmstruct aiocb;
295232153Smmstruct fd_set;
296232153Smmstruct iovec;
297232153Smmstruct kevent;
298232153Smmstruct msghdr;
299232153Smmstruct pollfd;
300232153Smmstruct rusage;
301232153Smmstruct sigaction;
302232153Smmstruct sockaddr;
303232153Smmstruct timespec;
304232153Smmstruct timeval;
305232153Smmstruct timezone;
306232153Smmstruct __siginfo;
307232153Smmstruct __ucontext;
308232153Smmint		__sys_aio_suspend(const struct aiocb * const[], int,
309232153Smm		    const struct timespec *);
310305192Smmint		__sys_accept(int, struct sockaddr *, __socklen_t *);
311232153Smmint		__sys_accept4(int, struct sockaddr *, __socklen_t *, int);
312232153Smmint		__sys_clock_gettime(__clockid_t, struct timespec *ts);
313232153Smmint		__sys_close(int);
314232153Smmint		__sys_connect(int, const struct sockaddr *, __socklen_t);
315232153Smmint		__sys_fcntl(int, int, ...);
316232153Smmint		__sys_fsync(int);
317232153Smm__pid_t		__sys_fork(void);
318232153Smmint		__sys_ftruncate(int, __off_t);
319232153Smmint		__sys_gettimeofday(struct timeval *, struct timezone *);
320232153Smmint		__sys_kevent(int, const struct kevent *, int, struct kevent *,
321232153Smm		    int, const struct timespec *);
322232153Smm__off_t		__sys_lseek(int, __off_t, int);
323232153Smmvoid	       *__sys_mmap(void *, __size_t, int, int, int, __off_t);
324232153Smmint		__sys_msync(void *, __size_t, int);
325232153Smmint		__sys_nanosleep(const struct timespec *, struct timespec *);
326232153Smmint		__sys_open(const char *, int, ...);
327232153Smmint		__sys_openat(int, const char *, int, ...);
328232153Smmint		__sys_pselect(int, struct fd_set *, struct fd_set *,
329232153Smm		    struct fd_set *, const struct timespec *,
330232153Smm		    const __sigset_t *);
331232153Smmint		__sys_poll(struct pollfd *, unsigned, int);
332232153Smm__ssize_t	__sys_pread(int, void *, __size_t, __off_t);
333232153Smm__ssize_t	__sys_pwrite(int, const void *, __size_t, __off_t);
334232153Smm__ssize_t	__sys_read(int, void *, __size_t);
335228753Smm__ssize_t	__sys_readv(int, const struct iovec *, int);
336228753Smm__ssize_t	__sys_recv(int, void *, __size_t, int);
337228753Smm__ssize_t	__sys_recvfrom(int, void *, __size_t, int, struct sockaddr *,
338232153Smm		    __socklen_t *);
339311042Smm__ssize_t	__sys_recvmsg(int, struct msghdr *, int);
340232153Smmint		__sys_select(int, struct fd_set *, struct fd_set *,
341232153Smm		    struct fd_set *, struct timeval *);
342232153Smm__ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
343232153Smm__ssize_t	__sys_sendto(int, const void *, __size_t, int,
344232153Smm		    const struct sockaddr *, __socklen_t);
345232153Smmint		__sys_setcontext(const struct __ucontext *);
346232153Smmint		__sys_sigaction(int, const struct sigaction *,
347232153Smm		    struct sigaction *);
348232153Smmint		__sys_sigprocmask(int, const __sigset_t *, __sigset_t *);
349232153Smmint		__sys_sigsuspend(const __sigset_t *);
350232153Smmint		__sys_sigtimedwait(const __sigset_t *, struct __siginfo *,
351232153Smm		    const struct timespec *);
352232153Smmint		__sys_sigwait(const __sigset_t *, int *);
353232153Smmint		__sys_sigwaitinfo(const __sigset_t *, struct __siginfo *);
354232153Smmint		__sys_swapcontext(struct __ucontext *,
355232153Smm		    const struct __ucontext *);
356344674Smmint		__sys_thr_kill(long, int);
357228753Smmint		__sys_thr_self(long *);
358232153Smmint		__sys_truncate(const char *, __off_t);
359232153Smm__pid_t		__sys_wait4(__pid_t, int *, int, struct rusage *);
360232153Smm__ssize_t	__sys_write(int, const void *, __size_t);
361344674Smm__ssize_t	__sys_writev(int, const struct iovec *, int);
362228753Smm
363232153Smmint		__libc_sigwait(const __sigset_t * __restrict,
364228753Smm		    int * restrict sig);
365228753Smmint		__libc_system(const char *);
366228753Smmint		__libc_tcdrain(int);
367228753Smmint		__fcntl_compat(int fd, int cmd, ...);
368228753Smm
369344674Smm/* execve() with PATH processing to implement posix_spawnp() */
370228753Smmint _execvpe(const char *, char * const *, char * const *);
371228753Smm
372228753Smmint _elf_aux_info(int aux, void *buf, int buflen);
373228753Smmstruct dl_phdr_info;
374228753Smmint __elf_phdr_match_addr(struct dl_phdr_info *, void *);
375228753Smmvoid __init_elf_aux_vector(void);
376228753Smm
377228753Smmvoid	_pthread_cancel_enter(int);
378228753Smmvoid	_pthread_cancel_leave(int);
379228753Smm
380228753Smm#endif /* _LIBC_PRIVATE_H_ */
381228753Smm