/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
/*
* Syscall definitions for NOLIBC (those in man(2))
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
*/
#ifndef _NOLIBC_SYS_H
#define _NOLIBC_SYS_H
#include "std.h"
/* system includes */
#include <asm/unistd.h>
#include <asm/signal.h> /* for SIGCHLD */
#include <asm/ioctls.h>
#include <asm/mman.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <linux/time.h>
#include <linux/auxvec.h>
#include <linux/fcntl.h> /* for O_* and AT_* */
#include <linux/stat.h> /* for statx() */
#include <linux/prctl.h>
#include <linux/resource.h>
#include "arch.h"
#include "errno.h"
#include "stdarg.h"
#include "types.h"
/* Syscall return helper: takes the syscall value in argument and checks for an
* error in it. This may only be used with signed returns (int or long), but
* not with pointers. An error is any value < 0. When an error is encountered,
* -ret is set into errno and -1 is returned. Otherwise the returned value is
* passed as-is with its type preserved.
*/
#define __sysret(arg) \
({ \
__typeof__(arg) __sysret_arg = (arg); \
(__sysret_arg < 0) /* error ? */ \
? (({ SET_ERRNO(-__sysret_arg); }), -1) /* ret -1 with errno = -arg */ \
: __sysret_arg; /* return original value */ \
})
/* Syscall ENOSYS helper: Avoids unused-parameter warnings and provides a
* debugging hook.
*/
static __inline__ int __nolibc_enosys(const char *syscall, ...)
{
(void)syscall;
return -ENOSYS;
}
/* Functions in this file only describe syscalls. They're declared static so
* that the compiler usually decides to inline them while still being allowed
* to pass a pointer to one of their instances. Each syscall exists in two
* versions:
* - the "internal" ones, which matches the raw syscall interface at the
* kernel level, which may sometimes slightly differ from the documented
* libc-level ones. For example most of them return either a valid value
* or -errno. All of these are prefixed with "sys_". They may be called
* by non-portable applications if desired.
*
* - the "exported" ones, whose interface must closely match the one
* documented in man(2), that applications are supposed to expect. These
* ones rely on the internal ones, and set errno.
*
* Each syscall will be defined with the two functions, sorted in alphabetical
* order applied to the exported names.
*
* In case of doubt about the relevance of a function here, only those which
* set errno should be defined here. Wrappers like those appearing in man(3)
* should not be placed here.
*/
/*
* int brk(void *addr);
* void *sbrk(intptr_t inc)
*/
static __attribute__((unused))
void *sys_brk(void *addr)
{
return (void *)my_syscall1(__NR_brk, addr);
}
static __attribute__((unused))
int brk(void *addr)
{
void *ret = sys_brk(addr);
if (!ret) {
SET_ERRNO(ENOMEM);
return -1;
}
return 0;
}
static __attribute__((unused))
void *sbrk(intptr_t inc)
{
/* first call to find current end */
void *ret = sys_brk(0);
if (ret && sys_brk(ret + inc) == ret + inc)
return ret + inc;
SET_ERRNO(ENOMEM);
return (void *)-1;
}
/*
* int chdir(const char *path);
*/
static __attribute__((unused))
int sys_chdir(const char *path)
{
return my_syscall1(__NR_chdir, path);
}
static __attribute__((unused))
int chdir(const char *path)
{
return __sysret(sys_chdir(path));
}
/*
* int chmod(const char *path, mode_t mode);
*/
static __attribute__((unused))
int sys_chmod(const char *path, mode_t mode)
{
#ifdef __NR_fchmodat
return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
#elif defined(__NR_chmod)
return my_syscall2(__NR_chmod, path, mode);
#else
return __nolibc_enosys(__func__, path, mode);
#endif
}
static __attribute__((unused))
int chmod(const char *path, mode_t mode)
{
return __sysret(sys_chmod(path, mode));
}
/*
* int chown(const char *path, uid_t owner, gid_t group);
*/
static __attribute__((unused))
int sys_chown(const char *path, uid_t owner, gid_t group)
{
#ifdef __NR_fchownat
return my_syscall5(__NR_fchownat, AT_FDCWD, path