syscalls.c 5.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3 4 5 6 7
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2021-02-11     Meco Man     remove _gettimeofday_r() and _times_r()
9
 * 2020-02-13     Meco Man     re-implement exit() and abort()
mysterywolf's avatar
mysterywolf 已提交
10
 * 2020-02-21     Meco Man     improve and beautify syscalls
mysterywolf's avatar
mysterywolf 已提交
11
 * 2020-02-24     Meco Man     fix bug of _isatty_r()
12
 */
13

14
#include <reent.h>
mysterywolf's avatar
mysterywolf 已提交
15
#include <errno.h>
16
#include <stdio.h>
mysterywolf's avatar
mysterywolf 已提交
17
#include <sys/time.h>
18

19 20
#include <rtthread.h>

B
Bernard Xiong 已提交
21 22 23 24
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif

25 26 27 28
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

29 30
/* Reentrant versions of system calls.  */

31
#ifndef _REENT_ONLY
mysterywolf's avatar
mysterywolf 已提交
32
int *__errno ()
33 34 35 36 37
{
  return _rt_errno();
}
#endif

mysterywolf's avatar
mysterywolf 已提交
38
int _getpid_r(struct _reent *ptr)
mysterywolf's avatar
update  
mysterywolf 已提交
39
{
mysterywolf's avatar
update  
mysterywolf 已提交
40
    return 0;
mysterywolf's avatar
update  
mysterywolf 已提交
41 42
}

mysterywolf's avatar
mysterywolf 已提交
43
int _close_r(struct _reent *ptr, int fd)
44
{
45
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
46 47 48
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
49
#else
B
BernardXiong 已提交
50
    return close(fd);
51
#endif
52 53
}

mysterywolf's avatar
mysterywolf 已提交
54
int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
55
{
B
BernardXiong 已提交
56 57 58
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
59 60
}

mysterywolf's avatar
mysterywolf 已提交
61
int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
62
{
B
BernardXiong 已提交
63 64 65
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
66 67
}

mysterywolf's avatar
mysterywolf 已提交
68
int _fork_r(struct _reent *ptr)
69
{
B
BernardXiong 已提交
70 71 72
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
73 74
}

mysterywolf's avatar
mysterywolf 已提交
75
int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
76
{
B
BernardXiong 已提交
77 78 79
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
80 81
}

mysterywolf's avatar
mysterywolf 已提交
82
int _isatty_r(struct _reent *ptr, int fd)
83
{
mysterywolf's avatar
mysterywolf 已提交
84
    if (fd >=0 && fd < 3)
mysterywolf's avatar
mysterywolf 已提交
85
    {
mysterywolf's avatar
mysterywolf 已提交
86
        return 1;
mysterywolf's avatar
mysterywolf 已提交
87 88 89 90 91
    }
    else
    {
        return 0;
    }
92
}
mysterywolf's avatar
mysterywolf 已提交
93

mysterywolf's avatar
mysterywolf 已提交
94
int _kill_r(struct _reent *ptr, int pid, int sig)
95
{
B
BernardXiong 已提交
96 97 98
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
99 100
}

mysterywolf's avatar
mysterywolf 已提交
101
int _link_r(struct _reent *ptr, const char *old, const char *new)
102
{
B
BernardXiong 已提交
103 104 105
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
106 107
}

mysterywolf's avatar
mysterywolf 已提交
108
_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
109
{
110
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
111 112 113
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
114
#else
B
BernardXiong 已提交
115
    _off_t rc;
116

B
BernardXiong 已提交
117 118
    rc = lseek(fd, pos, whence);
    return rc;
119
#endif
120 121
}

mysterywolf's avatar
mysterywolf 已提交
122
int _mkdir_r(struct _reent *ptr, const char *name, int mode)
123
{
124
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
125 126 127
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
128
#else
B
BernardXiong 已提交
129
    int rc;
130

B
BernardXiong 已提交
131 132
    rc = mkdir(name, mode);
    return rc;
133
#endif
134 135
}

mysterywolf's avatar
mysterywolf 已提交
136
int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
137
{
138
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
139 140 141
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
142
#else
B
BernardXiong 已提交
143
    int rc;
144

B
BernardXiong 已提交
145 146
    rc = open(file, flags, mode);
    return rc;
147
#endif
148 149
}

mysterywolf's avatar
mysterywolf 已提交
150
_ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
151
{
152
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
153 154 155
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
156
#else
B
BernardXiong 已提交
157
    _ssize_t rc;
158

B
BernardXiong 已提交
159 160
    rc = read(fd, buf, nbytes);
    return rc;
161
#endif
162 163
}

