xref: /haiku/headers/posix/unistd.h (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2004-2024, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _UNISTD_H_
6 #define _UNISTD_H_
7 
8 
9 #include <null.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 
13 #include <config/HaikuConfig.h>
14 
15 
16 /* access modes */
17 #define R_OK	4
18 #define W_OK	2
19 #define X_OK	1
20 #define F_OK	0
21 
22 /* standard file descriptors */
23 #define STDIN_FILENO	0
24 #define STDOUT_FILENO	1
25 #define STDERR_FILENO	2
26 
27 /* function arguments needed by lockf() */
28 #define F_ULOCK		0	/* unlock locked sections */
29 #define F_LOCK		1	/* lock a section for exclusive use */
30 #define F_TLOCK		2	/* test and lock a section for exclusive use */
31 #define F_TEST		3	/* test a section for locks by other processes */
32 
33 /* POSIX version support */
34 #define _POSIX_VERSION			(200809L)
35 #define	_POSIX2_VERSION			(200809L)
36 #define _XOPEN_VERSION			(700)
37 
38 
39 #define _POSIX_VDISABLE			((unsigned char)-2)
40 
41 /*
42  * POSIX options and options groups.
43  * Please keep this list in the same order as the applicable standard.
44  * https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html#tag_02_01_06
45  */
46 #define _POSIX_ADVISORY_INFO				(200809L)
47 #define _POSIX_ASYNCHRONOUS_IO				(-1)
48 #define _POSIX_BARRIERS						(200809L)
49 #define _POSIX_CHOWN_RESTRICTED				(1)
50 #define _POSIX_CLOCK_SELECTION				(200809L)
51 #define _POSIX_CPUTIME						(200809L)
52 #define _POSIX_FSYNC						(200809L)
53 #define _POSIX_IPV6							(200809L)
54 #define _POSIX_JOB_CONTROL					(1)
55 #define _POSIX_MAPPED_FILES					(200809L)
56 #define _POSIX_MEMLOCK						(-1)
57 #define _POSIX_MEMLOCK_RANGE				(200809L)
58 #define _POSIX_MEMORY_PROTECTION			(200809L)
59 #define _POSIX_MESSAGE_PASSING				(-1)
60 #define _POSIX_MONOTONIC_CLOCK				(200809L) /* aio_suspend() is missing in Haiku1 */
61 #define _POSIX_NO_TRUNC						(0)
62 #define _POSIX_PRIORITIZED_IO				(-1)
63 #define _POSIX_PRIORITY_SCHEDULING			(-1)
64 #define _POSIX_RAW_SOCKETS					(-1)
65 #define _POSIX_READER_WRITER_LOCKS			(200809L)
66 #define _POSIX_REALTIME_SIGNALS				(200809L)
67 #define _POSIX_REGEXP						(200809L)
68 #define _POSIX_SAVED_IDS					(1)
69 #define _POSIX_SEMAPHORES					(200112L)
70 #define _POSIX_SHARED_MEMORY_OBJECTS		(200809L)
71 #define _POSIX_SHELL						(200809L)
72 #define _POSIX_SPAWN						(200809L)
73 #define _POSIX_SPIN_LOCKS					(-1)
74 #define _POSIX_SPORADIC_SERVER				(-1)
75 #define _POSIX_SYNCHRONIZED_IO				(-1) /* missing fdatasync() */
76 #define _POSIX_THREAD_ATTR_STACKADDR		(200809L)
77 #define _POSIX_THREAD_ATTR_STACKSIZE		(200809L)
78 #define _POSIX_THREAD_CPUTIME				(200809L)
79 #define _POSIX_THREAD_PRIO_INHERIT			(-1) /* not inplemented */
80 #define _POSIX_THREAD_PRIO_PROTECT          (-1)
81 #define _POSIX_THREAD_PRIORITY_SCHEDULING	(-1)
82 #define _POSIX_THREAD_PROCESS_SHARED		(200809L)
83 #define _POSIX_THREAD_ROBUST_PRIO_INHERIT	(-1)
84 #define _POSIX_THREAD_ROBUST_PRIO_PROTECT	(-1)
85 #define _POSIX_THREAD_SAFE_FUNCTIONS		(200809L)
86 #define _POSIX_THREAD_SPORADIC_SERVER		(-1)
87 #define _POSIX_THREADS						(200809L)
88 #define _POSIX_TIMEOUTS						(-1)
89 #define _POSIX_TIMERS						(200809L)
90 #define _POSIX_TRACE						(-1)
91 #define _POSIX_TRACE_EVENT_FILTER			(-1)
92 #define _POSIX_TRACE_INHERIT				(-1)
93 #define _POSIX_TRACE_LOG					(-1)
94 #define _POSIX_TYPED_MEMORY_OBJECTS			(-1)
95 
96 #define	__ILP32_OFF32						(-1)
97 #ifdef __HAIKU_ARCH_64_BIT
98 #define	__ILP32_OFFBIG						(-1)
99 #define	__LP64_OFF64						(0)
100 #else /* __HAIKU_ARCH_32_BIT */
101 #define	__ILP32_OFFBIG						(0)
102 #define	__LP64_OFF64						(-1)
103 #endif
104 #define	__LPBIG_OFFBIG						(-1)
105 
106 
107 #define	_POSIX_V6_ILP32_OFF32		__ILP32_OFF32	/* OB */
108 #define	_POSIX_V6_ILP32_OFFBIG		__ILP32_OFFBIG	/* OB */
109 #define	_POSIX_V6_LP64_OFF64		__LP64_OFF64	/* OB */
110 #define	_POSIX_V6_LPBIG_OFFBIG		__LPBIG_OFFBIG	/* OB */
111 
112 #define	_POSIX_V7_ILP32_OFF32		__ILP32_OFF32
113 #define	_POSIX_V7_ILP32_OFFBIG		__ILP32_OFFBIG
114 #define	_POSIX_V7_LP64_OFF64		__LP64_OFF64
115 #define	_POSIX_V7_LPBIG_OFFBIG		__LPBIG_OFFBIG
116 
117 
118 /* POSIX2 */
119 #define	_POSIX2_C_BIND						(200809L)
120 #define	_POSIX2_C_DEV						(-1)
121 #define	_POSIX2_CHAR_TERM					(1)
122 #define	_POSIX2_FORT_DEV					(-1)
123 #define	_POSIX2_FORT_RUN					(-1)
124 #define	_POSIX2_LOCALEDEF					(-1)
125 #define	_POSIX2_PBS							(-1)
126 #define	_POSIX2_PBS_ACCOUNTING				(-1)
127 #define	_POSIX2_PBS_CHECKPOINT				(-1)
128 #define	_POSIX2_PBS_LOCATE					(-1)
129 #define	_POSIX2_PBS_MESSAGE					(-1)
130 #define	_POSIX2_PBS_TRACK					(-1)
131 #define	_POSIX2_SW_DEV						(-1)
132 #define	_POSIX2_UPE							(-1)
133 
134 /* XSI */
135 #define _XOPEN_CRYPT						(-1)
136 #define _XOPEN_ENH_I18N						(-1)
137 #define	_XOPEN_LEGACY						(-1)
138 #define	_XOPEN_REALTIME						(-1)
139 #define	_XOPEN_REALTIME_THREADS				(-1)
140 #define	_XOPEN_SHM							(-1)
141 #define	_XOPEN_STREAMS						(-1)
142 #define	_XOPEN_UNIX							(-1)
143 #define _XOPEN_UUCP							(-1)
144 
145 /* pathconf() constants */
146 /* BeOS supported values, do not touch */
147 #define _PC_CHOWN_RESTRICTED	1
148 #define _PC_MAX_CANON			2
149 #define _PC_MAX_INPUT			3
150 #define _PC_NAME_MAX			4
151 #define _PC_NO_TRUNC			5
152 #define _PC_PATH_MAX			6
153 #define _PC_PIPE_BUF			7
154 #define _PC_VDISABLE			8
155 #define _PC_LINK_MAX			25
156 /* new values */
157 #define _PC_SYNC_IO				26
158 #define _PC_ASYNC_IO			27
159 #define _PC_PRIO_IO				28
160 #define _PC_SOCK_MAXBUF			29
161 #define _PC_FILESIZEBITS		30
162 #define _PC_REC_INCR_XFER_SIZE	31
163 #define _PC_REC_MAX_XFER_SIZE	32
164 #define _PC_REC_MIN_XFER_SIZE	33
165 #define _PC_REC_XFER_ALIGN		34
166 #define _PC_ALLOC_SIZE_MIN		35
167 #define _PC_SYMLINK_MAX			36
168 #define _PC_2_SYMLINKS			37
169 #define _PC_XATTR_EXISTS		38
170 #define _PC_XATTR_ENABLED		39
171 
172 /* sysconf() constants */
173 /* BeOS supported values, do not touch */
174 #define _SC_ARG_MAX				15
175 #define _SC_CHILD_MAX			16
176 #define _SC_CLK_TCK				17
177 #define _SC_JOB_CONTROL			18
178 #define _SC_NGROUPS_MAX			19
179 #define _SC_OPEN_MAX			20
180 #define _SC_SAVED_IDS			21
181 #define _SC_STREAM_MAX			22
182 #define _SC_TZNAME_MAX			23
183 #define _SC_VERSION				24
184 /* new values */
185 #define _SC_GETGR_R_SIZE_MAX	25
186 #define _SC_GETPW_R_SIZE_MAX	26
187 #define _SC_PAGE_SIZE			27
188 #define _SC_PAGESIZE			_SC_PAGE_SIZE
189 #define _SC_SEM_NSEMS_MAX		28
190 #define _SC_SEM_VALUE_MAX		29
191 #define _SC_SEMAPHORES			30
192 #define _SC_THREADS				31
193 /* TODO: check */
194 #define _SC_IOV_MAX						32
195 #define _SC_UIO_MAXIOV					_SC_IOV_MAX
196 #define _SC_NPROCESSORS_CONF			34
197 #define _SC_NPROCESSORS_ONLN			35
198 #define _SC_ATEXIT_MAX					37
199 #define _SC_PASS_MAX					39
200 #define _SC_PHYS_PAGES					40
201 #define _SC_AVPHYS_PAGES				41
202 #define _SC_PIPE						42
203 #define _SC_SELECT						43
204 #define _SC_POLL						44
205 #define _SC_MAPPED_FILES				45
206 #define _SC_THREAD_PROCESS_SHARED		46
207 #define _SC_THREAD_STACK_MIN			47
208 #define _SC_THREAD_ATTR_STACKADDR		48
209 #define _SC_THREAD_ATTR_STACKSIZE		49
210 #define _SC_THREAD_PRIORITY_SCHEDULING	50
211 #define _SC_REALTIME_SIGNALS			51
212 #define	_SC_MEMORY_PROTECTION			52
213 #define _SC_SIGQUEUE_MAX				53
214 #define _SC_RTSIG_MAX					54
215 #define _SC_MONOTONIC_CLOCK				55
216 #define _SC_DELAYTIMER_MAX				56
217 #define _SC_TIMER_MAX					57
218 #define	_SC_TIMERS						58
219 #define	_SC_CPUTIME						59
220 #define	_SC_THREAD_CPUTIME				60
221 #define _SC_HOST_NAME_MAX				61
222 #define _SC_REGEXP						62
223 #define _SC_SYMLOOP_MAX					63
224 #define _SC_SHELL						64
225 #define _SC_TTY_NAME_MAX				65
226 #define _SC_ADVISORY_INFO				66
227 #define _SC_BARRIERS					67
228 #define _SC_CLOCK_SELECTION				68
229 #define _SC_FSYNC						69
230 #define _SC_IPV6						70
231 #define _SC_MEMLOCK						71
232 #define _SC_MEMLOCK_RANGE				72
233 #define _SC_MESSAGE_PASSING				73
234 #define _SC_PRIORITIZED_IO				74
235 #define _SC_PRIORITY_SCHEDULING			75
236 #define _SC_READER_WRITER_LOCKS			76
237 #define _SC_SHARED_MEMORY_OBJECTS		77
238 #define _SC_SPAWN						78
239 #define _SC_SPIN_LOCKS					79
240 #define _SC_SPORADIC_SERVER				80
241 #define _SC_SYNCHRONIZED_IO				81
242 #define _SC_THREAD_PRIO_INHERIT			82
243 #define _SC_THREAD_PRIO_PROTECT			83
244 #define _SC_THREAD_ROBUST_PRIO_INHERIT	84
245 #define _SC_THREAD_ROBUST_PRIO_PROTECT	85
246 #define _SC_THREAD_SAFE_FUNCTIONS		86
247 #define _SC_THREAD_SPORADIC_SERVER		87
248 #define _SC_TIMEOUTS					88
249 #define _SC_TRACE						89
250 #define _SC_TRACE_EVENT_FILTER			90
251 #define _SC_TRACE_INHERIT				91
252 #define _SC_TRACE_LOG					92
253 #define _SC_TYPED_MEMORY_OBJECTS		93
254 #define _SC_V6_ILP32_OFF32				94	/* OB */
255 #define _SC_V6_ILP32_OFFBIG				95	/* OB */
256 #define _SC_V6_LP64_OFF64				96	/* OB */
257 #define _SC_V6_LPBIG_OFFBIG				97	/* OB */
258 #define _SC_V7_ILP32_OFF32				98
259 #define _SC_V7_ILP32_OFFBIG				99
260 #define _SC_V7_LP64_OFF64				100
261 #define _SC_V7_LPBIG_OFFBIG				101
262 #define _SC_2_C_BIND					102
263 #define _SC_2_C_DEV						103
264 #define _SC_2_CHAR_TERM					104
265 #define _SC_2_FORT_DEV					105
266 #define _SC_2_FORT_RUN					106
267 #define _SC_2_LOCALEDEF					107
268 #define _SC_2_PBS						108	/* OB BE */
269 #define _SC_2_PBS_ACCOUNTING			109	/* OB BE */
270 #define _SC_2_PBS_CHECKPOINT			110	/* OB BE */
271 #define _SC_2_PBS_LOCATE				111 /* OB BE */
272 #define _SC_2_PBS_MESSAGE				112	/* OB BE */
273 #define _SC_2_PBS_TRACK					113	/* OB BE */
274 #define _SC_2_SW_DEV					114
275 #define _SC_2_UPE						115
276 #define _SC_2_VERSION					116
277 #define _SC_XOPEN_CRYPT					117
278 #define _SC_XOPEN_ENH_I18N				118
279 #define _SC_XOPEN_REALTIME				119
280 #define _SC_XOPEN_REALTIME_THREADS		120
281 #define _SC_XOPEN_SHM					121
282 #define _SC_XOPEN_STREAMS				122
283 #define _SC_XOPEN_UNIX					123
284 #define _SC_XOPEN_UUCP					124
285 #define _SC_XOPEN_VERSION				125
286 
287 #if _POSIX_ASYNCHRONOUS_IO >= 0
288 #define _SC_AIO_LISTIO_MAX				126
289 #define _SC_AIO_MAX						127
290 #define _SC_AIO_PRIO_DELTA_MAX			128
291 #endif
292 
293 #define _SC_BC_BASE_MAX					129
294 #define _SC_BC_DIM_MAX					130
295 #define _SC_BC_SCALE_MAX				131
296 #define _SC_BC_STRING_MAX				132
297 
298 #define _SC_COLL_WEIGHTS_MAX			133
299 #define _SC_EXPR_NEST_MAX				134
300 #define _SC_LINE_MAX					135
301 #define _SC_LOGIN_NAME_MAX				136
302 #define _SC_MQ_OPEN_MAX					137
303 #define _SC_MQ_PRIO_MAX					138
304 #define _SC_THREAD_DESTRUCTOR_ITERATIONS	139
305 #define _SC_THREAD_KEYS_MAX				140
306 #define _SC_THREAD_THREADS_MAX			141
307 #define _SC_RE_DUP_MAX					142
308 
309 
310 /* confstr() constants */
311 #define _CS_PATH				1
312 
313 /* lseek() constants */
314 #ifndef SEEK_SET
315 #	define SEEK_SET 0
316 #endif
317 #ifndef SEEK_CUR
318 #	define SEEK_CUR 1
319 #endif
320 #ifndef SEEK_END
321 #	define SEEK_END 2
322 #endif
323 #ifndef SEEK_DATA
324 #	define SEEK_DATA 3
325 #endif
326 #ifndef SEEK_HOLE
327 #	define SEEK_HOLE 4
328 #endif
329 
330 
331 #ifdef __cplusplus
332 extern "C" {
333 #endif
334 
335 /* file functions */
336 extern int		access(const char *path, int accessMode);
337 extern int		faccessat(int fd, const char *path, int accessMode, int flag);
338 
339 extern int		chdir(const char *path);
340 extern int		fchdir(int fd);
341 extern char		*getcwd(char *buffer, size_t size);
342 
343 extern int		pipe(int fildes[2]);
344 extern int		pipe2(int fildes[2], int flags);
345 extern int		dup(int fd);
346 extern int		dup2(int fd1, int fd2);
347 extern int		dup3(int fd1, int fd2, int flags);
348 extern int		close(int fd);
349 extern int		link(const char *toPath, const char *path);
350 extern int		linkat(int toFD, const char *toPath, int pathFD,
351 					const char *path, int flag);
352 extern int		unlink(const char *name);
353 extern int		unlinkat(int fd, const char *path, int flag);
354 extern int		rmdir(const char *path);
355 
356 extern ssize_t  readlink(const char *path, char *buffer, size_t bufferSize);
357 extern ssize_t	readlinkat(int fd, const char *path, char *buffer,
358 					size_t bufferSize);
359 extern int      symlink(const char *toPath, const char *symlinkPath);
360 extern int		symlinkat(const char *toPath, int fd, const char *symlinkPath);
361 
362 extern int      ftruncate(int fd, off_t newSize);
363 extern int      truncate(const char *path, off_t newSize);
364 
365 /* We want the 3rd and 4th arguments to ioctl to be optional, but we can't use varargs because
366  * then we have no way to know if the optional arguments are present, and calling va_next is
367  * undefined behavior (it may or may not work depending on the calling convention).
368  *
369  * In C++, we implement ioctl using default function arguments, but in C this isn't possible.
370  * So, the C implementation is done as vararg macro that use preprocessor magic to call the
371  * 4 argument function and fill in the omitted arguments.
372  *
373  * The legacy ioctl function in C with vararg arguments is still provided for ABI compatibility
374  * with existing applications, but should not be used anymore by newly compiled code.
375  */
376 extern int		__ioctl(int fd, ulong cmd, void* argument, size_t size);
377 #ifndef __cplusplus
378 extern int		ioctl(int fd, unsigned long op, ...);
379 #define _IOCTL2(a, b) 		__ioctl(a, b, NULL, 0)
380 #define _IOCTL3(a, b, c)	__ioctl(a, b, (void*)(c), 0)
381 #define _IOCTL4(a, b, c, d)	__ioctl(a, b, (void*)(c), d)
382 #define _IOCTL(ARG1, ARG2, ARG3, ARG4, NAME, ...) NAME
383 #define ioctl(...) _IOCTL(__VA_ARGS__, _IOCTL4, _IOCTL3, _IOCTL2)(__VA_ARGS__)
384 #else
385 inline int
386 ioctl(int fd, unsigned long op, void* argument = NULL, size_t size = 0)
387 {
388 	return __ioctl(fd, op, argument, size);
389 }
390 #endif
391 
392 extern ssize_t	read(int fd, void *buffer, size_t count);
393 extern ssize_t  read_pos(int fd, off_t pos, void *buffer, size_t count);
394 extern ssize_t	pread(int fd, void *buffer, size_t count, off_t pos);
395 extern ssize_t	write(int fd, const void *buffer, size_t count);
396 extern ssize_t  write_pos(int fd, off_t pos, const void *buffer,size_t count);
397 extern ssize_t	pwrite(int fd, const void *buffer, size_t count, off_t pos);
398 extern off_t	lseek(int fd, off_t offset, int whence);
399 
400 extern void		sync(void);
401 extern int		fsync(int fd);
402 
403 extern int		chown(const char *path, uid_t owner, gid_t group);
404 extern int		fchown(int fd, uid_t owner, gid_t group);
405 extern int		lchown(const char *path, uid_t owner, gid_t group);
406 extern int		fchownat(int fd, const char *path, uid_t owner, gid_t group,
407 					int flag);
408 
409 extern int		getpagesize(void);
410 extern int      getdtablesize(void);
411 extern long		sysconf(int name);
412 extern long		fpathconf(int fd, int name);
413 extern long		pathconf(const char *path, int name);
414 extern size_t	confstr(int name, char *buf, size_t len);
415 extern int		lockf(int fd, int function, off_t size);
416 
417 /* process functions */
418 extern pid_t	fork(void);
419 extern pid_t	vfork(void);
420 extern int		execve(const char *path, char * const argv[],
421 					char *const environment[]);
422 extern int		execl(const char *path, const char *arg, ...);
423 extern int		execv(const char *path, char *const argv[]);
424 extern int		execlp(const char *file, const char *arg, ...);
425 extern int		execle(const char *path, const char *arg , ... /*, char **envp */);
426 extern int		execvp(const char *file, char *const argv[]);
427 extern int		execvpe(const char *file, char *const argv[],
428 					char *const environment[]);
429 
430 extern void		_exit(int status) __attribute__ ((noreturn));
431 
432 extern pid_t	tcgetpgrp(int fd);
433 extern int		tcsetpgrp(int fd, pid_t pgrpid);
434 
435 extern int		brk(void *addr);
436 extern void		*sbrk(intptr_t increment);
437 
438 extern unsigned	int	alarm(unsigned int seconds);
439 extern useconds_t	ualarm(useconds_t microSeconds, useconds_t interval);
440 extern unsigned int	sleep(unsigned int seconds);
441 extern int			usleep(unsigned int microSeconds);
442 extern int 			pause(void);
443 
444 /* process */
445 extern pid_t	getpid(void);
446 extern pid_t	getpgrp(void);
447 extern pid_t	getppid(void);
448 extern pid_t	getsid(pid_t pid);
449 extern pid_t	getpgid(pid_t pid);
450 
451 extern pid_t	setsid(void);
452 extern int		setpgid(pid_t pid, pid_t pgid);
453 extern pid_t	setpgrp(void);
454 
455 extern int		chroot(const char *path);
456 
457 extern int		nice(int incr);
458 
459 /* access permissions */
460 extern gid_t	getegid(void);
461 extern uid_t	geteuid(void);
462 extern gid_t	getgid(void);
463 extern uid_t	getuid(void);
464 
465 extern int		setgid(gid_t gid);
466 extern int		setuid(uid_t uid);
467 extern int		setegid(gid_t gid);
468 extern int		seteuid(uid_t uid);
469 extern int		setregid(gid_t rgid, gid_t egid);
470 extern int		setreuid(uid_t ruid, uid_t euid);
471 
472 extern int		getgrouplist(const char* user, gid_t baseGroup,
473 					gid_t* groupList, int* groupCount);
474 extern int		getgroups(int groupCount, gid_t groupList[]);
475 extern int		initgroups(const char* user, gid_t baseGroup);
476 extern int		setgroups(int groupCount, const gid_t* groupList);
477 
478 extern char		*getlogin(void);
479 extern int		getlogin_r(char *name, size_t nameSize);
480 
481 /* host name */
482 extern int		sethostname(const char *hostName, size_t nameSize);
483 extern int		gethostname(char *hostName, size_t nameSize);
484 
485 /* tty */
486 extern int		isatty(int fd);
487 extern char		*ttyname(int fd);
488 extern int		ttyname_r(int fd, char *buffer, size_t bufferSize);
489 
490 /* misc */
491 extern char 	*crypt(const char *key, const char *salt);
492 extern void 	encrypt(char block[64], int edflag);
493 extern int		getopt(int argc, char *const *argv, const char *shortOpts);
494 extern void 	swab(const void *src, void *dest, ssize_t nbytes);
495 
496 int				getentropy(void *buf, size_t buflen);
497 
498 /* getopt() related external variables */
499 extern char *optarg;
500 extern int optind, opterr, optopt;
501 
502 #ifdef __cplusplus
503 }
504 #endif
505 
506 #endif  /* _UNISTD_H_ */
507