xref: /openbmc/linux/tools/include/nolibc/unistd.h (revision e021ae7f)
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 #ifndef _NOLIBC_UNISTD_H
8 #define _NOLIBC_UNISTD_H
9 
10 #include "std.h"
11 #include "arch.h"
12 #include "types.h"
13 #include "sys.h"
14 
15 
16 #define STDIN_FILENO  0
17 #define STDOUT_FILENO 1
18 #define STDERR_FILENO 2
19 
20 
21 static __attribute__((unused))
22 int msleep(unsigned int msecs)
23 {
24 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
25 
26 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
27 		return (my_timeval.tv_sec * 1000) +
28 			(my_timeval.tv_usec / 1000) +
29 			!!(my_timeval.tv_usec % 1000);
30 	else
31 		return 0;
32 }
33 
34 static __attribute__((unused))
35 unsigned int sleep(unsigned int seconds)
36 {
37 	struct timeval my_timeval = { seconds, 0 };
38 
39 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
40 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
41 	else
42 		return 0;
43 }
44 
45 static __attribute__((unused))
46 int usleep(unsigned int usecs)
47 {
48 	struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
49 
50 	return sys_select(0, 0, 0, 0, &my_timeval);
51 }
52 
53 static __attribute__((unused))
54 int tcsetpgrp(int fd, pid_t pid)
55 {
56 	return ioctl(fd, TIOCSPGRP, &pid);
57 }
58 
59 #define _syscall(N, ...)                                                      \
60 ({                                                                            \
61 	long _ret = my_syscall##N(__VA_ARGS__);                               \
62 	if (_ret < 0) {                                                       \
63 		SET_ERRNO(-_ret);                                             \
64 		_ret = -1;                                                    \
65 	}                                                                     \
66 	_ret;                                                                 \
67 })
68 
69 #define _syscall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
70 #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
71 #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
72 #define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__)
73 
74 /* make sure to include all global symbols */
75 #include "nolibc.h"
76 
77 #endif /* _NOLIBC_UNISTD_H */
78