xref: /haiku/headers/posix/unistd.h (revision a127b88ecbfab58f64944c98aa47722a18e363b2)
1 /*
2  * Copyright 2004-2015 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		dup(int fd);
345 extern int		dup2(int fd1, int fd2);
346 extern int		close(int fd);
347 extern int		link(const char *toPath, const char *path);
348 extern int		linkat(int toFD, const char *toPath, int pathFD,
349 					const char *path, int flag);
350 extern int		unlink(const char *name);
351 extern int		unlinkat(int fd, const char *path, int flag);
352 extern int		rmdir(const char *path);
353 
354 extern ssize_t  readlink(const char *path, char *buffer, size_t bufferSize);
355 extern ssize_t	readlinkat(int fd, const char *path, char *buffer,
356 					size_t bufferSize);
357 extern int      symlink(const char *toPath, const char *symlinkPath);
358 extern int		symlinkat(const char *toPath, int fd, const char *symlinkPath);
359 
360 extern int      ftruncate(int fd, off_t newSize);
361 extern int      truncate(const char *path, off_t newSize);
362 struct ioctl_args {
363     void* argument;
364     size_t size;
365 };
366 int __ioctl(int fd, ulong cmd, struct ioctl_args args);
367 #ifndef __cplusplus
368 extern int		ioctl(int fd, unsigned long op, ...);
369 #ifndef _KERNEL_MODE
370 #define ioctl(a, b, c...) __ioctl(a, b, (struct ioctl_args){ c })
371 #endif
372 #else
373 inline int
374 ioctl(int fd, unsigned long op, void* argument = NULL, size_t size = 0)
375 {
376 	return __ioctl(fd, op, (struct ioctl_args){ argument, size });
377 }
378 #endif
379 
380 extern ssize_t	read(int fd, void *buffer, size_t count);
381 extern ssize_t  read_pos(int fd, off_t pos, void *buffer, size_t count);
382 extern ssize_t	pread(int fd, void *buffer, size_t count, off_t pos);
383 extern ssize_t	write(int fd, const void *buffer, size_t count);
384 extern ssize_t  write_pos(int fd, off_t pos, const void *buffer,size_t count);
385 extern ssize_t	pwrite(int fd, const void *buffer, size_t count, off_t pos);
386 extern off_t	lseek(int fd, off_t offset, int whence);
387 
388 extern void		sync(void);
389 extern int		fsync(int fd);
390 
391 extern int		chown(const char *path, uid_t owner, gid_t group);
392 extern int		fchown(int fd, uid_t owner, gid_t group);
393 extern int		lchown(const char *path, uid_t owner, gid_t group);
394 extern int		fchownat(int fd, const char *path, uid_t owner, gid_t group,
395 					int flag);
396 
397 extern int		getpagesize(void);
398 extern int      getdtablesize(void);
399 extern long		sysconf(int name);
400 extern long		fpathconf(int fd, int name);
401 extern long		pathconf(const char *path, int name);
402 extern size_t	confstr(int name, char *buf, size_t len);
403 extern int		lockf(int fd, int function, off_t size);
404 
405 /* process functions */
406 extern pid_t	fork(void);
407 extern pid_t	vfork(void);
408 extern int		execve(const char *path, char * const argv[],
409 					char *const environment[]);
410 extern int		execl(const char *path, const char *arg, ...);
411 extern int		execv(const char *path, char *const argv[]);
412 extern int		execlp(const char *file, const char *arg, ...);
413 extern int		execle(const char *path, const char *arg , ... /*, char **envp */);
414 extern int		execvp(const char *file, char *const argv[]);
415 extern int		execvpe(const char *file, char *const argv[],
416 					char *const environment[]);
417 
418 extern void		_exit(int status) __attribute__ ((noreturn));
419 
420 extern pid_t	tcgetpgrp(int fd);
421 extern int		tcsetpgrp(int fd, pid_t pgrpid);
422 
423 extern int		brk(void *addr);
424 extern void		*sbrk(intptr_t increment);
425 
426 extern unsigned	int	alarm(unsigned int seconds);
427 extern useconds_t	ualarm(useconds_t microSeconds, useconds_t interval);
428 extern unsigned int	sleep(unsigned int seconds);
429 extern int			usleep(unsigned int microSeconds);
430 extern int 			pause(void);
431 
432 /* process */
433 extern pid_t	getpid(void);
434 extern pid_t	getpgrp(void);
435 extern pid_t	getppid(void);
436 extern pid_t	getsid(pid_t pid);
437 extern pid_t	getpgid(pid_t pid);
438 
439 extern pid_t	setsid(void);
440 extern int		setpgid(pid_t pid, pid_t pgid);
441 extern pid_t	setpgrp(void);
442 
443 extern int		chroot(const char *path);
444 
445 extern int		nice(int incr);
446 
447 /* access permissions */
448 extern gid_t	getegid(void);
449 extern uid_t	geteuid(void);
450 extern gid_t	getgid(void);
451 extern uid_t	getuid(void);
452 
453 extern int		setgid(gid_t gid);
454 extern int		setuid(uid_t uid);
455 extern int		setegid(gid_t gid);
456 extern int		seteuid(uid_t uid);
457 extern int		setregid(gid_t rgid, gid_t egid);
458 extern int		setreuid(uid_t ruid, uid_t euid);
459 
460 extern int		getgrouplist(const char* user, gid_t baseGroup,
461 					gid_t* groupList, int* groupCount);
462 extern int		getgroups(int groupCount, gid_t groupList[]);
463 extern int		initgroups(const char* user, gid_t baseGroup);
464 extern int		setgroups(int groupCount, const gid_t* groupList);
465 
466 extern char		*getlogin(void);
467 extern int		getlogin_r(char *name, size_t nameSize);
468 
469 /* host name */
470 extern int		sethostname(const char *hostName, size_t nameSize);
471 extern int		gethostname(char *hostName, size_t nameSize);
472 
473 /* tty */
474 extern int		isatty(int fd);
475 extern char		*ttyname(int fd);
476 extern int		ttyname_r(int fd, char *buffer, size_t bufferSize);
477 
478 /* misc */
479 extern char 	*crypt(const char *key, const char *salt);
480 extern void 	encrypt(char block[64], int edflag);
481 extern int		getopt(int argc, char *const *argv, const char *shortOpts);
482 extern void 	swab(const void *src, void *dest, ssize_t nbytes);
483 
484 /* getopt() related external variables */
485 extern char *optarg;
486 extern int optind, opterr, optopt;
487 
488 #ifdef __cplusplus
489 }
490 #endif
491 
492 #endif  /* _UNISTD_H_ */
493