1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3 * unistd function definitions for NOLIBC
4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
5 */
6
7 /* make sure to include all global symbols */
8 #include "nolibc.h"
9
10 #ifndef _NOLIBC_UNISTD_H
11 #define _NOLIBC_UNISTD_H
12
13 #include "std.h"
14 #include "arch.h"
15 #include "types.h"
16 #include "sys.h"
17
18
19 #define STDIN_FILENO 0
20 #define STDOUT_FILENO 1
21 #define STDERR_FILENO 2
22
23 #define F_OK 0
24 #define X_OK 1
25 #define W_OK 2
26 #define R_OK 4
27
28 /*
29 * int access(const char *path, int amode);
30 * int faccessat(int fd, const char *path, int amode, int flag);
31 */
32
33 static __attribute__((unused))
_sys_faccessat(int fd,const char * path,int amode,int flag)34 int _sys_faccessat(int fd, const char *path, int amode, int flag)
35 {
36 return __nolibc_syscall4(__NR_faccessat, fd, path, amode, flag);
37 }
38
39 static __attribute__((unused))
faccessat(int fd,const char * path,int amode,int flag)40 int faccessat(int fd, const char *path, int amode, int flag)
41 {
42 return __sysret(_sys_faccessat(fd, path, amode, flag));
43 }
44
45 static __attribute__((unused))
access(const char * path,int amode)46 int access(const char *path, int amode)
47 {
48 return faccessat(AT_FDCWD, path, amode, 0);
49 }
50
51 #if !defined(_sys_ftruncate64) && defined(__NR_ftruncate64)
52 static __attribute__((unused))
_sys_ftruncate64(int fd,uint32_t length0,uint32_t length1)53 int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
54 {
55 #if defined(__NOLIBC_PAD_64BIT_SYSCALL_ARGUMENT_PAIR)
56 return __nolibc_syscall4(__NR_ftruncate64, fd, 0, length0, length1);
57 #else
58 return __nolibc_syscall3(__NR_ftruncate64, fd, length0, length1);
59 #endif
60 }
61 #define _sys_ftruncate64 _sys_ftruncate64
62 #endif
63
64 static __attribute__((unused))
_sys_ftruncate(int fd,off_t length)65 int _sys_ftruncate(int fd, off_t length)
66 {
67 #if defined(_sys_ftruncate64)
68 return _sys_ftruncate64(fd, __NOLIBC_LLARGPART(length, 0), __NOLIBC_LLARGPART(length, 1));
69 #else
70 return __nolibc_syscall2(__NR_ftruncate, fd, length);
71 #endif
72 }
73
74 static __attribute__((unused))
ftruncate(int fd,off_t length)75 int ftruncate(int fd, off_t length)
76 {
77 return __sysret(_sys_ftruncate(fd, length));
78 }
79
80 /*
81 * char *getcwd(char *buf, size_t size);
82 */
83
84 static __attribute__((unused))
_sys_getcwd(char * buf,size_t size)85 int _sys_getcwd(char *buf, size_t size)
86 {
87 return __nolibc_syscall2(__NR_getcwd, buf, size);
88 }
89
90 static __attribute__((unused))
getcwd(char * buf,size_t size)91 char *getcwd(char *buf, size_t size)
92 {
93 int ret;
94
95 /* Unlike other libc's we don't handle passing NULL for buf */
96 if (!buf || !size) {
97 SET_ERRNO(EINVAL);
98 return NULL;
99 }
100
101 ret = __sysret(_sys_getcwd(buf, size));
102
103 /* On error return NULL, __sysret() above will have set errno */
104 if (ret < 0)
105 return NULL;
106
107 /* Handle no path being written or the kernel putting
108 * "(unreachable)" into the buffer instead of a path.
109 * This matches what musl is doing.
110 */
111 if (ret == 0 || buf[0] != '/') {
112 SET_ERRNO(ENOENT);
113 return NULL;
114 }
115
116 /* ret must be the number of bytes written at this point,
117 * so return the pointer to buf.
118 */
119 return buf;
120 }
121
122 static __attribute__((unused))
msleep(unsigned int msecs)123 int msleep(unsigned int msecs)
124 {
125 struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
126
127 if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
128 return (my_timeval.tv_sec * 1000) +
129 (my_timeval.tv_usec / 1000) +
130 !!(my_timeval.tv_usec % 1000);
131 else
132 return 0;
133 }
134
135 /*
136 * ssize_t readlink(const char *path, char *buf, size_t bufsiz);
137 */
138
139 static __attribute__((unused))
_sys_readlink(const char * path,char * buf,size_t bufsiz)140 ssize_t _sys_readlink(const char *path, char *buf, size_t bufsiz)
141 {
142 return __nolibc_syscall4(__NR_readlinkat, AT_FDCWD, path, buf, bufsiz);
143 }
144
145 static __attribute__((unused))
readlink(const char * path,char * buf,size_t bufsiz)146 ssize_t readlink(const char *path, char *buf, size_t bufsiz)
147 {
148 return __sysret(_sys_readlink(path, buf, bufsiz));
149 }
150
151 static __attribute__((unused))
sleep(unsigned int seconds)152 unsigned int sleep(unsigned int seconds)
153 {
154 struct timeval my_timeval = { seconds, 0 };
155
156 if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
157 return my_timeval.tv_sec + !!my_timeval.tv_usec;
158 else
159 return 0;
160 }
161
162 static __attribute__((unused))
usleep(unsigned int usecs)163 int usleep(unsigned int usecs)
164 {
165 struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
166
167 return _sys_select(0, NULL, NULL, NULL, &my_timeval);
168 }
169
170 static __attribute__((unused))
tcsetpgrp(int fd,pid_t pid)171 int tcsetpgrp(int fd, pid_t pid)
172 {
173 return ioctl(fd, TIOCSPGRP, &pid);
174 }
175
176 #endif /* _NOLIBC_UNISTD_H */
177