mysterywolf's avatar
mysterywolf 已提交
164
int _rename_r(struct _reent *ptr, const char *old, const char *new)
165
{
166
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
167 168 169
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
170
#else
B
BernardXiong 已提交
171
    int rc;
172

B
BernardXiong 已提交
173 174
    rc = rename(old, new);
    return rc;
175
#endif
176 177
}

mysterywolf's avatar
mysterywolf 已提交
178
int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
179
{
180
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
181 182 183
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
184
#else
B
BernardXiong 已提交
185
    int rc;
186

B
BernardXiong 已提交
187 188
    rc = stat(file, pstat);
    return rc;
189
#endif
190 191
}

mysterywolf's avatar
mysterywolf 已提交
192
int _unlink_r(struct _reent *ptr, const char *file)
193
{
194
#ifndef RT_USING_DFS
mysterywolf's avatar
mysterywolf 已提交
195 196
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
197
    return -1;
198
#else
199
    return unlink(file);
200
#endif
201 202
}

mysterywolf's avatar
mysterywolf 已提交
203
int _wait_r(struct _reent *ptr, int *status)
204
{
B
BernardXiong 已提交
205 206 207
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
208 209
}

mysterywolf's avatar
mysterywolf 已提交
210
_ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
211
{
B
bernard 已提交
212
#ifndef RT_USING_DFS
213
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
214
    if (fileno(stdout) == fd)
B
BernardXiong 已提交
215 216
    {
        rt_device_t console;
217

B
BernardXiong 已提交
218 219 220
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
221

B
bernard 已提交
222
    return 0;
mysterywolf's avatar
mysterywolf 已提交
223 224 225 226 227
#else
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
#endif /*RT_USING_DEVICE*/
228
#else
B
BernardXiong 已提交
229
    _ssize_t rc;
B
bernard 已提交
230

B
BernardXiong 已提交
231 232
    rc = write(fd, buf, nbytes);
    return rc;
233
#endif
234 235
}

236
#ifdef RT_USING_HEAP /* Memory routine */
mysterywolf's avatar
mysterywolf 已提交
237
void *_malloc_r (struct _reent *ptr, size_t size)
238
{
B
BernardXiong 已提交
239
    void* result;
240

B
BernardXiong 已提交
241 242 243 244 245
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
246

B
BernardXiong 已提交
247
    return result;
248 249
}

mysterywolf's avatar
mysterywolf 已提交
250
void *_realloc_r (struct _reent *ptr, void *old, size_t newlen)
251
{
B
BernardXiong 已提交
252
    void* result;
253

B
BernardXiong 已提交
254 255 256 257 258
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
259

B
BernardXiong 已提交
260
    return result;
261 262 263 264
}

void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
{
B
BernardXiong 已提交
265
    void* result;
266

B
BernardXiong 已提交
267 268 269 270 271
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
272

B
BernardXiong 已提交
273
    return result;
274 275
}

mysterywolf's avatar
mysterywolf 已提交
276
void _free_r (struct _reent *ptr, void *addr)
277
{
B
BernardXiong 已提交
278
    rt_free (addr);
279 280
}

281 282 283 284 285 286 287 288
#else
void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
    return RT_NULL;
}
#endif /*RT_USING_HEAP*/

mysterywolf's avatar
update  
mysterywolf 已提交
289
/* for exit() and abort() */
mysterywolf's avatar
mysterywolf 已提交
290
__attribute__ ((noreturn)) void _exit (int status)
291
{
mysterywolf's avatar
update  
mysterywolf 已提交
292 293
    extern void __rt_libc_exit(int status);
    __rt_libc_exit(status);
mysterywolf's avatar
update  
mysterywolf 已提交
294
    while(1);
295
}
296

mysterywolf's avatar
mysterywolf 已提交
297
void _system(const char *s)
298
{
mysterywolf's avatar
update  
mysterywolf 已提交
299 300
    extern int __rt_libc_system(const char *string);
    __rt_libc_system(s);
301
}
302 303 304

void __libc_init_array(void)
{
B
BernardXiong 已提交
305
    /* we not use __libc init_aray to initialize C++ objects */
306
}
307

308 309 310 311 312
mode_t umask(mode_t mask)
{
    return 022;
}

313 314 315 316
int flock(int fd, int operation)
{
    return 0;
}
317 318

/*
mysterywolf's avatar
mysterywolf 已提交
319
These functions are implemented and replaced by the 'common/time.c' file
320 321 322
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_  _times_r(struct _reent *ptr, struct tms *ptms);
*/