All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls: implemented a test for sched_setaffinity() error values
@ 2014-04-15 12:55 Stanislav Kholmanskikh
  2014-04-28  7:56 ` [LTP] [PATCH V2] " Stanislav Kholmanskikh
  2014-05-19 14:39 ` [LTP] [PATCH] " chrubis
  0 siblings, 2 replies; 5+ messages in thread
From: Stanislav Kholmanskikh @ 2014-04-15 12:55 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko

This test verifies sched_setaffinity(2) for all error conditions
to occur correctly.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
Hi!

This patch is not a candidate for this release.

I found that
1) testcases/kernel/sched/hyperthreading/ht_affinity is not specific to 
   the Intel HyperThreading technology. It's just a test for sched_setaffinity()
2) testcases/kernel/syscalls doesn't contain any tests for sched_setaffinity().

This is a test case for sched_setaffinity() errors checking.

Actually, I've not found a better way of checking that ncpus < CPU_SETSIZE than
implementing this check as a part of CPU_ALLOC macro :(

The next on the queue is to convert ht_affinity into a plain C test case in
testcases/kernel/syscalls/sched_setaffinity directory.

Thank you.

 testcases/kernel/syscalls/.gitignore               |    1 +
 .../kernel/syscalls/sched_setaffinity/Makefile     |   23 +++
 .../syscalls/sched_setaffinity/sched_setaffinity.h |   57 ++++++
 .../sched_setaffinity/sched_setaffinity01.c        |  186 ++++++++++++++++++++
 4 files changed, 267 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/Makefile
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c

diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index d5c7bac..21c6cf1 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -719,6 +719,7 @@
 /sched_rr_get_interval/sched_rr_get_interval01
 /sched_rr_get_interval/sched_rr_get_interval02
 /sched_rr_get_interval/sched_rr_get_interval03
+/sched_setaffinity/sched_setaffinity01
 /sched_setparam/sched_setparam01
 /sched_setparam/sched_setparam02
 /sched_setparam/sched_setparam03
diff --git a/testcases/kernel/syscalls/sched_setaffinity/Makefile b/testcases/kernel/syscalls/sched_setaffinity/Makefile
new file mode 100644
index 0000000..2d825f1
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/Makefile
@@ -0,0 +1,23 @@
+#
+#  Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+#
+#  This program is free software;  you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+#  the GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program;  if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
new file mode 100644
index 0000000..a954ef1
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * Some old libcs (like glibc < 2.7) do not provide interfaces for
+ * dynamically sized cpu sets, but provide only static cpu_set_t type
+ * with no more than CPU_SETSIZE cpus in it.
+ *
+ * This file is a wrapper of the dynamic interfaces using the static ones.
+ *
+ * If the number of cpus available on the system is greater than
+ * CPU_SETSIZE, this interface will not work. Update libc in this case :)
+ */
+
+#ifndef __LTP_SCHED_SETAFFINITY_H__
+#define __LTP_SCHED_SETAFFINITY_H__
+
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	tst_brkm(TCONF, cleanup, \
+		"Your libc does not support masks with %ld cpus", ncpus); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr);
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t);
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask);
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask);
+#endif
+
+#endif /* __LTP_SCHED_SETAFFINITY_H__ */
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
new file mode 100644
index 0000000..8279714
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * This test verifies sched_setaffinity(2) for all error conditions
+ * to occur correctly.
+ *
+ * sched_setaffinity() returns -1 and sets the error code to:
+ *
+ * 1) EFAULT, if the supplied memory address is invalid
+ * 2) EINVAL, if the mask doesn't contain at least one
+ *    permitted cpu
+ * 3) ESRCH, if the process whose id is pid could not
+ *    be found
+ * 4) EPERM, if the calling process doesn't have appropriate
+ *    privileges
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sched.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "test.h"
+#include "usctest.h"
+#include "sched_setaffinity.h"
+
+char *TCID = "sched_setaffinity01";
+
+static cpu_set_t *mask, *emask;
+static cpu_set_t *fmask = (void *)-1;
+static size_t mask_size, emask_size;
+static pid_t self_pid, privileged_pid, free_pid;
+static uid_t uid;
+static long ncpus;
+
+static struct test_case_t {
+	pid_t *pid;
+	size_t *mask_size;
+	cpu_set_t **mask;
+	int exp_errno;
+} test_cases[] = {
+	{&self_pid, &mask_size, &fmask, EFAULT},
+	{&self_pid, &emask_size, &emask, EINVAL},
+	{&free_pid, &mask_size, &mask, ESRCH},
+	{&privileged_pid, &mask_size, &mask, EPERM},
+};
+
+int TST_TOTAL = ARRAY_SIZE(test_cases);
+
+static void cleanup(void)
+{
+	if (mask != NULL) {
+		CPU_FREE(mask);
+		mask = NULL;
+	}
+
+	if (emask != NULL) {
+		CPU_FREE(emask);
+		emask = NULL;
+	}
+
+	if (seteuid(uid) < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed", uid);
+
+	if (privileged_pid != 0) {
+		kill(privileged_pid, SIGUSR1);
+		waitpid(privileged_pid, NULL, 0);
+		privileged_pid = 0;
+	}
+}
+
+static void setup(void)
+{
+	/* Current mask */
+	mask = CPU_ALLOC(ncpus);
+	if (mask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus);
+	mask_size = CPU_ALLOC_SIZE(ncpus);
+	if (sched_getaffinity(0, mask_size, mask) < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "sched_getaffinity() failed");
+
+	/* Mask with one more cpu than available on the system */
+	emask = CPU_ALLOC(ncpus + 1);
+	if (emask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus + 1);
+	emask_size = CPU_ALLOC_SIZE(ncpus + 1);
+	CPU_ZERO_S(emask_size, emask);
+	CPU_SET_S(ncpus, emask_size, emask);
+
+	privileged_pid = fork();
+	if (privileged_pid == 0) {
+		sigset_t set;
+		struct timespec tv;
+
+		sigemptyset(&set);
+		sigaddset(&set, SIGUSR1);
+
+		tv.tv_sec = 30;
+		tv.tv_nsec = 0;
+
+		if (sigtimedwait(&set, NULL, &tv) < 0) {
+			if (errno == EAGAIN) {
+				printf("privileged child timeouted\n");
+				exit(1);
+			}
+		}
+
+		exit(0);
+	} else if (privileged_pid < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+	}
+
+	if (seteuid(uid + 1) < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "seteuid(%d) failed",
+			uid + 1);
+
+	/* we assume that this pid will not be used again
+	 * for some short period of time */
+	free_pid = fork();
+	if (free_pid == 0)
+		exit(0);
+	else if (free_pid < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+	waitpid(free_pid, NULL, 0);
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+	int i;
+
+	if ((msg = parse_opts(argc, argv, NULL, NULL)))
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	tst_require_root(NULL);
+	uid = geteuid();
+	ncpus = tst_ncpus_max();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+		setup();
+		for (i = 0; i < TST_TOTAL; i++) {
+			TEST(sched_setaffinity(*(test_cases[i].pid),
+						*(test_cases[i].mask_size),
+						*(test_cases[i].mask)));
+
+			if (TEST_RETURN != -1)
+				tst_resm(TFAIL,
+					"sched_setaffinity() unexpectedly succeded");
+
+			if (TEST_ERRNO == test_cases[i].exp_errno) {
+				tst_resm(TPASS, "expected failure with '%s'",
+					strerror(test_cases[i].exp_errno));
+			} else {
+				tst_resm(TFAIL,
+					"call returned '%s', expected - '%s'",
+					strerror(TEST_ERRNO),
+					strerror(test_cases[i].exp_errno));
+			}
+		}
+		cleanup();
+	}
+
+	tst_exit();
+}
-- 
1.7.1


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH V2] syscalls: implemented a test for sched_setaffinity() error values
  2014-04-15 12:55 [LTP] [PATCH] syscalls: implemented a test for sched_setaffinity() error values Stanislav Kholmanskikh
