All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] mlock/mlock02.c: cleanup
@ 2014-02-14 10:10 Zeng Linggang
  2014-02-14 10:12 ` [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-02-14 10:10 UTC (permalink / raw)
  To: ltp-list

* Delete some useless commtents.
* Use SAFE_* macros.
* Move the test body form main() to mlock_verify().
* Some cleanup.

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 110 +++++++++++-------------------
 1 file changed, 38 insertions(+), 72 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index edd9e45..a635d24 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -1,6 +1,6 @@
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
+ *	06/2002 Written by Paul Larson
  *
  *   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
@@ -13,69 +13,40 @@
  *   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 to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   along with this program;  if not, write to the Free Software Foundation,
+ *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-
 /*
- * NAME
- * 	mlock02.c
- *
- * DESCRIPTION
- * 	Test to see the proper errors are returned by mlock
- *$
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
- *
- * USAGE:  <for command-line>
- *         -c n    Run n copies concurrently
- *         -e      Turn on errno logging
- *         -f      Turn off functional testing
- *         -h      Show this help screen
- *         -i n    Execute test n times
- *         -I x    Execute test for x seconds
- *         -p      Pause for SIGUSR1 before starting
- *         -P x    Pause for x seconds between iterations
- *         -t      Turn on syscall timing
- *
- * HISTORY
- *	06/2002 Written by Paul Larson
- *
- * RESTRICTIONS
- * 	None
  */
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include "test.h"
 #include "usctest.h"
-
-void setup();
-void setup1();
-void cleanup();
+#include "safe_macros.h"
 
 char *TCID = "mlock02";
-int TST_TOTAL = 1;
-
-int exp_enos[] = { ENOMEM, 0 };
 
-void *addr1;
+static void *addr1;
+static void setup(void);
+static void mlock_verify(int);
+static void cleanup(void);
 
-struct test_case_t {
+static struct test_case_t {
 	void **addr;
 	int len;
 	int error;
-	void (*setupfunc) ();
 } TC[] = {
-	/* mlock should return ENOMEM when some or all of the address
-	 * range pointed to by addr and len are not valid mapped pages
-	 * in the address space of the process
-	 */
-	{
-	&addr1, 1024, ENOMEM, setup1}
+	{&addr1, 1024, ENOMEM},
 };
 
+int TST_TOTAL = ARRAY_SIZE(TC);
+static int exp_enos[] = { ENOMEM, 0 };
+
 #if !defined(UCLINUX)
 
 int main(int ac, char **av)
@@ -94,50 +65,28 @@ int main(int ac, char **av)
 
 		tst_count = 0;
 
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (TC[i].setupfunc != NULL)
-				TC[i].setupfunc();
-
-			TEST(mlock(*(TC[i].addr), TC[i].len));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO != TC[i].error)
-					tst_brkm(TFAIL | TTERRNO, cleanup,
-						 "mlock didn't fail as expected; "
-						 "expected - %d : %s",
-						 TC[i].error,
-						 strerror(TC[i].error));
-				else
-					tst_resm(TPASS | TTERRNO,
-						 "mlock failed as expected");
-			} else
-				tst_brkm(TFAIL, cleanup,
-					 "mlock succeeded unexpectedly");
-		}
+		for (i = 0; i < TST_TOTAL; i++)
+
+			mlock_verify(i);
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
 #else
 
-int main()
+int main(void)
 {
 	tst_brkm(TCONF, NULL, "test is not available on uClinux");
 }
 
 #endif /* if !defined(UCLINUX) */
 
-void setup()
+static void setup(void)
 {
 	TEST_PAUSE;
-}
 
-void setup1()
-{
 #ifdef __ia64__
 	TC[0].len = getpagesize() + 1;
 #else
@@ -145,8 +94,25 @@ void setup1()
 #endif
 }
 
-void cleanup()
+static void mlock_verify(int i)
 {
-	TEST_CLEANUP;
+	TEST(mlock(*(TC[i].addr), TC[i].len));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "mlock succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != TC[i].error) {
+		tst_resm(TFAIL | TTERRNO,
+			 "mlock didn't fail as expected; expected - %d : %s",
+			 TC[i].error, strerror(TC[i].error));
+	} else {
+		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
+	}
+}
 
+static void cleanup(void)
+{
+	TEST_CLEANUP;
 }
-- 
1.8.4.2




------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test
  2014-02-14 10:10 [LTP] [PATCH] mlock/mlock02.c: cleanup Zeng Linggang
@ 2014-02-14 10:12 ` Zeng Linggang
  2014-02-14 10:54   ` Jan Stancek
  0 siblings, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-02-14 10:12 UTC (permalink / raw)
  To: ltp-list

Add EPERM errno test for mlock(2).

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 42 +++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index a635d24..1e87777 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -20,11 +20,15 @@
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
+ *	test 2:
+ *		The  caller  was  not  privileged and its RLIMIT_MEMLOCK soft
+ *		resource limit was 0. EPERM should be returned
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <pwd.h>
 #include "test.h"
 #include "usctest.h"
 #include "safe_macros.h"
@@ -32,20 +36,26 @@
 char *TCID = "mlock02";
 
 static void *addr1;
+static struct passwd *ltpuser;
 static void setup(void);
 static void mlock_verify(int);
+static void setup2(void);
+static void cleanup2(void);
 static void cleanup(void);
 
 static struct test_case_t {
 	void **addr;
 	int len;
 	int error;
+	void (*setupfunc) ();
+	void (*cleanupfunc) ();
 } TC[] = {
-	{&addr1, 1024, ENOMEM},
+	{&addr1, 1024, ENOMEM, NULL, NULL},
+	{&addr1, 1024, EPERM, setup2, cleanup2},
 };
 
 int TST_TOTAL = ARRAY_SIZE(TC);
-static int exp_enos[] = { ENOMEM, 0 };
+static int exp_enos[] = { ENOMEM, EPERM, 0 };
 
 #if !defined(UCLINUX)
 
@@ -85,6 +95,8 @@ int main(void)
 
 static void setup(void)
 {
+	struct rlimit rl;
+
 	TEST_PAUSE;
 
 #ifdef __ia64__
@@ -92,12 +104,28 @@ static void setup(void)
 #else
 	addr1 = NULL;
 #endif
+	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
+
+	rl.rlim_cur = 0;
+
+	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
+		tst_resm(TWARN,
+			 "setrlimit failed to set the resource for "
+			 "RLIMIT_MEMLOCK to check for mlock()");
+		return;
+	}
 }
 
 static void mlock_verify(int i)
 {
+	if (TC[i].setupfunc != NULL)
+		TC[i].setupfunc();
+
 	TEST(mlock(*(TC[i].addr), TC[i].len));
 
+	if (TC[i].cleanupfunc != NULL)
+		TC[i].cleanupfunc();
+
 	if (TEST_RETURN != -1) {
 		tst_resm(TFAIL, "mlock succeeded unexpectedly");
 		return;
@@ -112,6 +140,16 @@ static void mlock_verify(int i)
 	}
 }
 
+static void setup2(void)
+{
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+}
+
+static void cleanup2(void)
+{
+	SAFE_SETEUID(cleanup, 0);
+}
+
 static void cleanup(void)
 {
 	TEST_CLEANUP;
-- 
1.8.4.2




------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test
  2014-02-14 10:12 ` [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test Zeng Linggang
@ 2014-02-14 10:54   ` Jan Stancek
  2014-02-19  9:38     ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Stancek @ 2014-02-14 10:54 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list





----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Friday, 14 February, 2014 11:12:26 AM
> Subject: [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test
> 
> Add EPERM errno test for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 42
>  +++++++++++++++++++++++++++++--
>  1 file changed, 40 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index a635d24..1e87777 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -20,11 +20,15 @@
>   * ALGORITHM
>   * 	test 1:
>   *		Call mlock with a NULL address.  ENOMEM should be returned

Hi,

ENOMEM (Linux 2.6.9 and later) the caller had a non-zero RLIMIT_MEMLOCK soft resource limit,
       but tried to lock more memory than the limit permitted.
ENOMEM Some of the specified address range does not correspond to mapped pages in
       the address space of the process.

We seem to be mixing these two together. Will test 1 return ENOMEM
because we passed NULL or because RLIMIT_MEMLOCK was low enough?
Consider this example:

$ cat a.c
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/resource.h>
#include <stdio.h>

int main(void)
{
        char dummy[4096];
        struct rlimit rl;

        rl.rlim_cur = 1;
        rl.rlim_max = 1;

        if (mlock(NULL, 1024))
                perror("mlock(NULL, 1024)");

        if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0)
                perror("setrlimit");

        if (mlock(dummy, 1024))
                perror("mlock(dummy, 1024)");

        return 0;
}

$ ./a.out
mlock(NULL, 1024): Cannot allocate memory
mlock(dummy, 1024): Cannot allocate memory


> + *	test 2:
> + *		The  caller  was  not  privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was 0. EPERM should be returned
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  #include "test.h"
>  #include "usctest.h"
>  #include "safe_macros.h"
> @@ -32,20 +36,26 @@
>  char *TCID = "mlock02";
>  
>  static void *addr1;
> +static struct passwd *ltpuser;
>  static void setup(void);
>  static void mlock_verify(int);
> +static void setup2(void);
> +static void cleanup2(void);
>  static void cleanup(void);
>  
>  static struct test_case_t {
>  	void **addr;
>  	int len;
>  	int error;
> +	void (*setupfunc) ();
> +	void (*cleanupfunc) ();
>  } TC[] = {
> -	{&addr1, 1024, ENOMEM},
> +	{&addr1, 1024, ENOMEM, NULL, NULL},
> +	{&addr1, 1024, EPERM, setup2, cleanup2},
>  };
>  
>  int TST_TOTAL = ARRAY_SIZE(TC);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  #if !defined(UCLINUX)
>  
> @@ -85,6 +95,8 @@ int main(void)
>  
>  static void setup(void)
>  {
> +	struct rlimit rl;
> +
>  	TEST_PAUSE;
>  
>  #ifdef __ia64__
> @@ -92,12 +104,28 @@ static void setup(void)
>  #else
>  	addr1 = NULL;
>  #endif
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
> +
> +	rl.rlim_cur = 0;

rlim_max is not initialised, so it can fail if you run mlock02 as user
and it has non-zero value:
setrlimit(RLIMIT_MEMLOCK, {rlim_cur=0, rlim_max=4216848}) = -1 EPERM (Operation not permitted)

Initialising it to zero:
    +rl.rlim_max = 0;
will make the testcase work for root, but it's failing for unprivileged user:

$ ./mlock02
mlock02     1  TFAIL  :  mlock didn't fail as expected; expected - 12 : Cannot allocate memory: TEST_ERRNO=EPERM(1): Operation not permitted
mlock02     2  TBROK  :  seteuid failed at mlock02.c:146: errno=EPERM(1): Operation not permitted
mlock02     3  TBROK  :  Remaining cases broken

Regards,
Jan

> +
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_resm(TWARN,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");
> +		return;
> +	}
>  }
>  
>  static void mlock_verify(int i)
>  {
> +	if (TC[i].setupfunc != NULL)
> +		TC[i].setupfunc();
> +
>  	TEST(mlock(*(TC[i].addr), TC[i].len));
>  
> +	if (TC[i].cleanupfunc != NULL)
> +		TC[i].cleanupfunc();
> +
>  	if (TEST_RETURN != -1) {
>  		tst_resm(TFAIL, "mlock succeeded unexpectedly");
>  		return;
> @@ -112,6 +140,16 @@ static void mlock_verify(int i)
>  	}
>  }
>  
> +static void setup2(void)
> +{
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +}
> +
> +static void cleanup2(void)
> +{
> +	SAFE_SETEUID(cleanup, 0);
> +}
> +
>  static void cleanup(void)
>  {
>  	TEST_CLEANUP;
> --
> 1.8.4.2
> 
> 
> 
> 
> ------------------------------------------------------------------------------
> Android apps run on BlackBerry 10
> Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
> Now with support for Jelly Bean, Bluetooth, Mapview and more.
> Get your Android app in front of a whole new audience.  Start now.
> http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
> 

------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup
  2014-02-14 10:54   ` Jan Stancek
@ 2014-02-19  9:38     ` Zeng Linggang
  2014-02-19  9:40       ` [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  2014-02-19 11:02       ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Jan Stancek
  0 siblings, 2 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-02-19  9:38 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

* Delete some useless commtents.
* Move the test body form main() to mlock_verify().
* Some cleanup.

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 143 +++++++++++++-----------------
 1 file changed, 63 insertions(+), 80 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index edd9e45..1d1c853 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -1,6 +1,6 @@
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
+ *	06/2002 Written by Paul Larson
  *
  *   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
@@ -13,70 +13,48 @@
  *   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 to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   along with this program;  if not, write to the Free Software Foundation,
+ *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-
 /*
- * NAME
- * 	mlock02.c
- *
- * DESCRIPTION
- * 	Test to see the proper errors are returned by mlock
- *$
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
- *
- * USAGE:  <for command-line>
- *         -c n    Run n copies concurrently
- *         -e      Turn on errno logging
- *         -f      Turn off functional testing
- *         -h      Show this help screen
- *         -i n    Execute test n times
- *         -I x    Execute test for x seconds
- *         -p      Pause for SIGUSR1 before starting
- *         -P x    Pause for x seconds between iterations
- *         -t      Turn on syscall timing
- *
- * HISTORY
- *	06/2002 Written by Paul Larson
- *
- * RESTRICTIONS
- * 	None
  */
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include "test.h"
 #include "usctest.h"
 
-void setup();
-void setup1();
-void cleanup();
-
 char *TCID = "mlock02";
-int TST_TOTAL = 1;
-
-int exp_enos[] = { ENOMEM, 0 };
 
-void *addr1;
+#if !defined(UCLINUX)
 
 struct test_case_t {
 	void **addr;
 	int len;
 	int error;
 	void (*setupfunc) ();
-} TC[] = {
-	/* mlock should return ENOMEM when some or all of the address
-	 * range pointed to by addr and len are not valid mapped pages
-	 * in the address space of the process
-	 */
-	{
-	&addr1, 1024, ENOMEM, setup1}
 };
 
-#if !defined(UCLINUX)
+static void *addr1;
+static void setup(void);
+#ifdef __ia64__
+static void setup1(const struct test_case_t *);
+#else
+static void setup1(void);
+#endif
+static void cleanup(void);
+static void mlock_verify(const struct test_case_t *);
+
+static struct test_case_t TC[] = {
+	{&addr1, 1024, ENOMEM, setup1},
+};
+
+int TST_TOTAL = ARRAY_SIZE(TC);
+static int exp_enos[] = { ENOMEM, 0 };
 
 int main(int ac, char **av)
 {
@@ -91,62 +69,67 @@ int main(int ac, char **av)
 	TEST_EXP_ENOS(exp_enos);
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
 		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (TC[i].setupfunc != NULL)
-				TC[i].setupfunc();
-
-			TEST(mlock(*(TC[i].addr), TC[i].len));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO != TC[i].error)
-					tst_brkm(TFAIL | TTERRNO, cleanup,
-						 "mlock didn't fail as expected; "
-						 "expected - %d : %s",
-						 TC[i].error,
-						 strerror(TC[i].error));
-				else
-					tst_resm(TPASS | TTERRNO,
-						 "mlock failed as expected");
-			} else
-				tst_brkm(TFAIL, cleanup,
-					 "mlock succeeded unexpectedly");
-		}
+		for (i = 0; i < TST_TOTAL; i++)
+			mlock_verify(&TC[i]);
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-#else
-
-int main()
+static void setup(void)
 {
-	tst_brkm(TCONF, NULL, "test is not available on uClinux");
-}
-
-#endif /* if !defined(UCLINUX) */
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-void setup()
-{
 	TEST_PAUSE;
 }
 
