syscalls.c 5.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2021-02-11     Meco Man     remove _gettimeofday_r() and _times_r()
9
 */
10

11 12 13
#include <reent.h>
#include <sys/errno.h>
#include <sys/time.h>
14 15
#include <stdio.h>

16 17
#include <rtthread.h>

B
Bernard Xiong 已提交
18 19 20 21
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif

22 23 24 25
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

26 27
/* Reentrant versions of system calls.  */

28 29 30 31 32 33 34 35
#ifndef _REENT_ONLY
int *
__errno ()
{
  return _rt_errno();
}
#endif

36 37 38
int
_close_r(struct _reent *ptr, int fd)
{
39
#ifndef RT_USING_DFS
B
BernardXiong 已提交
40
    return 0;
41
#else
B
BernardXiong 已提交
42
    return close(fd);
43
#endif
44 45 46 47 48
}

int
_execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
B
BernardXiong 已提交
49 50 51
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
52 53 54 55 56
}

int
_fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
B
BernardXiong 已提交
57 58 59
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
60 61 62 63 64
}

int
_fork_r(struct _reent *ptr)
{
B
BernardXiong 已提交
65 66 67
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
68 69 70 71 72
}

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

int
_getpid_r(struct _reent *ptr)
{
B
BernardXiong 已提交
81
    return 0;
82 83 84 85 86
}

int
_isatty_r(struct _reent *ptr, int fd)
{
B
BernardXiong 已提交
87
    if (fd >=0 && fd < 3) return 1;
88

B
BernardXiong 已提交
89 90 91
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
92 93 94 95 96
}

int
_kill_r(struct _reent *ptr, int pid, int sig)
{
B
BernardXiong 已提交
97 98 99
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
100 101 102 103 104
}

int
_link_r(struct _reent *ptr, const char *old, const char *new)
{
B
BernardXiong 已提交
105 106 107
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
108 109 110 111 112
}

_off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
113
#ifndef RT_USING_DFS
B
BernardXiong 已提交
114
    return 0;
115
#else
B
BernardXiong 已提交
116
    _off_t rc;
117

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

int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
126
#ifndef RT_USING_DFS
B
BernardXiong 已提交
127
    return 0;
128
#else
B
BernardXiong 已提交
129
    int rc;
130

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

int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
139
#ifndef RT_USING_DFS
B
BernardXiong 已提交
140
    return 0;
141
#else
B
BernardXiong 已提交
142
    int rc;
143

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

149
_ssize_t
150 151
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
152
#ifndef RT_USING_DFS
B
BernardXiong 已提交
153
    return 0;
154
#else
B
BernardXiong 已提交
155
    _ssize_t rc;
156

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

int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
165
#ifndef RT_USING_DFS
B
BernardXiong 已提交
166
    return 0;
167
#else
B
BernardXiong 已提交
168
    int rc;
169

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

void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
B
BernardXiong 已提交
178 179
    /* no use this routine to get memory */
    return RT_NULL;
180 181 182 183 184
}

int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
185
#ifndef RT_USING_DFS
B
BernardXiong 已提交
186
    return 0;
187
#else
B
BernardXiong 已提交
188
    int rc;
189

B
BernardXiong 已提交
190 191
    rc = stat(file, pstat);
    return rc;
192
#endif
193 194 195 196 197
}

int
_unlink_r(struct _reent *ptr, const char *file)
{
198
#ifndef RT_USING_DFS
B
BernardXiong 已提交
199
    return 0;
200
#else
B
BernardXiong 已提交
201
    int rc;
202

B
BernardXiong 已提交
203 204
    rc = unlink(file);
    return rc;
205
#endif
206 207 208 209 210
}

int
_wait_r(struct _reent *ptr, int *status)
{
B
BernardXiong 已提交
211 212 213
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
214 215
}

216
#ifdef RT_USING_DEVICE
217 218 219
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
B
bernard 已提交
220
#ifndef RT_USING_DFS
221
    if (fileno(stdout) == fd)
B
BernardXiong 已提交
222 223
    {
        rt_device_t console;
224

B
BernardXiong 已提交
225 226 227
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
228

B
bernard 已提交
229 230
    return 0;

231
#else
B
BernardXiong 已提交
232
    _ssize_t rc;
B
bernard 已提交
233

B
BernardXiong 已提交
234 235
    rc = write(fd, buf, nbytes);
    return rc;
236
#endif
237
}
238
#endif
239 240 241 242 243

/* Memory routine */
void *
_malloc_r (struct _reent *ptr, size_t size)
{
B
BernardXiong 已提交
244
    void* result;
245

B
BernardXiong 已提交
246 247 248 249 250
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
251

B
BernardXiong 已提交
252
    return result;
253 254 255 256 257
}

void *
_realloc_r (struct _reent *ptr, void *old, size_t newlen)
{
B
BernardXiong 已提交
258
    void* result;
259

B
BernardXiong 已提交
260 261 262 263 264
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
265

B
BernardXiong 已提交
266
    return result;
267 268 269 270
}

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

B
BernardXiong 已提交
273 274 275 276 277
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
278

B
BernardXiong 已提交
279
    return result;
280 281
}

282
void
283 284
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
285
    rt_free (addr);
286 287 288
}

void
289
exit (int status)
290
{
291
#ifdef RT_USING_MODULE
292
    if (dlmodule_self())
B
BernardXiong 已提交
293
    {
294
        dlmodule_exit(status);
B
BernardXiong 已提交
295
    }
296
#endif
297

B
BernardXiong 已提交
298 299
    rt_kprintf("thread:%s exit with %d\n", rt_thread_self()->name, status);
    RT_ASSERT(0);
300

B
BernardXiong 已提交
301
    while (1);
302
}
303

304
void
305 306 307 308 309
_system(const char *s)
{
    /* not support this call */
    return;
}
310 311 312

void __libc_init_array(void)
{
B
BernardXiong 已提交
313
    /* we not use __libc init_aray to initialize C++ objects */
314
}
315 316 317

void abort(void)
{
318 319 320 321 322 323 324 325 326 327
    if (rt_thread_self())
    {
        rt_thread_t self = rt_thread_self();

        rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
        rt_thread_suspend(self);

        rt_schedule();
    }

B
BernardXiong 已提交
328
    while (1);
329
}
330 331 332 333 334 335 336 337 338 339 340

uid_t getuid(void)
{
    return 0;
}

mode_t umask(mode_t mask)
{
    return 022;
}

341 342 343 344
int flock(int fd, int operation)
{
    return 0;
}
345 346 347 348 349 350

/*
These function will be implemented in the 'common/time.c' file
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_  _times_r(struct _reent *ptr, struct tms *ptms);
*/