syscalls.c 5.5 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
 * 2020-02-13     Meco Man     re-implement exit() and abort()
10
 */
11

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

17 18
#include <rtthread.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
bernard 已提交
230 231
    return 0;

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

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

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

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

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

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

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

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

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

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

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

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

void
290
exit (int status)
291
{
292 293
    rt_thread_t self = rt_thread_self();

294
#ifdef RT_USING_MODULE
295
    if (dlmodule_self())
B
BernardXiong 已提交
296
    {
297
        dlmodule_exit(status);
B
BernardXiong 已提交
298
    }
299
#endif
300

301 302 303 304 305 306
    if (self != RT_NULL)
    {
        rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, status);
        rt_thread_suspend(self);
        rt_schedule();
    }
mysterywolf's avatar
mysterywolf 已提交
307 308

    while(1); /* noreturn */
309
}
310

311
void
312 313 314 315 316
_system(const char *s)
{
    /* not support this call */
    return;
}
317 318 319

void __libc_init_array(void)
{
B
BernardXiong 已提交
320
    /* we not use __libc init_aray to initialize C++ objects */
321
}
322 323 324

void abort(void)
{
325 326 327 328
    rt_thread_t self = rt_thread_self();

#ifdef RT_USING_MODULE
    if (dlmodule_self())
329
    {
330 331 332
        dlmodule_exit(-1);
    }
#endif
333

334 335
    if (self != RT_NULL)
    {
336 337 338 339
        rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
        rt_thread_suspend(self);
        rt_schedule();
    }
mysterywolf's avatar
mysterywolf 已提交
340 341

    while(1); /* noreturn */
342
}
343 344 345 346 347 348 349 350 351 352 353

uid_t getuid(void)
{
    return 0;
}

mode_t umask(mode_t mask)
{
    return 022;
}

354 355 356 357
int flock(int fd, int operation)
{
    return 0;
}
358 359

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