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
199
    return -1;
200
#else
201
    return unlink(file);
202
#endif
203 204 205 206 207
}

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

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

B
BernardXiong 已提交
222 223 224
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
225

B
bernard 已提交
226 227
    return 0;

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
#endif
236 237 238 239 240

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

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

B
BernardXiong 已提交
249
    return result;
250 251 252 253 254
}

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

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

B
BernardXiong 已提交
263
    return result;
264 265 266 267
}

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

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

B
BernardXiong 已提交
276
    return result;
277 278
}

279
void
280 281
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
282
    rt_free (addr);
283 284 285
}

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

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

B
BernardXiong 已提交
298
    while (1);
299
}
300

301
void
302 303 304 305 306
_system(const char *s)
{
    /* not support this call */
    return;
}
307 308 309

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

void abort(void)
{
315 316 317 318 319 320 321 322 323 324
    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 已提交
325
    while (1);
326
}
327 328 329 330 331 332 333 334 335 336 337

uid_t getuid(void)
{
    return 0;
}

mode_t umask(mode_t mask)
{
    return 022;
}

338 339 340 341
int flock(int fd, int operation)
{
    return 0;
}
342 343

/*
mysterywolf's avatar
mysterywolf 已提交
344
These functions will be implemented and replaced by the 'common/time.c' file
345 346 347
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_  _times_r(struct _reent *ptr, struct tms *ptms);
*/