-void setup1()
+static void mlock_verify(const struct test_case_t *test)
 {
+	if (test->setupfunc(test) == -1)
+		return;
+
+	TEST(mlock(*(test->addr), test->len));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "mlock succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != test->error) {
+		tst_resm(TFAIL | TTERRNO,
+			 "mlock didn't fail as expected; expected - %d : %s",
+			 test->error, strerror(test->error));
+	} else {
+		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
+	}
+}
+
 #ifdef __ia64__
-	TC[0].len = getpagesize() + 1;
+static void setup1(const struct test_case_t *test)
+{
+	test->len = getpagesize() + 1;
+}
 #else
+static void setup1(void)
+{
 	addr1 = NULL;
-#endif
 }
+#endif
 
-void cleanup()
+static void cleanup(void)
 {
 	TEST_CLEANUP;
+}
+
+#else
 
+int TST_TOTAL = 1;
+
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "test is not available on uClinux");
 }
+
+#endif /* if !defined(UCLINUX) */
-- 
1.8.4.2




------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-19  9:38     ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Zeng Linggang
@ 2014-02-19  9:40       ` Zeng Linggang
  2014-02-19 11:29         ` Jan Stancek
  2014-02-19 11:02       ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Jan Stancek
  1 sibling, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-02-19  9:40 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Add EPERM and ENOMEM errno tests for mlock(2).

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 88 ++++++++++++++++++++++++++++---
 1 file changed, 81 insertions(+), 7 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index 1d1c853..6ccd281 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -20,13 +20,22 @@
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
+ *	test 2:
+ *		The caller was not privileged and its RLIMIT_MEMLOCK soft
+ *		resource limit was 0. EPERM should be returned
+ *	test 3:
+ *		The caller was not privileged and its RLIMIT_MEMLOCK soft
+ *		resource limit was nonzero, but tried to lock more memory than
+ *		the limit permitted. ENOMEM should be returned
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <pwd.h>
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 
 char *TCID = "mlock02";
 
@@ -36,25 +45,32 @@ struct test_case_t {
 	void **addr;
 	int len;
 	int error;
-	void (*setupfunc) ();
+	int (*setupfunc) ();
+	void (*cleanupfunc) ();
 };
 
 static void *addr1;
+static struct passwd *ltpuser;
 static void setup(void);
 #ifdef __ia64__
-static void setup1(const struct test_case_t *);
+static int setup1(const struct test_case_t *);
 #else
-static void setup1(void);
+static int setup1(void);
 #endif
+static int setup2(void);
+static int setup3(void);
+static void cleanup2(void);
 static void cleanup(void);
 static void mlock_verify(const struct test_case_t *);
 
 static struct test_case_t TC[] = {
-	{&addr1, 1024, ENOMEM, setup1},
+	{&addr1, 1024, ENOMEM, setup1, NULL},
+	{&addr1, 1024, EPERM, setup2, cleanup2},
+	{&addr1+1024, 1024, ENOMEM, setup3, cleanup2},
 };
 
 int TST_TOTAL = ARRAY_SIZE(TC);
-static int exp_enos[] = { ENOMEM, 0 };
+static int exp_enos[] = { ENOMEM, EPERM, 0 };
 
 int main(int ac, char **av)
 {
@@ -83,6 +99,8 @@ static void setup(void)
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	TEST_PAUSE;
+
+	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
 }
 
 static void mlock_verify(const struct test_case_t *test)
@@ -92,6 +110,9 @@ static void mlock_verify(const struct test_case_t *test)
 
 	TEST(mlock(*(test->addr), test->len));
 
+	if (test->cleanupfunc != NULL)
+		test->cleanupfunc();
+
 	if (TEST_RETURN != -1) {
 		tst_resm(TFAIL, "mlock succeeded unexpectedly");
 		return;
@@ -107,17 +128,70 @@ static void mlock_verify(const struct test_case_t *test)
 }
 
 #ifdef __ia64__
-static void setup1(const struct test_case_t *test)
+static int setup1(const struct test_case_t *test)
 {
 	test->len = getpagesize() + 1;
+	return 0;
 }
 #else
-static void setup1(void)
+static int setup1(void)
 {
 	addr1 = NULL;
+	return 0;
 }
 #endif
 
