xref: /aosp_15_r20/external/ltp/lib/tst_module.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Author: Alexey Kodanev <[email protected]>
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "ltp_priv.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "old_module.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
tst_module_exists_(void (cleanup_fn)(void),const char * mod_name,char ** mod_path)31*49cdfc7eSAndroid Build Coastguard Worker void tst_module_exists_(void (cleanup_fn)(void),
32*49cdfc7eSAndroid Build Coastguard Worker 	const char *mod_name, char **mod_path)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	/* check current working directory */
35*49cdfc7eSAndroid Build Coastguard Worker 	if (access(mod_name, F_OK) == 0) {
36*49cdfc7eSAndroid Build Coastguard Worker 		if (mod_path != NULL)
37*49cdfc7eSAndroid Build Coastguard Worker 			*mod_path = strdup(mod_name);
38*49cdfc7eSAndroid Build Coastguard Worker 		return;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 	char *buf = NULL;
41*49cdfc7eSAndroid Build Coastguard Worker 	int err = -1;
42*49cdfc7eSAndroid Build Coastguard Worker 	/* check LTP installation path */
43*49cdfc7eSAndroid Build Coastguard Worker 	const char *ltproot = getenv("LTPROOT");
44*49cdfc7eSAndroid Build Coastguard Worker 	if (ltproot != NULL) {
45*49cdfc7eSAndroid Build Coastguard Worker 		if (asprintf(&buf, "%s/testcases/bin/%s",
46*49cdfc7eSAndroid Build Coastguard Worker 			ltproot, mod_name) == -1) {
47*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup_fn,
48*49cdfc7eSAndroid Build Coastguard Worker 				"asprintf failed at %s:%d",
49*49cdfc7eSAndroid Build Coastguard Worker 				__FILE__, __LINE__);
50*49cdfc7eSAndroid Build Coastguard Worker 			return;
51*49cdfc7eSAndroid Build Coastguard Worker 		}
52*49cdfc7eSAndroid Build Coastguard Worker 		err = access(buf, F_OK);
53*49cdfc7eSAndroid Build Coastguard Worker 	}
54*49cdfc7eSAndroid Build Coastguard Worker 	/* check start working directory */
55*49cdfc7eSAndroid Build Coastguard Worker 	if (err == -1 && tst_tmpdir_created()) {
56*49cdfc7eSAndroid Build Coastguard Worker 		free(buf);
57*49cdfc7eSAndroid Build Coastguard Worker 		if (asprintf(&buf, "%s/%s", tst_get_startwd(),
58*49cdfc7eSAndroid Build Coastguard Worker 			mod_name) == -1) {
59*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup_fn,
60*49cdfc7eSAndroid Build Coastguard Worker 				"asprintf failed at %s:%d",
61*49cdfc7eSAndroid Build Coastguard Worker 				__FILE__, __LINE__);
62*49cdfc7eSAndroid Build Coastguard Worker 			return;
63*49cdfc7eSAndroid Build Coastguard Worker 		}
64*49cdfc7eSAndroid Build Coastguard Worker 		err = access(buf, F_OK);
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	if (err != 0) {
68*49cdfc7eSAndroid Build Coastguard Worker 		free(buf);
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, cleanup_fn, "Failed to find module '%s'",
70*49cdfc7eSAndroid Build Coastguard Worker 			mod_name);
71*49cdfc7eSAndroid Build Coastguard Worker 		return;
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	if (mod_path != NULL)
75*49cdfc7eSAndroid Build Coastguard Worker 		*mod_path = buf;
76*49cdfc7eSAndroid Build Coastguard Worker 	else
77*49cdfc7eSAndroid Build Coastguard Worker 		free(buf);
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
tst_module_load_(void (cleanup_fn)(void),const char * mod_name,char * const argv[])80*49cdfc7eSAndroid Build Coastguard Worker void tst_module_load_(void (cleanup_fn)(void),
81*49cdfc7eSAndroid Build Coastguard Worker 	const char *mod_name, char *const argv[])
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker 	char *mod_path = NULL;
84*49cdfc7eSAndroid Build Coastguard Worker 	tst_module_exists_(cleanup_fn, mod_name, &mod_path);
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	const int offset = 2; /* command name & module path */
87*49cdfc7eSAndroid Build Coastguard Worker 	int size = 0;
88*49cdfc7eSAndroid Build Coastguard Worker 	while (argv && argv[size])
89*49cdfc7eSAndroid Build Coastguard Worker 		++size;
90*49cdfc7eSAndroid Build Coastguard Worker 	size += offset;
91*49cdfc7eSAndroid Build Coastguard Worker 	const char *mod_argv[size + 1]; /* + NULL in the end */
92*49cdfc7eSAndroid Build Coastguard Worker 	mod_argv[size] = NULL;
93*49cdfc7eSAndroid Build Coastguard Worker 	mod_argv[0] = "insmod";
94*49cdfc7eSAndroid Build Coastguard Worker 	mod_argv[1] = mod_path;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	int i;
97*49cdfc7eSAndroid Build Coastguard Worker 	for (i = offset; i < size; ++i)
98*49cdfc7eSAndroid Build Coastguard Worker 		mod_argv[i] = argv[i - offset];
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	tst_cmd(cleanup_fn, mod_argv, NULL, NULL, 0);
101*49cdfc7eSAndroid Build Coastguard Worker 	free(mod_path);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
tst_module_unload_(void (cleanup_fn)(void),const char * mod_name)104*49cdfc7eSAndroid Build Coastguard Worker void tst_module_unload_(void (cleanup_fn)(void), const char *mod_name)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	int i, rc;
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	const char *const argv[] = { "rmmod", mod_name, NULL };
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	rc = 1;
111*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 50; i++) {
112*49cdfc7eSAndroid Build Coastguard Worker 		rc = tst_cmd(NULL, argv, "/dev/null", "/dev/null",
113*49cdfc7eSAndroid Build Coastguard Worker 				 TST_CMD_PASS_RETVAL);
114*49cdfc7eSAndroid Build Coastguard Worker 		if (!rc)
115*49cdfc7eSAndroid Build Coastguard Worker 			break;
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 		usleep(20000);
118*49cdfc7eSAndroid Build Coastguard Worker 	}
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	if (rc) {
121*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup_fn,
122*49cdfc7eSAndroid Build Coastguard Worker 			 "could not unload %s module", mod_name);
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker }
125