• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Xiao yang <yangx.jy@cn.fujitsu.com>
4  */
5 
6 #include <sys/types.h>
7 #include <sys/ipc.h>
8 #include <sys/msg.h>
9 #include <sys/shm.h>
10 #define TST_NO_DEFAULT_MAIN
11 #include "tst_test.h"
12 #include "tst_safe_sysv_ipc.h"
13 
14 /*
15  * The IPC_STAT, IPC_SET and IPC_RMID can return either 0 or -1.
16  *
17  * Linux specific cmds either returns -1 on failure or positive integer
18  * either index into an kernel array or shared primitive indentifier.
19  */
ret_check(int cmd,int ret)20 static int ret_check(int cmd, int ret)
21 {
22 	switch (cmd) {
23 	case IPC_STAT:
24 	case IPC_SET:
25 	case IPC_RMID:
26 		return ret != 0;
27 	default:
28 		return ret == -1;
29 	}
30 }
31 
safe_msgget(const char * file,const int lineno,key_t key,int msgflg)32 int safe_msgget(const char *file, const int lineno, key_t key, int msgflg)
33 {
34 	int rval;
35 
36 	rval = msgget(key, msgflg);
37 	if (rval == -1) {
38 		tst_brk(TBROK | TERRNO, "%s:%d: msgget(%i, %x) failed",
39 			file, lineno, (int)key, msgflg);
40 	}
41 
42 	return rval;
43 }
44 
safe_msgsnd(const char * file,const int lineno,int msqid,const void * msgp,size_t msgsz,int msgflg)45 int safe_msgsnd(const char *file, const int lineno, int msqid, const void *msgp,
46 		size_t msgsz, int msgflg)
47 {
48 	int rval;
49 
50 	rval = msgsnd(msqid, msgp, msgsz, msgflg);
51 	if (rval == -1) {
52 		tst_brk(TBROK | TERRNO,
53 			"%s:%d: msgsnd(%i, %p, %zu, %x) failed",
54 			file, lineno, msqid, msgp, msgsz, msgflg);
55 	}
56 
57 	return rval;
58 }
59 
safe_msgrcv(const char * file,const int lineno,int msqid,void * msgp,size_t msgsz,long msgtyp,int msgflg)60 ssize_t safe_msgrcv(const char *file, const int lineno, int msqid, void *msgp,
61 		size_t msgsz, long msgtyp, int msgflg)
62 {
63 	ssize_t rval;
64 
65 	rval = msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
66 	if (rval == -1) {
67 		tst_brk(TBROK | TERRNO,
68 			"%s:%d: msgrcv(%i, %p, %zu, %li, %x) failed",
69 			file, lineno, msqid, msgp, msgsz, msgtyp, msgflg);
70 	}
71 
72 	return rval;
73 }
74 
safe_msgctl(const char * file,const int lineno,int msqid,int cmd,struct msqid_ds * buf)75 int safe_msgctl(const char *file, const int lineno, int msqid, int cmd,
76 		struct msqid_ds *buf)
77 {
78 	int rval;
79 
80 	rval = msgctl(msqid, cmd, buf);
81 	if (ret_check(cmd, rval)) {
82 		tst_brk(TBROK | TERRNO,
83 			"%s:%d: msgctl(%i, %i, %p) = %i failed",
84 			file, lineno, msqid, cmd, buf, rval);
85 	}
86 
87 
88 	return rval;
89 }
90 
safe_shmget(const char * file,const int lineno,key_t key,size_t size,int shmflg)91 int safe_shmget(const char *file, const int lineno, key_t key, size_t size,
92 		int shmflg)
93 {
94 	int rval;
95 
96 	rval = shmget(key, size, shmflg);
97 	if (rval == -1) {
98 		tst_brk(TBROK | TERRNO, "%s:%d: shmget(%i, %zu, %x) failed",
99 			file, lineno, (int)key, size, shmflg);
100 	}
101 
102 	return rval;
103 }
104 
safe_shmat(const char * file,const int lineno,int shmid,const void * shmaddr,int shmflg)105 void *safe_shmat(const char *file, const int lineno, int shmid,
106 		const void *shmaddr, int shmflg)
107 {
108 	void *rval;
109 
110 	rval = shmat(shmid, shmaddr, shmflg);
111 	if (rval == (void *)-1) {
112 		tst_brk(TBROK | TERRNO, "%s:%d: shmat(%i, %p, %x) failed",
113 			file, lineno, shmid, shmaddr, shmflg);
114 	}
115 
116 	return rval;
117 }
118 
safe_shmdt(const char * file,const int lineno,const void * shmaddr)119 int safe_shmdt(const char *file, const int lineno, const void *shmaddr)
120 {
121 	int rval;
122 
123 	rval = shmdt(shmaddr);
124 	if (rval == -1) {
125 		tst_brk(TBROK | TERRNO, "%s:%d: shmdt(%p) failed",
126 			file, lineno, shmaddr);
127 	}
128 
129 	return rval;
130 }
131 
safe_shmctl(const char * file,const int lineno,int shmid,int cmd,struct shmid_ds * buf)132 int safe_shmctl(const char *file, const int lineno, int shmid, int cmd,
133 		struct shmid_ds *buf)
134 {
135 	int rval;
136 
137 	rval = shmctl(shmid, cmd, buf);
138 	if (ret_check(cmd, rval)) {
139 		tst_brk(TBROK | TERRNO,
140 			"%s:%d: shmctl(%i, %i, %p) = %i failed",
141 			file, lineno, shmid, cmd, buf, rval);
142 	}
143 
144 	return rval;
145 }
146