+static int setup2(void)
+{
+	struct rlimit rl;
+
+	if (geteuid() != 0) {
+		tst_resm(TWARN, "Test needs to be run as root");
+		return -1;
+	}
+
+	rl.rlim_max = 0;
+	rl.rlim_cur = 0;
+	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
+		tst_resm(TWARN,
+			 "setrlimit failed to set the resource for "
+			 "RLIMIT_MEMLOCK to check for mlock()");
+		return -1;
+	}
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	return 0;
+}
+
+static int setup3(void)
+{
+	struct rlimit rl;
+
+	if (geteuid() != 0) {
+		tst_resm(TWARN, "Test needs to be run as root");
+		return -1;
+	}
+
+	rl.rlim_max = 1;
+	rl.rlim_cur = 1;
+	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
+		tst_resm(TWARN,
+			 "setrlimit failed to set the resource for "
+			 "RLIMIT_MEMLOCK to check for mlock()");
+		return -1;
+	}
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	return 0;
+}
+
+static void cleanup2(void)
+{
+	SAFE_SETEUID(cleanup, 0);
+}
+
 static void cleanup(void)
 {
 	TEST_CLEANUP;
-- 
1.8.4.2




------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup
  2014-02-19  9:38     ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Zeng Linggang
  2014-02-19  9:40       ` [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-02-19 11:02       ` Jan Stancek
  1 sibling, 0 replies; 24+ messages in thread
From: Jan Stancek @ 2014-02-19 11:02 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Wednesday, 19 February, 2014 10:38:36 AM
> Subject: [PATCH v2 1/2] mlock/mlock02.c: cleanup
> 
> * Delete some useless commtents.
> * Move the test body form main() to mlock_verify().
> * Some cleanup.
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>

Hi,

I have trouble compiling this patch (comments inline):
$ make
make -C "/usr/src/ltp/testcases/kernel/include" -f "/usr/src/ltp/testcases/kernel/include/Makefile" all
make[1]: Entering directory `/usr/src/ltp/testcases/kernel/include'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/usr/src/ltp/testcases/kernel/include'
gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -D_FORTIFY_SOURCE=2 -I/usr/src/ltp/testcases/kernel/include -I../../../../include -I../../../../include   -L../../../../lib  mlock02.c   -lltp -o mlock02
mlock02.c: In function ‘mlock_verify’:
mlock02.c:90: error: void value not ignored as it ought to be
make: *** [mlock02] Error 1

> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 143
>  +++++++++++++-----------------
>  1 file changed, 63 insertions(+), 80 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index edd9e45..1d1c853 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -1,6 +1,6 @@
>  /*
> - *
>   *   Copyright (c) International Business Machines  Corp., 2002
> + *	06/2002 Written by Paul Larson
>   *
>   *   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
> @@ -13,70 +13,48 @@
>   *   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 to the Free Software
> - *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA
> + *   along with this program;  if not, write to the Free Software
> Foundation,
> + *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
> -
>  /*
> - * NAME
> - * 	mlock02.c
> - *
> - * DESCRIPTION
> - * 	Test to see the proper errors are returned by mlock
> - *$
>   * ALGORITHM
>   * 	test 1:
>   *		Call mlock with a NULL address.  ENOMEM should be returned
> - *
> - * USAGE:  <for command-line>
> - *         -c n    Run n copies concurrently
> - *         -e      Turn on errno logging
> - *         -f      Turn off functional testing
> - *         -h      Show this help screen
> - *         -i n    Execute test n times
> - *         -I x    Execute test for x seconds
> - *         -p      Pause for SIGUSR1 before starting
> - *         -P x    Pause for x seconds between iterations
> - *         -t      Turn on syscall timing
> - *
> - * HISTORY
> - *	06/2002 Written by Paul Larson
> - *
> - * RESTRICTIONS
> - * 	None
>   */
> +
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
>  #include "test.h"
>  #include "usctest.h"
>  
> -void setup();
> -void setup1();
> -void cleanup();
> -
>  char *TCID = "mlock02";
> -int TST_TOTAL = 1;
> -
> -int exp_enos[] = { ENOMEM, 0 };
>  
> -void *addr1;
> +#if !defined(UCLINUX)
>  
>  struct test_case_t {
>  	void **addr;
>  	int len;
>  	int error;
>  	void (*setupfunc) ();

should list parameters or void

> -} TC[] = {
> -	/* mlock should return ENOMEM when some or all of the address
> -	 * range pointed to by addr and len are not valid mapped pages
> -	 * in the address space of the process
> -	 */
> -	{
> -	&addr1, 1024, ENOMEM, setup1}
>  };
>  
> -#if !defined(UCLINUX)
> +static void *addr1;
> +static void setup(void);
> +#ifdef __ia64__
> +static void setup1(const struct test_case_t *);

Why different prototype for ia64?
I don't think this parameter should be const, it's likely setupfunc()
will want to modify the struct.

> +#else
> +static void setup1(void);
> +#endif
> +static void cleanup(void);
> +static void mlock_verify(const struct test_case_t *);
> +
> +static struct test_case_t TC[] = {
> +	{&addr1, 1024, ENOMEM, setup1},
> +};
> +
> +int TST_TOTAL = ARRAY_SIZE(TC);
> +static int exp_enos[] = { ENOMEM, 0 };
>  
>  int main(int ac, char **av)
>  {
> @@ -91,62 +69,67 @@ int main(int ac, char **av)
>  	TEST_EXP_ENOS(exp_enos);
>  
>  	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
>  		tst_count = 0;
> -
> -		for (i = 0; i < TST_TOTAL; i++) {
> -
> -			if (TC[i].setupfunc != NULL)
> -				TC[i].setupfunc();
> -
> -			TEST(mlock(*(TC[i].addr), TC[i].len));
> -
> -			if (TEST_RETURN == -1) {
> -				if (TEST_ERRNO != TC[i].error)
> -					tst_brkm(TFAIL | TTERRNO, cleanup,
> -						 "mlock didn't fail as expected; "
> -						 "expected - %d : %s",
> -						 TC[i].error,
> -						 strerror(TC[i].error));
> -				else
> -					tst_resm(TPASS | TTERRNO,
> -						 "mlock failed as expected");
> -			} else
> -				tst_brkm(TFAIL, cleanup,
> -					 "mlock succeeded unexpectedly");
> -		}
> +		for (i = 0; i < TST_TOTAL; i++)
> +			mlock_verify(&TC[i]);
>  	}
>  
>  	cleanup();
> -
>  	tst_exit();
>  }
>  
> -#else
> -
> -int main()
> +static void setup(void)
>  {
> -	tst_brkm(TCONF, NULL, "test is not available on uClinux");
> -}
> -
> -#endif /* if !defined(UCLINUX) */
> +	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>  
> -void setup()
> -{
>  	TEST_PAUSE;
>  }
>  
> -void setup1()
> +static void mlock_verify(const struct test_case_t *test)
>  {
> +	if (test->setupfunc(test) == -1)

setupfunc returns void

> +		return;
> +
> +	TEST(mlock(*(test->addr), test->len));
> +
> +	if (TEST_RETURN != -1) {
> +		tst_resm(TFAIL, "mlock succeeded unexpectedly");
> +		return;
> +	}
> +
> +	if (TEST_ERRNO != test->error) {
> +		tst_resm(TFAIL | TTERRNO,
> +			 "mlock didn't fail as expected; expected - %d : %s",
> +			 test->error, strerror(test->error));
> +	} else {
> +		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
> +	}
> +}
> +
>  #ifdef __ia64__
> -	TC[0].len = getpagesize() + 1;
> +static void setup1(const struct test_case_t *test)
> +{
> +	test->len = getpagesize() + 1;

Not relevant to this cleanup patch: Any idea why ia64 needs this setup? 

> +}
>  #else
> +static void setup1(void)
> +{
>  	addr1 = NULL;

If you had same prototype you could also do:
  *(test->addr) = NULL;
which is not needed for this single testcase, but I'm assuming part 2
of the patchset will modify addr1, so resetting value will be needed.

> -#endif
>  }
> +#endif
>  
> -void cleanup()
> +static void cleanup(void)
>  {
>  	TEST_CLEANUP;
> +}
> +
> +#else
>  
> +int TST_TOTAL = 1;
> +
> +int main(void)
> +{
> +	tst_brkm(TCONF, NULL, "test is not available on uClinux");
>  }
> +
> +#endif /* if !defined(UCLINUX) */
> --
> 1.8.4.2
> 
> 

Here is patch which made it compile-able for me:

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index 1d1c853..cfa9176 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -36,18 +36,14 @@ struct test_case_t {
 	void **addr;
 	int len;
 	int error;
-	void (*setupfunc) ();
+	int (*setupfunc) (struct test_case_t *);
 };
 
 static void *addr1;
 static void setup(void);
-#ifdef __ia64__
-static void setup1(const struct test_case_t *);
-#else
-static void setup1(void);
-#endif
+static int setup1(struct test_case_t *);
 static void cleanup(void);
-static void mlock_verify(const struct test_case_t *);
+static void mlock_verify(struct test_case_t *);
 
 static struct test_case_t TC[] = {
 	{&addr1, 1024, ENOMEM, setup1},
@@ -85,7 +81,7 @@ static void setup(void)
 	TEST_PAUSE;
 }
 
-static void mlock_verify(const struct test_case_t *test)
+static void mlock_verify(struct test_case_t *test)
 {
 	if (test->setupfunc(test) == -1)
 		return;
@@ -106,17 +102,14 @@ static void mlock_verify(const struct test_case_t *test)
 	}
 }
 
-#ifdef __ia64__
-static void setup1(const struct test_case_t *test)
+static int setup1(struct test_case_t *test)
 {
+#ifdef __ia64__
 	test->len = getpagesize() + 1;
-}
-#else
-static void setup1(void)
-{
-	addr1 = NULL;
-}
 #endif
+	*(test->addr) = NULL;
+	return 0;
+}
 
 static void cleanup(void)
 {

Regards,
Jan

------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-19  9:40       ` [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-02-19 11:29         ` Jan Stancek
  2014-02-20  9:40           ` [LTP] [PATCH v3 1/2] mlock/mlock02.c: cleanup Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Stancek @ 2014-02-19 11:29 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Wednesday, 19 February, 2014 10:40:03 AM
> Subject: [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> 
> Add EPERM and ENOMEM errno tests for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 88
>  ++++++++++++++++++++++++++++---
>  1 file changed, 81 insertions(+), 7 deletions(-)

Hi,

as mentioned in part1, there are some compilation issues:
Let's pretend we are on ia64 (extra -D__ia64__ passed to gcc):
$ gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -D_FORTIFY_SOURCE=2 -I/usr/src/ltp/testcases/kernel/include -I../../../../include -I../../../../include   -L../../../../lib  mlock02.c   -lltp -o mlock02 -D__ia64__
mlock02.c: In function ‘setup1’:
mlock02.c:133: error: assignment of read-only location ‘*test’


> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index 1d1c853..6ccd281 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -20,13 +20,22 @@
>   * ALGORITHM
>   * 	test 1:
>   *		Call mlock with a NULL address.  ENOMEM should be returned
> + *	test 2:
> + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was 0. EPERM should be returned
> + *	test 3:
> + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was nonzero, but tried to lock more memory than
> + *		the limit permitted. ENOMEM should be returned
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  #include "test.h"
>  #include "usctest.h"
> +#include "safe_macros.h"
>  
>  char *TCID = "mlock02";
>  
> @@ -36,25 +45,32 @@ struct test_case_t {
>  	void **addr;
>  	int len;
>  	int error;
> -	void (*setupfunc) ();
> +	int (*setupfunc) ();
> +	void (*cleanupfunc) ();

should list parameters or void

>  };
>  
>  static void *addr1;
> +static struct passwd *ltpuser;
>  static void setup(void);
>  #ifdef __ia64__
> -static void setup1(const struct test_case_t *);
> +static int setup1(const struct test_case_t *);
>  #else
> -static void setup1(void);
> +static int setup1(void);
>  #endif
> +static int setup2(void);
> +static int setup3(void);
> +static void cleanup2(void);
>  static void cleanup(void);
>  static void mlock_verify(const struct test_case_t *);
>  
>  static struct test_case_t TC[] = {
> -	{&addr1, 1024, ENOMEM, setup1},
> +	{&addr1, 1024, ENOMEM, setup1, NULL},
> +	{&addr1, 1024, EPERM, setup2, cleanup2},
> +	{&addr1+1024, 1024, ENOMEM, setup3, cleanup2},

What is at this memory location? It appears to be from heap:

(gdb) b setup3
Breakpoint 1 at 0x401b30: file mlock02.c, line 168.
(gdb) r
Starting program: /usr/src/ltp/testcases/kernel/syscalls/mlock/mlock02 
...
Breakpoint 1, setup3 () at mlock02.c:168
168	{
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64

(gdb) p/x &addr1
$3 = 0x608728

(gdb) p/x &addr1+1024
$4 = 0x60a728

(gdb) info proc map
process 524
cmdline = '/usr/src/ltp/testcases/kernel/syscalls/mlock/mlock02'
cwd = '/usr/src/ltp/testcases/kernel/syscalls/mlock'
exe = '/usr/src/ltp/testcases/kernel/syscalls/mlock/mlock02'
Mapped address spaces:

          Start Addr           End Addr       Size     Offset objfile
            0x400000           0x409000     0x9000          0                             /usr/src/ltp/testcases/kernel/syscalls/mlock/mlock02
            0x608000           0x609000     0x1000     0x8000                             /usr/src/ltp/testcases/kernel/syscalls/mlock/mlock02
            0x609000           0x62e000    0x25000          0                                   [heap]


>  };
>  
>  int TST_TOTAL = ARRAY_SIZE(TC);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  int main(int ac, char **av)
>  {
> @@ -83,6 +99,8 @@ static void setup(void)
>  	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>  
>  	TEST_PAUSE;
> +
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
>  }
>  
>  static void mlock_verify(const struct test_case_t *test)
> @@ -92,6 +110,9 @@ static void mlock_verify(const struct test_case_t *test)
>  
>  	TEST(mlock(*(test->addr), test->len));
>  
> +	if (test->cleanupfunc != NULL)
> +		test->cleanupfunc();
> +
>  	if (TEST_RETURN != -1) {
>  		tst_resm(TFAIL, "mlock succeeded unexpectedly");
>  		return;
> @@ -107,17 +128,70 @@ static void mlock_verify(const struct test_case_t
> *test)
>  }
>  
>  #ifdef __ia64__
> -static void setup1(const struct test_case_t *test)
> +static int setup1(const struct test_case_t *test)
>  {
>  	test->len = getpagesize() + 1;
> +	return 0;
>  }
>  #else
> -static void setup1(void)
> +static int setup1(void)
>  {
>  	addr1 = NULL;
> +	return 0;
>  }
>  #endif
>  
> +static int setup2(void)
> +{
> +	struct rlimit rl;
> +
> +	if (geteuid() != 0) {
> +		tst_resm(TWARN, "Test needs to be run as root");
> +		return -1;
> +	}

If you add tst_require_root(NULL); to setup() that will break with TCONF and you can remove this.

> +
> +	rl.rlim_max = 0;
> +	rl.rlim_cur = 0;
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_resm(TWARN,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");

I'd expect TBROK here.

> +		return -1;
> +	}
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	return 0;
> +}
> +
> +static int setup3(void)
> +{
> +	struct rlimit rl;
> +
> +	if (geteuid() != 0) {
> +		tst_resm(TWARN, "Test needs to be run as root");
> +		return -1;
> +	}

same as in setup2, can be removed if you add tst_require_root(NULL); to setup()

> +
> +	rl.rlim_max = 1;
> +	rl.rlim_cur = 1;
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_resm(TWARN,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");

I'd expect TBROK here as well.

> +		return -1;
> +	}
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	return 0;
> +}

For setup2 and setup3 I'd suggest to set *addr to some reasonable value,
so that when mlock returns ENOMEM we know it wasn't because *addr was NULL.

Regards,
Jan

> +
> +static void cleanup2(void)
> +{
> +	SAFE_SETEUID(cleanup, 0);
> +}
> +
>  static void cleanup(void)
>  {
>  	TEST_CLEANUP;
> --
> 1.8.4.2
> 
> 
> 
> 

------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v3 1/2] mlock/mlock02.c: cleanup
  2014-02-19 11:29         ` Jan Stancek
@ 2014-02-20  9:40           ` Zeng Linggang
  2014-02-20  9:50             ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-02-20  9:40 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

* Delete some useless commtents.
* Move the test body form main() to mlock_verify().
* Some cleanup.

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 136 ++++++++++++------------------
 1 file changed, 56 insertions(+), 80 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index edd9e45..811d141 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -1,6 +1,6 @@
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
+ *	06/2002 Written by Paul Larson
  *
  *   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
@@ -13,70 +13,44 @@
  *   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 to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   along with this program;  if not, write to the Free Software Foundation,
+ *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-
 /*
- * NAME
- * 	mlock02.c
- *
- * DESCRIPTION
- * 	Test to see the proper errors are returned by mlock
- *$
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
- *
- * USAGE:  <for command-line>
- *         -c n    Run n copies concurrently
- *         -e      Turn on errno logging
- *         -f      Turn off functional testing
- *         -h      Show this help screen
- *         -i n    Execute test n times
- *         -I x    Execute test for x seconds
- *         -p      Pause for SIGUSR1 before starting
- *         -P x    Pause for x seconds between iterations
- *         -t      Turn on syscall timing
- *
- * HISTORY
- *	06/2002 Written by Paul Larson
- *
- * RESTRICTIONS
- * 	None
  */
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include "test.h"
 #include "usctest.h"
 
-void setup();
-void setup1();
-void cleanup();
-
 char *TCID = "mlock02";
-int TST_TOTAL = 1;
 
-int exp_enos[] = { ENOMEM, 0 };
-
-void *addr1;
+#if !defined(UCLINUX)
 
 struct test_case_t {
 	void **addr;
 	int len;
 	int error;
-	void (*setupfunc) ();
-} TC[] = {
-	/* mlock should return ENOMEM when some or all of the address
-	 * range pointed to by addr and len are not valid mapped pages
-	 * in the address space of the process
-	 */
-	{
-	&addr1, 1024, ENOMEM, setup1}
+	void (*setupfunc) (struct test_case_t *);
 };
 
-#if !defined(UCLINUX)
+static void *addr1;
+static void setup(void);
+static void setup1(struct test_case_t *);
+static void cleanup(void);
+static void mlock_verify(struct test_case_t *);
+
+static struct test_case_t TC[] = {
+	{&addr1, 1024, ENOMEM, setup1},
+};
+
+int TST_TOTAL = ARRAY_SIZE(TC);
+static int exp_enos[] = { ENOMEM, 0 };
 
 int main(int ac, char **av)
 {
@@ -91,62 +65,64 @@ int main(int ac, char **av)
 	TEST_EXP_ENOS(exp_enos);
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
 		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (TC[i].setupfunc != NULL)
-				TC[i].setupfunc();
-
-			TEST(mlock(*(TC[i].addr), TC[i].len));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO != TC[i].error)
-					tst_brkm(TFAIL | TTERRNO, cleanup,
-						 "mlock didn't fail as expected; "
-						 "expected - %d : %s",
-						 TC[i].error,
-						 strerror(TC[i].error));
-				else
-					tst_resm(TPASS | TTERRNO,
-						 "mlock failed as expected");
-			} else
-				tst_brkm(TFAIL, cleanup,
-					 "mlock succeeded unexpectedly");
-		}
+		for (i = 0; i < TST_TOTAL; i++)
+			mlock_verify(&TC[i]);
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-#else
-
-int main()
+static void setup(void)
 {
-	tst_brkm(TCONF, NULL, "test is not available on uClinux");
-}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-#endif /* if !defined(UCLINUX) */
+	TEST_PAUSE;
+}
 
-void setup()
+static void mlock_verify(struct test_case_t *test)
 {
-	TEST_PAUSE;
+	if (test->setupfunc != NULL)
+		test->setupfunc(test);
+
+	TEST(mlock(*(test->addr), test->len));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "mlock succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != test->error) {
+		tst_resm(TFAIL | TTERRNO,
+			 "mlock didn't fail as expected; expected - %d : %s",
+			 test->error, strerror(test->error));
+	} else {
+		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
+	}
 }
 
