Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifdef __serenity__
28#include <Kernel/Syscall.h>
29#endif
30#include <errno.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <sys/stdint.h>
35
36extern "C" {
37
38#ifdef __serenity__
39int fcntl(int fd, int cmd, ...)
40{
41 va_list ap;
42 va_start(ap, cmd);
43 u32 extra_arg = va_arg(ap, u32);
44 int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
45 __RETURN_WITH_ERRNO(rc, rc, -1);
46}
47#endif
48
49int watch_file(const char* path, size_t path_length)
50{
51#ifdef __serenity__
52 int rc = syscall(SC_watch_file, path, path_length);
53 __RETURN_WITH_ERRNO(rc, rc, -1);
54#else
55 /* TODO: use kqueue? */
56 (void)path;
57 (void)path_length;
58 return 0;
59#endif
60}
61
62int creat(const char* path, mode_t mode)
63{
64 return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
65}
66
67int creat_with_path_length(const char* path, size_t path_length, mode_t mode)
68{
69 return open_with_path_length(path, path_length, O_CREAT | O_WRONLY | O_TRUNC, mode);
70}
71
72int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode)
73{
74 return openat_with_path_length(AT_FDCWD, path, path_length, options, mode);
75}
76
77int openat_with_path_length(int dirfd, const char* path, size_t path_length, int options, mode_t mode)
78{
79 if (!path) {
80 errno = EFAULT;
81 return -1;
82 }
83 if (path_length > INT32_MAX) {
84 errno = EINVAL;
85 return -1;
86 }
87#ifdef __serenity__
88 Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
89 int rc = syscall(SC_open, ¶ms);
90 __RETURN_WITH_ERRNO(rc, rc, -1);
91#else
92 return openat(dirfd, path, options, mode);
93#endif
94}
95
96#ifdef __serenity__
97int open(const char* path, int options, ...)
98{
99 if (!path) {
100 errno = EFAULT;
101 return -1;
102 }
103 va_list ap;
104 va_start(ap, options);
105 auto mode = (mode_t)va_arg(ap, unsigned);
106 va_end(ap);
107 return open_with_path_length(path, strlen(path), options, mode);
108}
109
110int openat(int dirfd, const char* path, int options, ...)
111{
112 if (!path) {
113 errno = EFAULT;
114 return -1;
115 }
116 va_list ap;
117 va_start(ap, options);
118 auto mode = (mode_t)va_arg(ap, unsigned);
119 va_end(ap);
120 return openat_with_path_length(dirfd, path, strlen(path), options, mode);
121}
122#endif
123}