* [LTP] [PATCH v1 1/5] setregid01: Convert to newlib
@ 2018-09-10 9:09 Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 2/5] tst_safe_macros: add SAFE_GETGRGID() Clemens Famulla-Conrad
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 9:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
---
testcases/kernel/syscalls/setregid/setregid01.c | 171 +++++-------------------
1 file changed, 36 insertions(+), 135 deletions(-)
diff --git a/testcases/kernel/syscalls/setregid/setregid01.c b/testcases/kernel/syscalls/setregid/setregid01.c
index 57a38eb62..120dff8a9 100644
--- a/testcases/kernel/syscalls/setregid/setregid01.c
+++ b/testcases/kernel/syscalls/setregid/setregid01.c
@@ -1,25 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like. Any license provided herein, whether implied or
- * otherwise, applies only to this software file. Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
* Mountain View, CA 94043, or:
*
@@ -37,128 +19,47 @@
* Testcase to test the basic functionality of setregid(2) systemm call.
*/
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/types.h>
-
-#include "test.h"
-#include "compat_16.h"
-
-static void setup(void);
-
-TCID_DEFINE(setregid01);
-int TST_TOTAL = 5;
-
-static gid_t gid, egid; /* current real and effective group id */
-
-int main(int ac, char **av)
+#include "tst_test.h"
+#include "compat_tst_16.h"
+
+static gid_t gid, egid; /* current real and effective group id */
+static gid_t neg_one = -1;
+
+static struct tcase {
+ gid_t *arg1;
+ gid_t *arg2;
+ const char *msg;
+} tcases[] = {
+ {&neg_one, &neg_one, "Dont change either real or effective gid" },
+ {&neg_one, &egid, "Change effective to effective gid" },
+ {&gid, &neg_one, "Change real to real gid" },
+ {&neg_one, &gid, "Change effective to real gid" },
+ {&gid, &gid, "Try to change real to current real" }
+};
+
+static void run(unsigned int n)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * TEST CASE:
- * Dont change either real or effective gid
- */
- gid = getgid();
- GID16_CHECK(gid, setregid, NULL);
-
- egid = getegid();
- GID16_CHECK(egid, setregid, NULL);
+ struct tcase *tc = &tcases[n];
- TEST(SETREGID(NULL, -1, -1));
+ TEST(SETREGID(*tc->arg1, *tc->arg2));
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL,
- "setregid - Dont change either real or effective gid failed, errno=%d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- tst_resm(TPASS,
- "setregid - Dont change either real or effective gid returned %ld",
- TEST_RETURN);
- }
-
- /*
- * TEST CASE:
- * change effective to effective gid
- */
-
- TEST(SETREGID(NULL, -1, egid));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL,
- "setregid - change effective to effective gid failed, errno=%d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- tst_resm(TPASS,
- "setregid - change effective to effective gid returned %ld",
- TEST_RETURN);
- }
-
- /*
- * TEST CASE:
- * change real to real gid
- */
-
- TEST(SETREGID(NULL, gid, -1));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL,
- "setregid - change real to real gid failed, errno=%d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- tst_resm(TPASS,
- "setregid - change real to real gid returned %ld",
- TEST_RETURN);
- }
-
- /*
- * TEST CASE:
- * change effective to real gid
- */
-
- TEST(SETREGID(NULL, -1, gid));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL,
- "setregid - change effective to real gid failed, errno=%d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- tst_resm(TPASS,
- "setregid - change effective to real gid returned %ld",
- TEST_RETURN);
- }
-
- /*
- * TEST CASE:
- * try to change real to current real
- */
-
- TEST(SETREGID(NULL, gid, gid));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL | TTERRNO, "setregid failed");
- } else {
- tst_resm(TPASS, "setregid return %ld",
- TEST_RETURN);
- }
-
- }
-
- tst_exit();
+ if (TST_RET == -1)
+ tst_brk(TFAIL | TTERRNO, tc->msg);
+ else
+ tst_res(TPASS, tc->msg);
}
static void setup(void)
{
- tst_sig(NOFORK, DEF_HANDLER, NULL);
+ gid = getgid();
+ GID16_CHECK(gid, setregid);
- TEST_PAUSE;
+ egid = getegid();
+ GID16_CHECK(egid, setregid);
}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = run,
+ .setup = setup,
+};
--
2.16.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 2/5] tst_safe_macros: add SAFE_GETGRGID()
2018-09-10 9:09 [LTP] [PATCH v1 1/5] setregid01: Convert to newlib Clemens Famulla-Conrad
@ 2018-09-10 9:10 ` Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 3/5] setregid02: Convert to newlib Clemens Famulla-Conrad
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 9:10 UTC (permalink / raw)
To: ltp
Add tst safe wrapper for getgrgid() function.
Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
---
include/tst_safe_macros.h | 4 ++++
lib/tst_safe_macros.c | 13 +++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 03657a410..d457ae92a 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -441,6 +441,10 @@ struct group *safe_getgrnam(const char *file, const int lineno,
#define SAFE_GETGRNAM(name) \
safe_getgrnam(__FILE__, __LINE__, (name))
+struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
+#define SAFE_GETGRGID(gid) \
+ safe_getgrgid(__FILE__, __LINE__, (gid))
+
int safe_setxattr(const char *file, const int lineno, const char *path,
const char *name, const void *value, size_t size, int flags);
#define SAFE_SETXATTR(path, name, value, size, flags) \
diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
index 17384f32c..2e041c460 100644
--- a/lib/tst_safe_macros.c
+++ b/lib/tst_safe_macros.c
@@ -153,6 +153,19 @@ struct group *safe_getgrnam(const char *file, const int lineno,
return rval;
}
+struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
+{
+ struct group *rval;
+
+ rval = getgrgid(gid);
+ if (rval == NULL) {
+ tst_brk_(file, lineno, TBROK | TERRNO,
+ "getgrgid(%li) failed", (long)gid);
+ }
+
+ return rval;
+}
+
int safe_chroot(const char *file, const int lineno, const char *path)
{
int rval;
--
2.16.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 3/5] setregid02: Convert to newlib
2018-09-10 9:09 [LTP] [PATCH v1 1/5] setregid01: Convert to newlib Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 2/5] tst_safe_macros: add SAFE_GETGRGID() Clemens Famulla-Conrad
@ 2018-09-10 9:10 ` Clemens Famulla-Conrad
2018-09-10 12:44 ` Richard Palethorpe
2018-09-10 9:11 ` [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK() Clemens Famulla-Conrad
2018-09-10 9:11 ` [LTP] [PATCH v1 5/5] setregid04: Convert to newlib Clemens Famulla-Conrad
3 siblings, 1 reply; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 9:10 UTC (permalink / raw)
To: ltp
Also changed some messaged to show errno as string.
Used SAVE_* functions for getgrnam(), getgrgid() and getpwnam()
Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
---
testcases/kernel/syscalls/setregid/setregid02.c | 136 ++++++++----------------
1 file changed, 42 insertions(+), 94 deletions(-)
diff --git a/testcases/kernel/syscalls/setregid/setregid02.c b/testcases/kernel/syscalls/setregid/setregid02.c
index 21d1c823a..747515645 100644
--- a/testcases/kernel/syscalls/setregid/setregid02.c
+++ b/testcases/kernel/syscalls/setregid/setregid02.c
@@ -1,21 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
/*
- *
* Copyright (c) International Business Machines Corp., 2001
*
- * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
* Ported by John George
*/
@@ -29,13 +15,9 @@
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>
-#include <string.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "compat_16.h"
-
-TCID_DEFINE(setregid02);
+#include "tst_test.h"
+#include "compat_tst_16.h"
static gid_t neg_one = -1;
@@ -48,14 +30,14 @@ static struct group ltpgroup, root, bin;
* is used for a separate test. The tests are executed in the for loop below.
*/
-struct test_data_t {
+static struct tcase {
gid_t *real_gid;
gid_t *eff_gid;
int exp_errno;
struct group *exp_real_usr;
struct group *exp_eff_usr;
char *test_msg;
-} test_data[] = {
+} tcases[] = {
{
&neg_one, &root.gr_gid, EPERM, <pgroup, <pgroup,
"After setregid(-1, root),"}, {
@@ -71,113 +53,79 @@ struct test_data_t {
"After setregid(bin, root),"}
};
-int TST_TOTAL = ARRAY_SIZE(test_data);
-
-static void setup(void);
static void gid_verify(struct group *ru, struct group *eu, char *when);
static struct group get_group_by_name(const char *name);
static struct group get_group_by_gid(gid_t gid);
-int main(int ac, char **av)
+static void run(unsigned int n)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- int i;
-
- tst_count = 0;
-
- for (i = 0; i < TST_TOTAL; i++) {
- /* Set the real or effective group id */
- TEST(SETREGID(NULL, *test_data[i].real_gid,
- *test_data[i].eff_gid));
-
- if (TEST_RETURN == -1) {
- if (TEST_ERRNO == test_data[i].exp_errno) {
- tst_resm(TPASS, "setregid(%d, %d) "
- "failed as expected.",
- *test_data[i].real_gid,
- *test_data[i].eff_gid);
- } else {
- tst_resm(TFAIL, "setregid(%d, %d) "
- "failed (%d) but did not set the "
- "expected errno (%d).",
- *test_data[i].real_gid,
- *test_data[i].eff_gid,
- TEST_ERRNO,
- test_data[i].exp_errno);
- }
- } else {
- tst_resm(TFAIL, "setregid(%d, %d) "
- "did not fail (ret: %ld) as expected (ret: -1).",
- *test_data[i].real_gid,
- *test_data[i].eff_gid, TEST_RETURN);
- }
- gid_verify(test_data[i].exp_real_usr,
- test_data[i].exp_eff_usr,
- test_data[i].test_msg);
+ struct tcase *tc = &tcases[n];
+
+ /* Set the real or effective group id */
+ TEST(SETREGID(*tc->real_gid, *tc->eff_gid));
+
+ if (TST_RET == -1) {
+ if (TST_ERR == tc->exp_errno) {
+ tst_res(TPASS | TTERRNO, "setregid(%d, %d) failed "
+ "as expected", *tc->real_gid, *tc->eff_gid);
+ } else {
+ tst_res(TFAIL | TTERRNO, "setregid(%d, %d) failed "
+ "unexpectedly, expected %s", *tc->real_gid,
+ *tc->eff_gid, tst_strerrno(tc->exp_errno));
}
+ } else {
+ tst_res(TFAIL, "setregid(%d, %d) did not fail (ret: %ld) "
+ "as expected (ret: -1).", *tc->real_gid,
+ *tc->eff_gid, TST_RET);
}
-
- tst_exit();
+ gid_verify(tc->exp_real_usr, tc->exp_eff_usr, tc->test_msg);
}
static void setup(void)
{
- tst_require_root();
+ ltpuser = SAFE_GETPWNAM("nobody");
- tst_sig(FORK, DEF_HANDLER, NULL);
-
- ltpuser = getpwnam("nobody");
- if (ltpuser == NULL)
- tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
-
- SAFE_SETGID(NULL, ltpuser->pw_gid);
- SAFE_SETUID(NULL, ltpuser->pw_uid);
+ SAFE_SETGID(ltpuser->pw_gid);
+ SAFE_SETUID(ltpuser->pw_uid);
root = get_group_by_name("root");
ltpgroup = get_group_by_gid(ltpuser->pw_gid);
bin = get_group_by_name("bin");
-
- TEST_PAUSE;
}
static struct group get_group_by_name(const char *name)
{
- struct group *ret = getgrnam(name);
-
- if (ret == NULL)
- tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", name);
+ struct group *ret = SAFE_GETGRNAM(name);
- GID16_CHECK(ret->gr_gid, setregid, NULL);
+ GID16_CHECK(ret->gr_gid, setregid);
return *ret;
}
static struct group get_group_by_gid(gid_t gid)
{
- struct group *ret = getgrgid(gid);
+ struct group *ret = SAFE_GETGRGID(gid);
- if (ret == NULL)
- tst_brkm(TBROK|TERRNO, NULL, "getgrgid(\"%d\") failed", gid);
-
- GID16_CHECK(ret->gr_gid, setregid, NULL);
+ GID16_CHECK(ret->gr_gid, setregid);
return *ret;
}
-
void gid_verify(struct group *rg, struct group *eg, char *when)
{
if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
- tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
+ tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
when, getgid(), getegid());
- tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
+ tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
rg->gr_gid, eg->gr_gid);
} else {
- tst_resm(TPASS, "real or effective gid was modified as expected");
+ tst_res(TPASS,
+ "real or effective gid was modified as expected");
}
}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tcases),
+ .needs_root = 1,
+ .test = run,
+ .setup = setup,
+};
--
2.16.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK()
2018-09-10 9:09 [LTP] [PATCH v1 1/5] setregid01: Convert to newlib Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 2/5] tst_safe_macros: add SAFE_GETGRGID() Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 3/5] setregid02: Convert to newlib Clemens Famulla-Conrad
@ 2018-09-10 9:11 ` Clemens Famulla-Conrad
2018-09-10 12:51 ` Richard Palethorpe
2018-09-10 9:11 ` [LTP] [PATCH v1 5/5] setregid04: Convert to newlib Clemens Famulla-Conrad
3 siblings, 1 reply; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 9:11 UTC (permalink / raw)
To: ltp
This function retrieves the group by name. If not exists fall back
to the second name given. If the second group dosn't exists, exit
with TBROK.
Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
---
include/tst_safe_macros.h | 5 +++++
lib/tst_safe_macros.c | 16 ++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index d457ae92a..e8b68ce9d 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -441,6 +441,11 @@ struct group *safe_getgrnam(const char *file, const int lineno,
#define SAFE_GETGRNAM(name) \
safe_getgrnam(__FILE__, __LINE__, (name))
+struct group *safe_getgrnam_fallback(const char *file, const int lineno,
+ const char *name, const char *fallback);
+#define SAFE_GETGRNAM_FALLBACK(name, fallback) \
+ safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
+
struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
#define SAFE_GETGRGID(gid) \
safe_getgrgid(__FILE__, __LINE__, (gid))
diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
index 2e041c460..614b6b4b5 100644
--- a/lib/tst_safe_macros.c
+++ b/lib/tst_safe_macros.c
@@ -153,6 +153,22 @@ struct group *safe_getgrnam(const char *file, const int lineno,
return rval;
}
+struct group *safe_getgrnam_fallback(const char *file, const int lineno,
+ const char *name, const char *fallback)
+{
+ struct group *rval;
+
+ rval = getgrnam(name);
+ if (rval == NULL) {
+ tst_res_(file, lineno, TINFO,
+ "getgrnam(%s) failed - try fallback %s",
+ name, fallback);
+ rval = safe_getgrnam(file, lineno, name);
+ }
+
+ return rval;
+}
+
struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
{
struct group *rval;
--
2.16.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 5/5] setregid04: Convert to newlib
2018-09-10 9:09 [LTP] [PATCH v1 1/5] setregid01: Convert to newlib Clemens Famulla-Conrad
` (2 preceding siblings ...)
2018-09-10 9:11 ` [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK() Clemens Famulla-Conrad
@ 2018-09-10 9:11 ` Clemens Famulla-Conrad
3 siblings, 0 replies; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 9:11 UTC (permalink / raw)
To: ltp
Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
---
testcases/kernel/syscalls/setregid/setregid04.c | 114 ++++++------------------
1 file changed, 26 insertions(+), 88 deletions(-)
diff --git a/testcases/kernel/syscalls/setregid/setregid04.c b/testcases/kernel/syscalls/setregid/setregid04.c
index bf744ff05..1ecf03f9b 100644
--- a/testcases/kernel/syscalls/setregid/setregid04.c
+++ b/testcases/kernel/syscalls/setregid/setregid04.c
@@ -1,20 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) International Business Machines Corp., 2001
*
- * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
* Ported by John George
*/
@@ -22,16 +9,8 @@
* Test setregid() when executed by root.
*/
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "test.h"
-#include "compat_16.h"
-
-TCID_DEFINE(setregid04);
+#include "tst_test.h"
+#include "compat_tst_16.h"
static gid_t neg_one = -1;
@@ -70,88 +49,47 @@ struct test_data_t {
"After setregid(-1, nobody)"}
};
-int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
-
static void setup(void);
-static void cleanup(void);
static void gid_verify(struct group *ru, struct group *eu, const char *when);
-int main(int ac, char **av)
+static void run(unsigned int i)
{
- int lc;
+ /* Set the real or effective group id */
+ TEST(SETREGID(*test_data[i].real_gid, *test_data[i].eff_gid));
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- int i;
-
- tst_count = 0;
-
- for (i = 0; i < TST_TOTAL; i++) {
- /* Set the real or effective group id */
- TEST(SETREGID(cleanup, *test_data[i].real_gid,
- *test_data[i].eff_gid));
-
- if (TEST_RETURN == -1) {
- tst_resm(TBROK, "setregid(%d, %d) failed",
- *test_data[i].real_gid,
- *test_data[i].eff_gid);
- } else {
- gid_verify(test_data[i].exp_real_usr,
- test_data[i].exp_eff_usr,
- test_data[i].test_msg);
- }
- }
+ if (TST_RET == -1) {
+ tst_res(TBROK | TTERRNO, "setregid(%d, %d) failed",
+ *test_data[i].real_gid, *test_data[i].eff_gid);
+ } else {
+ gid_verify(test_data[i].exp_real_usr, test_data[i].exp_eff_usr,
+ test_data[i].test_msg);
}
-
- cleanup();
- tst_exit();
}
-#define SAFE_GETGROUP(GROUPNAME) \
- if (getgrnam(#GROUPNAME) == NULL) { \
- tst_brkm(TBROK, NULL, "Couldn't find the `" #GROUPNAME "' group"); \
- } \
- GROUPNAME ## _gr = *(getgrnam(#GROUPNAME));
-
-#define SAFE_GETGROUP_FALLBACK(GROUPNAME, GROUPNAME2) \
- if (getgrnam(#GROUPNAME) != NULL) \
- GROUPNAME ## _gr = *(getgrnam(#GROUPNAME)); \
- else if (getgrnam(#GROUPNAME2) != NULL) { \
- GROUPNAME ## _gr = *(getgrnam(#GROUPNAME2)); \
- tst_resm(TINFO, "`" #GROUPNAME "' group not found, trying fallback `" #GROUPNAME2 "' group"); \
- } else \
- tst_brkm(TBROK, NULL, "Couldn't find neither`" #GROUPNAME "' `" #GROUPNAME2 "' nor group");
-
static void setup(void)
{
- tst_require_root();
-
- tst_sig(FORK, DEF_HANDLER, cleanup);
-
- SAFE_GETGROUP(root);
- SAFE_GETGROUP_FALLBACK(nobody, nogroup);
- SAFE_GETGROUP(daemon);
- SAFE_GETGROUP(bin);
-
- TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
+ root_gr = *SAFE_GETGRNAM("root");
+ nobody_gr = *SAFE_GETGRNAM_FALLBACK("nobody", "nogroup");
+ daemon_gr = *SAFE_GETGRNAM("daemon");
+ bin_gr = *SAFE_GETGRNAM("bin");
}
static void gid_verify(struct group *rg, struct group *eg, const char *when)
{
if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
- tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
+ tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
when, getgid(), getegid());
- tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
+ tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
rg->gr_gid, eg->gr_gid);
} else {
- tst_resm(TPASS, "real or effective gid was modified as "
+ tst_res(TPASS, "real or effective gid was modified as "
"expected");
}
}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(test_data),
+ .needs_root = 1,
+ .test = run,
+ .setup = setup,
+};
--
2.16.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 3/5] setregid02: Convert to newlib
2018-09-10 9:10 ` [LTP] [PATCH v1 3/5] setregid02: Convert to newlib Clemens Famulla-Conrad
@ 2018-09-10 12:44 ` Richard Palethorpe
0 siblings, 0 replies; 8+ messages in thread
From: Richard Palethorpe @ 2018-09-10 12:44 UTC (permalink / raw)
To: ltp
Hello,
Clemens Famulla-Conrad <cfamullaconrad@suse.de> writes:
> Also changed some messaged to show errno as string.
> Used SAVE_* functions for getgrnam(), getgrgid() and getpwnam()
>
> Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
> ---
> testcases/kernel/syscalls/setregid/setregid02.c | 136 ++++++++----------------
> 1 file changed, 42 insertions(+), 94 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/setregid/setregid02.c b/testcases/kernel/syscalls/setregid/setregid02.c
> index 21d1c823a..747515645 100644
> --- a/testcases/kernel/syscalls/setregid/setregid02.c
> +++ b/testcases/kernel/syscalls/setregid/setregid02.c
> @@ -1,21 +1,7 @@
> +// SPDX-License-Identifier: GPL-2.0
> /*
> - *
> * Copyright (c) International Business Machines Corp., 2001
> *
> - * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
> - *
> * Ported by John George
> */
>
> @@ -29,13 +15,9 @@
> #include <pwd.h>
> #include <grp.h>
> #include <stdlib.h>
> -#include <string.h>
>
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "compat_16.h"
> -
> -TCID_DEFINE(setregid02);
> +#include "tst_test.h"
> +#include "compat_tst_16.h"
>
> static gid_t neg_one = -1;
>
> @@ -48,14 +30,14 @@ static struct group ltpgroup, root, bin;
> * is used for a separate test. The tests are executed in the for loop below.
> */
>
> -struct test_data_t {
> +static struct tcase {
> gid_t *real_gid;
> gid_t *eff_gid;
> int exp_errno;
> struct group *exp_real_usr;
> struct group *exp_eff_usr;
> char *test_msg;
> -} test_data[] = {
> +} tcases[] = {
> {
> &neg_one, &root.gr_gid, EPERM, <pgroup, <pgroup,
> "After setregid(-1, root),"}, {
> @@ -71,113 +53,79 @@ struct test_data_t {
> "After setregid(bin, root),"}
> };
>
> -int TST_TOTAL = ARRAY_SIZE(test_data);
> -
> -static void setup(void);
> static void gid_verify(struct group *ru, struct group *eu, char *when);
> static struct group get_group_by_name(const char *name);
> static struct group get_group_by_gid(gid_t gid);
>
> -int main(int ac, char **av)
> +static void run(unsigned int n)
> {
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - int i;
> -
> - tst_count = 0;
> -
> - for (i = 0; i < TST_TOTAL; i++) {
> - /* Set the real or effective group id */
> - TEST(SETREGID(NULL, *test_data[i].real_gid,
> - *test_data[i].eff_gid));
> -
> - if (TEST_RETURN == -1) {
> - if (TEST_ERRNO == test_data[i].exp_errno) {
> - tst_resm(TPASS, "setregid(%d, %d) "
> - "failed as expected.",
> - *test_data[i].real_gid,
> - *test_data[i].eff_gid);
> - } else {
> - tst_resm(TFAIL, "setregid(%d, %d) "
> - "failed (%d) but did not set the "
> - "expected errno (%d).",
> - *test_data[i].real_gid,
> - *test_data[i].eff_gid,
> - TEST_ERRNO,
> - test_data[i].exp_errno);
> - }
> - } else {
> - tst_resm(TFAIL, "setregid(%d, %d) "
> - "did not fail (ret: %ld) as expected (ret: -1).",
> - *test_data[i].real_gid,
> - *test_data[i].eff_gid, TEST_RETURN);
> - }
> - gid_verify(test_data[i].exp_real_usr,
> - test_data[i].exp_eff_usr,
> - test_data[i].test_msg);
> + struct tcase *tc = &tcases[n];
> +
> + /* Set the real or effective group id */
> + TEST(SETREGID(*tc->real_gid, *tc->eff_gid));
> +
> + if (TST_RET == -1) {
> + if (TST_ERR == tc->exp_errno) {
Only a minor nit, but please run check_patch from the kernel on this
(e.g. linux/scripts/checkpatch.pl --no-tree -f setregid02.c)
WARNING: Comparisons should place the constant on the right side of the test
#68: FILE: setregid02.c:68:
+ if (TST_ERR == tc->exp_errno) {
I don't think all the warnings are worth fixing, but this seems reasonable.
> + tst_res(TPASS | TTERRNO, "setregid(%d, %d) failed "
> + "as expected", *tc->real_gid, *tc->eff_gid);
> + } else {
> + tst_res(TFAIL | TTERRNO, "setregid(%d, %d) failed "
> + "unexpectedly, expected %s", *tc->real_gid,
> + *tc->eff_gid, tst_strerrno(tc->exp_errno));
> }
> + } else {
> + tst_res(TFAIL, "setregid(%d, %d) did not fail (ret: %ld) "
> + "as expected (ret: -1).", *tc->real_gid,
> + *tc->eff_gid, TST_RET);
> }
> -
> - tst_exit();
> + gid_verify(tc->exp_real_usr, tc->exp_eff_usr, tc->test_msg);
> }
>
> static void setup(void)
> {
> - tst_require_root();
> + ltpuser = SAFE_GETPWNAM("nobody");
>
> - tst_sig(FORK, DEF_HANDLER, NULL);
> -
> - ltpuser = getpwnam("nobody");
> - if (ltpuser == NULL)
> - tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
> -
> - SAFE_SETGID(NULL, ltpuser->pw_gid);
> - SAFE_SETUID(NULL, ltpuser->pw_uid);
> + SAFE_SETGID(ltpuser->pw_gid);
> + SAFE_SETUID(ltpuser->pw_uid);
>
> root = get_group_by_name("root");
> ltpgroup = get_group_by_gid(ltpuser->pw_gid);
> bin = get_group_by_name("bin");
> -
> - TEST_PAUSE;
> }
>
> static struct group get_group_by_name(const char *name)
> {
> - struct group *ret = getgrnam(name);
> -
> - if (ret == NULL)
> - tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", name);
> + struct group *ret = SAFE_GETGRNAM(name);
>
> - GID16_CHECK(ret->gr_gid, setregid, NULL);
> + GID16_CHECK(ret->gr_gid, setregid);
>
> return *ret;
> }
>
> static struct group get_group_by_gid(gid_t gid)
> {
> - struct group *ret = getgrgid(gid);
> + struct group *ret = SAFE_GETGRGID(gid);
>
> - if (ret == NULL)
> - tst_brkm(TBROK|TERRNO, NULL, "getgrgid(\"%d\") failed", gid);
> -
> - GID16_CHECK(ret->gr_gid, setregid, NULL);
> + GID16_CHECK(ret->gr_gid, setregid);
>
> return *ret;
> }
> -
> void gid_verify(struct group *rg, struct group *eg, char *when)
> {
> if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
> - tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
> + tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
> when, getgid(), getegid());
> - tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
> + tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
> rg->gr_gid, eg->gr_gid);
> } else {
> - tst_resm(TPASS, "real or effective gid was modified as expected");
> + tst_res(TPASS,
> + "real or effective gid was modified as expected");
> }
> }
> +
> +static struct tst_test test = {
> + .tcnt = ARRAY_SIZE(tcases),
> + .needs_root = 1,
> + .test = run,
> + .setup = setup,
> +};
> --
> 2.16.4
--
Thank you,
Richard.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK()
2018-09-10 9:11 ` [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK() Clemens Famulla-Conrad
@ 2018-09-10 12:51 ` Richard Palethorpe
2018-09-10 12:54 ` Clemens Famulla-Conrad
0 siblings, 1 reply; 8+ messages in thread
From: Richard Palethorpe @ 2018-09-10 12:51 UTC (permalink / raw)
To: ltp
Hello,
Clemens Famulla-Conrad <cfamullaconrad@suse.de> writes:
> This function retrieves the group by name. If not exists fall back
> to the second name given. If the second group dosn't exists, exit
> with TBROK.
>
> Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
> ---
> include/tst_safe_macros.h | 5 +++++
> lib/tst_safe_macros.c | 16 ++++++++++++++++
> 2 files changed, 21 insertions(+)
>
> diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
> index d457ae92a..e8b68ce9d 100644
> --- a/include/tst_safe_macros.h
> +++ b/include/tst_safe_macros.h
> @@ -441,6 +441,11 @@ struct group *safe_getgrnam(const char *file, const int lineno,
> #define SAFE_GETGRNAM(name) \
> safe_getgrnam(__FILE__, __LINE__, (name))
>
> +struct group *safe_getgrnam_fallback(const char *file, const int lineno,
> + const char *name, const char *fallback);
> +#define SAFE_GETGRNAM_FALLBACK(name, fallback) \
> + safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
> +
> struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
> #define SAFE_GETGRGID(gid) \
> safe_getgrgid(__FILE__, __LINE__, (gid))
> diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
> index 2e041c460..614b6b4b5 100644
> --- a/lib/tst_safe_macros.c
> +++ b/lib/tst_safe_macros.c
> @@ -153,6 +153,22 @@ struct group *safe_getgrnam(const char *file, const int lineno,
> return rval;
> }
>
> +struct group *safe_getgrnam_fallback(const char *file, const int lineno,
> + const char *name, const char *fallback)
Indentation seems to be wrong
> +{
> + struct group *rval;
> +
> + rval = getgrnam(name);
> + if (rval == NULL) {
> + tst_res_(file, lineno, TINFO,
> + "getgrnam(%s) failed - try fallback %s",
> + name, fallback);
> + rval = safe_getgrnam(file, lineno, name);
^^ Shouldn't this be fallback?
> + }
> +
> + return rval;
> +}
> +
> struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
> {
> struct group *rval;
> --
> 2.16.4
--
Thank you,
Richard.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK()
2018-09-10 12:51 ` Richard Palethorpe
@ 2018-09-10 12:54 ` Clemens Famulla-Conrad
0 siblings, 0 replies; 8+ messages in thread
From: Clemens Famulla-Conrad @ 2018-09-10 12:54 UTC (permalink / raw)
To: ltp
On 9/10/18 2:51 PM, Richard Palethorpe wrote:
> Hello,
>
> Clemens Famulla-Conrad <cfamullaconrad@suse.de> writes:
>
>> This function retrieves the group by name. If not exists fall back
>> to the second name given. If the second group dosn't exists, exit
>> with TBROK.
>>
>> Signed-off-by: Clemens Famulla-Conrad <cfamullaconrad@suse.de>
>> ---
>> include/tst_safe_macros.h | 5 +++++
>> lib/tst_safe_macros.c | 16 ++++++++++++++++
>> 2 files changed, 21 insertions(+)
>>
>> diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
>> index d457ae92a..e8b68ce9d 100644
>> --- a/include/tst_safe_macros.h
>> +++ b/include/tst_safe_macros.h
>> @@ -441,6 +441,11 @@ struct group *safe_getgrnam(const char *file, const int lineno,
>> #define SAFE_GETGRNAM(name) \
>> safe_getgrnam(__FILE__, __LINE__, (name))
>>
>> +struct group *safe_getgrnam_fallback(const char *file, const int lineno,
>> + const char *name, const char *fallback);
>> +#define SAFE_GETGRNAM_FALLBACK(name, fallback) \
>> + safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
>> +
>> struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
>> #define SAFE_GETGRGID(gid) \
>> safe_getgrgid(__FILE__, __LINE__, (gid))
>> diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
>> index 2e041c460..614b6b4b5 100644
>> --- a/lib/tst_safe_macros.c
>> +++ b/lib/tst_safe_macros.c
>> @@ -153,6 +153,22 @@ struct group *safe_getgrnam(const char *file, const int lineno,
>> return rval;
>> }
>>
>> +struct group *safe_getgrnam_fallback(const char *file, const int lineno,
>> + const char *name, const char *fallback)
>
> Indentation seems to be wrong
>
>> +{
>> + struct group *rval;
>> +
>> + rval = getgrnam(name);
>> + if (rval == NULL) {
>> + tst_res_(file, lineno, TINFO,
>> + "getgrnam(%s) failed - try fallback %s",
>> + name, fallback);
>> + rval = safe_getgrnam(file, lineno, name);
> ^^ Shouldn't this be fallback?
yes for sure, thx!
>
>> + }
>> +
>> + return rval;
>> +}
>> +
>> struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
>> {
>> struct group *rval;
>> --
>> 2.16.4
>
>
--
Clemens Famulla-Conrad QA
cfamullaconrad@suse.de +49 30 23474951
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton,
HRB 21284 (AG Nürnberg)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2018-09-10 12:54 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-10 9:09 [LTP] [PATCH v1 1/5] setregid01: Convert to newlib Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 2/5] tst_safe_macros: add SAFE_GETGRGID() Clemens Famulla-Conrad
2018-09-10 9:10 ` [LTP] [PATCH v1 3/5] setregid02: Convert to newlib Clemens Famulla-Conrad
2018-09-10 12:44 ` Richard Palethorpe
2018-09-10 9:11 ` [LTP] [PATCH v1 4/5] tst_safe_macros: add SAFE_GETGRNAM_FALLBACK() Clemens Famulla-Conrad
2018-09-10 12:51 ` Richard Palethorpe
2018-09-10 12:54 ` Clemens Famulla-Conrad
2018-09-10 9:11 ` [LTP] [PATCH v1 5/5] setregid04: Convert to newlib Clemens Famulla-Conrad
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.