public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] tst_module: create new library functions for kernel modules
@ 2013-06-20 11:54 Alexey Kodanev
  2013-06-22 18:15 ` Mike Frysinger
  0 siblings, 1 reply; 7+ messages in thread
From: Alexey Kodanev @ 2013-06-20 11:54 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko, Alexey Kodanev

There are new functions: tst_module_exists(), tst_module_load() and
tst_module_unload(). These functions help to load and unload kernel
modules in the tests.

Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
 include/tst_module.h |   72 +++++++++++++++++++++++++++++++++
 lib/tst_module.c     |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 179 insertions(+), 0 deletions(-)
 create mode 100644 include/tst_module.h
 create mode 100644 lib/tst_module.c

diff --git a/include/tst_module.h b/include/tst_module.h
new file mode 100644
index 0000000..0eb7dd5
--- /dev/null
+++ b/include/tst_module.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author:
+ * Alexey Kodanev <alexey.kodanev@oracle.com>
+ *
+ * These functions help to load and unload kernel modules in the tests.
+ *
+ * tst_module_load and tst_module_unload functions already include
+ * tst_module_exists function, which is checking the following possible
+ * module's locations:
+ *
+ * 1. Current working directory
+ *
+ * 2. LTP installation path (using env LTPROOT, which is usally /opt/ltp)
+ *
+ * 3. If tmp directory created, it'll look at the test start working directory
+ *
+ */
+
+#ifndef TST_MODULE
+#define TST_MODULE
+
+/*
+ * Check module existence.
+ *
+ * @res_path: if this pointer isn't NULL, found module's path will be written
+ * to it.
+ *
+ * In case of failure, test'll call cleanup_fn and exit with TCONF return value.
+ */
+void tst_module_exist(void (*cleanup_fn)(void), const char *mod_name,
+	char *res_path, size_t max_path);
+
+/*
+ * Load a module using insmod program.
+ *
+ * @mod_opt: pointer to a character string containing additional module's
+ * params. This character string can contain a format that follows the same
+ * specifications as format in printf. If this pointer is NULL, it will be
+ * ignored.
+ *
+ * This function may expect additional arguments to replace a format specifier
+ * in the mod_opt string.
+ *
+ * In case of insmod failure, test will call cleanup_fn and exit with TBROK
+ * return value.
+ */
+void tst_module_load(void (*cleanup_fn)(void),
+	const char *mod_name, char *mod_opt, ...);
+
+/*
+ * Unload a module using rmmod program. In case of failure, test will call
+ * cleanup_fn and exit with TBROK return value.
+ */
+void tst_module_unload(void (*cleanup_fn)(void), const char *mod_name);
+
+#endif /* TST_MODULE */
diff --git a/lib/tst_module.c b/lib/tst_module.c
new file mode 100644
index 0000000..0c6062f
--- /dev/null
+++ b/lib/tst_module.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
+ *
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "test.h"
+#include "usctest.h"
+#include "tst_module.h"
+
+/* declared in tst_tmpdir.c */
+const char *tst_get_startwd(void);
+
+void tst_module_exists(void (*cleanup_fn)(void),
+	const char *mod_name, char *res_path, size_t max_path)
+{
+	/* check current working directory */
+	if (access(mod_name, F_OK) == 0) {
+		if (res_path != NULL)
+			snprintf(res_path, max_path, "%s", mod_name);
+		return;
+	}
+
+	int err = -1;
+	char buf[PATH_MAX];
+	/* check LTP installation path */
+	const char *ltproot = getenv("LTPROOT");
+	if (ltproot != NULL) {
+		snprintf(buf, PATH_MAX, "%s/testcases/bin/%s",
+			ltproot, mod_name);
+		err = access(buf, F_OK);
+	}
+	/* check start working directory */
+	if (err == -1 && tst_tmpdir_created()) {
+		snprintf(buf, PATH_MAX, "%s/%s", tst_get_startwd(),
+			mod_name);
+		err = access(buf, F_OK);
+	}
+
+	if (err == 0) {
+		if (res_path != NULL)
+			snprintf(res_path, max_path, "%s", buf);
+	} else {
+		tst_brkm(TCONF, cleanup_fn, "Failed to find module '%s'",
+			mod_name);
+	}
+}
+
+void tst_module_load(void (*cleanup_fn)(void),
+	const char *mod_name, char *mod_opt, ...)
+{
+	char mod_path[PATH_MAX];
+
+	tst_module_exists(cleanup_fn, mod_name, mod_path, PATH_MAX);
+
+	char params[PATH_MAX];
+	params[0] = '\0';
+
+	if (mod_opt != NULL) {
+		va_list ap;
+		va_start(ap, mod_opt);
+		vsnprintf(params, PATH_MAX, mod_opt, ap);
+		va_end(ap);
+	}
+
+	char cmd[strlen(mod_path) + strlen(params) + 9];
+	snprintf(cmd, sizeof(cmd), "insmod %s %s", mod_path, params);
+
+	if (system(cmd) != 0) {
+		tst_brkm(TBROK, cleanup_fn, "Failed to load module '%s'",
+			mod_name);
+	}
+}
+
+void tst_module_unload(void (*cleanup_fn)(void), const char *mod_name)
+{
+	char mod_path[PATH_MAX];
+
+	tst_module_exists(cleanup_fn, mod_name, mod_path, PATH_MAX);
+
+	char cmd[strlen(mod_path) + 7];
+	snprintf(cmd, sizeof(cmd), "rmmod %s", mod_path);
+	if (system(cmd) != 0) {
+		tst_brkm(TBROK, cleanup_fn, "Failed to unload module '%s'",
+			mod_name);
+	}
+}
-- 
1.7.1


------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
  2013-06-20 11:54 [LTP] [PATCH] tst_module: create new library functions for kernel modules Alexey Kodanev
@ 2013-06-22 18:15 ` Mike Frysinger
  2013-06-24 10:01   ` alexey.kodanev
  2013-06-24 11:56   ` chrubis
  0 siblings, 2 replies; 7+ messages in thread