-void setup1()
+static void setup1(struct test_case_t *test)
 {
 #ifdef __ia64__
-	TC[0].len = getpagesize() + 1;
+	test->len = getpagesize() + 1;
 #else
-	addr1 = NULL;
+	*test->addr = NULL;
 #endif
 }
 
-void cleanup()
+static void cleanup(void)
 {
 	TEST_CLEANUP;
+}
+
+#else
+
+int TST_TOTAL = 1;
 
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "test is not available on uClinux");
 }
+
+#endif /* if !defined(UCLINUX) */
-- 
1.8.4.2




------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-20  9:40           ` [LTP] [PATCH v3 1/2] mlock/mlock02.c: cleanup Zeng Linggang
@ 2014-02-20  9:50             ` Zeng Linggang
  2014-02-20 11:05               ` Jan Stancek
  0 siblings, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-02-20  9:50 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Add EPERM and ENOMEM errno tests for mlock(2).

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 65 +++++++++++++++++++++++++++++--
 1 file changed, 62 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index 811d141..79f1d29 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -20,13 +20,22 @@
  * ALGORITHM
  * 	test 1:
  *		Call mlock with a NULL address.  ENOMEM should be returned
+ *	test 2:
+ *		The caller was not privileged and its RLIMIT_MEMLOCK soft
+ *		resource limit was 0. EPERM should be returned
+ *	test 3:
+ *		The caller was not privileged and its RLIMIT_MEMLOCK soft
+ *		resource limit was nonzero, but tried to lock more memory than
+ *		the limit permitted. ENOMEM should be returned
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <pwd.h>
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 
 char *TCID = "mlock02";
 
@@ -36,21 +45,29 @@ struct test_case_t {
 	void **addr;
 	int len;
 	int error;
-	void (*setupfunc) (struct test_case_t *);
+	void (*setupfunc) ();
+	void (*cleanupfunc) (void);
 };
 
 static void *addr1;
+static char addr2[1024];
+static struct passwd *ltpuser;
 static void setup(void);
 static void setup1(struct test_case_t *);
+static void setup2(void);
+static void setup3(void);
+static void cleanup2(void);
 static void cleanup(void);
 static void mlock_verify(struct test_case_t *);
 
 static struct test_case_t TC[] = {
-	{&addr1, 1024, ENOMEM, setup1},
+	{&addr1, 1024, ENOMEM, setup1, NULL},
+	{(void **)&addr2, 1024, EPERM, setup2, cleanup2},
+	{(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
 };
 
 int TST_TOTAL = ARRAY_SIZE(TC);
-static int exp_enos[] = { ENOMEM, 0 };
+static int exp_enos[] = { ENOMEM, EPERM, 0 };
 
 int main(int ac, char **av)
 {
@@ -76,9 +93,13 @@ int main(int ac, char **av)
 
 static void setup(void)
 {
+	tst_require_root(NULL);
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	TEST_PAUSE;
+
+	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
 }
 
 static void mlock_verify(struct test_case_t *test)
@@ -88,6 +109,9 @@ static void mlock_verify(struct test_case_t *test)
 
 	TEST(mlock(*(test->addr), test->len));
 
+	if (test->cleanupfunc != NULL)
+		test->cleanupfunc();
+
 	if (TEST_RETURN != -1) {
 		tst_resm(TFAIL, "mlock succeeded unexpectedly");
 		return;
@@ -111,6 +135,41 @@ static void setup1(struct test_case_t *test)
 #endif
 }
 
+static void setup2(void)
+{
+	struct rlimit rl;
+
+	rl.rlim_max = 0;
+	rl.rlim_cur = 0;
+	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
+		tst_brkm(TBROK, cleanup,
+			 "setrlimit failed to set the resource for "
+			 "RLIMIT_MEMLOCK to check for mlock()");
+	}
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+}
+
+static void setup3(void)
+{
+	struct rlimit rl;
+
+	rl.rlim_max = 1;
+	rl.rlim_cur = 1;
+	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
+		tst_brkm(TBROK, cleanup,
+			 "setrlimit failed to set the resource for "
+			 "RLIMIT_MEMLOCK to check for mlock()");
+	}
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+}
+
+static void cleanup2(void)
+{
+	SAFE_SETEUID(cleanup, 0);
+}
+
 static void cleanup(void)
 {
 	TEST_CLEANUP;
-- 
1.8.4.2




------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-20  9:50             ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-02-20 11:05               ` Jan Stancek
  2014-02-20 13:03                 ` Jan Stancek
  2014-02-21  1:04                 ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  0 siblings, 2 replies; 24+ messages in thread
From: Jan Stancek @ 2014-02-20 11:05 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Thursday, 20 February, 2014 10:50:13 AM
> Subject: [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> 
> Add EPERM and ENOMEM errno tests for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>

Hi,

part1 looks good to me, comments for part2 are inline.

> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 65
>  +++++++++++++++++++++++++++++--
>  1 file changed, 62 insertions(+), 3 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index 811d141..79f1d29 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -20,13 +20,22 @@
>   * ALGORITHM
>   * 	test 1:
>   *		Call mlock with a NULL address.  ENOMEM should be returned
> + *	test 2:
> + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was 0. EPERM should be returned
> + *	test 3:
> + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was nonzero, but tried to lock more memory than
> + *		the limit permitted. ENOMEM should be returned
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  #include "test.h"
>  #include "usctest.h"
> +#include "safe_macros.h"
>  
>  char *TCID = "mlock02";
>  
> @@ -36,21 +45,29 @@ struct test_case_t {
>  	void **addr;
>  	int len;
>  	int error;
> -	void (*setupfunc) (struct test_case_t *);
> +	void (*setupfunc) ();

If you don't want any parameters add void.

> +	void (*cleanupfunc) (void);
>  };
>  
>  static void *addr1;
> +static char addr2[1024];
> +static struct passwd *ltpuser;
>  static void setup(void);
>  static void setup1(struct test_case_t *);
> +static void setup2(void);
> +static void setup3(void);
> +static void cleanup2(void);
>  static void cleanup(void);
>  static void mlock_verify(struct test_case_t *);
>  
>  static struct test_case_t TC[] = {
> -	{&addr1, 1024, ENOMEM, setup1},
> +	{&addr1, 1024, ENOMEM, setup1, NULL},
> +	{(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> +	{(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
>  };

I think I misunderstood intent of **addr. As you outlined it
above, we can remove one pointer entirely along with addr1:

-static void *addr1;

 struct test_case_t {
-       void **addr;
+       void *addr;

 static struct test_case_t TC[] = {
-       {&addr1, 1024, ENOMEM, setup1, NULL},
-       {(void **)&addr2, 1024, EPERM, setup2, cleanup2},
-       {(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
+       {NULL, 1024, ENOMEM, setup1, NULL},
+       {addr2, 1024, EPERM, setup2, cleanup2},
+       {addr2, 1024, ENOMEM, setup3, cleanup2},

 static void mlock_verify(struct test_case_t *test)
-       TEST(mlock(*(test->addr), test->len));
+       TEST(mlock(test->addr, test->len));

 static void setup1(struct test_case_t *test)
-#else
-       *test->addr = NULL;


I'm going to try this testcase on ia64 to have a look at that
ia64 specific setup.

Regards,
Jan

>  
>  int TST_TOTAL = ARRAY_SIZE(TC);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  int main(int ac, char **av)
>  {
> @@ -76,9 +93,13 @@ int main(int ac, char **av)
>  
>  static void setup(void)
>  {
> +	tst_require_root(NULL);
> +
>  	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>  
>  	TEST_PAUSE;
> +
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
>  }
>  
>  static void mlock_verify(struct test_case_t *test)
> @@ -88,6 +109,9 @@ static void mlock_verify(struct test_case_t *test)
>  
>  	TEST(mlock(*(test->addr), test->len));
>  
> +	if (test->cleanupfunc != NULL)
> +		test->cleanupfunc();
> +
>  	if (TEST_RETURN != -1) {
>  		tst_resm(TFAIL, "mlock succeeded unexpectedly");
>  		return;
> @@ -111,6 +135,41 @@ static void setup1(struct test_case_t *test)
>  #endif
>  }
>  
> +static void setup2(void)
> +{
> +	struct rlimit rl;
> +
> +	rl.rlim_max = 0;
> +	rl.rlim_cur = 0;
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_brkm(TBROK, cleanup,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");
> +	}
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +}
> +
> +static void setup3(void)
> +{
> +	struct rlimit rl;
> +
> +	rl.rlim_max = 1;
> +	rl.rlim_cur = 1;
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_brkm(TBROK, cleanup,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");
> +	}
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +}
> +
> +static void cleanup2(void)
> +{
> +	SAFE_SETEUID(cleanup, 0);
> +}
> +
>  static void cleanup(void)
>  {
>  	TEST_CLEANUP;
> --
> 1.8.4.2
> 
> 
> 
> 

------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-20 11:05               ` Jan Stancek
@ 2014-02-20 13:03                 ` Jan Stancek
  2014-02-21  9:03                   ` Zeng Linggang
  2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  2014-02-21  1:04                 ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  1 sibling, 2 replies; 24+ messages in thread
