xref: /aosp_15_r20/external/ltp/testcases/lib/tst_cgctl.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 Canonical Ltd.
4  */
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include "tst_cgroup.h"
12 
cgctl_usage(void)13 static void cgctl_usage(void)
14 {
15 	fprintf(stderr, "Usage: tst_cgctl require [controller] [test_pid]\n\tcleanup [config (output of tst_cg_print_config)]\n\tprint\n\thelp\n");
16 }
17 
cgctl_require(const char * ctrl,int test_pid)18 static int cgctl_require(const char *ctrl, int test_pid)
19 {
20 	struct tst_cg_opts opts;
21 
22 	memset(&opts, 0, sizeof(opts));
23 	opts.test_pid = test_pid;
24 
25 	tst_cg_require(ctrl, &opts);
26 	tst_cg_print_config();
27 
28 	return 0;
29 }
30 
cgctl_cleanup(const char * const config)31 static int cgctl_cleanup(const char *const config)
32 {
33 	tst_cg_scan();
34 	tst_cg_load_config(config);
35 	tst_cg_cleanup();
36 
37 	return 0;
38 }
39 
cgctl_print(void)40 static int cgctl_print(void)
41 {
42 	tst_cg_scan();
43 	tst_cg_print_config();
44 
45 	return 0;
46 }
47 
main(int argc,char * argv[])48 int main(int argc, char *argv[])
49 {
50 	int test_pid;
51 	const char *cmd_name = argv[1];
52 
53 	if (argc < 2)
54 		goto error;
55 
56 	if (!strcmp(cmd_name, "require")) {
57 		if (argc != 4)
58 			goto arg_num_error;
59 		test_pid = atoi(argv[3]);
60 		if (!test_pid) {
61 			fprintf(stderr, "tst_cgctl: Invalid test_pid '%s' given\n",
62 				argv[3]);
63 			goto error;
64 		}
65 		return cgctl_require(argv[2], test_pid);
66 	} else if (!strcmp(cmd_name, "cleanup")) {
67 		if (argc != 3)
68 			goto arg_num_error;
69 		return cgctl_cleanup(argv[2]);
70 	} else if (!strcmp(cmd_name, "print")) {
71 		return cgctl_print();
72 	} else if (!strcmp(cmd_name, "help")) {
73 		cgctl_usage();
74 		return 0;
75 	}
76 
77 	fprintf(stderr, "tst_cgctl: Unknown command '%s' given\n", cmd_name);
78 	goto error;
79 
80 arg_num_error:
81 	fprintf(stderr,
82 		"tst_cgctl: Invalid number of arguments given for command '%s'\n",
83 		cmd_name);
84 error:
85 	cgctl_usage();
86 	return 1;
87 }
88