1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/swap.h>
20
21 #include "filepath_util.h"
22
23 /**
24 * @tc.name : swapon_0100
25 * @tc.desc : Start swapping to file/device
26 * @tc.level : Level 0
27 */
swapon_0100(void)28 void swapon_0100(void)
29 {
30 char dir_path[PATH_MAX] = {0};
31 FILE_ABSOLUTE_DIR(dir_path);
32 errno = 0;
33 char cmd[PATH_MAX] = {0};
34 snprintf(cmd, sizeof(cmd), "cd %s; dd if=/dev/zero of=swapfile count=1 bs=1k; mkswap swapfile", dir_path);
35 system(cmd);
36
37 char path[PATH_MAX] = {0};
38 FILE_ABSOLUTE_PATH(STR_FILE_SWAP, path);
39 int result = swapon(path, SWAP_FLAG_PREFER);
40 if (result == -1) {
41 t_error("%s swapon failed\n", __func__);
42 remove(path);
43 }
44
45 if (errno == ENOSYS) {
46 t_error("%s errno is %d\n", __func__, errno);
47 return;
48 }
49 }
50
main(int argc,char * argv[])51 int main(int argc, char *argv[])
52 {
53 // swapon_0100();
54 return t_status;
55 }
56