From: Jan Stancek @ 2014-02-20 13:03 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Jan Stancek" <jstancek@redhat.com>
> To: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Thursday, 20 February, 2014 12:05:29 PM
> Subject: Re: [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> 
> 
> 
> ----- Original Message -----
> > From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> > To: "Jan Stancek" <jstancek@redhat.com>
> > Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> > Sent: Thursday, 20 February, 2014 10:50:13 AM
> > Subject: [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> > 
> > Add EPERM and ENOMEM errno tests for mlock(2).
> > 
> > Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> 
> Hi,
> 
> part1 looks good to me, comments for part2 are inline.
> 
> > ---
> >  testcases/kernel/syscalls/mlock/mlock02.c | 65
> >  +++++++++++++++++++++++++++++--
> >  1 file changed, 62 insertions(+), 3 deletions(-)
> > 
> > diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> > b/testcases/kernel/syscalls/mlock/mlock02.c
> > index 811d141..79f1d29 100644
> > --- a/testcases/kernel/syscalls/mlock/mlock02.c
> > +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> > @@ -20,13 +20,22 @@
> >   * ALGORITHM
> >   * 	test 1:
> >   *		Call mlock with a NULL address.  ENOMEM should be returned
> > + *	test 2:
> > + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> > + *		resource limit was 0. EPERM should be returned
> > + *	test 3:
> > + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> > + *		resource limit was nonzero, but tried to lock more memory than
> > + *		the limit permitted. ENOMEM should be returned
> >   */
> >  
> >  #include <errno.h>
> >  #include <unistd.h>
> >  #include <sys/mman.h>
> > +#include <pwd.h>
> >  #include "test.h"
> >  #include "usctest.h"
> > +#include "safe_macros.h"
> >  
> >  char *TCID = "mlock02";
> >  
> > @@ -36,21 +45,29 @@ struct test_case_t {
> >  	void **addr;
> >  	int len;
> >  	int error;
> > -	void (*setupfunc) (struct test_case_t *);
> > +	void (*setupfunc) ();
> 
> If you don't want any parameters add void.
> 
> > +	void (*cleanupfunc) (void);
> >  };
> >  
> >  static void *addr1;
> > +static char addr2[1024];
> > +static struct passwd *ltpuser;
> >  static void setup(void);
> >  static void setup1(struct test_case_t *);
> > +static void setup2(void);
> > +static void setup3(void);
> > +static void cleanup2(void);
> >  static void cleanup(void);
> >  static void mlock_verify(struct test_case_t *);
> >  
> >  static struct test_case_t TC[] = {
> > -	{&addr1, 1024, ENOMEM, setup1},
> > +	{&addr1, 1024, ENOMEM, setup1, NULL},
> > +	{(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> > +	{(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> >  };
> 
> I think I misunderstood intent of **addr. As you outlined it
> above, we can remove one pointer entirely along with addr1:
> 
> -static void *addr1;
> 
>  struct test_case_t {
> -       void **addr;
> +       void *addr;
> 
>  static struct test_case_t TC[] = {
> -       {&addr1, 1024, ENOMEM, setup1, NULL},
> -       {(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> -       {(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> +       {NULL, 1024, ENOMEM, setup1, NULL},
> +       {addr2, 1024, EPERM, setup2, cleanup2},
> +       {addr2, 1024, ENOMEM, setup3, cleanup2},
> 
>  static void mlock_verify(struct test_case_t *test)
> -       TEST(mlock(*(test->addr), test->len));
> +       TEST(mlock(test->addr, test->len));
> 
>  static void setup1(struct test_case_t *test)
> -#else
> -       *test->addr = NULL;
> 
> 
> I'm going to try this testcase on ia64 to have a look at that
> ia64 specific setup.

ia64 maps something at that area with pagesize length:

# cat /proc/self/maps 
00000000-00004000 r--p 00000000 00:00 0 
2000000000000000-200000000003c000 r-xp 00000000 fd:00 950277             /lib/ld-2.5.so
2000000000048000-2000000000050000 rw-p 00038000 fd:00 950277             /lib/ld-2.5.so
2000000000050000-20000000002c0000 r-xp 00000000 fd:00 950284             /lib/libc-2.5.so
20000000002c0000-20000000002cc000 ---p 00270000 fd:00 950284             /lib/libc-2.5.so
20000000002cc000-20000000002d4000 rw-p 0026c000 fd:00 950284             /lib/libc-2.5.so
20000000002d4000-20000000002e4000 rw-p 20000000002d4000 00:00 0 
20000000002e4000-2000000003af4000 r--p 00000000 fd:00 69955              /usr/lib/locale/locale-archive
4000000000000000-4000000000008000 r-xp 00000000 fd:00 524317             /bin/cat
6000000000004000-600000000000c000 rw-p 00004000 fd:00 524317             /bin/cat
600000000000c000-6000000000030000 rw-p 600000000000c000 00:00 0          [heap]
600007fffffa8000-600007fffffac000 rw-p 600007fffffa8000 00:00 0 
60000ffffff50000-60000ffffffa4000 rw-p 60000ffffffa8000 00:00 0          [stack]
a000000000000000-a000000000020000 r-xp 00000000 00:00 0                  [vdso]

I think we can make that generic and get rid of that ifdef:

 static void setup1(struct test_case_t *test)
 {
-#ifdef __ia64__
-       test->len = getpagesize() + 1;
-#else
-       *test->addr = NULL;
-#endif
+       /* find some unmapped area */
+       test->addr = mmap(NULL, getpagesize(), PROT_NONE,
+                MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+       if (test->addr == MAP_FAILED)
+               tst_brkm(TBROK | TERRNO, cleanup, "mmap");
+       if (munmap(test->addr, getpagesize()) < 0)
+               tst_brkm(TBROK | TERRNO, cleanup, "munmap");
 }

Works for me on x86 and ia64. I can post it afterwards (rebased to latest
version of your patches).

Regards,
Jan

> 
> Regards,
> Jan
> 
> >  
> >  int TST_TOTAL = ARRAY_SIZE(TC);
> > -static int exp_enos[] = { ENOMEM, 0 };
> > +static int exp_enos[] = { ENOMEM, EPERM, 0 };
> >  
> >  int main(int ac, char **av)
> >  {
> > @@ -76,9 +93,13 @@ int main(int ac, char **av)
> >  
> >  static void setup(void)
> >  {
> > +	tst_require_root(NULL);
> > +
> >  	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> >  
> >  	TEST_PAUSE;
> > +
> > +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
> >  }
> >  
> >  static void mlock_verify(struct test_case_t *test)
> > @@ -88,6 +109,9 @@ static void mlock_verify(struct test_case_t *test)
> >  
> >  	TEST(mlock(*(test->addr), test->len));
> >  
> > +	if (test->cleanupfunc != NULL)
> > +		test->cleanupfunc();
> > +
> >  	if (TEST_RETURN != -1) {
> >  		tst_resm(TFAIL, "mlock succeeded unexpectedly");
> >  		return;
> > @@ -111,6 +135,41 @@ static void setup1(struct test_case_t *test)
> >  #endif
> >  }
> >  
> > +static void setup2(void)
> > +{
> > +	struct rlimit rl;
> > +
> > +	rl.rlim_max = 0;
> > +	rl.rlim_cur = 0;
> > +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> > +		tst_brkm(TBROK, cleanup,
> > +			 "setrlimit failed to set the resource for "
> > +			 "RLIMIT_MEMLOCK to check for mlock()");
> > +	}
> > +
> > +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> > +}
> > +
> > +static void setup3(void)
> > +{
> > +	struct rlimit rl;
> > +
> > +	rl.rlim_max = 1;
> > +	rl.rlim_cur = 1;
> > +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> > +		tst_brkm(TBROK, cleanup,
> > +			 "setrlimit failed to set the resource for "
> > +			 "RLIMIT_MEMLOCK to check for mlock()");
> > +	}
> > +
> > +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> > +}
> > +
> > +static void cleanup2(void)
> > +{
> > +	SAFE_SETEUID(cleanup, 0);
> > +}
> > +
> >  static void cleanup(void)
> >  {
> >  	TEST_CLEANUP;
> > --
> > 1.8.4.2
> > 
> > 
> > 
> > 
> 
> ------------------------------------------------------------------------------
> Managing the Performance of Cloud-Based Applications
> Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
> Read the Whitepaper.
> http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
> 

------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-20 11:05               ` Jan Stancek
  2014-02-20 13:03                 ` Jan Stancek
@ 2014-02-21  1:04                 ` Zeng Linggang
  1 sibling, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-02-21  1:04 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

On Thu, 2014-02-20 at 06:05 -0500, Jan Stancek wrote:
> 
> ----- Original Message -----
> > From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> > To: "Jan Stancek" <jstancek@redhat.com>
> > Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> > Sent: Thursday, 20 February, 2014 10:50:13 AM
> > Subject: [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> > 
> > Add EPERM and ENOMEM errno tests for mlock(2).
> > 
> > Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> 
> Hi,
> 
> part1 looks good to me, comments for part2 are inline.
> 

Thanks for your review.

> > ---
> >  testcases/kernel/syscalls/mlock/mlock02.c | 65
> >  +++++++++++++++++++++++++++++--
> >  1 file changed, 62 insertions(+), 3 deletions(-)
> > 
> > diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> > b/testcases/kernel/syscalls/mlock/mlock02.c
> > index 811d141..79f1d29 100644
> > --- a/testcases/kernel/syscalls/mlock/mlock02.c
> > +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> > @@ -20,13 +20,22 @@
> >   * ALGORITHM
> >   * 	test 1:
> >   *		Call mlock with a NULL address.  ENOMEM should be returned
> > + *	test 2:
> > + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> > + *		resource limit was 0. EPERM should be returned
> > + *	test 3:
> > + *		The caller was not privileged and its RLIMIT_MEMLOCK soft
> > + *		resource limit was nonzero, but tried to lock more memory than
> > + *		the limit permitted. ENOMEM should be returned
> >   */
> >  
> >  #include <errno.h>
> >  #include <unistd.h>
> >  #include <sys/mman.h>
> > +#include <pwd.h>
> >  #include "test.h"
> >  #include "usctest.h"
> > +#include "safe_macros.h"
> >  
> >  char *TCID = "mlock02";
> >  
> > @@ -36,21 +45,29 @@ struct test_case_t {
> >  	void **addr;
> >  	int len;
> >  	int error;
> > -	void (*setupfunc) (struct test_case_t *);
> > +	void (*setupfunc) ();
> 
> If you don't want any parameters add void.
> 
> > +	void (*cleanupfunc) (void);
> >  };
> >  
> >  static void *addr1;
> > +static char addr2[1024];
> > +static struct passwd *ltpuser;
> >  static void setup(void);
> >  static void setup1(struct test_case_t *);
> > +static void setup2(void);
> > +static void setup3(void);
> > +static void cleanup2(void);
> >  static void cleanup(void);
> >  static void mlock_verify(struct test_case_t *);
> >  
> >  static struct test_case_t TC[] = {
> > -	{&addr1, 1024, ENOMEM, setup1},
> > +	{&addr1, 1024, ENOMEM, setup1, NULL},
> > +	{(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> > +	{(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> >  };
> 
> I think I misunderstood intent of **addr. As you outlined it
> above, we can remove one pointer entirely along with addr1:
> 
> -static void *addr1;
> 
>  struct test_case_t {
> -       void **addr;
> +       void *addr;
> 
>  static struct test_case_t TC[] = {
> -       {&addr1, 1024, ENOMEM, setup1, NULL},
> -       {(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> -       {(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> +       {NULL, 1024, ENOMEM, setup1, NULL},
> +       {addr2, 1024, EPERM, setup2, cleanup2},
> +       {addr2, 1024, ENOMEM, setup3, cleanup2},
> 
>  static void mlock_verify(struct test_case_t *test)
> -       TEST(mlock(*(test->addr), test->len));
> +       TEST(mlock(test->addr, test->len));
> 
>  static void setup1(struct test_case_t *test)
> -#else
> -       *test->addr = NULL;
> 
> 

Yes. We can remove that. And it looks well. :)

> I'm going to try this testcase on ia64 to have a look at that
> ia64 specific setup.
> 

Thank you very much.
Best regards,
Zeng

> Regards,
> Jan
> 



------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-02-20 13:03                 ` Jan Stancek
@ 2014-02-21  9:03                   ` Zeng Linggang
  2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  1 sibling, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-02-21  9:03 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

On Thu, 2014-02-20 at 08:03 -0500, Jan Stancek wrote:
> >  	int error;
> > > -	void (*setupfunc) (struct test_case_t *);
> > > +	void (*setupfunc) ();
> > 
> > If you don't want any parameters add void.
> > 
> > > +	void (*cleanupfunc) (void);
> > >  };
> > >  
> > >  static void *addr1;
> > > +static char addr2[1024];
> > > +static struct passwd *ltpuser;
> > >  static void setup(void);
> > >  static void setup1(struct test_case_t *);
> > > +static void setup2(void);
> > > +static void setup3(void);
> > > +static void cleanup2(void);
> > >  static void cleanup(void);
> > >  static void mlock_verify(struct test_case_t *);
> > >  
> > >  static struct test_case_t TC[] = {
> > > -	{&addr1, 1024, ENOMEM, setup1},
> > > +	{&addr1, 1024, ENOMEM, setup1, NULL},
> > > +	{(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> > > +	{(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> > >  };
> > 
> > I think I misunderstood intent of **addr. As you outlined it
> > above, we can remove one pointer entirely along with addr1:
> > 
> > -static void *addr1;
> > 
> >  struct test_case_t {
> > -       void **addr;
> > +       void *addr;
> > 
> >  static struct test_case_t TC[] = {
> > -       {&addr1, 1024, ENOMEM, setup1, NULL},
> > -       {(void **)&addr2, 1024, EPERM, setup2, cleanup2},
> > -       {(void **)&addr2, 1024, ENOMEM, setup3, cleanup2},
> > +       {NULL, 1024, ENOMEM, setup1, NULL},
> > +       {addr2, 1024, EPERM, setup2, cleanup2},
> > +       {addr2, 1024, ENOMEM, setup3, cleanup2},
> > 
> >  static void mlock_verify(struct test_case_t *test)
> > -       TEST(mlock(*(test->addr), test->len));
> > +       TEST(mlock(test->addr, test->len));
> > 
> >  static void setup1(struct test_case_t *test)
> > -#else
> > -       *test->addr = NULL;
> > 
> > 
> > I'm going to try this testcase on ia64 to have a look at that
> > ia64 specific setup.
> 
> ia64 maps something at that area with pagesize length:
> 
> # cat /proc/self/maps 
> 00000000-00004000 r--p 00000000 00:00 0 
> 2000000000000000-200000000003c000 r-xp 00000000 fd:00 950277             /lib/ld-2.5.so
> 2000000000048000-2000000000050000 rw-p 00038000 fd:00 950277             /lib/ld-2.5.so
> 2000000000050000-20000000002c0000 r-xp 00000000 fd:00 950284             /lib/libc-2.5.so
> 20000000002c0000-20000000002cc000 ---p 00270000 fd:00 950284             /lib/libc-2.5.so
> 20000000002cc000-20000000002d4000 rw-p 0026c000 fd:00 950284             /lib/libc-2.5.so
> 20000000002d4000-20000000002e4000 rw-p 20000000002d4000 00:00 0 
> 20000000002e4000-2000000003af4000 r--p 00000000 fd:00 69955              /usr/lib/locale/locale-archive
> 4000000000000000-4000000000008000 r-xp 00000000 fd:00 524317             /bin/cat
> 6000000000004000-600000000000c000 rw-p 00004000 fd:00 524317             /bin/cat
> 600000000000c000-6000000000030000 rw-p 600000000000c000 00:00 0          [heap]
> 600007fffffa8000-600007fffffac000 rw-p 600007fffffa8000 00:00 0 
> 60000ffffff50000-60000ffffffa4000 rw-p 60000ffffffa8000 00:00 0          [stack]
> a000000000000000-a000000000020000 r-xp 00000000 00:00 0                  [vdso]
> 
> I think we can make that generic and get rid of that ifdef:
> 
>  static void setup1(struct test_case_t *test)
>  {
> -#ifdef __ia64__
> -       test->len = getpagesize() + 1;
> -#else
> -       *test->addr = NULL;
> -#endif
> +       /* find some unmapped area */
> +       test->addr = mmap(NULL, getpagesize(), PROT_NONE,
> +                MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +       if (test->addr == MAP_FAILED)
> +               tst_brkm(TBROK | TERRNO, cleanup, "mmap");
> +       if (munmap(test->addr, getpagesize()) < 0)
> +               tst_brkm(TBROK | TERRNO, cleanup, "munmap");
>  }
> 
> Works for me on x86 and ia64. I can post it afterwards (rebased to latest
> version of your patches).
> 

Using SAFE_MACRO() will look well.
 {
-#ifdef __ia64__
-       test->len = getpagesize() + 1;
-#else
-       *test->addr = NULL;
-#endif
+       test->addr = SAFE_MMAP(cleanup, NULL, getpagesize(), PROT_NONE,
+                              MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 }

And thanks a lot.
Best regards,
Zeng


> Regards,
> Jan
> 



------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT
  2014-02-20 13:03                 ` Jan Stancek
  2014-02-21  9:03                   ` Zeng Linggang
@ 2014-03-03  7:47                   ` Zeng Linggang
  2014-03-03  7:50                     ` [LTP] [PATCH v4 2/3] mlock/mlock02.c: cleanup Zeng Linggang
  2014-03-03  7:51                     ` [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  1 sibling, 2 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03  7:47 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 include/safe_macros.h | 10 ++++++++++
 lib/safe_macros.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/include/safe_macros.h b/include/safe_macros.h
index 207affc..bdd974c 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -196,5 +196,15 @@ int safe_lstat(const char *file, const int lineno, void (cleanup_fn)(void),
 #define SAFE_LSTAT(cleanup_fn, path, buf) \
 	safe_lstat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
 
+int safe_getrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, struct rlimit *rlim);
+#define SAFE_GETRLIMIT(cleanup_fn, resource, rlim) \
+	safe_getrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
+
+int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, const struct rlimit *rlim);
+#define SAFE_SETRLIMIT(cleanup_fn, resource, rlim) \
+	safe_setrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
+
 #endif /* __SAFE_MACROS_H__ */
 #endif /* __TEST_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index ad24195..e8795ef 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -500,3 +500,33 @@ int safe_lstat(const char *file, const int lineno,
 
 	return rval;
 }
+
+int safe_getrlimit(const char *file, const int lineno,
+		   void (cleanup_fn)(void), int resource, struct rlimit *rlim)
+{
+	int rval;
+
+	rval = getrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "getrlimit failed at %s:%d", file, lineno);
+	}
+
+	return rval;
+}
+
+int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, const struct rlimit *rlim)
+{
+	int rval;
+
+	rval = setrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "setrlimit failed at %s:%d", file, lineno);
+	}
+
+	return rval;
+}
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v4 2/3] mlock/mlock02.c: cleanup
  2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
@ 2014-03-03  7:50                     ` Zeng Linggang
  2014-03-03  7:51                     ` [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  1 sibling, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03  7:50 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

* Delete some useless commtents.
* Use SAFE_* macros.
* Some cleanup.

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 172 ++++++++++++++----------------
 1 file changed, 83 insertions(+), 89 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index edd9e45..a6f99e6 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -1,6 +1,6 @@
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
+ *	06/2002 Written by Paul Larson
  *
  *   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
@@ -13,70 +13,41 @@
  *   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 to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   along with this program;  if not, write to the Free Software Foundation,
+ *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /*
- * NAME
- * 	mlock02.c
- *
- * DESCRIPTION
- * 	Test to see the proper errors are returned by mlock
- *$
- * ALGORITHM
- * 	test 1:
- *		Call mlock with a NULL address.  ENOMEM should be returned
- *
- * USAGE:  <for command-line>
- *         -c n    Run n copies concurrently
- *         -e      Turn on errno logging
- *         -f      Turn off functional testing
- *         -h      Show this help screen
- *         -i n    Execute test n times
- *         -I x    Execute test for x seconds
- *         -p      Pause for SIGUSR1 before starting
- *         -P x    Pause for x seconds between iterations
- *         -t      Turn on syscall timing
- *
- * HISTORY
- *	06/2002 Written by Paul Larson
- *
- * RESTRICTIONS
- * 	None
+ * Test Description:
+ *  Verify that,
+ *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
+ *      if some of the specified address range does not correspond to
+ *      mapped pages in the address space of the process.
  */
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+
 #include "test.h"
 #include "usctest.h"
-
-void setup();
-void setup1();
-void cleanup();
+#include "safe_macros.h"
 
 char *TCID = "mlock02";
-int TST_TOTAL = 1;
 
-int exp_enos[] = { ENOMEM, 0 };
+#if !defined(UCLINUX)
 
-void *addr1;
+static void setup(void);
+static void cleanup(void);
+static void test_enomem1(void);
+static void mlock_verify(const void *, const size_t, const int);
 
-struct test_case_t {
-	void **addr;
-	int len;
-	int error;
-	void (*setupfunc) ();
-} TC[] = {
-	/* mlock should return ENOMEM when some or all of the address
-	 * range pointed to by addr and len are not valid mapped pages
-	 * in the address space of the process
-	 */
-	{
-	&addr1, 1024, ENOMEM, setup1}
-};
+static size_t len;
 
-#if !defined(UCLINUX)
+static void (*test_func[])(void) = { test_enomem1 };
+
+int TST_TOTAL = ARRAY_SIZE(test_func);
+static int exp_enos[] = { ENOMEM, 0 };
 
 int main(int ac, char **av)
 {
@@ -91,62 +62,85 @@ int main(int ac, char **av)
 	TEST_EXP_ENOS(exp_enos);
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
 		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (TC[i].setupfunc != NULL)
-				TC[i].setupfunc();
-
-			TEST(mlock(*(TC[i].addr), TC[i].len));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO != TC[i].error)
-					tst_brkm(TFAIL | TTERRNO, cleanup,
-						 "mlock didn't fail as expected; "
-						 "expected - %d : %s",
-						 TC[i].error,
-						 strerror(TC[i].error));
-				else
-					tst_resm(TPASS | TTERRNO,
-						 "mlock failed as expected");
-			} else
-				tst_brkm(TFAIL, cleanup,
-					 "mlock succeeded unexpectedly");
-		}
+		for (i = 0; i < TST_TOTAL; i++)
+			(*test_func[i])();
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-#else
-
-int main()
+static void setup(void)
 {
-	tst_brkm(TCONF, NULL, "test is not available on uClinux");
-}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-#endif /* if !defined(UCLINUX) */
+	TEST_PAUSE;
+
+	len = getpagesize();
+}
 
-void setup()
+static void test_enomem1(void)
 {
-	TEST_PAUSE;
+	void *addr;
+	struct rlimit rl;
+
+	/*
+	 * RLIMIT_MEMLOCK resource limit.
+	 * In Linux kernels before 2.6.9, this limit controlled the amount
+	 * of  memory that could be locked by a privileged process. Since
+	 * Linux 2.6.9, no limits are placed on the amount of memory that a
+	 * privileged process may lock, and this limit instead governs the
+	 * amount of memory that an unprivileged process may lock. So here
+	 * we set RLIMIT_MEMLOCK resource limit to RLIM_INFINITY when kernel
+	 * is under 2.6.9, to make sure this ENOMEM error is indeed caused by
+	 * that some of the specified address range does not correspond to
+	 * mapped pages in the address space of the process.
+	 */
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		rl.rlim_cur = RLIM_INFINITY;
+		rl.rlim_max = RLIM_INFINITY;
+		SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+	}
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_NONE,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	mlock_verify(addr, len, ENOMEM);
 }
 
-void setup1()
+static void mlock_verify(const void *addr, const size_t len, const int error)
 {
-#ifdef __ia64__
-	TC[0].len = getpagesize() + 1;
-#else
-	addr1 = NULL;
-#endif
+	TEST(mlock(addr, len));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "mlock succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != error) {
+		tst_resm(TFAIL | TTERRNO,
+			 "mlock didn't fail as expected; expected - %d : %s",
+			 error, strerror(error));
+	} else {
+		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
+	}
 }
 
-void cleanup()
+static void cleanup(void)
 {
 	TEST_CLEANUP;
+}
 
+#else
+
+int TST_TOTAL = 1;
+
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "test is not available on uClinux");
 }
