1/* Copyright (C) 2021 Huawei Device Co., Ltd.
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 *     http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14#include <cstring>
15#include <cerrno>
16#include <cstdio>
17#include <sys/resource.h>
18
19#include "runtest.h"
20namespace OHOS {
21int Runtest::TSetrlim(int r, long lim)
22{
23    struct rlimit rl;
24    // Gets the current stack size
25    if (getrlimit(r, &rl) != 0) {
26        printf("getrlimit %d: %s\n", r, strerror(errno));
27        return -1;
28    }
29    if (lim > rl.rlim_max) {
30        return -1;
31    }
32    if (lim == rl.rlim_max && lim == rl.rlim_cur) {
33        return 0;
34    }
35    rl.rlim_max = lim;
36    rl.rlim_cur = lim;
37    if (setrlimit(r, &rl) != 0) {
38        printf("setrlimit(%d, %ld): %s\n", r, lim, strerror(errno));
39        return -1;
40    }
41    return 0;
42}
43} // namespace OHOS