fcntl.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <syscall.h>
  13. extern "C" {
  14. int fcntl(int fd, int cmd, ...)
  15. {
  16. va_list ap;
  17. va_start(ap, cmd);
  18. u32 extra_arg = va_arg(ap, u32);
  19. int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
  20. va_end(ap);
  21. __RETURN_WITH_ERRNO(rc, rc, -1);
  22. }
  23. int create_inode_watcher(unsigned flags)
  24. {
  25. int rc = syscall(SC_create_inode_watcher, flags);
  26. __RETURN_WITH_ERRNO(rc, rc, -1);
  27. }
  28. int inode_watcher_add_watch(int fd, const char* path, size_t path_length, unsigned event_mask)
  29. {
  30. Syscall::SC_inode_watcher_add_watch_params params { { path, path_length }, fd, event_mask };
  31. int rc = syscall(SC_inode_watcher_add_watch, &params);
  32. __RETURN_WITH_ERRNO(rc, rc, -1);
  33. }
  34. int inode_watcher_remove_watch(int fd, int wd)
  35. {
  36. int rc = syscall(SC_inode_watcher_remove_watch, fd, wd);
  37. __RETURN_WITH_ERRNO(rc, rc, -1);
  38. }
  39. int creat(const char* path, mode_t mode)
  40. {
  41. return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
  42. }
  43. int open(const char* path, int options, ...)
  44. {
  45. if (!path) {
  46. errno = EFAULT;
  47. return -1;
  48. }
  49. auto path_length = strlen(path);
  50. if (path_length > INT32_MAX) {
  51. errno = EINVAL;
  52. return -1;
  53. }
  54. va_list ap;
  55. va_start(ap, options);
  56. auto mode = (mode_t)va_arg(ap, unsigned);
  57. va_end(ap);
  58. Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
  59. int rc = syscall(SC_open, &params);
  60. __RETURN_WITH_ERRNO(rc, rc, -1);
  61. }
  62. int openat(int dirfd, const char* path, int options, ...)
  63. {
  64. if (!path) {
  65. errno = EFAULT;
  66. return -1;
  67. }
  68. auto path_length = strlen(path);
  69. if (path_length > INT32_MAX) {
  70. errno = EINVAL;
  71. return -1;
  72. }
  73. va_list ap;
  74. va_start(ap, options);
  75. auto mode = (mode_t)va_arg(ap, unsigned);
  76. va_end(ap);
  77. Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
  78. int rc = syscall(SC_open, &params);
  79. __RETURN_WITH_ERRNO(rc, rc, -1);
  80. }
  81. }