Skip to contentSkip to conversation

lapi/sem.h: Add SEM_STAT_ANY

PATCH New Feiyu Zhu 2 participants 4 messages Dec 24, 2020 – Dec 30, 2020 Library code

lore
Share & tools
lore

Feiyu Zhu

author
#0 lore raw

PATCH v2 1/3 lapi/sem.h: Add SEM_STAT_ANY

1) Rename semun.h to sem.h and apply it
2) Add SEM_STAT_ANY

---
 include/ipcsem.h                                |  2 +-
 include/lapi/sem.h                              | 28 +++++++++++++++++++++++++
 include/lapi/semun.h                            | 22 -------------------
 testcases/kernel/containers/sysvipc/sem_comm.c  |  3 +--
 testcases/kernel/ipc/pipeio/pipeio.c            |  3 +--
 testcases/kernel/ipc/semaphore/sem01.c          |  3 +--
 testcases/kernel/ipc/semaphore/sem02.c          |  3 +--
 testcases/kernel/sched/process_stress/process.c |  3 +--
 testcases/kernel/syscalls/ipc/semctl/semctl08.c |  2 +-
 testcases/kernel/syscalls/ipc/semop/semop01.c   |  3 +--
 testcases/kernel/syscalls/ipc/semop/semop02.c   |  3 +--
 testcases/kernel/syscalls/ipc/semop/semop03.c   |  3 +--
 testcases/kernel/syscalls/sendmsg/sendmsg02.c   |  3 +--
 13 files changed, 39 insertions(+), 42 deletions(-)
 create mode 100644 include/lapi/sem.h
 delete mode 100644 include/lapi/semun.h
include/ipcsem.h +1 -1
index 6a37672..09a0b3c 100644
@@ -29,7 +29,7 @@
 #include <sys/sem.h>
 
 #include "test.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 void cleanup(void);
 void setup(void);
include/lapi/sem.h new file +28 -0
new file mode 100644
index 0000000..7cef3d5
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2015 Linux Test Project
+ */
+
+#include <sys/sem.h>
+
+#ifndef LAPI_SEM_H
+#define LAPI_SEM_H
+
+#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
+/* union semun is defined by including <sys/sem.h> */
+#else
+/* according to X/OPEN we have to define it ourselves */
+union semun {
+	int val;                /* value for SETVAL */
+	struct semid_ds *buf;   /* buffer for IPC_STAT, IPC_SET */
+	unsigned short *array;  /* array for GETALL, SETALL */
+	/* Linux specific part: */
+	struct seminfo *__buf;  /* buffer for IPC_INFO */
+};
+#endif
+
+#ifndef SEM_STAT_ANY
+# define SEM_STAT_ANY 20
+#endif
+
+#endif /* LAPI_SEM_H */
include/lapi/semun.h deleted +0 -22
deleted file mode 100644
index 1a9dc98..0000000
@@ -1,22 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- * Copyright (c) 2015 Linux Test Project
- */
-
-#ifndef SEMUN_H__
-#define SEMUN_H__
-
-#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
-/* union semun is defined by including <sys/sem.h> */
-#else
-/* according to X/OPEN we have to define it ourselves */
-union semun {
-	int val;                /* value for SETVAL */
-	struct semid_ds *buf;   /* buffer for IPC_STAT, IPC_SET */
-	unsigned short *array;  /* array for GETALL, SETALL */
-	/* Linux specific part: */
-	struct seminfo *__buf;  /* buffer for IPC_INFO */
-};
-#endif
-
-#endif /* SEMUN_H__ */
testcases/kernel/containers/sysvipc/sem_comm.c +1 -2
index 09cc163..a2c354a 100644
@@ -27,7 +27,6 @@
 
 #define _GNU_SOURCE
 #include <sys/ipc.h>
-#include <sys/sem.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <stdio.h>
@@ -35,7 +34,7 @@
 #include "ipcns_helper.h"
 #include "test.h"
 #include "safe_macros.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 #define TESTKEY 124426L
 char *TCID	= "sem_comm";
testcases/kernel/ipc/pipeio/pipeio.c +1 -2
index b9421cb..ab5c2cf 100644
@@ -47,13 +47,12 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/stat.h>
-#include <sys/sem.h>
 
 #include "tlibio.h"
 
 #include "test.h"
 #include "safe_macros.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 char *TCID = "pipeio";
 int TST_TOTAL = 1;
testcases/kernel/ipc/semaphore/sem01.c +1 -2
index 5a45238..582624d 100644
@@ -40,8 +40,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/ipc.h>
-#include <sys/sem.h>
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 int verbose = 0;
 int loops = 100;
testcases/kernel/ipc/semaphore/sem02.c +1 -2
index 81e77d6..34b714b 100644
@@ -45,12 +45,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <sys/sem.h>
 #include <errno.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 #include "test.h"
 
 #define KEY IPC_PRIVATE
testcases/kernel/sched/process_stress/process.c +1 -2
index f23dcbb..a5ff809 100644
@@ -40,11 +40,10 @@
 #include <sys/msg.h>
 #include <sys/resource.h>
 #include <sys/select.h>
-#include <sys/sem.h>
 #include <sys/shm.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 /* indexes into environment variable array */
 #define ADBG 0
testcases/kernel/syscalls/ipc/semctl/semctl08.c +1 -1
index 40c2e17..1878bd4 100644
@@ -6,7 +6,7 @@
  * Cross verify the _high fields being set to 0 by the kernel.
  */
 #include "lapi/sembuf.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 #include "tst_test.h"
 #include "libnewipc.h"
 
testcases/kernel/syscalls/ipc/semop/semop01.c +1 -2
index b20eef4..2072635 100644
@@ -8,10 +8,9 @@
  */
 
 #include <stdlib.h>
-#include <sys/sem.h>
 #include "tst_test.h"
 #include "libnewipc.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 #include "semop.h"
 
 #define NSEMS 4
testcases/kernel/syscalls/ipc/semop/semop02.c +1 -2
index 2c3ec2c..d8181db 100644
@@ -22,10 +22,9 @@
 #define _GNU_SOURCE
 #include <pwd.h>
 #include <sys/ipc.h>
-#include <sys/sem.h>
 #include "tst_test.h"
 #include "libnewipc.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 #include "semop.h"
 
 static int valid_sem_id = -1;
testcases/kernel/syscalls/ipc/semop/semop03.c +1 -2
index 3a79ed3..636b715 100644
@@ -11,10 +11,9 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <sys/sem.h>
 #include "tst_test.h"
 #include "libnewipc.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 #include "semop.h"
 
 static key_t semkey;
testcases/kernel/syscalls/sendmsg/sendmsg02.c +1 -2
index aa3c88d..f72e9db 100644
@@ -34,7 +34,6 @@
 #define _GNU_SOURCE
 #include <sys/ipc.h>
 #include <sys/stat.h>
-#include <sys/sem.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
@@ -45,7 +44,7 @@
 #include "config.h"
 #include "test.h"
 #include "safe_macros.h"
-#include "lapi/semun.h"
+#include "lapi/sem.h"
 
 char *TCID = "sendmsg02";
 
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v2 2/3 Add SAFE_SEMCTL() and SAFE_SEMGET() macro

---
 include/tst_safe_sysv_ipc.h | 14 ++++++++++++++
 lib/tst_safe_sysv_ipc.c     | 47 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
include/tst_safe_sysv_ipc.h +14 -0
index 3e0e50e..bb65326 100644
@@ -9,6 +9,7 @@
 #include <sys/ipc.h>
 #include <sys/msg.h>
 #include <sys/shm.h>
+#include <sys/sem.h>
 
 int safe_msgget(const char *file, const int lineno, key_t key, int msgflg);
 #define SAFE_MSGGET(key, msgflg) \
@@ -51,4 +52,17 @@ int safe_shmctl(const char *file, const int lineno, int shmid, int cmd,
 	(shmid) = ((cmd) == IPC_RMID ? -1 : (shmid)); \
 	tst_ret_;})
 
+int safe_semget(const char *file, const int lineno, key_t key, int nsems,
+		int semflg);
+#define SAFE_SEMGET(key, nsems, semflg) \
+	safe_semget(__FILE__, __LINE__, (key), (nsems), (semflg))
+
+int safe_semctl(const char *file, const int lineno, int semid, int semnum,
+		int cmd, ...);
+#define SAFE_SEMCTL(semid, semnum, cmd, ...) ({ \
+	int tst_ret_ = safe_semctl(__FILE__, __LINE__, (semid), (semnum), \
+				(cmd), ##__VA_ARGS__); \
+	(semid) = ((cmd) == IPC_RMID ? -1 : (semid)); \
+	tst_ret_; })
+
 #endif /* TST_SAFE_SYSV_IPC_H__ */
lib/tst_safe_sysv_ipc.c +47 -0

56 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v2 3/3 syscalls/ipc: semctl09: add a test for SEM_STAT_ANY

In reply to Feiyu Zhu

Validate the content of the seminfo structure and the return value.

The return value is highest used index to a kernel table, so we call semctl() with SEM_STAT_ANY which shouldn't fail if the value is correct.

We also test SEM_STAT_ANY by calling semctl() directly by syscall(), because glibc have a bug that caused fails to pass the buffer specified by the caller to the kernel.

We parse /proc/sysvipc/sem and check that the information is consistent with the content of seminfo structure.

---
 runtest/syscalls                                |   1 +
 runtest/syscalls-ipc                            |   1 +
 testcases/kernel/syscalls/ipc/semctl/.gitignore |   1 +
 testcases/kernel/syscalls/ipc/semctl/Makefile   |   2 +-
 testcases/kernel/syscalls/ipc/semctl/semctl09.c | 197 ++++++++++++++++++++++++
 5 files changed, 201 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/ipc/semctl/semctl09.c
runtest/syscalls +1 -0
index 28174dd..1549916 100644
@@ -1179,6 +1179,7 @@ semctl05 semctl05
 semctl06 semctl06
 semctl07 semctl07
 semctl08 semctl08
+semctl09 semctl09
 
 semget01 semget01
 semget02 semget02
runtest/syscalls-ipc +1 -0
index 9524b1a..68fff40 100644
@@ -37,6 +37,7 @@ semctl05 semctl05
 semctl06 semctl06
 semctl07 semctl07
 semctl08 semctl08
+semctl09 semctl09
 
 semget01 semget01
 semget02 semget02
testcases/kernel/syscalls/ipc/semctl/.gitignore +1 -0
index 6189a04..87d8393 100644
@@ -6,3 +6,4 @@
 /semctl06
 /semctl07
 /semctl08
+/semctl09
testcases/kernel/syscalls/ipc/semctl/Makefile +1 -1
index f711e77..c6d8597 100644
@@ -8,6 +8,6 @@ LTPLIBS = ltpipc ltpnewipc
 include $(top_srcdir)/include/mk/testcases.mk
 
 semctl01 semctl02 semctl03 semctl04 semctl05 semctl06 semctl07: LTPLDLIBS = -lltpipc
-semctl08: LTPLDLIBS = -lltpnewipc
+semctl08 semctl09: LTPLDLIBS = -lltpnewipc
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +197 -0

197 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v2 3/3 syscalls/ipc: semctl09: add a test for SEM_STAT_ANY

Hi!
Merged this patchset, thanks!

Best Regards
Yang Xu

Quoted 15 lines

Validate the content of the seminfo structure and the return value.

The return value is highest used index to a kernel table, so we call semctl() with SEM_STAT_ANY which shouldn't fail if the value is correct.

We also test SEM_STAT_ANY by calling semctl() directly by syscall(), because glibc have a bug that caused fails to pass the buffer specified by the caller to the kernel.

We parse /proc/sysvipc/sem and check that the information is consistent with the content of seminfo structure.

---