+
+#endif /* if !defined(UCLINUX) */
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  2014-03-03  7:50                     ` [LTP] [PATCH v4 2/3] mlock/mlock02.c: cleanup Zeng Linggang
@ 2014-03-03  7:51                     ` Zeng Linggang
  2014-03-03  9:07                       ` Jan Stancek
  1 sibling, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03  7:51 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Add EPERM and ENOMEM errno tests for mlock(2).

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 82 ++++++++++++++++++++++++++++++-
 1 file changed, 80 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index a6f99e6..b7579ac 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -23,11 +23,20 @@
  *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
  *      if some of the specified address range does not correspond to
  *      mapped pages in the address space of the process.
+ *   2. mlock() fails with -1 return value and sets errno to ENOMEM,
+ *      if (Linux  2.6.9  and  later)  the caller had a non-zero RLIMIT_MEMLOCK
+ *      soft resource limit, but tried to lock more memory than the limit
+ *      permitted.  This limit is not enforced if the process is privileged
+ *      (CAP_IPC_LOCK).
+ *   3. mlock() fails with -1 return value and sets errno to EPERM,
+ *      if (Linux 2.6.9 and later) the caller was not privileged (CAP_IPC_LOCK)
+ *      and its RLIMIT_MEMLOCK soft resource limit was 0.
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <pwd.h>
 
 #include "test.h"
 #include "usctest.h"
@@ -40,14 +49,18 @@ char *TCID = "mlock02";
 static void setup(void);
 static void cleanup(void);
 static void test_enomem1(void);
+static void test_enomem2(void);
+static void test_eperm(void);
 static void mlock_verify(const void *, const size_t, const int);
 
 static size_t len;
+static struct rlimit original;
+static struct passwd *ltpuser;
 
-static void (*test_func[])(void) = { test_enomem1 };
+static void (*test_func[])(void) = { test_enomem1, test_enomem2, test_eperm };
 
 int TST_TOTAL = ARRAY_SIZE(test_func);
-static int exp_enos[] = { ENOMEM, 0 };
+static int exp_enos[] = { ENOMEM, EPERM, 0 };
 
 int main(int ac, char **av)
 {
@@ -73,11 +86,17 @@ int main(int ac, char **av)
 
 static void setup(void)
 {
+	tst_require_root(NULL);
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	TEST_PAUSE;
 
+	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
+
 	len = getpagesize();
+
+	SAFE_GETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
 }
 
 static void test_enomem1(void)
@@ -111,6 +130,65 @@ static void test_enomem1(void)
 	mlock_verify(addr, len, ENOMEM);
 }
 
+static void test_enomem2(void)
+{
+	void *addr;
+	struct rlimit rl;
+
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		tst_resm(TCONF,
+			 "ENOMEM error value test for this condition needs "
+			 "kernel 2.6.9 or higher");
+		return;
+	}
+
+	rl.rlim_max = len;
+	rl.rlim_cur = len;
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_NONE,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	mlock_verify(&addr, len, ENOMEM);
+
+	SAFE_SETEUID(cleanup, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
+}
+
+static void test_eperm(void)
+{
+	void *addr;
+	struct rlimit rl;
+
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		tst_resm(TCONF,
+			 "EPERM error value test needs kernel 2.6.9 or higher");
+		return;
+	}
+
+	rl.rlim_max = 0;
+	rl.rlim_cur = 0;
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_NONE,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	mlock_verify(&addr, len, EPERM);
+
+	SAFE_SETEUID(cleanup, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
+}
+
 static void mlock_verify(const void *addr, const size_t len, const int error)
 {
 	TEST(mlock(addr, len));
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-03-03  7:51                     ` [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-03-03  9:07                       ` Jan Stancek
  2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Stancek @ 2014-03-03  9:07 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Monday, 3 March, 2014 8:51:19 AM
> Subject: [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> 
> Add EPERM and ENOMEM errno tests for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 82
>  ++++++++++++++++++++++++++++++-
>  1 file changed, 80 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index a6f99e6..b7579ac 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -23,11 +23,20 @@
>   *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
>   *      if some of the specified address range does not correspond to
>   *      mapped pages in the address space of the process.
> + *   2. mlock() fails with -1 return value and sets errno to ENOMEM,
> + *      if (Linux  2.6.9  and  later)  the caller had a non-zero
> RLIMIT_MEMLOCK
> + *      soft resource limit, but tried to lock more memory than the limit
> + *      permitted.  This limit is not enforced if the process is privileged
> + *      (CAP_IPC_LOCK).
> + *   3. mlock() fails with -1 return value and sets errno to EPERM,
> + *      if (Linux 2.6.9 and later) the caller was not privileged
> (CAP_IPC_LOCK)
> + *      and its RLIMIT_MEMLOCK soft resource limit was 0.
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  
>  #include "test.h"
>  #include "usctest.h"
> @@ -40,14 +49,18 @@ char *TCID = "mlock02";
>  static void setup(void);
>  static void cleanup(void);
>  static void test_enomem1(void);
> +static void test_enomem2(void);
> +static void test_eperm(void);
>  static void mlock_verify(const void *, const size_t, const int);
>  
>  static size_t len;
> +static struct rlimit original;
> +static struct passwd *ltpuser;
>  
> -static void (*test_func[])(void) = { test_enomem1 };
> +static void (*test_func[])(void) = { test_enomem1, test_enomem2, test_eperm
> };
>  
>  int TST_TOTAL = ARRAY_SIZE(test_func);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  int main(int ac, char **av)
>  {
> @@ -73,11 +86,17 @@ int main(int ac, char **av)
>  
>  static void setup(void)
>  {
> +	tst_require_root(NULL);
> +
>  	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>  
>  	TEST_PAUSE;
>  
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
> +
>  	len = getpagesize();
> +
> +	SAFE_GETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
>  }
>  
>  static void test_enomem1(void)
> @@ -111,6 +130,65 @@ static void test_enomem1(void)
>  	mlock_verify(addr, len, ENOMEM);
>  }
>  
> +static void test_enomem2(void)
> +{
> +	void *addr;
> +	struct rlimit rl;
> +
> +	if ((tst_kvercmp(2, 6, 9)) < 0) {
> +		tst_resm(TCONF,
> +			 "ENOMEM error value test for this condition needs "
> +			 "kernel 2.6.9 or higher");
> +		return;
> +	}
> +

Hi,



> +	rl.rlim_max = len;
> +	rl.rlim_cur = len;

"tried to  lock  more  memory than the limit permitted."
You currently allow as much as you try to mlock.

I'd suggest:
-       rl.rlim_max = len;
-       rl.rlim_cur = len;
+       rl.rlim_max = len - 1;
+       rl.rlim_cur = len - 1;


> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
> +
> +	addr = SAFE_MMAP(cleanup, NULL, len, PROT_NONE,

Trying to mlock PROT_NONE will give you ENOMEM no matter how big the limit is.
I suggest PROT_READ here.

> +			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	mlock_verify(&addr, len, ENOMEM);

This is trying to mlock memory location of "addr" variable,
not the area that was just allocated with mmap.

Notice the address passed to mlock() in this strace output:
  mmap(NULL, 4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) = 0x7fe39bfbc000
  setresuid(-1, 99, -1)                   = 0
  mlock(0x7fff721089a8, 4096)             = -1 ENOMEM (Cannot allocate memory)

-       mlock_verify(&addr, len, ENOMEM);
+       mlock_verify(addr, len, ENOMEM);

> +
> +	SAFE_SETEUID(cleanup, 0);
> +
> +	SAFE_MUNMAP(cleanup, addr, len);
> +
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
> +}
> +
> +static void test_eperm(void)
> +{
> +	void *addr;
> +	struct rlimit rl;
> +
> +	if ((tst_kvercmp(2, 6, 9)) < 0) {
> +		tst_resm(TCONF,
> +			 "EPERM error value test needs kernel 2.6.9 or higher");
> +		return;
> +	}
> +
> +	rl.rlim_max = 0;
> +	rl.rlim_cur = 0;
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
> +
> +	addr = SAFE_MMAP(cleanup, NULL, len, PROT_NONE,
> +			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	mlock_verify(&addr, len, EPERM);

Same as in test_enomem2():
-       mlock_verify(&addr, len, EPERM);
+       mlock_verify(addr, len, EPERM);

Regards,
Jan

> +
> +	SAFE_SETEUID(cleanup, 0);
> +
> +	SAFE_MUNMAP(cleanup, addr, len);
> +
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
> +}
> +
>  static void mlock_verify(const void *addr, const size_t len, const int
>  error)
>  {
>  	TEST(mlock(addr, len));
> --
> 1.8.4.2
> 
> 
> 
> 

------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT
  2014-03-03  9:07                       ` Jan Stancek
@ 2014-03-03 11:22                         ` Zeng Linggang
  2014-03-03 11:23                           ` [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup Zeng Linggang
                                             ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03 11:22 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 include/safe_macros.h | 10 ++++++++++
 lib/safe_macros.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/include/safe_macros.h b/include/safe_macros.h
index 207affc..bdd974c 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -196,5 +196,15 @@ int safe_lstat(const char *file, const int lineno, void (cleanup_fn)(void),
 #define SAFE_LSTAT(cleanup_fn, path, buf) \
 	safe_lstat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
 
+int safe_getrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, struct rlimit *rlim);
+#define SAFE_GETRLIMIT(cleanup_fn, resource, rlim) \
+	safe_getrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
+
+int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, const struct rlimit *rlim);
+#define SAFE_SETRLIMIT(cleanup_fn, resource, rlim) \
+	safe_setrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
+
 #endif /* __SAFE_MACROS_H__ */
 #endif /* __TEST_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index ad24195..e8795ef 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -500,3 +500,33 @@ int safe_lstat(const char *file, const int lineno,
 
 	return rval;
 }
