The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
unistd.h
Go to the documentation of this file.
1#pragma once
2
3#include <sys/types.h>
4
5#include <fcntl.h>
6#include <sys/wait.h>
7
8/**
9 * @file unistd.h
10 * @brief This file contains the declarations of POSIX-like system call wrappers.
11 */
12
13#include "syscall.h"
14
15// POSIX: unistd.h declares intptr_t (as in stdint.h, the same type)
16typedef __INTPTR_TYPE__ intptr_t;
17
18/**
19 * write bytes to a file descriptor.
20 * @param fd file descriptor (1 = stdout, 2 = stderr).
21 * @param buf pointer to data to write.
22 * @param count number of bytes to write.
23 * @return number of bytes written, or -1 on error.
24 */
25ssize_t write(int fd, const void *buf, size_t count);
26
27/**
28 * read bytes from a file descriptor.
29 * @param fd file descriptor (0 = stdin).
30 * @param buf buffer to read into.
31 * @param count maximum bytes to read.
32 * @return number of bytes read, or -1 on error.
33 */
34ssize_t read(int fd, void *buf, size_t count);
35
36/**
37 * close a file descriptor.
38 * @param fd file descriptor to close.
39 * @return 0 on success, -1 on failure.
40 */
41int close(int fd);
42
43/**
44 * execute a program, spawning a child process.
45 * @param path path to the ELF executable.
46 * @param argv null-terminated array of argument strings (may be NULL).
47 * @return child PID on success, -1 on failure.
48 */
49pid_t exec(const char *path, const char **argv);
50
51/**
52 * create a child process that is a copy of the caller. the two share
53 * nothing after the call: memory is copied (lazily, on the first write),
54 * and the child gets its own copy of every fd.
55 * @return the child's PID in the parent, 0 in the child, -1 on failure
56 * (errno set).
57 */
58pid_t fork(void);
59
60/**
61 * replace the calling process's program. the pid, cwd and open fds stay.
62 * @param path path to the ELF executable.
63 * @param argv NULL-terminated argument strings.
64 * @param envp NULL-terminated "KEY=VALUE" environment strings.
65 * @return only on failure: -1 (errno set).
66 */
67int execve(const char *path, char *const argv[], char *const envp[]);
68
69/**
70 * execve with the environment of the calling process.
71 * @return only on failure: -1 (errno set).
72 */
73int execv(const char *path, char *const argv[]);
74
75/**
76 * execv, but a file name with no '/' is looked for in the directories of
77 * PATH (/bin when PATH is not set).
78 * @return only on failure: -1 (errno set).
79 */
80int execvp(const char *file, char *const argv[]);
81
82/**
83 * execv, execvp and execve with the arguments as a list that ends with
84 * NULL. execle takes the environment after the NULL.
85 * @return only on failure: -1 (errno set).
86 */
87int execl(const char *path, const char *arg, ...);
88int execlp(const char *file, const char *arg, ...);
89int execle(const char *path, const char *arg, ...);
90
91/**
92 * terminate the current process.
93 * @param status exit code.
94 */
95void _exit(int status) __attribute__((noreturn));
96
97/**
98 * duplicate a descriptor to the lowest free one; both share the open file.
99 * @return the new descriptor, or -1 (errno set).
100 */
101int dup(int fd);
102
103// access() modes
104#define STDIN_FILENO 0
105#define STDOUT_FILENO 1
106#define STDERR_FILENO 2
107
108#define F_OK 0 // the file exists
109#define X_OK 1
110#define W_OK 2
111#define R_OK 4
112
113/**
114 * check that a file exists and may be accessed. there are no users yet,
115 * so every existing file allows every access.
116 * @param path the file.
117 * @param mode F_OK, or R_OK | W_OK | X_OK.
118 * @return 0, or -1 (errno set).
119 */
120int access(const char *path, int mode);
121int faccessat(int dirfd, const char *path, int mode, int flags);
122
123/**
124 * read or write at an offset, without moving the file offset.
125 * @return bytes transferred, or -1 (errno set; ESPIPE for a pipe).
126 */
127ssize_t pread(int fd, void *buf, size_t count, off_t offset);
128ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
129
130/**
131 * write an open file's data to disk.
132 * @return 0, or -1 (errno set).
133 */
134int fsync(int fd);
135
136/**
137 * send SIGALRM to the calling process after a number of seconds.
138 * @param seconds 0 cancels the alarm.
139 * @return the seconds left of the previous alarm, or 0.
140 */
141unsigned int alarm(unsigned int seconds);
142
143/**
144 * sleep until a signal is caught. always returns -1 with EINTR.
145 */
146int pause(void);
147
148/**
149 * sleep for a number of seconds (or microseconds).
150 * @return sleep: the seconds left if a signal ended it early, else 0.
151 * usleep: 0, or -1 (errno set).
152 */
153unsigned int sleep(unsigned int seconds);
154int usleep(unsigned int microseconds);
155
156/**
157 * process groups and sessions (job control).
158 * setpgid(pid, pgid): pid 0 is the caller, pgid 0 makes a new group led
159 * by pid. getpgrp() is getpgid(0). setsid() starts a session and returns
160 * its id.
161 * @return as named, or -1 (errno set).
162 */
163int setpgid(pid_t pid, pid_t pgid);
167
168/**
169 * add increment to the caller's nice value (see sys/resource.h).
170 * @return the new value, or -1 (errno set).
171 */
172int nice(int increment);
173
174/**
175 * make a directory the root of the caller's paths ("/" is then that
176 * directory; root only). the working directory stays: chdir("/") after.
177 * @return 0, or -1 (errno set).
178 */
179int chroot(const char *path);
181
182/**
183 * user and group ids: real (who started the process) and effective (what
184 * it may do). setuid/setgid as root set all ids; otherwise only to the
185 * real or saved id. set the group before giving up root.
186 */
191int setuid(uid_t uid);
192int setgid(gid_t gid);
193int seteuid(uid_t uid);
194int setegid(gid_t gid);
195
196/**
197 * change the owner of a file. root may give a file to anyone; an owner may
198 * only change its group to its own. (uid_t)-1 or (gid_t)-1 keeps one.
199 */
200int chown(const char *path, uid_t uid, gid_t gid);
201int fchown(int fd, uid_t uid, gid_t gid);
202int lchown(const char *path, uid_t uid, gid_t gid);
203int fchownat(int dirfd, const char *path, uid_t uid, gid_t gid, int flags);
204
205/**
206 * get the parent's process ID.
207 * @return the parent's PID, or 0 if the parent has exited.
208 */
210
211/**
212 * let other ready processes run before this one continues.
213 * @return 0.
214 */
215int sched_yield(void);
216
217/**
218 * get the current process ID.
219 * @return the current PID.
220 */
222
223/**
224 * grow the program break (allocate heap memory).
225 * @param increment number of bytes to grow by.
226 * @return pointer to the previous break, or (void*)-1 on failure.
227 */
228void *sbrk(int increment);
229
230/**
231 * get the current working directory.
232 * @param buf buffer to write the path into; NULL: a new one from malloc
233 * (of size bytes, or as long as needed when size is 0).
234 * @param size size of the buffer.
235 * @return buf (or the new buffer), or NULL (errno: ERANGE, ENOMEM).
236 */
237char *getcwd(char *buf, size_t size);
238
239/**
240 * change the current working directory.
241 * @param path path to change to.
242 * @return 0 on success, -1 on failure.
243 */
244int chdir(const char *path);
245
246/**
247 * remove (unlink) a file.
248 * @param path path to the file to remove.
249 * @return 0 on success, -1 on failure.
250 */
251int unlink(const char *path);
252
253/** make a symbolic link at path that points at target */
254int symlink(const char *target, const char *path);
255int symlinkat(const char *target, int dirfd, const char *path);
256/**
257 * read a symbolic link's target (no NUL; a longer target is cut).
258 * @return the bytes put in buffer, or -1 (EINVAL: path is not a link).
259 */
260ssize_t readlink(const char *restrict path, char *restrict buffer, size_t size);
261ssize_t readlinkat(int dirfd, const char *restrict path, char *restrict buffer, size_t size);
262/** give a file one more name (a hard link) */
263int link(const char *old_path, const char *new_path);
264int linkat(int old_dirfd, const char *old_path, int new_dirfd, const char *new_path, int flags);
265/** remove a file, or with AT_REMOVEDIR a directory, relative to dirfd */
266int unlinkat(int dirfd, const char *path, int flags);
267
268/**
269 * remove a directory.
270 * @param path path to the directory to remove.
271 * @return 0 on success, -1 on failure.
272 */
273int rmdir(const char *path);
274
275/**
276 * create a file, or truncate it if it exists (the file is not left open).
277 * @param path path to the file to create.
278 * @return 0 on success, -1 on failure.
279 */
280int create(const char *path);
281
282/* terminal mode constants */
283#define TTY_COOKED 0
284#define TTY_RAW 1
285
286/**
287 * switch the terminal on fd 0 between two modes (a wrapper over termios):
288 * TTY_COOKED is line input with echo and signal keys; TTY_RAW gives one
289 * key_event_t per read (read_event), with no echo and Ctrl+C as a key.
290 * the mode belongs to the terminal, so it stays after the program exits.
291 * @param mode TTY_COOKED or TTY_RAW.
292 * @return the previous mode, or -1 on error.
293 */
294int ttyctl(int mode);
295
296/**
297 * ask for a password on the terminal (standard input), without echo.
298 * @return the line without its newline, in a static buffer (up to 127
299 * bytes), or NULL at the end of input.
300 */
301char *getpass(const char *prompt);
302
303
304/**
305 * true if fd refers to a terminal.
306 */
307int isatty(int fd);
308
309/**
310 * issue a device-control request on an open file descriptor.
311 * only device nodes under /dev service ioctls; other fds return -1/ENOTTY.
312 * @param fd open file descriptor (e.g. an open "/dev/fb0").
313 * @param request driver-defined request code.
314 * @param arg request-specific argument, usually a pointer to a struct.
315 * @return 0 (or a request-specific value) on success, -1 on error (errno set).
316 */
317int ioctl(int fd, unsigned int request, void *arg);
318
319/* console keyboard event types (matches kernel keyboard_event_type_t).
320 * named CONSOLE_KEY_ so they do not clash with the Linux key codes of
321 * <sys/input.h> */
322#define CONSOLE_KEY_EVENT_CHAR 0
323#define CONSOLE_KEY_EVENT_SPECIAL 1
324#define CONSOLE_KEY_EVENT_RAW 2 // with the XKEYRAW termios flag (termios.h)
325
326/* special key codes (matches kernel keyboard_special_key_t) */
327#define CONSOLE_KEY_UP 1
328#define CONSOLE_KEY_DOWN 2
329#define CONSOLE_KEY_LEFT 3
330#define CONSOLE_KEY_RIGHT 4
331#define CONSOLE_KEY_HOME 5
332#define CONSOLE_KEY_END 6
333#define CONSOLE_KEY_PAGE_UP 7
334#define CONSOLE_KEY_PAGE_DOWN 8
335#define CONSOLE_KEY_INSERT 9
336#define CONSOLE_KEY_DELETE 10
337
338/**
339 * keyboard event structure (matches kernel keyboard_event_t).
340 * in raw mode, read() on stdin fills this struct.
341 */
342typedef struct
343{
344 int type; // CONSOLE_KEY_EVENT_CHAR or CONSOLE_KEY_EVENT_SPECIAL
345 union
346 {
347 char character; // valid when type == CONSOLE_KEY_EVENT_CHAR
348 int special; // valid when type == CONSOLE_KEY_EVENT_SPECIAL
349 struct
350 {
351 unsigned char code; // PS/2 set 1 scan code; bit 7: an 0xE0 key
352 unsigned char pressed; // 1: down, 0: up
353 } raw; // valid when type == CONSOLE_KEY_EVENT_RAW
354 };
356
357/**
358 * read a single keyboard event from stdin (raw mode).
359 * blocks until an event is available.
360 * @param event pointer to event struct to fill.
361 * @return 0 on success, -1 on error.
362 */
364
365/**
366 * create a pipe (pair of connected file descriptors).
367 * @param fds array of two ints: fds[0] = read end, fds[1] = write end.
368 * @return 0 on success, -1 on failure.
369 */
370int pipe(int fds[2]);
371
372/**
373 * duplicate a file descriptor to a specific number.
374 * if new_fd is already open, it is closed first.
375 * @param old_fd the fd to duplicate.
376 * @param new_fd the target fd number.
377 * @return new_fd on success, -1 on failure.
378 */
379int dup2(int old_fd, int new_fd);
380
381/**
382 * cut or grow a file to length bytes (the new bytes read as zeros). the
383 * file offset stays.
384 * @return 0, or -1 (errno set).
385 */
386int ftruncate(int fd, off_t length);
387int truncate(const char *path, off_t length);
388
389/* lseek whence constants */
390#define SEEK_SET 0
391#define SEEK_CUR 1
392#define SEEK_END 2
393
394/**
395 * set file cursor position.
396 * @param fd file descriptor.
397 * @param offset byte offset.
398 * @param whence SEEK_SET (from start), SEEK_CUR (from current), SEEK_END (from end).
399 * @return new cursor position, or -1 on error (errno set).
400 */
401off_t lseek(int fd, off_t offset, int whence);
402
403// sysconf names
404#define _SC_ARG_MAX 0
405#define _SC_CHILD_MAX 1
406#define _SC_CLK_TCK 2
407#define _SC_OPEN_MAX 4
408#define _SC_PAGESIZE 30
409#define _SC_PAGE_SIZE _SC_PAGESIZE
410#define _SC_NPROCESSORS_CONF 83
411#define _SC_NPROCESSORS_ONLN 84
412#define _SC_LINE_MAX 43
413#define _SC_HOST_NAME_MAX 180
414#define _SC_LOGIN_NAME_MAX 71
415#define _SC_PHYS_PAGES 85
416
417/** a limit of the system (a _SC_ name), or -1 with EINVAL */
418long sysconf(int name);
419
420// pathconf names (Linux's numbers)
421#define _PC_LINK_MAX 0
422#define _PC_MAX_CANON 1
423#define _PC_MAX_INPUT 2
424#define _PC_NAME_MAX 3
425#define _PC_PATH_MAX 4
426#define _PC_PIPE_BUF 5
427#define _PC_CHOWN_RESTRICTED 6
428#define _PC_NO_TRUNC 7
429#define _PC_VDISABLE 8
430
431/**
432 * a limit of the files at path (a _PC_ name). ginnOS's limits are the
433 * same on every filesystem.
434 * @return the limit, or -1 (errno: EINVAL for a name, or as stat sets it).
435 */
436long pathconf(const char *path, int name);
437long fpathconf(int fd, int name);
438
439// confstr names
440#define _CS_PATH 0 // a PATH that finds the standard tools
441
442/**
443 * a string value of the system (a _CS_ name) into buffer.
444 * @return the size the whole value needs, NUL included; 0 with EINVAL for
445 * an unknown name.
446 */
447size_t confstr(int name, char *buffer, size_t size);
448/** the name of this computer (uname's nodename) into name */
449int gethostname(char *name, size_t length);
450/** set the host name (root only; at most 64 bytes) */
451int sethostname(const char *name, size_t length);
452
453/**
454 * the name of the terminal open as fd (as "/dev/pts/0").
455 * @return the name in a static buffer, or NULL (errno: ENOTTY, EBADF).
456 */
457char *ttyname(int fd);
458int ttyname_r(int fd, char *buffer, size_t size);
459
460/**
461 * make every write reach the disk. ginnOS writes through its cache at
462 * once, so there is nothing to wait for.
463 */
464void sync(void);
465
466/**
467 * go on in the background: a new process in a new session, with no
468 * terminal.
469 * @param keep_dir 0: change to "/".
470 * @param keep_files 0: read and write /dev/null on 0, 1 and 2.
471 * @return 0 in the new process, or -1 (errno set).
472 */
473int daemon(int keep_dir, int keep_files);
474
475/**
476 * the next option of a command line (POSIX getopt).
477 * @param options the option letters; a letter with ':' after it takes an
478 * argument. a ':' first: no messages, and ':' for a missing argument.
479 * @return the option letter, '?' for an unknown one (in optopt), or -1
480 * after the last option (optind is then the first operand).
481 */
482int getopt(int argc, char *const argv[], const char *options);
483extern char *optarg; // the argument of the option getopt returned
484extern int optind; // the next argument getopt looks at (0 starts again)
485extern int opterr; // 0: getopt writes no messages
486extern int optopt; // the option letter of the last error
487/**
488 * fill buf with length random bytes (at most 256) from the kernel.
489 * @return 0, or -1 (errno EIO when length is more than 256).
490 */
491int getentropy(void *buf, size_t length);
492
493/** the name of the user who runs the program (from the password file), or NULL */
494char *getlogin(void);
file open flags and open().
uint8_t access
Definition gdt.c:3
uint8_t flags
Definition idt.c:3
int ioctl(int fd, unsigned int request, void *arg)
int dirfd(DIR *dir)
the file descriptor under a directory stream.
__INTPTR_TYPE__ intptr_t
Definition stdint.h:18
This file contains the declarations of system call numbers and the raw syscall interface.
int sched_yield(void)
give the CPU to another thread that is ready to run.
ELF32 file header.
Definition elf.h:51
an open-file description.
Definition file.h:64
keyboard event structure (matches kernel keyboard_event_t).
Definition unistd.h:343
int type
Definition unistd.h:344
basic system types.
__INT64_TYPE__ off_t
Definition types.h:20
__PTRDIFF_TYPE__ ssize_t
Definition types.h:15
int pid_t
Definition types.h:16
unsigned int uid_t
Definition types.h:17
unsigned int gid_t
Definition types.h:18
struct key_event_t::@170152312251240147374310354114104162231366201101::@001370202274002372066256151164273173007356006000 raw
int seteuid(uid_t uid)
int chdir(const char *path)
change the current working directory.
pid_t getpgrp(void)
int ftruncate(int fd, off_t length)
cut or grow a file to length bytes (the new bytes read as zeros).
int symlink(const char *target, const char *path)
make a symbolic link at path that points at target
int execl(const char *path, const char *arg,...)
execv, execvp and execve with the arguments as a list that ends with NULL.
int fchownat(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
int nice(int increment)
add increment to the caller's nice value (see sys/resource.h).
int unlink(const char *path)
remove (unlink) a file.
int execle(const char *path, const char *arg,...)
uid_t getuid(void)
user and group ids: real (who started the process) and effective (what it may do).
int create(const char *path)
create a file, or truncate it if it exists (the file is not left open).
int setgid(gid_t gid)
pid_t getsid(pid_t pid)
int faccessat(int dirfd, const char *path, int mode, int flags)
pid_t setsid(void)
int linkat(int old_dirfd, const char *old_path, int new_dirfd, const char *new_path, int flags)
gid_t getegid(void)
int optopt
int symlinkat(const char *target, int dirfd, const char *path)
int pause(void)
sleep until a signal is caught.
pid_t getpgid(pid_t pid)
int isatty(int fd)
true if fd refers to a terminal.
int fchown(int fd, uid_t uid, gid_t gid)
int dup(int fd)
duplicate a descriptor to the lowest free one; both share the open file.
int sethostname(const char *name, size_t length)
set the host name (root only; at most 64 bytes)
gid_t getgid(void)
int getentropy(void *buf, size_t length)
fill buf with length random bytes (at most 256) from the kernel.
void * sbrk(int increment)
grow the program break (allocate heap memory).
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
int execlp(const char *file, const char *arg,...)
ssize_t pread(int fd, void *buf, size_t count, off_t offset)
read or write at an offset, without moving the file offset.
long sysconf(int name)
a limit of the system (a SC name), or -1 with EINVAL
unsigned int sleep(unsigned int seconds)
sleep for a number of seconds (or microseconds).
int lchown(const char *path, uid_t uid, gid_t gid)
int execve(const char *path, char *const argv[], char *const envp[])
replace the calling process's program.
long fpathconf(int fd, int name)
char * ttyname(int fd)
the name of the terminal open as fd (as "/dev/pts/0").
int truncate(const char *path, off_t length)
size_t confstr(int name, char *buffer, size_t size)
a string value of the system (a CS name) into buffer.
uid_t geteuid(void)
off_t lseek(int fd, off_t offset, int whence)
set file cursor position.
unsigned int alarm(unsigned int seconds)
send SIGALRM to the calling process after a number of seconds.
ssize_t readlinkat(int dirfd, const char *restrict path, char *restrict buffer, size_t size)
int execvp(const char *file, char *const argv[])
execv, but a file name with no '/' is looked for in the directories of PATH (/bin when PATH is not se...
int fsync(int fd)
write an open file's data to disk.
int read_event(key_event_t *event)
read a single keyboard event from stdin (raw mode).
void _exit(int status) __attribute__((noreturn))
terminate the current process.
int setuid(uid_t uid)
pid_t fork(void)
create a child process that is a copy of the caller.
long pathconf(const char *path, int name)
a limit of the files at path (a PC name).
char * getpass(const char *prompt)
ask for a password on the terminal (standard input), without echo.
int pipe(int fds[2])
create a pipe (pair of connected file descriptors).
int getopt(int argc, char *const argv[], const char *options)
the next option of a command line (POSIX getopt).
int close(int fd)
close a file descriptor.
int rmdir(const char *path)
remove a directory.
int ttyname_r(int fd, char *buffer, size_t size)
ssize_t write(int fd, const void *buf, size_t count)
write bytes to a file descriptor.
void sync(void)
make every write reach the disk.
pid_t getpid(void)
get the current process ID.
pid_t getppid(void)
get the parent's process ID.
char * getcwd(char *buf, size_t size)
get the current working directory.
int usleep(unsigned int microseconds)
int unlinkat(int dirfd, const char *path, int flags)
remove a file, or with AT_REMOVEDIR a directory, relative to dirfd
int optind
int daemon(int keep_dir, int keep_files)
go on in the background: a new process in a new session, with no terminal.
char * optarg
int link(const char *old_path, const char *new_path)
give a file one more name (a hard link)
int execv(const char *path, char *const argv[])
execve with the environment of the calling process.
int opterr
int setpgid(pid_t pid, pid_t pgid)
process groups and sessions (job control).
int gethostname(char *name, size_t length)
the name of this computer (uname's nodename) into name
ssize_t readlink(const char *restrict path, char *restrict buffer, size_t size)
read a symbolic link's target (no NUL; a longer target is cut).
int setegid(gid_t gid)
int dup2(int old_fd, int new_fd)
duplicate a file descriptor to a specific number.
ssize_t read(int fd, void *buf, size_t count)
read bytes from a file descriptor.
int ttyctl(int mode)
switch the terminal on fd 0 between two modes (a wrapper over termios): TTY_COOKED is line input with...
pid_t exec(const char *path, const char **argv)
execute a program, spawning a child process.
char * getlogin(void)
the name of the user who runs the program (from the password file), or NULL
int chroot(const char *path)
make a directory the root of the caller's paths ("/" is then that directory; root only).
int chown(const char *path, uid_t uid, gid_t gid)
change the owner of a file.
waiting for child processes.