syscalls.c 7.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */
9 10 11
#include <reent.h>
#include <sys/errno.h>
#include <sys/time.h>
12 13
#include <stdio.h>

14 15
#include <rtthread.h>

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

20 21 22 23
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

24 25
/* Reentrant versions of system calls.  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

_CLOCK_T_
_times_r(struct _reent *ptr, struct tms *ptms)
{
B
BernardXiong 已提交
196 197 198
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
199 200 201 202 203
}

int
_unlink_r(struct _reent *ptr, const char *file)
{
204
#ifndef RT_USING_DFS
B
BernardXiong 已提交
205
    return 0;
206
#else
B
BernardXiong 已提交
207
    int rc;
208

B
BernardXiong 已提交
209 210
    rc = unlink(file);
    return rc;
211
#endif
212 213 214 215 216
}

int
_wait_r(struct _reent *ptr, int *status)
{
B
BernardXiong 已提交
217 218 219
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
220 221
}

222
#ifdef RT_USING_DEVICE
223 224 225
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
B
bernard 已提交
226
#ifndef RT_USING_DFS
227
    if (fileno(stdout) == fd)
B
BernardXiong 已提交
228 229
    {
        rt_device_t console;
230

B
BernardXiong 已提交
231 232 233
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
234

B
bernard 已提交
235 236
    return 0;

237
#else
B
BernardXiong 已提交
238
    _ssize_t rc;
B
bernard 已提交
239

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

B
BernardXiong 已提交
246
#ifdef RT_USING_PTHREADS
247

B
BernardXiong 已提交
248 249 250 251 252 253 254
#include <clock_time.h>
/* POSIX timer provides clock_gettime function */
#include <time.h>
int
_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
{
    struct timespec tp;
255

B
BernardXiong 已提交
256 257 258 259 260 261 262
    if (clock_gettime(CLOCK_REALTIME, &tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
263

B
BernardXiong 已提交
264 265 266 267 268 269 270
        return tp.tv_sec;
    }

    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
}
271

B
BernardXiong 已提交
272 273 274 275 276
#else

#define MILLISECOND_PER_SECOND  1000UL
#define MICROSECOND_PER_SECOND  1000000UL
#define NANOSECOND_PER_SECOND   1000000000UL
277

B
BernardXiong 已提交
278 279 280
#define MILLISECOND_PER_TICK    (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
#define MICROSECOND_PER_TICK    (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
#define NANOSECOND_PER_TICK     (NANOSECOND_PER_SECOND  / RT_TICK_PER_SECOND)
281 282

struct timeval _timevalue = {0};
283 284
#ifdef RT_USING_DEVICE
static void libc_system_time_init(void)
285
{
B
BernardXiong 已提交
286 287 288
    time_t time;
    rt_tick_t tick;
    rt_device_t device;
289

B
BernardXiong 已提交
290 291 292 293 294 295 296
    time = 0;
    device = rt_device_find("rtc");
    if (device != RT_NULL)
    {
        /* get realtime seconds */
        rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
    }
297

B
BernardXiong 已提交
298 299
    /* get tick */
    tick = rt_tick_get();
300

B
BernardXiong 已提交
301 302
    _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
    _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
303
}
304
#endif
305 306 307

int libc_get_time(struct timespec *time)
{
B
BernardXiong 已提交
308 309
    rt_tick_t tick;
    static rt_bool_t inited = 0;
310

B
BernardXiong 已提交
311
    RT_ASSERT(time != RT_NULL);
312

B
BernardXiong 已提交
313 314 315 316 317 318
    /* initialize system time */
    if (inited == 0)
    {
        libc_system_time_init();
        inited = 1;
    }
319

B
BernardXiong 已提交
320 321
    /* get tick */
    tick = rt_tick_get();
322

B
BernardXiong 已提交
323 324
    time->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
    time->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
325

B
BernardXiong 已提交
326
    return 0;
327 328 329 330 331
}

int
_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
{
B
BernardXiong 已提交
332
    struct timespec tp;
333

B
BernardXiong 已提交
334 335 336 337 338 339 340
    if (libc_get_time(&tp) == 0)
    {
        if (__tp != RT_NULL)
        {
            __tp->tv_sec  = tp.tv_sec;
            __tp->tv_usec = tp.tv_nsec / 1000UL;
        }
341

B
BernardXiong 已提交
342 343
        return tp.tv_sec;
    }
344

B
BernardXiong 已提交
345 346 347
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
348 349 350 351 352 353 354
}
#endif

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

B
BernardXiong 已提交
357 358 359 360 361
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
362

B
BernardXiong 已提交
363
    return result;
364 365 366 367 368
}

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

B
BernardXiong 已提交
371 372 373 374 375
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
376

B
BernardXiong 已提交
377
    return result;
378 379 380 381
}

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

B
BernardXiong 已提交
384 385 386 387 388
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
389

B
BernardXiong 已提交
390
    return result;
391 392
}

393
void
394 395
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
396
    rt_free (addr);
397 398 399
}

void
400
exit (int status)
401
{
402
#ifdef RT_USING_MODULE
403
    if (dlmodule_self())
B
BernardXiong 已提交
404
    {
405
        dlmodule_exit(status);
B
BernardXiong 已提交
406
    }
407
#endif
408

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

B
BernardXiong 已提交
412
    while (1);
413
}
414

415
void
416 417 418 419 420
_system(const char *s)
{
    /* not support this call */
    return;
}
421 422 423

void __libc_init_array(void)
{
B
BernardXiong 已提交
424
    /* we not use __libc init_aray to initialize C++ objects */
425
}
426 427 428

void abort(void)
{
429 430 431 432 433 434 435 436 437 438
    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 已提交
439
    while (1);
440
}
441 442 443 444 445 446 447 448 449 450 451

uid_t getuid(void)
{
    return 0;
}

mode_t umask(mode_t mask)
{
    return 022;
}

452 453 454 455
int flock(int fd, int operation)
{
    return 0;
}