+
+int safe_getrlimit(const char *file, const int lineno,
+		   void (cleanup_fn)(void), int resource, struct rlimit *rlim)
+{
+	int rval;
+
+	rval = getrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "getrlimit failed at %s:%d", file, lineno);
+	}
+
+	return rval;
+}
+
+int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
+		   int resource, const struct rlimit *rlim)
+{
+	int rval;
+
+	rval = setrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "setrlimit failed at %s:%d", file, lineno);
+	}
+
+	return rval;
+}
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup
  2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
@ 2014-03-03 11:23                           ` Zeng Linggang
  2014-03-03 11:25                           ` [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
  2014-03-04  5:33                           ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Wanlong Gao
  2 siblings, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03 11:23 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

* Delete some useless commtents.
* Use SAFE_* macros.
* Some cleanup.

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 172 ++++++++++++++----------------
 1 file changed, 83 insertions(+), 89 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index edd9e45..95c499d 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -1,6 +1,6 @@
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
+ *	06/2002 Written by Paul Larson
  *
  *   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
@@ -13,70 +13,41 @@
  *   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 to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   along with this program;  if not, write to the Free Software Foundation,
+ *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /*
- * NAME
- * 	mlock02.c
- *
- * DESCRIPTION
- * 	Test to see the proper errors are returned by mlock
- *$
- * ALGORITHM
- * 	test 1:
- *		Call mlock with a NULL address.  ENOMEM should be returned
- *
- * USAGE:  <for command-line>
- *         -c n    Run n copies concurrently
- *         -e      Turn on errno logging
- *         -f      Turn off functional testing
- *         -h      Show this help screen
- *         -i n    Execute test n times
- *         -I x    Execute test for x seconds
- *         -p      Pause for SIGUSR1 before starting
- *         -P x    Pause for x seconds between iterations
- *         -t      Turn on syscall timing
- *
- * HISTORY
- *	06/2002 Written by Paul Larson
- *
- * RESTRICTIONS
- * 	None
+ * Test Description:
+ *  Verify that,
+ *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
+ *      if some of the specified address range does not correspond to
+ *      mapped pages in the address space of the process.
  */
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+
 #include "test.h"
 #include "usctest.h"
-
-void setup();
-void setup1();
-void cleanup();
+#include "safe_macros.h"
 
 char *TCID = "mlock02";
-int TST_TOTAL = 1;
 
-int exp_enos[] = { ENOMEM, 0 };
+#if !defined(UCLINUX)
 
-void *addr1;
+static void setup(void);
+static void cleanup(void);
+static void test_enomem1(void);
+static void mlock_verify(const void *, const size_t, const int);
 
-struct test_case_t {
-	void **addr;
-	int len;
-	int error;
-	void (*setupfunc) ();
-} TC[] = {
-	/* mlock should return ENOMEM when some or all of the address
-	 * range pointed to by addr and len are not valid mapped pages
-	 * in the address space of the process
-	 */
-	{
-	&addr1, 1024, ENOMEM, setup1}
-};
+static size_t len;
 
-#if !defined(UCLINUX)
+static void (*test_func[])(void) = { test_enomem1 };
+
+int TST_TOTAL = ARRAY_SIZE(test_func);
+static int exp_enos[] = { ENOMEM, 0 };
 
 int main(int ac, char **av)
 {
@@ -91,62 +62,85 @@ int main(int ac, char **av)
 	TEST_EXP_ENOS(exp_enos);
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
 		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (TC[i].setupfunc != NULL)
-				TC[i].setupfunc();
-
-			TEST(mlock(*(TC[i].addr), TC[i].len));
-
-			if (TEST_RETURN == -1) {
-				if (TEST_ERRNO != TC[i].error)
-					tst_brkm(TFAIL | TTERRNO, cleanup,
-						 "mlock didn't fail as expected; "
-						 "expected - %d : %s",
-						 TC[i].error,
-						 strerror(TC[i].error));
-				else
-					tst_resm(TPASS | TTERRNO,
-						 "mlock failed as expected");
-			} else
-				tst_brkm(TFAIL, cleanup,
-					 "mlock succeeded unexpectedly");
-		}
+		for (i = 0; i < TST_TOTAL; i++)
+			(*test_func[i])();
 	}
 
 	cleanup();
-
 	tst_exit();
 }
 
-#else
-
-int main()
+static void setup(void)
 {
-	tst_brkm(TCONF, NULL, "test is not available on uClinux");
-}
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
-#endif /* if !defined(UCLINUX) */
+	TEST_PAUSE;
+
+	len = getpagesize();
+}
 
-void setup()
+static void test_enomem1(void)
 {
-	TEST_PAUSE;
+	void *addr;
+	struct rlimit rl;
+
+	/*
+	 * RLIMIT_MEMLOCK resource limit.
+	 * In Linux kernels before 2.6.9, this limit controlled the amount
+	 * of  memory that could be locked by a privileged process. Since
+	 * Linux 2.6.9, no limits are placed on the amount of memory that a
+	 * privileged process may lock, and this limit instead governs the
+	 * amount of memory that an unprivileged process may lock. So here
+	 * we set RLIMIT_MEMLOCK resource limit to RLIM_INFINITY when kernel
+	 * is under 2.6.9, to make sure this ENOMEM error is indeed caused by
+	 * that some of the specified address range does not correspond to
+	 * mapped pages in the address space of the process.
+	 */
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		rl.rlim_cur = RLIM_INFINITY;
+		rl.rlim_max = RLIM_INFINITY;
+		SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+	}
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_READ,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	mlock_verify(addr, len, ENOMEM);
 }
 
