xref: /linux/tools/include/nolibc/unistd.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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))
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))
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))
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))
53 int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
54 {
55 	return __nolibc_syscall3(__NR_ftruncate64, fd, length0, length1);
56 }
57 #define _sys_ftruncate64 _sys_ftruncate64
58 #endif
59 
60 static __attribute__((unused))
61 int _sys_ftruncate(int fd, off_t length)
62 {
63 #if defined(_sys_ftruncate64)
64 	return _sys_ftruncate64(fd, __NOLIBC_LLARGPART(length, 0), __NOLIBC_LLARGPART(length, 1));
65 #else
66 	return __nolibc_syscall2(__NR_ftruncate, fd, length);
67 #endif
68 }
69 
70 static __attribute__((unused))
71 int ftruncate(int fd, off_t length)
72 {
73 	return __sysret(_sys_ftruncate(fd, length));
74 }
75 
76 /*
77  * char *getcwd(char *buf, size_t size);
78  */
79 
80 static __attribute__((unused))
81 int _sys_getcwd(char *buf, size_t size)
82 {
83 	return __nolibc_syscall2(__NR_getcwd, buf, size);
84 }
85 
86 static __attribute__((unused))
87 char *getcwd(char *buf, size_t size)
88 {
89 	int ret;
90 
91 	/* Unlike other libc's we don't handle passing NULL for buf */
92 	if (!buf || !size) {
93 		SET_ERRNO(EINVAL);
94 		return NULL;
95 	}
96 
97 	ret = __sysret(_sys_getcwd(buf, size));
98 
99 	/* On error return NULL, __sysret() above will have set errno */
100 	if (ret < 0)
101 		return NULL;
102 
103 	/* Handle no path being written or the kernel putting
104 	 * "(unreachable)" into the buffer instead of a path.
105 	 * This matches what musl is doing.
106 	 */
107 	if (ret == 0 || buf[0] != '/') {
108 		SET_ERRNO(ENOENT);
109 		return NULL;
110 	}
111 
112 	/* ret must be the number of bytes written at this point,
113 	 * so return the pointer to buf.
114 	 */
115 	return buf;
116 }
117 
118 static __attribute__((unused))
119 int msleep(unsigned int msecs)
120 {
121 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
122 
123 	if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
124 		return (my_timeval.tv_sec * 1000) +
125 			(my_timeval.tv_usec / 1000) +
126 			!!(my_timeval.tv_usec % 1000);
127 	else
128 		return 0;
129 }
130 
131 /*
132  * ssize_t readlink(const char *path, char *buf, size_t bufsiz);
133  */
134 
135 static __attribute__((unused))
136 ssize_t _sys_readlink(const char *path, char *buf, size_t bufsiz)
137 {
138 	return __nolibc_syscall4(__NR_readlinkat, AT_FDCWD, path, buf, bufsiz);
139 }
140 
141 static __attribute__((unused))
142 ssize_t readlink(const char *path, char *buf, size_t bufsiz)
143 {
144 	return __sysret(_sys_readlink(path, buf, bufsiz));
145 }
146 
147 static __attribute__((unused))
148 unsigned int sleep(unsigned int seconds)
149 {
150 	struct timeval my_timeval = { seconds, 0 };
151 
152 	if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
153 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
154 	else
155 		return 0;
156 }
157 
158 static __attribute__((unused))
159 int usleep(unsigned int usecs)
160 {
161 	struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
162 
163 	return _sys_select(0, NULL, NULL, NULL, &my_timeval);
164 }
165 
166 static __attribute__((unused))
167 int tcsetpgrp(int fd, pid_t pid)
168 {
169 	return ioctl(fd, TIOCSPGRP, &pid);
170 }
171 
172 #endif /* _NOLIBC_UNISTD_H */
173