@ 2014-04-28  7:56 ` Stanislav Kholmanskikh
  2014-05-19 14:39 ` [LTP] [PATCH] " chrubis
  1 sibling, 0 replies; 5+ messages in thread
From: Stanislav Kholmanskikh @ 2014-04-28  7:56 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko

This test verifies sched_setaffinity(2) for all error conditions
to occur correctly.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
Changes:
* added sched_setaffinity01 to runtest/syscalls
* changed __LTP_SCHED_SETAFFINITY_H__ to LTP_SCHED_SETAFFINITY_H in sched_setaffinity.h
* added includes to sched_setaffinity.h
* made use of SAFE_SETEUID

 runtest/syscalls                                   |    1 +
 testcases/kernel/syscalls/.gitignore               |    1 +
 .../kernel/syscalls/sched_setaffinity/Makefile     |   23 +++
 .../syscalls/sched_setaffinity/sched_setaffinity.h |   60 +++++++
 .../sched_setaffinity/sched_setaffinity01.c        |  185 ++++++++++++++++++++
 5 files changed, 270 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/Makefile
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index fb3e59f..6c8106e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -897,6 +897,7 @@ sched_setscheduler02 sched_setscheduler02
 
 sched_yield01 sched_yield01
 
+sched_setaffinity01 sched_setaffinity01
 sched_getaffinity01 sched_getaffinity01
 
 select01 select01
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index d5c7bac..21c6cf1 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -719,6 +719,7 @@
 /sched_rr_get_interval/sched_rr_get_interval01
 /sched_rr_get_interval/sched_rr_get_interval02
 /sched_rr_get_interval/sched_rr_get_interval03
+/sched_setaffinity/sched_setaffinity01
 /sched_setparam/sched_setparam01
 /sched_setparam/sched_setparam02
 /sched_setparam/sched_setparam03
diff --git a/testcases/kernel/syscalls/sched_setaffinity/Makefile b/testcases/kernel/syscalls/sched_setaffinity/Makefile
new file mode 100644
index 0000000..2d825f1
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/Makefile
@@ -0,0 +1,23 @@
+#
+#  Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+#
+#  This program is free software;  you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+#  the GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program;  if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
new file mode 100644
index 0000000..8083894
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * Some old libcs (like glibc < 2.7) do not provide interfaces for
+ * dynamically sized cpu sets, but provide only static cpu_set_t type
+ * with no more than CPU_SETSIZE cpus in it.
+ *
+ * This file is a wrapper of the dynamic interfaces using the static ones.
+ *
+ * If the number of cpus available on the system is greater than
+ * CPU_SETSIZE, this interface will not work. Update libc in this case :)
+ */
+
+#define _GNU_SOURCE
+#include <sched.h>
+
+#ifndef LTP_SCHED_SETAFFINITY_H
+#define LTP_SCHED_SETAFFINITY_H
+
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	tst_brkm(TCONF, cleanup, \
+		"Your libc does not support masks with %ld cpus", ncpus); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr);
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t);
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask);
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask);
+#endif
+
+#endif /* LTP_SCHED_SETAFFINITY_H */
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
new file mode 100644
index 0000000..3b23445
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * This test verifies sched_setaffinity(2) for all error conditions
+ * to occur correctly.
+ *
+ * sched_setaffinity() returns -1 and sets the error code to:
+ *
+ * 1) EFAULT, if the supplied memory address is invalid
+ * 2) EINVAL, if the mask doesn't contain at least one
+ *    permitted cpu
+ * 3) ESRCH, if the process whose id is pid could not
+ *    be found
+ * 4) EPERM, if the calling process doesn't have appropriate
+ *    privileges
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <sched.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "test.h"
+#include "usctest.h"
+#include "safe_macros.h"
+#include "sched_setaffinity.h"
+
+char *TCID = "sched_setaffinity01";
+
+static cpu_set_t *mask, *emask;
+static cpu_set_t *fmask = (void *)-1;
+static size_t mask_size, emask_size;
+static pid_t self_pid, privileged_pid, free_pid;
+static uid_t uid;
+static long ncpus;
+
+static struct test_case_t {
+	pid_t *pid;
+	size_t *mask_size;
+	cpu_set_t **mask;
+	int exp_errno;
+} test_cases[] = {
+	{&self_pid, &mask_size, &fmask, EFAULT},
+	{&self_pid, &emask_size, &emask, EINVAL},
+	{&free_pid, &mask_size, &mask, ESRCH},
+	{&privileged_pid, &mask_size, &mask, EPERM},
+};
+
+int TST_TOTAL = ARRAY_SIZE(test_cases);
+
+static void cleanup(void)
+{
+	if (mask != NULL) {
+		CPU_FREE(mask);
+		mask = NULL;
+	}
+
+	if (emask != NULL) {
+		CPU_FREE(emask);
+		emask = NULL;
+	}
+
+	SAFE_SETEUID(NULL, uid);
+
+	if (privileged_pid != 0) {
+		kill(privileged_pid, SIGUSR1);
+		waitpid(privileged_pid, NULL, 0);
+		privileged_pid = 0;
+	}
+}
+
+static void setup(void)
+{
+	/* Current mask */
+	mask = CPU_ALLOC(ncpus);
+	if (mask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus);
+	mask_size = CPU_ALLOC_SIZE(ncpus);
+	if (sched_getaffinity(0, mask_size, mask) < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "sched_getaffinity() failed");
+
+	/* Mask with one more cpu than available on the system */
+	emask = CPU_ALLOC(ncpus + 1);
+	if (emask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus + 1);
+	emask_size = CPU_ALLOC_SIZE(ncpus + 1);
+	CPU_ZERO_S(emask_size, emask);
+	CPU_SET_S(ncpus, emask_size, emask);
+
+	privileged_pid = fork();
+	if (privileged_pid == 0) {
+		sigset_t set;
+		struct timespec tv;
+
+		sigemptyset(&set);
+		sigaddset(&set, SIGUSR1);
+
+		tv.tv_sec = 30;
+		tv.tv_nsec = 0;
+
+		if (sigtimedwait(&set, NULL, &tv) < 0) {
+			if (errno == EAGAIN) {
+				printf("privileged child timeouted\n");
+				exit(1);
+			}
+		}
+
+		exit(0);
+	} else if (privileged_pid < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+	}
+
+	SAFE_SETEUID(cleanup, uid + 1);
+
+	/* we assume that this pid will not be used again
+	 * for some short period of time */
+	free_pid = fork();
+	if (free_pid == 0)
+		exit(0);
+	else if (free_pid < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+	waitpid(free_pid, NULL, 0);
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+	int i;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	tst_require_root(NULL);
+	uid = geteuid();
+	ncpus = tst_ncpus_max();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+		setup();
+		for (i = 0; i < TST_TOTAL; i++) {
+			TEST(sched_setaffinity(*(test_cases[i].pid),
+						*(test_cases[i].mask_size),
+						*(test_cases[i].mask)));
+
+			if (TEST_RETURN != -1)
+				tst_resm(TFAIL,
+					"sched_setaffinity() unexpectedly succeded");
+
+			if (TEST_ERRNO == test_cases[i].exp_errno) {
+				tst_resm(TPASS, "expected failure with '%s'",
+					strerror(test_cases[i].exp_errno));
+			} else {
+				tst_resm(TFAIL,
+					"call returned '%s', expected - '%s'",
+					strerror(TEST_ERRNO),
+					strerror(test_cases[i].exp_errno));
+			}
+		}
+		cleanup();
+	}
+
+	tst_exit();
+}
-- 
1.7.1


------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.  Get 
unparalleled scalability from the best Selenium testing platform available.
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH] syscalls: implemented a test for sched_setaffinity() error values
  2014-04-15 12:55 [LTP] [PATCH] syscalls: implemented a test for sched_setaffinity() error values Stanislav Kholmanskikh
  2014-04-28  7:56 ` [LTP] [PATCH V2] " Stanislav Kholmanskikh
@ 2014-05-19 14:39 ` chrubis
  2014-06-17 14:32   ` [LTP] [PATCH V3] " Stanislav Kholmanskikh
  1 sibling, 1 reply; 5+ messages in thread
From: chrubis @ 2014-05-19 14:39 UTC (permalink / raw)
  To: Stanislav Kholmanskikh; +Cc: vasily.isaenko, ltp-list

Hi!
> This test verifies sched_setaffinity(2) for all error conditions
> to occur correctly.
> 
> Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
> ---
> Hi!
> 
> This patch is not a candidate for this release.
> 
> I found that
> 1) testcases/kernel/sched/hyperthreading/ht_affinity is not specific to 
>    the Intel HyperThreading technology. It's just a test for sched_setaffinity()
> 2) testcases/kernel/syscalls doesn't contain any tests for sched_setaffinity().
> 
> This is a test case for sched_setaffinity() errors checking.
> 
> Actually, I've not found a better way of checking that ncpus < CPU_SETSIZE than
> implementing this check as a part of CPU_ALLOC macro :(

I can see the problem, we need to do that check only and only if we use
fixed size cpuset. I think that it's fine where it is.

> The next on the queue is to convert ht_affinity into a plain C test case in
> testcases/kernel/syscalls/sched_setaffinity directory.
> 
> Thank you.
> 
>  testcases/kernel/syscalls/.gitignore               |    1 +
>  .../kernel/syscalls/sched_setaffinity/Makefile     |   23 +++
>  .../syscalls/sched_setaffinity/sched_setaffinity.h |   57 ++++++
>  .../sched_setaffinity/sched_setaffinity01.c        |  186 ++++++++++++++++++++
>  4 files changed, 267 insertions(+), 0 deletions(-)
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/Makefile
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> 
> diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
> index d5c7bac..21c6cf1 100644
> --- a/testcases/kernel/syscalls/.gitignore
> +++ b/testcases/kernel/syscalls/.gitignore
> @@ -719,6 +719,7 @@
>  /sched_rr_get_interval/sched_rr_get_interval01
>  /sched_rr_get_interval/sched_rr_get_interval02
>  /sched_rr_get_interval/sched_rr_get_interval03
> +/sched_setaffinity/sched_setaffinity01
>  /sched_setparam/sched_setparam01
>  /sched_setparam/sched_setparam02
>  /sched_setparam/sched_setparam03
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/Makefile b/testcases/kernel/syscalls/sched_setaffinity/Makefile
> new file mode 100644
> index 0000000..2d825f1
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/Makefile
> @@ -0,0 +1,23 @@
> +#
> +#  Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> +#
> +#  This program is free software;  you can redistribute it and/or modify
> +#  it under the terms of the GNU General Public License as published by
> +#  the Free Software Foundation; either version 2 of the License, or
> +#  (at your option) any later version.
> +#
> +#  This program is distributed in the hope that it will be useful,
> +#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
> +#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
> +#  the GNU General Public License for more details.
> +#
> +#  You should have received a copy of the GNU General Public License
> +#  along with this program;  if not, write to the Free Software
> +#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> +#
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
> new file mode 100644
> index 0000000..a954ef1
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
> @@ -0,0 +1,57 @@
> +/*
> + * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it would be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write the Free Software Foundation,
> + * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + */
> +
> +/*
> + * Some old libcs (like glibc < 2.7) do not provide interfaces for
> + * dynamically sized cpu sets, but provide only static cpu_set_t type
> + * with no more than CPU_SETSIZE cpus in it.
> + *
> + * This file is a wrapper of the dynamic interfaces using the static ones.
> + *
> + * If the number of cpus available on the system is greater than
> + * CPU_SETSIZE, this interface will not work. Update libc in this case :)
> + */
> +
> +#ifndef __LTP_SCHED_SETAFFINITY_H__
> +#define __LTP_SCHED_SETAFFINITY_H__
> +
> +#ifndef CPU_ALLOC
> +#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
> +if (ncpus > CPU_SETSIZE) { \
> +	tst_brkm(TCONF, cleanup, \
> +		"Your libc does not support masks with %ld cpus", ncpus); \
> +}
> +#endif
> +
> +#ifndef CPU_FREE
> +#define CPU_FREE(ptr) free(ptr);
> +#endif
> +
> +#ifndef CPU_ALLOC_SIZE
> +#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t);
> +#endif
> +
> +#ifndef CPU_ZERO_S
> +#define CPU_ZERO_S(size, mask) CPU_ZERO(mask);
> +#endif
> +
> +#ifndef CPU_SET_S
> +#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask);
> +#endif
> +
> +#endif /* __LTP_SCHED_SETAFFINITY_H__ */
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> new file mode 100644
> index 0000000..8279714
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> @@ -0,0 +1,186 @@
> +/*
> + * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it would be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write the Free Software Foundation,
> + * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + */
> +
> +/*
> + * This test verifies sched_setaffinity(2) for all error conditions
> + * to occur correctly.
> + *
> + * sched_setaffinity() returns -1 and sets the error code to:
> + *
> + * 1) EFAULT, if the supplied memory address is invalid
> + * 2) EINVAL, if the mask doesn't contain at least one
> + *    permitted cpu
> + * 3) ESRCH, if the process whose id is pid could not
> + *    be found
> + * 4) EPERM, if the calling process doesn't have appropriate
> + *    privileges
> + */
> +
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <sched.h>
> +#include <signal.h>
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sys/wait.h>
> +#include "test.h"
> +#include "usctest.h"
> +#include "sched_setaffinity.h"
> +
> +char *TCID = "sched_setaffinity01";
> +
> +static cpu_set_t *mask, *emask;
> +static cpu_set_t *fmask = (void *)-1;
> +static size_t mask_size, emask_size;
> +static pid_t self_pid, privileged_pid, free_pid;
> +static uid_t uid;
> +static long ncpus;
> +
> +static struct test_case_t {
> +	pid_t *pid;
> +	size_t *mask_size;
> +	cpu_set_t **mask;
> +	int exp_errno;
> +} test_cases[] = {
> +	{&self_pid, &mask_size, &fmask, EFAULT},
> +	{&self_pid, &emask_size, &emask, EINVAL},
> +	{&free_pid, &mask_size, &mask, ESRCH},
> +	{&privileged_pid, &mask_size, &mask, EPERM},
> +};
> +
> +int TST_TOTAL = ARRAY_SIZE(test_cases);
> +
> +static void cleanup(void)
> +{
> +	if (mask != NULL) {
> +		CPU_FREE(mask);
> +		mask = NULL;
> +	}
> +
> +	if (emask != NULL) {
> +		CPU_FREE(emask);
> +		emask = NULL;
> +	}
> +
> +	if (seteuid(uid) < 0)
> +		tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed", uid);
> +
> +	if (privileged_pid != 0) {
> +		kill(privileged_pid, SIGUSR1);
> +		waitpid(privileged_pid, NULL, 0);
> +		privileged_pid = 0;
> +	}
> +}
> +
> +static void setup(void)
> +{
> +	/* Current mask */
> +	mask = CPU_ALLOC(ncpus);
> +	if (mask == NULL)
> +		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
> +			ncpus);
> +	mask_size = CPU_ALLOC_SIZE(ncpus);
> +	if (sched_getaffinity(0, mask_size, mask) < 0)
> +		tst_brkm(TBROK | TERRNO, cleanup, "sched_getaffinity() failed");
> +
> +	/* Mask with one more cpu than available on the system */
> +	emask = CPU_ALLOC(ncpus + 1);
> +	if (emask == NULL)
> +		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
> +			ncpus + 1);
> +	emask_size = CPU_ALLOC_SIZE(ncpus + 1);
> +	CPU_ZERO_S(emask_size, emask);
> +	CPU_SET_S(ncpus, emask_size, emask);
> +
> +	privileged_pid = fork();
> +	if (privileged_pid == 0) {
> +		sigset_t set;
> +		struct timespec tv;
> +
> +		sigemptyset(&set);
> +		sigaddset(&set, SIGUSR1);
> +
> +		tv.tv_sec = 30;
> +		tv.tv_nsec = 0;
> +
> +		if (sigtimedwait(&set, NULL, &tv) < 0) {
> +			if (errno == EAGAIN) {
> +				printf("privileged child timeouted\n");
> +				exit(1);
> +			}
> +		}
> +
> +		exit(0);

Hmm, is there a point in the timeout here? Wouldn't it be easier to just
wait in pause() here?

Also if we remove the timeout here, can't we just run the setup once
instead of before each iteration?

> +	} else if (privileged_pid < 0) {
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
> +	}
> +
> +	if (seteuid(uid + 1) < 0)
> +		tst_brkm(TBROK | TERRNO, cleanup, "seteuid(%d) failed",
> +			uid + 1);

It would be a bit better to use nobody user id here as the rest of the
testcases.

> +	/* we assume that this pid will not be used again
> +	 * for some short period of time */
> +	free_pid = fork();
> +	if (free_pid == 0)
> +		exit(0);
> +	else if (free_pid < 0)
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
> +	waitpid(free_pid, NULL, 0);

What about -1 or /proc/sys/kernel/pid_max + 1? Either one should be a
bit more stable than relying on fact that pids are not reused quickly.

Perpahs we can create tst_get_invalid_pid(), if we find more uses for
this.

> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	const char *msg;
> +	int lc;
> +	int i;
> +
> +	if ((msg = parse_opts(argc, argv, NULL, NULL)))
> +		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
> +
> +	tst_require_root(NULL);
> +	uid = geteuid();
> +	ncpus = tst_ncpus_max();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +		tst_count = 0;
> +		setup();
> +		for (i = 0; i < TST_TOTAL; i++) {
> +			TEST(sched_setaffinity(*(test_cases[i].pid),
> +						*(test_cases[i].mask_size),
> +						*(test_cases[i].mask)));
> +
> +			if (TEST_RETURN != -1)
> +				tst_resm(TFAIL,
> +					"sched_setaffinity() unexpectedly succeded");
> +
> +			if (TEST_ERRNO == test_cases[i].exp_errno) {
> +				tst_resm(TPASS, "expected failure with '%s'",
> +					strerror(test_cases[i].exp_errno));
> +			} else {
> +				tst_resm(TFAIL,
> +					"call returned '%s', expected - '%s'",
> +					strerror(TEST_ERRNO),
> +					strerror(test_cases[i].exp_errno));
> +			}
> +		}
> +		cleanup();
> +	}
> +
> +	tst_exit();
> +}

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH V3] syscalls: implemented a test for sched_setaffinity() error values
  2014-05-19 14:39 ` [LTP] [PATCH] " chrubis
@ 2014-06-17 14:32   ` Stanislav Kholmanskikh
  2014-06-17 15:00     ` chrubis
  0 siblings, 1 reply; 5+ messages in thread
From: Stanislav Kholmanskikh @ 2014-06-17 14:32 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko

This test verifies sched_setaffinity(2) for all error conditions
to occur correctly.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
Thank you, Cyril, for review.

This patch accomodates changes mentioned by you:
 * the privileged_pid child uses pause() instead of sigtimedwait()
 * setup() and cleanup() are moved out of the testcase iterations
 * it use nobody user uid instead uid+1
 * for the ESRCH  test case '/proc/sys/kernel/pid_max + 1' is used


 runtest/syscalls                                   |    1 +
 testcases/kernel/syscalls/.gitignore               |    1 +
 .../kernel/syscalls/sched_setaffinity/Makefile     |   23 +++
 .../syscalls/sched_setaffinity/sched_setaffinity.h |   60 +++++++
 .../sched_setaffinity/sched_setaffinity01.c        |  178 ++++++++++++++++++++
 5 files changed, 263 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/Makefile
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
 create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index c132a69..b647d46 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -898,6 +898,7 @@ sched_setscheduler02 sched_setscheduler02
 
 sched_yield01 sched_yield01
 
+sched_setaffinity01 sched_setaffinity01
 sched_getaffinity01 sched_getaffinity01
 
 select01 select01
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 256cad0..f245550 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -722,6 +722,7 @@
 /sched_rr_get_interval/sched_rr_get_interval01
 /sched_rr_get_interval/sched_rr_get_interval02
 /sched_rr_get_interval/sched_rr_get_interval03
+/sched_setaffinity/sched_setaffinity01
 /sched_setparam/sched_setparam01
 /sched_setparam/sched_setparam02
 /sched_setparam/sched_setparam03
diff --git a/testcases/kernel/syscalls/sched_setaffinity/Makefile b/testcases/kernel/syscalls/sched_setaffinity/Makefile
new file mode 100644
index 0000000..2d825f1
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/Makefile
@@ -0,0 +1,23 @@
+#
+#  Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+#
+#  This program is free software;  you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+#  the GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program;  if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
new file mode 100644
index 0000000..8083894
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * Some old libcs (like glibc < 2.7) do not provide interfaces for
+ * dynamically sized cpu sets, but provide only static cpu_set_t type
+ * with no more than CPU_SETSIZE cpus in it.
+ *
+ * This file is a wrapper of the dynamic interfaces using the static ones.
+ *
+ * If the number of cpus available on the system is greater than
+ * CPU_SETSIZE, this interface will not work. Update libc in this case :)
+ */
+
+#define _GNU_SOURCE
+#include <sched.h>
+
+#ifndef LTP_SCHED_SETAFFINITY_H
+#define LTP_SCHED_SETAFFINITY_H
+
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	tst_brkm(TCONF, cleanup, \
+		"Your libc does not support masks with %ld cpus", ncpus); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr);
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t);
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask);
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask);
+#endif
+
+#endif /* LTP_SCHED_SETAFFINITY_H */
diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
new file mode 100644
index 0000000..125e158
--- /dev/null
+++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/*
+ * This test verifies sched_setaffinity(2) for all error conditions
+ * to occur correctly.
+ *
+ * sched_setaffinity() returns -1 and sets the error code to:
+ *
+ * 1) EFAULT, if the supplied memory address is invalid
+ * 2) EINVAL, if the mask doesn't contain at least one
+ *    permitted cpu
+ * 3) ESRCH, if the process whose id is pid could not
+ *    be found
+ * 4) EPERM, if the calling process doesn't have appropriate
+ *    privileges
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <pwd.h>
+#include <sched.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "test.h"
+#include "usctest.h"
+#include "safe_macros.h"
+#include "sched_setaffinity.h"
+
+char *TCID = "sched_setaffinity01";
+
+#define PID_MAX_PATH "/proc/sys/kernel/pid_max"
+
+static cpu_set_t *mask, *emask;
+static cpu_set_t *fmask = (void *)-1;
+static size_t mask_size, emask_size;
+static pid_t self_pid, privileged_pid, free_pid;
+static uid_t uid;
+static const char nobody_uid[] = "nobody";
+static struct passwd *ltpuser;
+static long ncpus;
+
+static struct test_case_t {
+	pid_t *pid;
+	size_t *mask_size;
+	cpu_set_t **mask;
+	int exp_errno;
+} test_cases[] = {
+	{&self_pid, &mask_size, &fmask, EFAULT},
+	{&self_pid, &emask_size, &emask, EINVAL},
+	{&free_pid, &mask_size, &mask, ESRCH},
+	{&privileged_pid, &mask_size, &mask, EPERM},
+};
+
+int TST_TOTAL = ARRAY_SIZE(test_cases);
+
+static void cleanup(void)
+{
+	if (mask != NULL) {
+		CPU_FREE(mask);
+		mask = NULL;
+	}
+
+	if (emask != NULL) {
+		CPU_FREE(emask);
+		emask = NULL;
+	}
+
+	SAFE_SETEUID(NULL, uid);
+
+	if (privileged_pid != 0) {
+		kill(privileged_pid, SIGKILL);
+		waitpid(privileged_pid, NULL, 0);
+		privileged_pid = 0;
+	}
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	uid = geteuid();
+	ncpus = tst_ncpus_max();
+
+	/* Current mask */
+	mask = CPU_ALLOC(ncpus);
+	if (mask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus);
+	mask_size = CPU_ALLOC_SIZE(ncpus);
+	if (sched_getaffinity(0, mask_size, mask) < 0)
+		tst_brkm(TBROK | TERRNO, cleanup, "sched_getaffinity() failed");
+
+	/* Mask with one more cpu than available on the system */
+	emask = CPU_ALLOC(ncpus + 1);
+	if (emask == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
+			ncpus + 1);
+	emask_size = CPU_ALLOC_SIZE(ncpus + 1);
+	CPU_ZERO_S(emask_size, emask);
+	CPU_SET_S(ncpus, emask_size, emask);
+
+	privileged_pid = fork();
+	if (privileged_pid == 0) {
+		pause();
+
+		exit(0);
+	} else if (privileged_pid < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
+	}
+
+	/* Dropping the root privileges */
+	ltpuser = getpwnam(nobody_uid);
+	if (ltpuser == NULL)
+		tst_brkm(TBROK | TERRNO, cleanup,
+			"getpwnam failed for user id %s", nobody_uid);
+
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
+
+	/* this pid is not used by the OS */
+	SAFE_FILE_SCANF(cleanup, PID_MAX_PATH, "%d", &free_pid);
+	free_pid++;
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+	int i;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+		for (i = 0; i < TST_TOTAL; i++) {
+			TEST(sched_setaffinity(*(test_cases[i].pid),
+						*(test_cases[i].mask_size),
+						*(test_cases[i].mask)));
+
+			if (TEST_RETURN != -1)
+				tst_resm(TFAIL,
+					"sched_setaffinity() unexpectedly succeded");
+
+			if (TEST_ERRNO == test_cases[i].exp_errno) {
+				tst_resm(TPASS, "expected failure with '%s'",
+					strerror(test_cases[i].exp_errno));
+			} else {
+				tst_resm(TFAIL,
+					"call returned '%s', expected - '%s'",
+					strerror(TEST_ERRNO),
+					strerror(test_cases[i].exp_errno));
+			}
+		}
+	}
+
+	cleanup();
+	tst_exit();
+}
-- 
1.7.1


------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH V3] syscalls: implemented a test for sched_setaffinity() error values
  2014-06-17 14:32   ` [LTP] [PATCH V3] " Stanislav Kholmanskikh
@ 2014-06-17 15:00     ` chrubis
  0 siblings, 0 replies; 5+ messages in thread
From: chrubis @ 2014-06-17 15:00 UTC (permalink / raw)
  To: Stanislav Kholmanskikh; +Cc: vasily.isaenko, ltp-list

Hi!
> This test verifies sched_setaffinity(2) for all error conditions
> to occur correctly.
> 
> Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
> ---
> Thank you, Cyril, for review.
> 
> This patch accomodates changes mentioned by you:
>  * the privileged_pid child uses pause() instead of sigtimedwait()
>  * setup() and cleanup() are moved out of the testcase iterations
>  * it use nobody user uid instead uid+1
>  * for the ESRCH  test case '/proc/sys/kernel/pid_max + 1' is used
> 
> 
>  runtest/syscalls                                   |    1 +
>  testcases/kernel/syscalls/.gitignore               |    1 +
>  .../kernel/syscalls/sched_setaffinity/Makefile     |   23 +++
>  .../syscalls/sched_setaffinity/sched_setaffinity.h |   60 +++++++
>  .../sched_setaffinity/sched_setaffinity01.c        |  178 ++++++++++++++++++++
>  5 files changed, 263 insertions(+), 0 deletions(-)
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/Makefile
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
>  create mode 100644 testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index c132a69..b647d46 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -898,6 +898,7 @@ sched_setscheduler02 sched_setscheduler02
>  
>  sched_yield01 sched_yield01
>  
> +sched_setaffinity01 sched_setaffinity01
>  sched_getaffinity01 sched_getaffinity01
>  
>  select01 select01
> diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
> index 256cad0..f245550 100644
> --- a/testcases/kernel/syscalls/.gitignore
> +++ b/testcases/kernel/syscalls/.gitignore
> @@ -722,6 +722,7 @@
>  /sched_rr_get_interval/sched_rr_get_interval01
>  /sched_rr_get_interval/sched_rr_get_interval02
>  /sched_rr_get_interval/sched_rr_get_interval03
> +/sched_setaffinity/sched_setaffinity01
>  /sched_setparam/sched_setparam01
>  /sched_setparam/sched_setparam02
>  /sched_setparam/sched_setparam03
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/Makefile b/testcases/kernel/syscalls/sched_setaffinity/Makefile
> new file mode 100644
> index 0000000..2d825f1
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/Makefile
> @@ -0,0 +1,23 @@
> +#
> +#  Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> +#
> +#  This program is free software;  you can redistribute it and/or modify
> +#  it under the terms of the GNU General Public License as published by
> +#  the Free Software Foundation; either version 2 of the License, or
> +#  (at your option) any later version.
> +#
> +#  This program is distributed in the hope that it will be useful,
> +#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
> +#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
> +#  the GNU General Public License for more details.
> +#
> +#  You should have received a copy of the GNU General Public License
> +#  along with this program;  if not, write to the Free Software
> +#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> +#
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
> new file mode 100644
> index 0000000..8083894
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity.h
> @@ -0,0 +1,60 @@
> +/*
> + * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it would be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write the Free Software Foundation,
> + * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + */
> +
> +/*
> + * Some old libcs (like glibc < 2.7) do not provide interfaces for
> + * dynamically sized cpu sets, but provide only static cpu_set_t type
> + * with no more than CPU_SETSIZE cpus in it.
> + *
> + * This file is a wrapper of the dynamic interfaces using the static ones.
> + *
> + * If the number of cpus available on the system is greater than
> + * CPU_SETSIZE, this interface will not work. Update libc in this case :)
> + */
> +
> +#define _GNU_SOURCE
> +#include <sched.h>
> +
> +#ifndef LTP_SCHED_SETAFFINITY_H
> +#define LTP_SCHED_SETAFFINITY_H
> +
> +#ifndef CPU_ALLOC
> +#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
> +if (ncpus > CPU_SETSIZE) { \
> +	tst_brkm(TCONF, cleanup, \
> +		"Your libc does not support masks with %ld cpus", ncpus); \
> +}
> +#endif
> +
> +#ifndef CPU_FREE
> +#define CPU_FREE(ptr) free(ptr);
> +#endif
> +
> +#ifndef CPU_ALLOC_SIZE
> +#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t);
> +#endif
> +
> +#ifndef CPU_ZERO_S
> +#define CPU_ZERO_S(size, mask) CPU_ZERO(mask);
> +#endif
> +
> +#ifndef CPU_SET_S
> +#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask);
> +#endif
> +
> +#endif /* LTP_SCHED_SETAFFINITY_H */
> diff --git a/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> new file mode 100644
> index 0000000..125e158
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sched_setaffinity/sched_setaffinity01.c
> @@ -0,0 +1,178 @@
> +/*
> + * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it would be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write the Free Software Foundation,
> + * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + */
> +
> +/*
> + * This test verifies sched_setaffinity(2) for all error conditions
> + * to occur correctly.
> + *
> + * sched_setaffinity() returns -1 and sets the error code to:
> + *
> + * 1) EFAULT, if the supplied memory address is invalid
> + * 2) EINVAL, if the mask doesn't contain at least one
> + *    permitted cpu
> + * 3) ESRCH, if the process whose id is pid could not
> + *    be found
> + * 4) EPERM, if the calling process doesn't have appropriate
> + *    privileges
> + */
> +
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <pwd.h>
> +#include <sched.h>
> +#include <signal.h>
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sys/wait.h>
> +#include "test.h"
> +#include "usctest.h"
> +#include "safe_macros.h"
> +#include "sched_setaffinity.h"
> +
> +char *TCID = "sched_setaffinity01";
> +
> +#define PID_MAX_PATH "/proc/sys/kernel/pid_max"
> +
> +static cpu_set_t *mask, *emask;
> +static cpu_set_t *fmask = (void *)-1;
> +static size_t mask_size, emask_size;
> +static pid_t self_pid, privileged_pid, free_pid;
> +static uid_t uid;
> +static const char nobody_uid[] = "nobody";
> +static struct passwd *ltpuser;
> +static long ncpus;
> +
> +static struct test_case_t {
> +	pid_t *pid;
> +	size_t *mask_size;
> +	cpu_set_t **mask;
> +	int exp_errno;
> +} test_cases[] = {
> +	{&self_pid, &mask_size, &fmask, EFAULT},
> +	{&self_pid, &emask_size, &emask, EINVAL},
> +	{&free_pid, &mask_size, &mask, ESRCH},
> +	{&privileged_pid, &mask_size, &mask, EPERM},
> +};
> +
> +int TST_TOTAL = ARRAY_SIZE(test_cases);
> +
> +static void cleanup(void)
> +{
> +	if (mask != NULL) {
> +		CPU_FREE(mask);
> +		mask = NULL;
> +	}
> +
> +	if (emask != NULL) {
> +		CPU_FREE(emask);
> +		emask = NULL;
> +	}
> +
> +	SAFE_SETEUID(NULL, uid);
> +
> +	if (privileged_pid != 0) {
> +		kill(privileged_pid, SIGKILL);
> +		waitpid(privileged_pid, NULL, 0);
> +		privileged_pid = 0;
> +	}
> +}
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	uid = geteuid();
> +	ncpus = tst_ncpus_max();
> +
> +	/* Current mask */
> +	mask = CPU_ALLOC(ncpus);
> +	if (mask == NULL)
> +		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
> +			ncpus);
> +	mask_size = CPU_ALLOC_SIZE(ncpus);
> +	if (sched_getaffinity(0, mask_size, mask) < 0)
> +		tst_brkm(TBROK | TERRNO, cleanup, "sched_getaffinity() failed");
> +
> +	/* Mask with one more cpu than available on the system */
> +	emask = CPU_ALLOC(ncpus + 1);
> +	if (emask == NULL)
> +		tst_brkm(TBROK | TERRNO, cleanup, "CPU_ALLOC(%ld) failed",
> +			ncpus + 1);
> +	emask_size = CPU_ALLOC_SIZE(ncpus + 1);
> +	CPU_ZERO_S(emask_size, emask);
> +	CPU_SET_S(ncpus, emask_size, emask);
> +
> +	privileged_pid = fork();

This should be tst_fork().

> +	if (privileged_pid == 0) {
> +		pause();
> +
> +		exit(0);
> +	} else if (privileged_pid < 0) {
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
> +	}
> +
> +	/* Dropping the root privileges */
> +	ltpuser = getpwnam(nobody_uid);
> +	if (ltpuser == NULL)
> +		tst_brkm(TBROK | TERRNO, cleanup,
> +			"getpwnam failed for user id %s", nobody_uid);
> +
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +
> +	/* this pid is not used by the OS */
> +	SAFE_FILE_SCANF(cleanup, PID_MAX_PATH, "%d", &free_pid);
> +	free_pid++;

I would like to move this code to tst_get_unused_pid() library function,
there are more testcases that needs unused pid as well and each of them
does it differently (kill04.c, getpgid02.c, getsid02.c, setpgid02.c,
...).

Looking at man proc says that pid_max was added to Linux 2.5.34 which
should be old enough for us not to care about systems without it...

Will you update the pach or should I do it?

> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	const char *msg;
> +	int lc;
> +	int i;
> +
> +	msg = parse_opts(argc, argv, NULL, NULL);
> +	if (msg != NULL)
> +		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
> +
> +	setup();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +		tst_count = 0;
> +		for (i = 0; i < TST_TOTAL; i++) {
> +			TEST(sched_setaffinity(*(test_cases[i].pid),
> +						*(test_cases[i].mask_size),
> +						*(test_cases[i].mask)));
> +
> +			if (TEST_RETURN != -1)
> +				tst_resm(TFAIL,
> +					"sched_setaffinity() unexpectedly succeded");
> +
> +			if (TEST_ERRNO == test_cases[i].exp_errno) {
> +				tst_resm(TPASS, "expected failure with '%s'",
> +					strerror(test_cases[i].exp_errno));
> +			} else {
> +				tst_resm(TFAIL,
> +					"call returned '%s', expected - '%s'",
> +					strerror(TEST_ERRNO),
> +					strerror(test_cases[i].exp_errno));
> +			}
> +		}
> +	}
> +
> +	cleanup();
> +	tst_exit();
> +}

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

end of thread, other threads:[~2014-06-17 15:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-15 12:55 [LTP] [PATCH] syscalls: implemented a test for sched_setaffinity() error values Stanislav Kholmanskikh
2014-04-28  7:56 ` [LTP] [PATCH V2] " Stanislav Kholmanskikh
2014-05-19 14:39 ` [LTP] [PATCH] " chrubis
2014-06-17 14:32   ` [LTP] [PATCH V3] " Stanislav Kholmanskikh
2014-06-17 15:00     ` chrubis

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.