-void setup1()
+static void mlock_verify(const void *addr, const size_t len, const int error)
 {
-#ifdef __ia64__
-	TC[0].len = getpagesize() + 1;
-#else
-	addr1 = NULL;
-#endif
+	TEST(mlock(addr, len));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "mlock succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO != error) {
+		tst_resm(TFAIL | TTERRNO,
+			 "mlock didn't fail as expected; expected - %d : %s",
+			 error, strerror(error));
+	} else {
+		tst_resm(TPASS | TTERRNO, "mlock failed as expected");
+	}
 }
 
-void cleanup()
+static void cleanup(void)
 {
 	TEST_CLEANUP;
+}
 
+#else
+
+int TST_TOTAL = 1;
+
+int main(void)
+{
+	tst_brkm(TCONF, NULL, "test is not available on uClinux");
 }
+
+#endif /* if !defined(UCLINUX) */
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  2014-03-03 11:23                           ` [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup Zeng Linggang
@ 2014-03-03 11:25                           ` Zeng Linggang
  2014-03-03 20:28                             ` Jan Stancek
  2014-03-04  5:33                           ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Wanlong Gao
  2 siblings, 1 reply; 24+ messages in thread
From: Zeng Linggang @ 2014-03-03 11:25 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

Add EPERM and ENOMEM errno tests for mlock(2).

Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mlock/mlock02.c | 82 ++++++++++++++++++++++++++++++-
 1 file changed, 80 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/mlock/mlock02.c b/testcases/kernel/syscalls/mlock/mlock02.c
index 95c499d..bfed030 100644
--- a/testcases/kernel/syscalls/mlock/mlock02.c
+++ b/testcases/kernel/syscalls/mlock/mlock02.c
@@ -23,11 +23,20 @@
  *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
  *      if some of the specified address range does not correspond to
  *      mapped pages in the address space of the process.
+ *   2. mlock() fails with -1 return value and sets errno to ENOMEM,
+ *      if (Linux  2.6.9  and  later)  the caller had a non-zero RLIMIT_MEMLOCK
+ *      soft resource limit, but tried to lock more memory than the limit
+ *      permitted.  This limit is not enforced if the process is privileged
+ *      (CAP_IPC_LOCK).
+ *   3. mlock() fails with -1 return value and sets errno to EPERM,
+ *      if (Linux 2.6.9 and later) the caller was not privileged (CAP_IPC_LOCK)
+ *      and its RLIMIT_MEMLOCK soft resource limit was 0.
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <pwd.h>
 
 #include "test.h"
 #include "usctest.h"
@@ -40,14 +49,18 @@ char *TCID = "mlock02";
 static void setup(void);
 static void cleanup(void);
 static void test_enomem1(void);
+static void test_enomem2(void);
+static void test_eperm(void);
 static void mlock_verify(const void *, const size_t, const int);
 
 static size_t len;
+static struct rlimit original;
+static struct passwd *ltpuser;
 
-static void (*test_func[])(void) = { test_enomem1 };
+static void (*test_func[])(void) = { test_enomem1, test_enomem2, test_eperm };
 
 int TST_TOTAL = ARRAY_SIZE(test_func);
-static int exp_enos[] = { ENOMEM, 0 };
+static int exp_enos[] = { ENOMEM, EPERM, 0 };
 
 int main(int ac, char **av)
 {
@@ -73,11 +86,17 @@ int main(int ac, char **av)
 
 static void setup(void)
 {
+	tst_require_root(NULL);
+
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
 	TEST_PAUSE;
 
+	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
+
 	len = getpagesize();
+
+	SAFE_GETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
 }
 
 static void test_enomem1(void)
@@ -111,6 +130,65 @@ static void test_enomem1(void)
 	mlock_verify(addr, len, ENOMEM);
 }
 
+static void test_enomem2(void)
+{
+	void *addr;
+	struct rlimit rl;
+
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		tst_resm(TCONF,
+			 "ENOMEM error value test for this condition needs "
+			 "kernel 2.6.9 or higher");
+		return;
+	}
+
+	rl.rlim_max = len - 1;
+	rl.rlim_cur = len - 1;
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_READ,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	mlock_verify(addr, len, ENOMEM);
+
+	SAFE_SETEUID(cleanup, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
+}
+
+static void test_eperm(void)
+{
+	void *addr;
+	struct rlimit rl;
+
+	if ((tst_kvercmp(2, 6, 9)) < 0) {
+		tst_resm(TCONF,
+			 "EPERM error value test needs kernel 2.6.9 or higher");
+		return;
+	}
+
+	rl.rlim_max = 0;
+	rl.rlim_cur = 0;
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
+
+	addr = SAFE_MMAP(cleanup, NULL, len, PROT_READ,
+			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	mlock_verify(addr, len, EPERM);
+
+	SAFE_SETEUID(cleanup, 0);
+
+	SAFE_MUNMAP(cleanup, addr, len);
+
+	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
+}
+
 static void mlock_verify(const void *addr, const size_t len, const int error)
 {
 	TEST(mlock(addr, len));
-- 
1.8.4.2




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-03-03 11:25                           ` [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-03-03 20:28                             ` Jan Stancek
  2014-03-04  5:02                               ` Zeng Linggang
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Stancek @ 2014-03-03 20:28 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list



----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Monday, 3 March, 2014 12:25:09 PM
> Subject: [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> 
> Add EPERM and ENOMEM errno tests for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>

Looks good to me.

Reviewed-by: Jan Stancek <jstancek@redhat.com>

> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 82
>  ++++++++++++++++++++++++++++++-
>  1 file changed, 80 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index 95c499d..bfed030 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -23,11 +23,20 @@
>   *   1. mlock() fails with -1 return value and sets errno to ENOMEM,
>   *      if some of the specified address range does not correspond to
>   *      mapped pages in the address space of the process.
> + *   2. mlock() fails with -1 return value and sets errno to ENOMEM,
> + *      if (Linux  2.6.9  and  later)  the caller had a non-zero
> RLIMIT_MEMLOCK
> + *      soft resource limit, but tried to lock more memory than the limit
> + *      permitted.  This limit is not enforced if the process is privileged
> + *      (CAP_IPC_LOCK).
> + *   3. mlock() fails with -1 return value and sets errno to EPERM,
> + *      if (Linux 2.6.9 and later) the caller was not privileged
> (CAP_IPC_LOCK)
> + *      and its RLIMIT_MEMLOCK soft resource limit was 0.
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  
>  #include "test.h"
>  #include "usctest.h"
> @@ -40,14 +49,18 @@ char *TCID = "mlock02";
>  static void setup(void);
>  static void cleanup(void);
>  static void test_enomem1(void);
> +static void test_enomem2(void);
> +static void test_eperm(void);
>  static void mlock_verify(const void *, const size_t, const int);
>  
>  static size_t len;
> +static struct rlimit original;
> +static struct passwd *ltpuser;
>  
> -static void (*test_func[])(void) = { test_enomem1 };
> +static void (*test_func[])(void) = { test_enomem1, test_enomem2, test_eperm
> };
>  
>  int TST_TOTAL = ARRAY_SIZE(test_func);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  int main(int ac, char **av)
>  {
> @@ -73,11 +86,17 @@ int main(int ac, char **av)
>  
>  static void setup(void)
>  {
> +	tst_require_root(NULL);
> +
>  	tst_sig(NOFORK, DEF_HANDLER, cleanup);
>  
>  	TEST_PAUSE;
>  
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
> +
>  	len = getpagesize();
> +
> +	SAFE_GETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
>  }
>  
>  static void test_enomem1(void)
> @@ -111,6 +130,65 @@ static void test_enomem1(void)
>  	mlock_verify(addr, len, ENOMEM);
>  }
>  
> +static void test_enomem2(void)
> +{
> +	void *addr;
> +	struct rlimit rl;
> +
> +	if ((tst_kvercmp(2, 6, 9)) < 0) {
> +		tst_resm(TCONF,
> +			 "ENOMEM error value test for this condition needs "
> +			 "kernel 2.6.9 or higher");
> +		return;
> +	}
> +
> +	rl.rlim_max = len - 1;
> +	rl.rlim_cur = len - 1;
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
> +
> +	addr = SAFE_MMAP(cleanup, NULL, len, PROT_READ,
> +			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	mlock_verify(addr, len, ENOMEM);
> +
> +	SAFE_SETEUID(cleanup, 0);
> +
> +	SAFE_MUNMAP(cleanup, addr, len);
> +
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
> +}
> +
> +static void test_eperm(void)
> +{
> +	void *addr;
> +	struct rlimit rl;
> +
> +	if ((tst_kvercmp(2, 6, 9)) < 0) {
> +		tst_resm(TCONF,
> +			 "EPERM error value test needs kernel 2.6.9 or higher");
> +		return;
> +	}
> +
> +	rl.rlim_max = 0;
> +	rl.rlim_cur = 0;
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &rl);
> +
> +	addr = SAFE_MMAP(cleanup, NULL, len, PROT_READ,
> +			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	mlock_verify(addr, len, EPERM);
> +
> +	SAFE_SETEUID(cleanup, 0);
> +
> +	SAFE_MUNMAP(cleanup, addr, len);
> +
> +	SAFE_SETRLIMIT(cleanup, RLIMIT_MEMLOCK, &original);
> +}
> +
>  static void mlock_verify(const void *addr, const size_t len, const int
>  error)
>  {
>  	TEST(mlock(addr, len));
> --
> 1.8.4.2
> 
> 
> 
> 

------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
  2014-03-03 20:28                             ` Jan Stancek
@ 2014-03-04  5:02                               ` Zeng Linggang
  0 siblings, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-04  5:02 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp-list

On Mon, 2014-03-03 at 15:28 -0500, Jan Stancek wrote:
> 
> ----- Original Message -----
> > From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> > To: "Jan Stancek" <jstancek@redhat.com>
> > Cc: "ltp-list" <ltp-list@lists.sourceforge.net>
> > Sent: Monday, 3 March, 2014 12:25:09 PM
> > Subject: [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests
> > 
> > Add EPERM and ENOMEM errno tests for mlock(2).
> > 
> > Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> 
> Looks good to me.
> 

Thanks for your review. :)
Best regards,
Zeng

> Reviewed-by: Jan Stancek <jstancek@redhat.com>
> 
> > ---



------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT
  2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
  2014-03-03 11:23                           ` [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup Zeng Linggang
  2014-03-03 11:25                           ` [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
@ 2014-03-04  5:33                           ` Wanlong Gao
  2014-03-04  6:17                             ` Zeng Linggang
  2 siblings, 1 reply; 24+ messages in thread
From: Wanlong Gao @ 2014-03-04  5:33 UTC (permalink / raw)
  To: Zeng Linggang; +Cc: ltp-list

On 03/03/2014 07:22 PM, Zeng Linggang wrote:
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> ---
>  include/safe_macros.h | 10 ++++++++++
>  lib/safe_macros.c     | 30 ++++++++++++++++++++++++++++++
>  2 files changed, 40 insertions(+)


Applied the series, thank you.

Wanlong Gao


------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT
  2014-03-04  5:33                           ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Wanlong Gao
@ 2014-03-04  6:17                             ` Zeng Linggang
  0 siblings, 0 replies; 24+ messages in thread
From: Zeng Linggang @ 2014-03-04  6:17 UTC (permalink / raw)
  To: gaowanlong; +Cc: ltp-list

On Tue, 2014-03-04 at 13:33 +0800, Wanlong Gao wrote:
> On 03/03/2014 07:22 PM, Zeng Linggang wrote:
> > Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> > ---
> >  include/safe_macros.h | 10 ++++++++++
> >  lib/safe_macros.c     | 30 ++++++++++++++++++++++++++++++
> >  2 files changed, 40 insertions(+)
> 
> 
> Applied the series, thank you.
> 

:) Thank you very much.

Best regards,
Zeng

> Wanlong Gao
> 



------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2014-03-04  6:17 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-14 10:10 [LTP] [PATCH] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-14 10:12 ` [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test Zeng Linggang
2014-02-14 10:54   ` Jan Stancek
2014-02-19  9:38     ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-19  9:40       ` [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-19 11:29         ` Jan Stancek
2014-02-20  9:40           ` [LTP] [PATCH v3 1/2] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-20  9:50             ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-20 11:05               ` Jan Stancek
2014-02-20 13:03                 ` Jan Stancek
2014-02-21  9:03                   ` Zeng Linggang
2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
2014-03-03  7:50                     ` [LTP] [PATCH v4 2/3] mlock/mlock02.c: cleanup Zeng Linggang
2014-03-03  7:51                     ` [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-03-03  9:07                       ` Jan Stancek
2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
2014-03-03 11:23                           ` [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup Zeng Linggang
2014-03-03 11:25                           ` [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-03-03 20:28                             ` Jan Stancek
2014-03-04  5:02                               ` Zeng Linggang
2014-03-04  5:33                           ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Wanlong Gao
2014-03-04  6:17                             ` Zeng Linggang
2014-02-21  1:04                 ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-19 11:02       ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Jan Stancek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.