Skip to contentSkip to conversation

SAFE_MACROS: Add SAFE_GETGRNAM()

PATCH Superseded Jinhui Huang 3 participants 8 messages Mar 16, 2018 – Jun 15, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 SAFE_MACROS: Add SAFE_GETGRNAM()

---
 include/old/safe_macros.h |  3 +++
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  9 +++------
 lib/safe_macros.c         | 15 +++++++++++++++
 4 files changed, 24 insertions(+), 6 deletions(-)
include/old/safe_macros.h +3 -0
index e778d30..0718060 100644
@@ -44,6 +44,9 @@
 #define SAFE_GETPWNAM(cleanup_fn, name)	\
 	safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
 
+#define SAFE_GETGRNAM(cleanup_fn, name) \
+	safe_getgrnam(__FILE__, __LINE__, cleanup_fn, (name))
+
 #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
 	safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
 
include/safe_macros_fn.h +3 -0
index 3df9528..2d3ddae 100644
@@ -45,6 +45,9 @@ char* safe_getcwd(const char *file, const int lineno,
 struct passwd* safe_getpwnam(const char *file, const int lineno,
                              void (*cleanup_fn)(void), const char *name);
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn)(void), const char *name);
+
 int safe_getrusage(const char *file, const int lineno,
                    void (*cleanup_fn)(void), int who, struct rusage *usage);
 
include/tst_safe_macros.h +3 -6
index f115a7b..56ff4c3 100644
@@ -72,6 +72,9 @@ static inline int safe_dup(const char *file, const int lineno,
 #define SAFE_GETPWNAM(name) \
 	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
 
+#define SAFE_GETGRNAM(name) \
+	safe_getgrnam(__FILE__, __LINE__, NULL, (name))
+
 #define SAFE_GETRUSAGE(who, usage) \
 	safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
 
@@ -397,12 +400,6 @@ static inline sighandler_t safe_signal(const char *file, const int lineno,
 #define SAFE_SIGNAL(signum, handler) \
 	safe_signal(__FILE__, __LINE__, (signum), (handler))
 
-int safe_sigaction(const char *file, const int lineno,
-                   int signum, const struct sigaction *act,
-                   struct sigaction *oldact);
-#define SAFE_SIGACTION(signum, act, oldact) \
-	safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
-
 #define SAFE_EXECLP(file, arg, ...) do {                   \
 	execlp((file), (arg), ##__VA_ARGS__);              \
 	tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,       \
lib/safe_macros.c +15 -0
index abdeca0..061819e 100644
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <grp.h>
 #include "test.h"
 #include "safe_macros.h"
 
@@ -126,6 +127,20 @@ struct passwd *safe_getpwnam(const char *file, const int lineno,
 	return rval;
 }
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn) (void), const char *name)
+{
+	struct group *rval;
+
+	rval = getgrnam(name);
+	if (rval == NULL) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			"%s:%d: getgrnam(%s) failed", file, lineno, name);
+	}
+
+	return rval;
+}
+
 int
 safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
 	       int who, struct rusage *usage)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 2/2 Make use of SAFE_GETGRNAM()

---
 testcases/kernel/syscalls/chmod/chmod05.c       |  4 +---
 testcases/kernel/syscalls/chmod/chmod07.c       |  3 +--
 testcases/kernel/syscalls/creat/creat08.c       | 16 +++++-----------
 testcases/kernel/syscalls/fchmod/fchmod02.c     |  4 +---
 testcases/kernel/syscalls/fchmod/fchmod05.c     |  5 +----
 testcases/kernel/syscalls/open/open10.c         | 14 ++++----------
 testcases/kernel/syscalls/setregid/setregid02.c |  5 +----
 testcases/kernel/syscalls/setregid/setregid03.c |  5 +----
 testcases/kernel/syscalls/setregid/setregid04.c |  6 ++----
 9 files changed, 17 insertions(+), 45 deletions(-)
testcases/kernel/syscalls/chmod/chmod05.c +1 -3
index 3cf4db5..c3bea4e 100644
@@ -187,9 +187,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "getpwnam(\"nobody\") failed");
 
-	bin_group = getgrnam("bin");
-	if (bin_group == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "getgrnam(\"bin\") failed");
+	bin_group = SAFE_GETGRNAM(cleanup, "bin");
 
 	/*
 	 * Create a test directory under temporary directory with specified
testcases/kernel/syscalls/chmod/chmod07.c +1 -2
index 6a39388..ae52b21 100644
@@ -174,8 +174,7 @@ void setup(void)
 	user1_uid = ltpuser->pw_uid;
 
 	/* Get the group id of guest user - ltpuser1 */