From: Mike Frysinger @ 2013-06-22 18:15 UTC (permalink / raw)
  To: ltp-list; +Cc: Alexey Kodanev, vasily.isaenko


[-- Attachment #1.1: Type: Text/Plain, Size: 1494 bytes --]

On Thursday 20 June 2013 07:54:37 Alexey Kodanev wrote:
> +/*
> + * Check module existence.
> + *
> + * @res_path: if this pointer isn't NULL, found module's path will be
> + * written to it.
> + *
> + * In case of failure, test'll call cleanup_fn and exit with TCONF return
> + */
> +void tst_module_exist(void (*cleanup_fn)(void), const char *mod_name,
> +	char *res_path, size_t max_path);

i hate APIs like this.  they're so 1990 :P.

look at the getline() func and behave like that instead (take char **res_path 
and size_t *max_path instead)

> +/* declared in tst_tmpdir.c */
> +const char *tst_get_startwd(void);

use a proper header instead.  extern func decls only leads to bit rot and hard 
to debug crashes.

> +	char buf[PATH_MAX];

PATH_MAX should be taken out back and shot.  there's no reason to use it 
anymore when we have things like asprintf().  plus, it isn't portable -- 
there's no guarantee it'll be defined.  i know the irony complaining about 
portability with PATH_MAX when asprintf() isn't in POSIX either, but the 
difference is, we have an asprintf() fallback already in lib/, and we have code 
using it today.

> +	char cmd[strlen(mod_path) + strlen(params) + 9];
> +	snprintf(cmd, sizeof(cmd), "insmod %s %s", mod_path, params);
> +
> +	if (system(cmd) != 0) {

use fork()+execvp() instead.  there's no need to normalize to a string and 
then run through a shell.

wonder if we should add a util func for this ...
-mike

[-- Attachment #1.2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

[-- Attachment #2: Type: text/plain, Size: 184 bytes --]

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev

[-- Attachment #3: Type: text/plain, Size: 155 bytes --]

_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
  2013-06-22 18:15 ` Mike Frysinger
@ 2013-06-24 10:01   ` alexey.kodanev
  2013-06-24 15:29     ` Mike Frysinger
  2013-06-24 11:56   ` chrubis
  1 sibling, 1 reply; 7+ messages in thread
From: alexey.kodanev @ 2013-06-24 10:01 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: vasily.isaenko, ltp-list

Hi!
On 06/22/2013 10:15 PM, Mike Frysinger wrote:
>
>> +/* declared in tst_tmpdir.c */
>> +const char *tst_get_startwd(void);
> use a proper header instead.  extern func decls only leads to bit rot and hard
> to debug crashes.
But, there is no header for this function in LTP, it is declared inside 
c file, and I
used it in the same way as it was already used in tst_resource.c.

Thanks,
Alexey

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
  2013-06-22 18:15 ` Mike Frysinger
  2013-06-24 10:01   ` alexey.kodanev
@ 2013-06-24 11:56   ` chrubis
       [not found]     ` <201306241136.07004.vapier@gentoo.org>
  1 sibling, 1 reply; 7+ messages in thread
From: chrubis @ 2013-06-24 11:56 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Alexey Kodanev, ltp-list, vasily.isaenko

Hi!
> > +/* declared in tst_tmpdir.c */
> > +const char *tst_get_startwd(void);
> 
> use a proper header instead.  extern func decls only leads to bit rot and hard 
> to debug crashes.

This is internal only API for the LTP lib that does not have a proper
header (I was too lazy to create a header for the function that is used
only once). I can fix that if desired.

> use fork()+execvp() instead.  there's no need to normalize to a string and 
> then run through a shell.
> 
> wonder if we should add a util func for this ...

That sounds like good idea.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
  2013-06-24 10:01   ` alexey.kodanev
@ 2013-06-24 15:29     ` Mike Frysinger
  0 siblings, 0 replies; 7+ messages in thread
From: Mike Frysinger @ 2013-06-24 15:29 UTC (permalink / raw)
  To: alexey.kodanev; +Cc: vasily.isaenko, ltp-list


[-- Attachment #1.1: Type: Text/Plain, Size: 640 bytes --]

On Monday 24 June 2013 06:01:39 alexey.kodanev@oracle.com wrote:
> On 06/22/2013 10:15 PM, Mike Frysinger wrote:
> >> +/* declared in tst_tmpdir.c */
> >> +const char *tst_get_startwd(void);
> > 
> > use a proper header instead.  extern func decls only leads to bit rot and
> > hard to debug crashes.
> 
> But, there is no header for this function in LTP, it is declared inside
> c file, and I
> used it in the same way as it was already used in tst_resource.c.

... so add the decl to an existing header and fix the existing usage.  the fact 
that someone else did it wrong doesn't justify extending that misbehavior.
-mike

[-- Attachment #1.2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

[-- Attachment #2: Type: text/plain, Size: 184 bytes --]

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev

[-- Attachment #3: Type: text/plain, Size: 155 bytes --]

_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
       [not found]     ` <201306241136.07004.vapier@gentoo.org>
@ 2013-06-24 15:43       ` chrubis
  2013-06-24 16:02         ` chrubis
  0 siblings, 1 reply; 7+ messages in thread
From: chrubis @ 2013-06-24 15:43 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Alexey Kodanev, ltp-list, vasily.isaenko

Hi!
> > > use a proper header instead.  extern func decls only leads to bit rot and
> > > hard to debug crashes.
> > 
> > This is internal only API for the LTP lib that does not have a proper
> > header (I was too lazy to create a header for the function that is used
> > only once). I can fix that if desired.
> 
> if it's meant as an internal-to-the-lib func, then we should start a
> header to hold that kind of code.  we can easily enforce it too by
> having the lib Makefile define some macro that the header will check
> and error out if it isn't present.

Agreed, I will fix that ASAP.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH] tst_module: create new library functions for kernel modules
  2013-06-24 15:43       ` chrubis
@ 2013-06-24 16:02         ` chrubis
  0 siblings, 0 replies; 7+ messages in thread
From: chrubis @ 2013-06-24 16:02 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Alexey Kodanev, ltp-list, vasily.isaenko

Hi!
> > > This is internal only API for the LTP lib that does not have a proper
> > > header (I was too lazy to create a header for the function that is used
> > > only once). I can fix that if desired.
> > 
> > if it's meant as an internal-to-the-lib func, then we should start a
> > header to hold that kind of code.  we can easily enforce it too by
> > having the lib Makefile define some macro that the header will check
> > and error out if it isn't present.
> 
> Agreed, I will fix that ASAP.

Fixed in git. (I've added ltp_priv.h header into lib/ and moved the
definition there).

Alexey: Make use of the header.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-06-24 16:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-20 11:54 [LTP] [PATCH] tst_module: create new library functions for kernel modules Alexey Kodanev
2013-06-22 18:15 ` Mike Frysinger
2013-06-24 10:01   ` alexey.kodanev
2013-06-24 15:29     ` Mike Frysinger
2013-06-24 11:56   ` chrubis
     [not found]     ` <201306241136.07004.vapier@gentoo.org>
2013-06-24 15:43       ` chrubis
2013-06-24 16:02         ` chrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox