OSDN Git Service

mkostemp: fix implementation
[uclinux-h8/uClibc.git] / ldso / include / dl-syscall.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
4  *
5  * GNU Lesser General Public License version 2.1 or later.
6  */
7
8 #ifndef _DL_SYSCALL_H
9 #define _DL_SYSCALL_H
10
11 /* We can't use the real errno in ldso, since it has not yet
12  * been dynamicly linked in yet. */
13 #include "sys/syscall.h"
14 extern int _dl_errno;
15 #undef __set_errno
16 #define __set_errno(X) {(_dl_errno) = (X);}
17
18 /* Pull in the arch specific syscall implementation */
19 #include <dl-syscalls.h>
20 /*  For MAP_ANONYMOUS -- differs between platforms */
21 #define _SYS_MMAN_H 1
22 #include <bits/mman.h>
23
24 #ifdef __ARCH_HAS_DEPRECATED_SYSCALLS__
25 /* Pull in whatever this particular arch's kernel thinks the kernel version of
26  * struct stat should look like.  It turns out that each arch has a different
27  * opinion on the subject, and different kernel revs use different names... */
28 #if defined(__sparc_v9__) && (__WORDSIZE == 64)
29 #define kernel_stat64 stat
30 #else
31 #define kernel_stat stat
32 #endif
33 #include <bits/kernel_stat.h>
34 #include <bits/kernel_types.h>
35
36 /* Protection bits.  */
37 #define S_ISUID         04000   /* Set user ID on execution.  */
38 #define S_ISGID         02000   /* Set group ID on execution.  */
39
40 #else
41 /* 1. common-generic ABI doesn't need kernel_stat translation
42  * 3. S_IS?ID already provided by stat.h
43  */
44 #include <sys/stat.h>
45 #endif
46
47
48 /* Here are the definitions for some syscalls that are used
49    by the dynamic linker.  The idea is that we want to be able
50    to call these before the errno symbol is dynamicly linked, so
51    we use our own version here.  Note that we cannot assume any
52    dynamic linking at all, so we cannot return any error codes.
53    We just punt if there is an error. */
54 #define __NR__dl_exit __NR_exit
55 static __always_inline _syscall1(void, _dl_exit, int, status)
56
57 #define __NR__dl_close __NR_close
58 static __always_inline _syscall1(int, _dl_close, int, fd)
59
60 #if defined __NR_openat && !defined __NR_open
61 static __always_inline int _dl_open(const char *fn,
62                                                 int flags, __kernel_mode_t mode)
63 {
64         return INLINE_SYSCALL(openat, 4, AT_FDCWD, fn, flags, mode);
65 }
66
67 #elif defined __NR_open
68 # define __NR__dl_open __NR_open
69 static __always_inline _syscall3(int, _dl_open, const char *, fn, int, flags,
70                         __kernel_mode_t, mode)
71 #endif
72
73 #define __NR__dl_write __NR_write
74 static __always_inline _syscall3(unsigned long, _dl_write, int, fd,
75                         const void *, buf, unsigned long, count)
76
77 #define __NR__dl_read __NR_read
78 static __always_inline _syscall3(unsigned long, _dl_read, int, fd,
79                         const void *, buf, unsigned long, count)
80
81 #define __NR__dl_mprotect __NR_mprotect
82 static __always_inline _syscall3(int, _dl_mprotect, const void *, addr,
83                         unsigned long, len, int, prot)
84
85 #if defined __NR_fstatat64 && !defined __NR_stat
86 # define __NR__dl_fstatat64 __NR_fstatat64
87 static __always_inline _syscall4(int, _dl_fstatat64, int, fd, const char *,
88                                  fn, struct stat *, stat, int, flags)
89
90 static __always_inline int _dl_stat(const char *file_name,
91                         struct stat *buf)
92 {
93         return _dl_fstatat64(AT_FDCWD, file_name, buf, 0);
94 }
95 #elif defined __NR_stat
96 # define __NR__dl_stat __NR_stat
97 static __always_inline _syscall2(int, _dl_stat, const char *, file_name,
98                         struct stat *, buf)
99 #endif
100
101 #if defined __NR_fstat64 && !defined __NR_fstat
102 # define __NR__dl_fstat __NR_fstat64
103 #elif defined __NR_fstat
104 # define __NR__dl_fstat __NR_fstat
105 #endif
106 static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
107
108 #define __NR__dl_munmap __NR_munmap
109 static __always_inline _syscall2(int, _dl_munmap, void *, start, unsigned long, length)
110
111 #ifdef __NR_getxuid
112 # define __NR_getuid __NR_getxuid
113 #endif
114 #define __NR__dl_getuid __NR_getuid
115 static __always_inline _syscall0(uid_t, _dl_getuid)
116
117 #ifndef __NR_geteuid
118 # define __NR_geteuid __NR_getuid
119 #endif
120 #define __NR__dl_geteuid __NR_geteuid
121 static __always_inline _syscall0(uid_t, _dl_geteuid)
122
123 #ifdef __NR_getxgid
124 # define __NR_getgid __NR_getxgid
125 #endif
126 #define __NR__dl_getgid __NR_getgid
127 static __always_inline _syscall0(gid_t, _dl_getgid)
128
129 #ifndef __NR_getegid
130 # define __NR_getegid __NR_getgid
131 #endif
132 #define __NR__dl_getegid __NR_getegid
133 static __always_inline _syscall0(gid_t, _dl_getegid)
134
135 #ifdef __NR_getxpid
136 # define __NR_getpid __NR_getxpid
137 #endif
138 #define __NR__dl_getpid __NR_getpid
139 static __always_inline _syscall0(gid_t, _dl_getpid)
140
141 #if defined __NR_readlinkat && !defined __NR_readlink
142 # define __NR__dl_readlink __NR_readlinkat
143 static __always_inline _syscall4(int, _dl_readlink, int, id, const char *, path,
144                                                 char *, buf, size_t, bufsiz)
145 #elif defined __NR_readlink
146 # define __NR__dl_readlink __NR_readlink
147 static __always_inline _syscall3(int, _dl_readlink, const char *, path, char *, buf,
148                         size_t, bufsiz)
149 #endif
150
151 #ifdef __NR_pread64
152 #define __NR___syscall_pread __NR_pread64
153 static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
154                         size_t, count, off_t, offset_hi, off_t, offset_lo)
155
156 static __always_inline ssize_t
157 _dl_pread(int fd, void *buf, size_t count, off_t offset)
158 {
159         return __syscall_pread(fd, buf, count, offset, offset >> 31);
160 }
161 #elif defined __NR_pread
162 #define __NR___syscall_pread __NR_pread
163 static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
164                         size_t, count, off_t, offset_hi, off_t, offset_lo)
165
166 static __always_inline ssize_t
167 _dl_pread(int fd, void *buf, size_t count, off_t offset)
168 {
169         return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
170 }
171 #endif
172
173 #ifdef __UCLIBC_HAS_SSP__
174 # include <sys/time.h>
175 # define __NR__dl_gettimeofday __NR_gettimeofday
176 static __always_inline _syscall2(int, _dl_gettimeofday, struct timeval *, tv,
177 # ifdef __USE_BSD
178                         struct timezone *
179 # else
180                         void *
181 # endif
182                                                 , tz)
183 #endif
184
185 /* Some architectures always use 12 as page shift for mmap2() eventhough the
186  * real PAGE_SHIFT != 12.  Other architectures use the same value as
187  * PAGE_SHIFT...
188  */
189 #ifndef MMAP2_PAGE_SHIFT
190 # define MMAP2_PAGE_SHIFT 12
191 #endif
192
193 #define MAP_FAILED ((void *) -1)
194 #define _dl_mmap_check_error(X) (((void *)X) == MAP_FAILED)
195
196 static __always_inline
197 void *_dl_mmap(void *addr, unsigned long size, int prot,
198                int flags, int fd, unsigned long offset)
199 {
200 #if defined(__UCLIBC_MMAP_HAS_6_ARGS__) && defined(__NR_mmap)
201         /* first try mmap(), syscall6() style */
202         return (void *)INLINE_SYSCALL(mmap, 6, addr, size, prot, flags, fd, offset);
203
204 #elif defined(__NR_mmap2) && !defined (__mcoldfire__)
205         /* then try mmap2() */
206         unsigned long shifted;
207
208         if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
209                 return MAP_FAILED;
210
211         /* gcc needs help with putting things onto the stack */
212         shifted = offset >> MMAP2_PAGE_SHIFT;
213         return (void *)INLINE_SYSCALL(mmap2, 6, addr, size, prot, flags, fd, shifted);
214
215 #elif defined(__NR_mmap)
216         /* finally, fall back to mmap(), syscall1() style */
217         unsigned long buffer[6];
218         buffer[0] = (unsigned long) addr;
219         buffer[1] = (unsigned long) size;
220         buffer[2] = (unsigned long) prot;
221         buffer[3] = (unsigned long) flags;
222         buffer[4] = (unsigned long) fd;
223         buffer[5] = (unsigned long) offset;
224         return (void *)INLINE_SYSCALL(mmap, 1, buffer);
225 #else
226 # error "Your architecture doesn't seem to provide mmap() !?"
227 #endif
228 }
229
230 #endif /* _DL_SYSCALL_H */