-	if ((ltpgroup = getgrnam(LTPGRP)) == NULL)
-		tst_brkm(TBROK, cleanup, "getgrnam failed");
+	ltpgroup = SAFE_GETGRNAM(cleanup, LTPGRP);
 	group1_gid = ltpgroup->gr_gid;
 
 	fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
testcases/kernel/syscalls/creat/creat08.c +5 -11
index 50f2b39..377dff1 100644
@@ -119,18 +119,12 @@ int main(int ac, char **av)
 		/*
 		 * Get the group IDs of group1 and group2.
 		 */
-		if ((group = getgrnam("nobody")) == NULL) {
-			if ((group = getgrnam("nogroup")) == NULL) {
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "getgrnam(\"nobody\") and "
-					 "getgrnam(\"nogroup\") failed");
-			}
-		}
+		group = getgrnam("nobody");
+		if (group == NULL)
+			group = SAFE_GETGRNAM(cleanup, "nogroup");
+
 		group1_gid = group->gr_gid;
-		if ((group = getgrnam("bin")) == NULL) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "getgrnam(\"bin\") failed");
-		}
+		group = SAFE_GETGRNAM(cleanup, "bin");
 		group2_gid = group->gr_gid;
 
 /*--------------------------------------------------------------*/
testcases/kernel/syscalls/fchmod/fchmod02.c +1 -3
index ea2bdb4..f9f8ab3 100644
@@ -177,9 +177,7 @@ void setup(void)
 	user1_uid = ltpuser->pw_uid;
 
 	/* Get the group id of guest user - ltpuser1 */
-	if ((ltpgroup = getgrnam(LTPGRP)) == NULL) {
-		tst_brkm(TBROK, cleanup, "%s not in /etc/group", LTPGRP);
-	}
+	ltpgroup = SAFE_GETGRNAM(cleanup, LTPGRP);
 	group1_gid = ltpgroup->gr_gid;
 
 	/*
testcases/kernel/syscalls/fchmod/fchmod05.c +1 -4
index cdd3d07..93b4fc5 100644
@@ -178,10 +178,7 @@ void setup(void)
 		tst_brkm(TBROK, cleanup,
 			 "Couldn't find uid of nobody: %s", strerror(errno));
 
-	bin_group = getgrnam("bin");
-	if (!bin_group)
-		tst_brkm(TBROK, cleanup,
-			 "Couldn't find gid of bin: %s", strerror(errno));
+	bin_group = SAFE_GETGRNAM(cleanup, "bin");
 
 	/*
 	 * Create a test directory under temporary directory with specified
testcases/kernel/syscalls/open/open10.c +4 -10
index 613f228..2c1a588 100644
@@ -42,6 +42,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "open10";
 int TST_TOTAL = 1;
@@ -106,18 +107,11 @@ int main(int ac, char *av[])
 		 * Get the group IDs of group1 and group2.
 		 */
 		group = getgrnam("nobody");
-		if (group == NULL) {
-			group = getgrnam("nogroup");
-			if (group == NULL) {
-				tst_brkm(TBROK, cleanup,
-					 "nobody/nogroup not in /etc/group");
-			}
-		}
-		group1_gid = group->gr_gid;
-		group = getgrnam("bin");
 		if (group == NULL)
-			tst_brkm(TBROK, cleanup, "bin not in /etc/group");
+			group = SAFE_GETGRNAM(cleanup, "nogroup");
 
+		group1_gid = group->gr_gid;
+		group = SAFE_GETGRNAM(cleanup, "bin");
 		group2_gid = group->gr_gid;
 
 		/*
testcases/kernel/syscalls/setregid/setregid02.c +1 -4
index 21d1c82..d7e141d 100644
@@ -148,10 +148,7 @@ static void setup(void)
 
 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(NULL, name);
 
 	GID16_CHECK(ret->gr_gid, setregid, NULL);
 
testcases/kernel/syscalls/setregid/setregid03.c +1 -4
index a51719e..6cadee5 100644
@@ -186,10 +186,7 @@ static void setup(void)
 	nobody = *(getpwnam("nobody"));
 
 #define GET_GID(group) do { \
-	junk = getgrnam(#group); \
-	if (junk == NULL) { \
-		tst_brkm(TBROK, NULL, "%s must be a valid group", #group); \
-	} \
+	junk = SAFE_GETGRNAM(NULL, #group); \
 	GID16_CHECK(junk->gr_gid, setregid, NULL); \
 	group = *(junk); \
 } while (0)
testcases/kernel/syscalls/setregid/setregid04.c +2 -4
index 0e0aae7..71f3c8b 100644
@@ -30,6 +30,7 @@
 
 #include "test.h"
 #include "compat_16.h"
+#include "safe_macros.h"
 
 TCID_DEFINE(setregid04);
 
@@ -111,10 +112,7 @@ int main(int ac, char **av)
 }
 
 #define SAFE_GETGROUP(GROUPNAME)	\
-	if (getgrnam(#GROUPNAME) == NULL) { \
-		tst_brkm(TBROK, NULL, "Couldn't find the `" #GROUPNAME "' group"); \
-	} \
-	GROUPNAME ## _gr = *(getgrnam(#GROUPNAME));
+	GROUPNAME ## _gr = *(SAFE_GETGRNAM(NULL, #GROUPNAME));
 
 static void setup(void)
 {
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 Make use of SAFE_GETGRNAM()

Hi Jinhui,

Quoted 12 lines
---
 testcases/kernel/syscalls/chmod/chmod05.c       |  4 +---
 testcases/kernel/syscalls/chmod/chmod07.c       |  3 +--
 testcases/kernel/syscalls/creat/creat08.c       | 16 +++++-----------
 testcases/kernel/syscalls/fchmod/fchmod02.c     |  4 +---
 testcases/kernel/syscalls/fchmod/fchmod05.c     |  5 +----
 testcases/kernel/syscalls/open/open10.c         | 14 ++++----------
 testcases/kernel/syscalls/setregid/setregid02.c |  5 +----
 testcases/kernel/syscalls/setregid/setregid03.c |  5 +----
 testcases/kernel/syscalls/setregid/setregid04.c |  6 ++----
 9 files changed, 17 insertions(+), 45 deletions(-)

I took only change in fchmod02 and adjusted it into new API.

Thanks for both patches!

Kind regards,
Petr

kernel/syscalls/fchmod/fchmod02.c +1 -3
index c41fd39e3..a855f9567 100644
@@ -54,9 +54,7 @@ static void setup(void)
 	struct group *ltpgroup;
 
 	ltpuser = SAFE_GETPWNAM(LTPUSER);
-	ltpgroup = getgrnam(LTPGRP);
-	if (ltpgroup == NULL)
-		tst_brk(TBROK, "%s not in /etc/group", LTPGRP);
+	ltpgroup = SAFE_GETGRNAM(LTPGRP);
 
 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
 	SAFE_CHOWN(TESTFILE, ltpuser->pw_uid, ltpgroup->gr_gid);
#3 lore raw

PATCH 1/2 SAFE_MACROS: Add SAFE_GETGRNAM()

In reply to Jinhui Huang

Hi Jinhui,

Quoted 13 lines
---
 include/old/safe_macros.h |  3 +++
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  9 +++------
 lib/safe_macros.c         | 15 +++++++++++++++
 4 files changed, 24 insertions(+), 6 deletions(-)
include/old/safe_macros.h +0 -0
index e778d30..0718060 100644

include/old/safe_macros.h line 48

Quoted 5 lines
include/old/safe_macros.h +2 -0
@@ -44,6 +44,9 @@
 #define SAFE_GETPWNAM(cleanup_fn, name)	\
 	safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
 
+#define SAFE_GETGRNAM(cleanup_fn, name) \
+	safe_getgrnam(__FILE__, __LINE__, cleanup_fn, (name))

Can you please sort it alphabetically (i.e. put SAFE_GETGRNAM() before SAFE_GETPWNAM())?

@Cyril: Do we want to change also safe_macros.h ?

Quoted 9 lines
include/old/safe_macros.h +1 -0
+
 #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
 	safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
 
include/safe_macros_fn.h +0 -0
index 3df9528..2d3ddae 100644

include/safe_macros_fn.h char* safe_getcwd(const char *file, const int lineno, · line 49

Quoted 5 lines
include/safe_macros_fn.h +2 -0
@@ -45,6 +45,9 @@ char* safe_getcwd(const char *file, const int lineno,
 struct passwd* safe_getpwnam(const char *file, const int lineno,
                              void (*cleanup_fn)(void), const char *name);
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn)(void), const char *name);

Again, sort it alphabetically please.

Quoted 9 lines
include/safe_macros_fn.h +1 -0
+
 int safe_getrusage(const char *file, const int lineno,
                    void (*cleanup_fn)(void), int who, struct rusage *usage);
 
include/tst_safe_macros.h +0 -0
index f115a7b..56ff4c3 100644

include/tst_safe_macros.h static inline int safe_dup(const char *file, const int lineno, · line 76

Quoted 5 lines
include/tst_safe_macros.h +2 -0
@@ -72,6 +72,9 @@ static inline int safe_dup(const char *file, const int lineno,
 #define SAFE_GETPWNAM(name) \
 	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
 
+#define SAFE_GETGRNAM(name) \
+	safe_getgrnam(__FILE__, __LINE__, NULL, (name))

And here.

Quoted 5 lines
include/tst_safe_macros.h +1 -0
+
 #define SAFE_GETRUSAGE(who, usage) \
 	safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
 

include/tst_safe_macros.h static inline sighandler_t safe_signal(const char *file, const int lineno, · line 405

Quoted 9 lines
include/tst_safe_macros.h +0 -6
@@ -397,12 +400,6 @@ static inline sighandler_t safe_signal(const char *file, const int lineno,
 #define SAFE_SIGNAL(signum, handler) \
 	safe_signal(__FILE__, __LINE__, (signum), (handler))
 
-int safe_sigaction(const char *file, const int lineno,
-                   int signum, const struct sigaction *act,
-                   struct sigaction *oldact);
-#define SAFE_SIGACTION(signum, act, oldact) \
-	safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
-

Removing SAFE_SIGACTION() & safe_sigaction() breaks build. I guess you removed it by accident.

Quoted 16 lines
include/tst_safe_macros.h +0 -0
 #define SAFE_EXECLP(file, arg, ...) do {                   \
 	execlp((file), (arg), ##__VA_ARGS__);              \
 	tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,       \
lib/safe_macros.c +1 -0
index abdeca0..061819e 100644
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <grp.h>
 #include "test.h"
 #include "safe_macros.h"
 

lib/safe_macros.c struct passwd *safe_getpwnam(const char *file, const int lineno, · line 131

Quoted 5 lines
lib/safe_macros.c +2 -0
@@ -126,6 +127,20 @@ struct passwd *safe_getpwnam(const char *file, const int lineno,
 	return rval;
 }
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn) (void), const char *name)

Again, sort it alphabetically please.

Quoted 5 lines
lib/safe_macros.c +5 -0
+{
+	struct group *rval;
+
+	rval = getgrnam(name);
+	if (rval == NULL) {

lib/safe_macros.c struct passwd *safe_getpwnam(const char *file, const int lineno, · line 146

Quoted 10 lines
lib/safe_macros.c +7 -0
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			"%s:%d: getgrnam(%s) failed", file, lineno, name);
+	}
+
+	return rval;
+}
+
 int
 safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
 	       int who, struct rusage *usage)

Kind regards,
Petr

#4 lore raw

PATCH 1/2 SAFE_MACROS: Add SAFE_GETGRNAM()

Hi!

Quoted 5 lines

include/old/safe_macros.h line 48

Quoted 2 lines
include/old/safe_macros.h +2 -0
+#define SAFE_GETGRNAM(cleanup_fn, name) \
+	safe_getgrnam(__FILE__, __LINE__, cleanup_fn, (name))

Can you please sort it alphabetically (i.e. put SAFE_GETGRNAM() before SAFE_GETPWNAM())?

@Cyril: Do we want to change also safe_macros.h ?

We are not supposed to add anything into oldlib safe macros, the old library is in a maintenance mode, only bugfixes should go there, no new functionality.

Signature · 2 lines
Cyril Hrubis
[email protected]
#5 lore raw

PATCH 1/2 SAFE_MACROS: Add SAFE_GETGRNAM()

In reply to Petr Vorel

To Petr

I will write V2 as you suggested,
I am sorry to reply so late.

Kind Regards,
Jinhui

On 2018/03/26 21:22, Petr Vorel Wrote:

Quoted 108 lines

Hi Jinhui,

Quoted 12 lines
---
  include/old/safe_macros.h |  3 +++
  include/safe_macros_fn.h  |  3 +++
  include/tst_safe_macros.h |  9 +++------
  lib/safe_macros.c         | 15 +++++++++++++++
  4 files changed, 24 insertions(+), 6 deletions(-)
include/old/safe_macros.h +0 -0
index e778d30..0718060 100644

include/old/safe_macros.h line 47

Quoted 4 lines
include/old/safe_macros.h +2 -0
@@ -44,6 +44,9 @@
  #define SAFE_GETPWNAM(cleanup_fn, name)	\
  	safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
+#define SAFE_GETGRNAM(cleanup_fn, name) \
+	safe_getgrnam(__FILE__, __LINE__, cleanup_fn, (name))

Can you please sort it alphabetically (i.e. put SAFE_GETGRNAM() before SAFE_GETPWNAM())?

@Cyril: Do we want to change also safe_macros.h ?

include/safe_macros_fn.h char* safe_getcwd(const char *file, const int lineno, · line 48

Quoted 12 lines
include/old/safe_macros.h +1 -0
+
  #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
  	safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
include/safe_macros_fn.h +2 -0
index 3df9528..2d3ddae 100644
@@ -45,6 +45,9 @@ char* safe_getcwd(const char *file, const int lineno,
  struct passwd* safe_getpwnam(const char *file, const int lineno,
                               void (*cleanup_fn)(void), const char *name);
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn)(void), const char *name);

Again, sort it alphabetically please.

include/tst_safe_macros.h static inline int safe_dup(const char *file, const int lineno, · line 75

Quoted 12 lines
include/safe_macros_fn.h +1 -0
+
  int safe_getrusage(const char *file, const int lineno,
                     void (*cleanup_fn)(void), int who, struct rusage *usage);
include/tst_safe_macros.h +2 -0
index f115a7b..56ff4c3 100644
@@ -72,6 +72,9 @@ static inline int safe_dup(const char *file, const int lineno,
  #define SAFE_GETPWNAM(name) \
  	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
+#define SAFE_GETGRNAM(name) \
+	safe_getgrnam(__FILE__, __LINE__, NULL, (name))

And here.

include/tst_safe_macros.h static inline sighandler_t safe_signal(const char *file, const int lineno, · line 404

Quoted 12 lines
include/tst_safe_macros.h +1 -6
+
  #define SAFE_GETRUSAGE(who, usage) \
  	safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
@@ -397,12 +400,6 @@ static inline sighandler_t safe_signal(const char *file, const int lineno,
  #define SAFE_SIGNAL(signum, handler) \
  	safe_signal(__FILE__, __LINE__, (signum), (handler))
-int safe_sigaction(const char *file, const int lineno,
-                   int signum, const struct sigaction *act,
-                   struct sigaction *oldact);
-#define SAFE_SIGACTION(signum, act, oldact) \
-	safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
-

Removing SAFE_SIGACTION()& safe_sigaction() breaks build. I guess you removed it by accident.

Quoted 15 lines
include/tst_safe_macros.h +0 -0
  #define SAFE_EXECLP(file, arg, ...) do {                   \
  	execlp((file), (arg), ##__VA_ARGS__);              \
  	tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,       \
lib/safe_macros.c +1 -0
index abdeca0..061819e 100644
@@ -15,6 +15,7 @@
  #include<stdlib.h>
  #include<unistd.h>
  #include<malloc.h>
+#include<grp.h>
  #include "test.h"
  #include "safe_macros.h"

lib/safe_macros.c struct passwd *safe_getpwnam(const char *file, const int lineno, · line 130

Quoted 4 lines
lib/safe_macros.c +2 -0
@@ -126,6 +127,20 @@ struct passwd *safe_getpwnam(const char *file, const int lineno,
  	return rval;
  }
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn) (void), const char *name)

Again, sort it alphabetically please.

Quoted 5 lines
lib/safe_macros.c +5 -0
+{
+	struct group *rval;
+
+	rval = getgrnam(name);
+	if (rval == NULL) {

lib/safe_macros.c struct passwd *safe_getpwnam(const char *file, const int lineno, · line 146

Quoted 10 lines
lib/safe_macros.c +7 -0
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			"%s:%d: getgrnam(%s) failed", file, lineno, name);
+	}
+
+	return rval;
+}
+
  int
  safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
  	       int who, struct rusage *usage)

Kind regards,
Petr

.

#6 lore raw

PATCH v2 1/1 SAFE_MACROS: Add SAFE_GETGRNAM()

In reply to Jinhui Huang

From: Jinhui Huang <[email protected]>

---
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  3 +++
 lib/safe_macros.c         | 15 +++++++++++++++
 3 files changed, 21 insertions(+)
include/safe_macros_fn.h +3 -0
index 3df9528..74b3ec1 100644
@@ -42,6 +42,9 @@ char* safe_dirname(const char *file, const int lineno,
 char* safe_getcwd(const char *file, const int lineno,
                   void (*cleanup_fn)(void), char *buf, size_t size);
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn)(void), const char *name);
+
 struct passwd* safe_getpwnam(const char *file, const int lineno,
                              void (*cleanup_fn)(void), const char *name);
 
include/tst_safe_macros.h +3 -0
index 64cdbbf..a8c2393 100644
@@ -69,6 +69,9 @@ static inline int safe_dup(const char *file, const int lineno,
 #define SAFE_GETCWD(buf, size) \
 	safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
 
+#define SAFE_GETGRNAM(name) \
+	safe_getgrnam(__FILE__, __LINE__, NULL, (name))
+
 #define SAFE_GETPWNAM(name) \
 	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
 
lib/safe_macros.c +15 -0
index abdeca0..ea9e1f6 100644
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <grp.h>
 #include "test.h"
 #include "safe_macros.h"
 
@@ -111,6 +112,20 @@ char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
 	return rval;
 }
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    void (*cleanup_fn) (void), const char *name)
+{
+	struct group *rval;
+
+	rval = getgrnam(name);
+	if (rval == NULL) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			"%s:%d: getgrnam(%s) failed", file, lineno, name);
+	}
+
+	return rval;
+}
+
 struct passwd *safe_getpwnam(const char *file, const int lineno,
 			     void (*cleanup_fn) (void), const char *name)
 {
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v2 1/1 SAFE_MACROS: Add SAFE_GETGRNAM()

Hi Jinhui,

Quoted 4 lines

From: Jinhui Huang <[email protected]>

---

I accepted your patch, but had to adjusted it for use in new API only. See following diff, hope you don't mind (removing cleanup parameter and moving code into new API only functions).

Kind regards,
Petr

safe_macros_fn.h +0 -3
index 74b3ec192..3df952811 100644
@@ -42,9 +42,6 @@ char* safe_dirname(const char *file, const int lineno,
 char* safe_getcwd(const char *file, const int lineno,
                   void (*cleanup_fn)(void), char *buf, size_t size);
 
-struct group *safe_getgrnam(const char *file, const int lineno,
-			    void (*cleanup_fn)(void), const char *name);
-
 struct passwd* safe_getpwnam(const char *file, const int lineno,
                              void (*cleanup_fn)(void), const char *name);
 
tst_safe_macros.h +6 -3
index a8c23934d..75c2a0803 100644
@@ -30,6 +30,7 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <grp.h>
 
 #include "safe_macros_fn.h"
 
@@ -69,9 +70,6 @@ static inline int safe_dup(const char *file, const int lineno,
 #define SAFE_GETCWD(buf, size) \
 	safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
 
-#define SAFE_GETGRNAM(name) \
-	safe_getgrnam(__FILE__, __LINE__, NULL, (name))
-
 #define SAFE_GETPWNAM(name) \
 	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
 
@@ -434,6 +432,11 @@ int safe_getpriority(const char *file, const int lineno, int which, id_t who);
 #define SAFE_GETPRIORITY(which, who) \
 	safe_getpriority(__FILE__, __LINE__, (which), (who))
 
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    const char *name);
+#define SAFE_GETGRNAM(name) \
+	safe_getgrnam(__FILE__, __LINE__, (name))
+
 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) \
safe_macros.c +0 -15
index ea9e1f63c..abdeca013 100644
@@ -15,7 +15,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <malloc.h>
-#include <grp.h>
 #include "test.h"
 #include "safe_macros.h"
 
@@ -112,20 +111,6 @@ char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
 	return rval;
 }
 
-struct group *safe_getgrnam(const char *file, const int lineno,
-			    void (*cleanup_fn) (void), const char *name)
-{
-	struct group *rval;
-
-	rval = getgrnam(name);
-	if (rval == NULL) {
-		tst_brkm(TBROK | TERRNO, cleanup_fn,
-			"%s:%d: getgrnam(%s) failed", file, lineno, name);
-	}
-
-	return rval;
-}
-
 struct passwd *safe_getpwnam(const char *file, const int lineno,
 			     void (*cleanup_fn) (void), const char *name)
 {
tst_safe_macros.c +14 -0
index 034f91751..e152bff7f 100644
@@ -138,3 +138,17 @@ int safe_sigaction(const char *file, const int lineno,
 
 	return rval;
 }
+
+struct group *safe_getgrnam(const char *file, const int lineno,
+			    const char *name)
+{
+	struct group *rval;
+
+	rval = getgrnam(name);
+	if (rval == NULL) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"getgrnam(%s) failed", name);
+	}
+
+	return rval;
+}