1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2011 Red Hat, Inc.
4 * Copyright (C) 2021 Xie Ziyao <xieziyao@huawei.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Child program executed by getrusage03.
11 */
12
13 #define TST_NO_DEFAULT_MAIN
14
15 #include <stdlib.h>
16
17 #include "tst_test.h"
18 #include "getrusage03.h"
19
main(int argc,char * argv[])20 int main(int argc, char *argv[])
21 {
22 if (argc < 3)
23 tst_brk(TFAIL, "argc is %d, expected more than two", argc);
24
25 pid_t pid;
26 struct rusage ru;
27 long maxrss_self, maxrss_children;
28 long consume_nr, grand_consume_nr, self_nr, child_nr;
29
30 tst_reinit();
31
32 if (!strcmp(argv[1], "consume")) {
33 consume_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
34 consume_mb(consume_nr);
35 } else if (!strcmp(argv[1], "grand_consume")) {
36 grand_consume_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
37
38 pid = fork();
39 if (pid == -1)
40 tst_brk(TBROK, "fork failed");
41 else if (pid == 0) {
42 consume_mb(grand_consume_nr);
43 exit(0);
44 }
45
46 tst_reap_children();
47 } else if (!strcmp(argv[1], "compare")) {
48 self_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
49 child_nr = SAFE_STRTOL(argv[3], 0, LONG_MAX);
50
51 SAFE_GETRUSAGE(RUSAGE_SELF, &ru);
52 maxrss_self = ru.ru_maxrss;
53 SAFE_GETRUSAGE(RUSAGE_CHILDREN, &ru);
54 maxrss_children = ru.ru_maxrss;
55
56 if (is_in_delta(maxrss_self - self_nr))
57 tst_res(TPASS, "initial.self ~= exec.self");
58 else
59 tst_res(TFAIL, "initial.self !~= exec.self");
60
61 if (is_in_delta(maxrss_children - child_nr))
62 tst_res(TPASS, "initial.children ~= exec.children");
63 else
64 tst_res(TFAIL, "initial.children !~= exec.children");
65 }
66
67 return 0;
68 }
69