1570af302Sopenharmony_ci#include <sys/resource.h>
2570af302Sopenharmony_ci#include <errno.h>
3570af302Sopenharmony_ci#include "syscall.h"
4570af302Sopenharmony_ci
5570af302Sopenharmony_ci#define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0)
6570af302Sopenharmony_ci
7570af302Sopenharmony_ciint getrlimit(int resource, struct rlimit *rlim)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci#ifdef __LITEOS_A__
10570af302Sopenharmony_ci	unsigned long long k_rlim[2];
11570af302Sopenharmony_ci	if (syscall(SYS_getrlimit, resource, k_rlim) < 0)
12570af302Sopenharmony_ci		return -1;
13570af302Sopenharmony_ci
14570af302Sopenharmony_ci	rlim->rlim_cur = k_rlim[0];
15570af302Sopenharmony_ci	rlim->rlim_max = k_rlim[1];
16570af302Sopenharmony_ci	return 0;
17570af302Sopenharmony_ci#else
18570af302Sopenharmony_ci	int ret = syscall(SYS_prlimit64, 0, resource, 0, rlim);
19570af302Sopenharmony_ci	if (!ret) {
20570af302Sopenharmony_ci		FIX(rlim->rlim_cur);
21570af302Sopenharmony_ci		FIX(rlim->rlim_max);
22570af302Sopenharmony_ci	}
23570af302Sopenharmony_ci#ifdef SYS_getrlimit
24570af302Sopenharmony_ci	unsigned long k_rlim[2];
25570af302Sopenharmony_ci	if (!ret || errno != ENOSYS)
26570af302Sopenharmony_ci		return ret;
27570af302Sopenharmony_ci	if (syscall(SYS_getrlimit, resource, k_rlim) < 0)
28570af302Sopenharmony_ci		return -1;
29570af302Sopenharmony_ci	rlim->rlim_cur = k_rlim[0] == -1UL ? RLIM_INFINITY : k_rlim[0];
30570af302Sopenharmony_ci	rlim->rlim_max = k_rlim[1] == -1UL ? RLIM_INFINITY : k_rlim[1];
31570af302Sopenharmony_ci	FIX(rlim->rlim_cur);
32570af302Sopenharmony_ci	FIX(rlim->rlim_max);
33570af302Sopenharmony_ci	return 0;
34570af302Sopenharmony_ci#else
35570af302Sopenharmony_ci	return ret;
36570af302Sopenharmony_ci#endif
37570af302Sopenharmony_ci#endif
38570af302Sopenharmony_ci}
39570af302Sopenharmony_ci
40570af302Sopenharmony_ciweak_alias(getrlimit, getrlimit64);
41