summaryrefslogtreecommitdiff
path: root/components/lwp/syscall/syscall_file.c
blob: 1650215e67384a6977b295002d2f0d7283e5f19c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
 * Copyright (c) 2006-2025 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "syscall_internal.h"

/**
 * @brief Reads data from a file descriptor into a buffer.
 *
 * This system call reads up to `nbyte` bytes of data from the file descriptor
 * specified by `fd` into the buffer pointed to by `buf`.
 *
 * @param fd The file descriptor to read from. This should be a valid file
 *           descriptor obtained through system calls like `open()` or `socket()`.
 * @param buf A pointer to the buffer where the read data will be stored. The buffer
 *            must have enough space to accommodate up to `nbyte` bytes.
 * @param nbyte The maximum number of bytes to read. If the file contains fewer
 *              bytes than `nbyte`, only the available bytes will be read.
 * @return The number of bytes actually read, which may be less than `nbyte` if fewer
 *         bytes are available or if the end of the file is reached. Returns `0` if
 *         the end of the file is encountered. On error, returns `errno`.
 * @warning Ensure the buffer `buf` has sufficient space to hold the requested number
 *          of bytes, as failing to do so may result in undefined behavior.
 */
ssize_t sys_read(int fd, void *buf, size_t nbyte)
{
#ifdef ARCH_MM_MMU
    void *kmem = RT_NULL;
    ssize_t ret = -1;

    if (nbyte)
    {
        if (!lwp_user_accessable((void *)buf, nbyte))
        {
            return -EFAULT;
        }

        kmem = kmem_get(nbyte);
        if (!kmem)
        {
            return -ENOMEM;
        }
    }

    ret = read(fd, kmem, nbyte);
    if (ret > 0)
    {
        if (ret != lwp_put_to_user(buf, kmem, ret))
        {
            return -EFAULT;
        }
    }

    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    kmem_put(kmem);

    return ret;
#else
    if (!lwp_user_accessable((void *)buf, nbyte))
    {
        return -EFAULT;
    }
    ssize_t ret = read(fd, buf, nbyte);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Writes data from a buffer to a file descriptor.
 *
 * This system call writes up to `nbyte` bytes of data from the buffer pointed
 * to by `buf` to the file descriptor specified by `fd`.
 *
 * @param fd The file descriptor to write to. This should be a valid file
 *           descriptor obtained through system calls like `open()` or `socket()`.
 * @param buf A pointer to the buffer containing the data to be written. The
 *            buffer must remain valid and accessible for the duration of the
 *            write operation.
 * @param nbyte The number of bytes to write from the buffer. If the file descriptor
 *              refers to a device or a socket, the actual number of bytes written
 *              may be less than `nbyte`.
 * @return The number of bytes actually written. This may be less than `nbyte` if
 *         the underlying resource cannot accept more data at the moment. On error,
 *         returns `errno`.
 *
 * @warning Ensure the buffer `buf` contains at least `nbyte` valid bytes to
 *          avoid undefined behavior. Additionally, verify that `fd` is writable
 *          to prevent errors.
 *
 * @see sys_read(), write()
 */
ssize_t sys_write(int fd, const void *buf, size_t nbyte)
{
#ifdef ARCH_MM_MMU
    void *kmem = RT_NULL;
    ssize_t ret = -1;

    if (nbyte)
    {
        if (!lwp_user_accessable((void *)buf, nbyte))
        {
            return -EFAULT;
        }

        kmem = kmem_get(nbyte);
        if (!kmem)
        {
            return -ENOMEM;
        }

        lwp_get_from_user(kmem, (void *)buf, nbyte);
    }

    ret = write(fd, kmem, nbyte);
    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    kmem_put(kmem);

    return ret;
#else
    if (!lwp_user_accessable((void *)buf, nbyte))
    {
        return -EFAULT;
    }
    ssize_t ret = write(fd, buf, nbyte);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Repositions the file offset of the open file descriptor.
 *
 * This system call sets the file offset for the open file descriptor `fd`
 * to a new position based on the `offset` and `whence` parameters. It is used
 * for seeking within files, enabling random access to file content.
 *
 * @param fd The file descriptor whose offset is to be modified. The descriptor
 *           must refer to a file capable of seeking (e.g., regular files, but
 *           not pipes or sockets).
 * @param offset The new offset value relative to the position specified by `whence`.
 *               Can be a positive or negative value, depending on the seek direction.
 * @param whence The reference point for the new offset. Must be one of the following:
 *               - `SEEK_SET`: Set the offset to `offset` bytes from the beginning of the file.
 *               - `SEEK_CUR`: Set the offset to its current position plus `offset`.
 *               - `SEEK_END`: Set the offset to the size of the file plus `offset`.
 * @return On success, returns the resulting offset location as measured in bytes
 *         from the beginning of the file. On failure, returns `errno`.
 *
 * @warning Ensure the file descriptor `fd` supports seeking. Using this function
 *          on non-seekable file descriptors (e.g., pipes or sockets) will result in an error.
 *
 * @see sys_read(), sys_write(), lseek()
 */
size_t sys_lseek(int fd, size_t offset, int whence)
{
    ssize_t ret = lseek(fd, offset, whence);
    return (ret < 0 ? GET_ERRNO() : ret);
}

/**
 * @brief Opens or creates a file, returning a file descriptor.
 *
 * This system call opens the file specified by `name` with the specified
 * access mode and flags. If the file does not exist and the `O_CREAT` flag
 * is provided, it will create the file with the specified mode.
 *
 * @param name The path to the file to be opened. This can be an absolute or
 *             relative path.
 * @param flag Flags controlling how the file is opened. Common values include:
 *             - `O_RDONLY`: Open the file for read-only access.
 *             - `O_WRONLY`: Open the file for write-only access.
 *             - `O_RDWR`: Open the file for both reading and writing.
 *             - `O_CREAT`: Create the file if it does not exist (requires a mode argument).
 *             - `O_TRUNC`: Truncate the file to zero length if it exists.
 *             - `O_APPEND`: Open the file in append mode.
 * @param ... Optional. If the `O_CREAT` flag is specified, an additional
 *            `mode_t` argument must be provided, defining the file permissions
 *            (e.g., `0644` for user-read/write and group/world-read).
 * @return On success, returns the file descriptor.
 *         On failure, returns `errno`.
 *
 * @note The file descriptor returned can be used with other system calls like
 *       `sys_read()`, `sys_write()`, and `sys_close()`.
 *
 * @warning When using the `O_CREAT` flag, ensure to provide the `mode` argument
 *          to avoid undefined behavior. Additionally, verify that the process
 *          has the necessary permissions to access or create the file.
 *
 * @see sys_close(), sys_read(), sys_write(), open()
 */
sysret_t sys_open(const char *name, int flag, ...)
{
#ifdef ARCH_MM_MMU
    int ret = -1;
    rt_size_t len = 0;
    char *kname = RT_NULL;
    mode_t mode = 0;

    if (!lwp_user_accessable((void *)name, 1))
    {
        return -EFAULT;
    }

    len = lwp_user_strlen(name);
    if (!len)
    {
        return -EINVAL;
    }

    kname = (char *)kmem_get(len + 1);
    if (!kname)
    {
        return -ENOMEM;
    }

    if ((flag & O_CREAT) || (flag & O_TMPFILE) == O_TMPFILE)
    {
        va_list ap;
        va_start(ap, flag);
        mode = va_arg(ap, mode_t);
        va_end(ap);
    }

    if (lwp_get_from_user(kname, (void *)name, len + 1) != (len + 1))
    {
        kmem_put(kname);
        return -EINVAL;
    }

    ret = open(kname, flag, mode);
    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    kmem_put(kname);

    return ret;
#else
    int ret;
    mode_t mode = 0;

    if (!lwp_user_accessable((void *)name, 1))
    {
        return -EFAULT;
    }

    if ((flag & O_CREAT) || (flag & O_TMPFILE) == O_TMPFILE)
    {
        va_list ap;
        va_start(ap, flag);
        mode = va_arg(ap, mode_t);
        va_end(ap);
    }

    ret = open(name, flag, mode);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Opens or creates a file relative to a directory file descriptor.
 *
 * This system call opens the file specified by `name`, relative to the directory
 * indicated by `dirfd`, with the specified flags and mode. It provides more
 * flexibility than `sys_open` for handling files in specific directory contexts.
 *
 * @param dirfd The file descriptor of the directory relative to which the file is opened.
 *              Special values include:
 *              - `AT_FDCWD`: Use the current working directory as the base.
 * @param name The path to the file to be opened. If `name` is absolute, `dirfd` is ignored.
 * @param flag Flags controlling how the file is opened. Common values include:
 *             - `O_RDONLY`: Open the file for read-only access.
 *             - `O_WRONLY`: Open the file for write-only access.
 *             - `O_RDWR`: Open the file for both reading and writing.
 *             - `O_CREAT`: Create the file if it does not exist.
 *             - `O_TRUNC`: Truncate the file to zero length if it exists.
 *             - `O_APPEND`: Open the file in append mode.
 *             - `O_EXCL`: Ensure the file is created exclusively (used with `O_CREAT`).
 * @param mode The permissions to set if the file is created (e.g., `0644` for user read/write
 *             and group/world read). This parameter is ignored unless `O_CREAT` is specified.
 * @return On success, returns the file descriptor.
 *         On failure, returns `errno`.
 *
 * @note The `sys_openat` system call is particularly useful for implementing secure
 *       directory traversal and operations, as it avoids race conditions when working
 *       with relative paths.
 *
 * @warning Ensure that `dirfd` is a valid directory file descriptor if a relative
 *          `name` is provided. Combining `O_CREAT` and `mode` requires proper handling
 *          to avoid unintended permission settings.
 *
 * @see sys_open(), sys_read(), sys_write(), sys_close()
 */
sysret_t sys_openat(int dirfd, const char *name, int flag, mode_t mode)
{
#ifdef ARCH_MM_MMU
    int ret = -1;
    rt_size_t len = 0;
    char *kname = RT_NULL;

    len = lwp_user_strlen(name);
    if (len <= 0)
    {
        return -EINVAL;
    }

    kname = (char *)kmem_get(len + 1);
    if (!kname)
    {
        return -ENOMEM;
    }

    lwp_get_from_user(kname, (void *)name, len + 1);
    ret = openat(dirfd, kname, flag, mode);
    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    kmem_put(kname);

    return ret;
#else
    if (!lwp_user_accessable((void *)name, 1))
    {
        return -EFAULT;
    }
    int ret = openat(dirfd, name, flag, mode);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Closes a file descriptor.
 *
 * This system call closes the file descriptor specified by `fd`, releasing any
 * resources associated with it. Once closed, the file descriptor can no longer
 * be used for operations such as reading or writing, and it may be reassigned
 * by subsequent calls to functions like `sys_open()`.
 *
 * @param fd The file descriptor to be closed. This must be a valid open file descriptor.
 * @return On success, returns 0. On failure, returns `errno`.
 *
 * @note Closing a file descriptor that is already closed results in an error.
 *       Additionally, if the file descriptor refers to a file or resource shared
 *       among multiple processes or threads, only the reference count is decremented,
 *       and the resource itself is not released until all references are closed.
 *
 * @warning Always ensure `fd` is valid before calling this function. Attempting
 *          to close an invalid or already closed descriptor may lead to undefined
 *          behavior or errors.
 *
 * @see sys_open(), sys_read(), sys_write(), close()
 */
sysret_t sys_close(int fd)
{
    int ret = close(fd);
    return (ret < 0 ? GET_ERRNO() : ret);
}

/**
 * @brief Performs control operations on a file descriptor.
 *
 * This system call allows a program to manipulate the behavior of a device or file
 * associated with the file descriptor `fd` by issuing a control command (`cmd`).
 * The function provides an interface to interact with device drivers, such as modifying
 * the settings of a device or performing custom operations.
 *
 * @param fd The file descriptor representing the device or file on which the ioctl
 *           command will be performed. This should be a valid, open file descriptor.
 * @param cmd The control command to be executed. The exact behavior depends on the
 *            device and the command. Commands are usually defined by the device driver
 *            or the kernel and can vary widely.
 * @param data A pointer to a buffer used for passing data to and from the command.
 *             The contents of the buffer depend on the specific command. For input
 *             commands, it may contain the data to be written to the device. For output
 *             commands, it may hold the data returned by the device.
 * @return On success, returns 0. On failure, returns `errno`.
 *
 * @note The actual functionality of `sys_ioctl` depends heavily on the specific `cmd`
 *       and the type of device associated with the file descriptor `fd`. Each device
 *       may have a different set of valid ioctl commands.
 *
 * @warning Ensure that `fd` refers to a valid file descriptor for a device that supports
 *          ioctl commands. Providing an invalid command or using an unsupported device
 *          may lead to undefined behavior or errors.
 *
 * @see sys_open(), sys_read(), sys_write(), ioctl()
 */
sysret_t sys_ioctl(int fd, unsigned long cmd, void *data)
{
#ifdef ARCH_MM_MMU
    if (cmd == F_GETLK || cmd == F_SETLK || cmd == F_SETLKW)
    {
        struct flock lock;
        int ret;

        if (data == RT_NULL ||
            !lwp_user_accessable(data, sizeof(lock)) ||
            lwp_get_from_user(&lock, data, sizeof(lock)) != sizeof(lock))
        {
            return -EFAULT;
        }

        ret = ioctl(fd, cmd, &lock);
        if (ret >= 0 && cmd == F_GETLK &&
            lwp_put_to_user(data, &lock, sizeof(lock)) != sizeof(lock))
        {
            return -EFAULT;
        }
        return ret < 0 ? GET_ERRNO() : ret;
    }
#endif
    int ret = ioctl(fd, cmd, data);
    return (ret < 0 ? GET_ERRNO() : ret);
}

/**
 * @brief Retrieves information about a file associated with a file descriptor.
 *
 * This system call retrieves the status information about the file referred to
 * by the file descriptor `file` and stores it in the structure pointed to by `buf`.
 * It is typically used to obtain metadata such as file size, permissions, type,
 * and timestamps.
 *
 * @param file The file descriptor referring to an open file. It must be a valid file
 *             descriptor returned by `sys_open()` or similar system calls.
 * @param buf A pointer to a `struct stat` that will be populated with the file's
 *            status information, including attributes like size, access times,
 *            permissions, and more.
 * @return On success, returns `0`. On failure, returns `errno` to indicate the error.
 *
 * @note The structure `struct stat` typically includes the following fields:
 *       - `st_size`: Size of the file in bytes.
 *       - `st_mode`: File type and permissions.
 *       - `st_mtime`: Last modification time.
 *       - `st_atime`: Last access time.
 *       - `st_ctime`: Time of last status change.
 *
 * @warning Ensure that `file` is a valid, open file descriptor. Invalid file
 *          descriptors or unsupported file types may lead to errors when using
 *          this function.
 *
 * @see sys_open(), sys_close(), stat(), fstat()
 */
sysret_t sys_fstat(int file, struct stat *buf)
{
#ifdef ARCH_MM_MMU
    int ret = -1;
    struct stat statbuff = { 0 };

    if (!lwp_user_accessable((void *)buf, sizeof(struct stat)))
    {
        return -EFAULT;
    }
    else
    {
        ret = fstat(file, &statbuff);

        if (ret == 0)
        {
            lwp_put_to_user(buf, &statbuff, sizeof statbuff);
        }
        else
        {
            ret = GET_ERRNO();
        }

        return ret;
    }
#else
    if (!lwp_user_accessable((void *)buf, sizeof(struct stat)))
    {
        return -EFAULT;
    }
    int ret = fstat(file, buf);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Monitors multiple file descriptors to see if they are ready for I/O.
 *
 * This system call monitors the file descriptors specified in `fds` for any
 * I/O events such as readiness for reading, writing, or exceptional conditions.
 * It waits up to the specified timeout and returns with information about which
 * file descriptors are ready for the requested operations.
 *
 * @param fds An array of `struct pollfd` structures, each specifying a file descriptor
 *            to be monitored and the events to be checked for. On return, each structure
 *            will contain the result of the poll operation for that file descriptor.
 * @param nfds The number of elements in the `fds` array.
 * @param timeout The maximum time (in milliseconds) to wait for events. If `timeout`
 *                is `-1`, the call will block indefinitely. If `timeout` is `0`,
 *                the call will return immediately.
 * @return On success, returns the number of file descriptors with events that are ready,
 *         or `0` if the timeout expired with no events. On error, returns `-1` and sets `errno`.
 *
 * @note The `pollfd` structure used for each file descriptor contains the following fields:
 *       - `fd`: The file descriptor to be monitored.
 *       - `events`: The events to monitor (e.g., `POLLIN`, `POLLOUT`).
 *       - `revents`: The events that actually occurred.
 *
 * @warning Ensure that `fds` contains valid file descriptors. Invalid descriptors or
 *          unsupported types (such as sockets) may result in errors. Also, be mindful
 *          of the timeout behavior - passing `0` will cause an immediate return, and passing
 *          `-1` will block indefinitely.
 *
 * @see sys_select(), poll(), select()
 */
sysret_t sys_poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
    int ret = -1;
#ifdef ARCH_MM_MMU
    struct pollfd *kfds = RT_NULL;

    if (!lwp_user_accessable((void *)fds, nfds * sizeof *fds))
    {
        return -EFAULT;
    }

    kfds = (struct pollfd *)kmem_get(nfds * sizeof *kfds);
    if (!kfds)
    {
        return -ENOMEM;
    }

    lwp_get_from_user(kfds, fds, nfds * sizeof *kfds);

    ret = poll(kfds, nfds, timeout);
    if (ret > 0)
    {
        lwp_put_to_user(fds, kfds, nfds * sizeof *kfds);
    }

    kmem_put(kfds);
    return ret;
#else
    if (!lwp_user_accessable((void *)fds, nfds * sizeof *fds))
    {
        return -EFAULT;
    }
    ret = poll(fds, nfds, timeout);
    return ret;
#endif /* ARCH_MM_MMU */
}

/**
 * @brief Monitors multiple file descriptors for readiness to perform I/O operations.
 *
 * This system call allows a program to monitor multiple file descriptors to see if
 * they are ready for reading, writing, or have exceptional conditions. It waits
 * for one or more of the file descriptors to become ready or for the specified
 * timeout to expire.
 *
 * @param nfds The highest-numbered file descriptor in any of the sets, plus one.
 *             This is the number of file descriptors to monitor.
 * @param readfds A pointer to an `fd_set` structure specifying the file descriptors
 *                to be checked for readability. If a file descriptor is ready to
 *                read, it will be set in the returned `fd_set`.
 * @param writefds A pointer to an `fd_set` structure specifying the file descriptors
 *                 to be checked for writability. If a file descriptor is ready to
 *                 write, it will be set in the returned `fd_set`.
 * @param exceptfds A pointer to an `fd_set` structure specifying the file descriptors
 *                  to be checked for exceptional conditions (e.g., out-of-band data).
 *                  If a file descriptor has exceptional conditions, it will be set in
 *                  the returned `fd_set`.
 * @param timeout A pointer to a `struct timeval` that specifies the maximum time
 *                to wait for an event. If `NULL`, the call blocks indefinitely. If
 *                `timeout` is `0`, the call will return immediately.
 * @return On success, returns the number of file descriptors that are ready for
 *         the requested operations, or `0` if the timeout expired without any events.
 *         On error, returns `-1` and sets `errno`.
 *
 * @note The `fd_set` structures should be initialized using the `FD_ZERO` macro
 *       and populated using the `FD_SET` macro. After the call, the `fd_set` structures
 *       will contain the file descriptors that are ready for the requested operations.
 *
 * @warning Ensure that the `fd_set` structures are properly initialized and that
 *          `nfds` correctly reflects the number of file descriptors to monitor.
 *          Passing invalid file descriptors or incorrect `nfds` can lead to undefined behavior.
 *
 * @see sys_poll(), sys_read(), sys_write(), poll(), select()
 */
sysret_t sys_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
#ifdef ARCH_MM_MMU
    int ret = -1;
    fd_set *kreadfds = RT_NULL, *kwritefds = RT_NULL, *kexceptfds = RT_NULL;
    struct timeval ktimeout, *ptimeout = RT_NULL;

    if (readfds)
    {
        if (!lwp_user_accessable((void *)readfds, sizeof *readfds))
        {
            SET_ERRNO(EFAULT);
            goto quit;
        }
        kreadfds = (fd_set *)kmem_get(sizeof *kreadfds);
        if (!kreadfds)
        {
            SET_ERRNO(ENOMEM);
            goto quit;
        }
        lwp_get_from_user(kreadfds, readfds, sizeof *kreadfds);
    }
    if (writefds)
    {
        if (!lwp_user_accessable((void *)writefds, sizeof *writefds))
        {
            SET_ERRNO(EFAULT);
            goto quit;
        }
        kwritefds = (fd_set *)kmem_get(sizeof *kwritefds);
        if (!kwritefds)
        {
            SET_ERRNO(ENOMEM);
            goto quit;
        }
        lwp_get_from_user(kwritefds, writefds, sizeof *kwritefds);
    }
    if (exceptfds)
    {
        if (!lwp_user_accessable((void *)exceptfds, sizeof *exceptfds))
        {
            SET_ERRNO(EFAULT);
            goto quit;
        }
        kexceptfds = (fd_set *)kmem_get(sizeof *kexceptfds);
        if (!kexceptfds)
        {
            SET_ERRNO(EINVAL);
            goto quit;
        }
        lwp_get_from_user(kexceptfds, exceptfds, sizeof *kexceptfds);
    }
    if (timeout)
    {
        if (!lwp_user_accessable((void *)timeout, sizeof *timeout))
        {
            SET_ERRNO(EFAULT);
            goto quit;
        }
        lwp_get_from_user(&ktimeout, timeout, sizeof ktimeout);
        ptimeout = &ktimeout;
    }

    ret = select(nfds, kreadfds, kwritefds, kexceptfds, ptimeout);
    if (kreadfds)
    {
        lwp_put_to_user(readfds, kreadfds, sizeof *kreadfds);
    }
    if (kwritefds)
    {
        lwp_put_to_user(writefds, kwritefds, sizeof *kwritefds);
    }
    if (kexceptfds)
    {
        lwp_put_to_user(exceptfds, kexceptfds, sizeof *kexceptfds);
    }
quit:
    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    if (kreadfds)
    {
        kmem_put(kreadfds);
    }
    if (kwritefds)
    {
        kmem_put(kwritefds);
    }
    if (kexceptfds)
    {
        kmem_put(kexceptfds);
    }
    return ret;
#else
    int ret;

    if (!lwp_user_accessable((void *)readfds, sizeof *readfds))
    {
        return -EFAULT;
    }
    if (!lwp_user_accessable((void *)writefds, sizeof *writefds))
    {
        return -EFAULT;
    }
    if (!lwp_user_accessable((void *)exceptfds, sizeof *exceptfds))
    {
        return -EFAULT;
    }
    ret = select(nfds, readfds, writefds, exceptfds, timeout);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

/**
 * @brief Removes a file or symbolic link from the filesystem.
 *
 * This system call deletes the file or symbolic link specified by `pathname`.
 * After the call, the file will be unlinked from the filesystem, meaning it will
 * no longer be accessible via its pathname. If the file is still open, it will
 * remain available to processes that have it open until all file descriptors are
 * closed. If the file is a regular file and has no other hard links, it will be
 * removed from the disk once all references to it are closed.
 *
 * @param pathname The path to the file or symbolic link to be removed. It can be
 *                 an absolute or relative path.
 * @return On success, returns `0`. On failure, returns `errno` to indicate
 *         the error.
 *
 * @note If the file is currently being used by a process (i.e., the file descriptor
 *       is still open), it will not be immediately removed from the disk. The actual
 *       removal occurs once the file is no longer in use. Symbolic links are removed
 *       as soon as the call is executed.
 *
 * @warning Ensure that the file or link to be removed exists and that the `pathname`
 *          is correctly specified. The operation will fail if the file is open and
 *          in use by other processes or if the pathname does not exist.
 *
 * @see sys_open(), sys_remove(), unlink()
 */
sysret_t sys_unlink(const char *pathname)
{
#ifdef ARCH_MM_MMU
    int ret = -1;
    rt_size_t len = 0;
    char *kname = RT_NULL;

    len = lwp_user_strlen(pathname);
    if (!len)
    {
        return -EINVAL;
    }

    kname = (char *)kmem_get(len + 1);
    if (!kname)
    {
        return -ENOMEM;
    }

    if (lwp_get_from_user(kname, (void *)pathname, len + 1) != (len + 1))
    {
        kmem_put(kname);
        return -EINVAL;
    }
    ret = unlink(kname);
    if (ret < 0)
    {
        ret = GET_ERRNO();
    }

    kmem_put(kname);
    return ret;
#else
    int ret = 0;
    ret = unlink(pathname);
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}