1/*
2 * Copyright (C) 2017 Petr Vorel pvorel@suse.cz
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef TST_SAFE_POSIX_IPC_H__
25#define TST_SAFE_POSIX_IPC_H__
26
27#include <mqueue.h>
28#include <stdarg.h>
29
30#define SAFE_MQ_OPEN(pathname, oflags, ...) \
31	safe_mq_open(__FILE__, __LINE__, (pathname), (oflags), ##__VA_ARGS__)
32
33static inline int safe_mq_open(const char *file, const int lineno,
34			       const char *pathname, int oflags, ...)
35{
36	va_list ap;
37	int rval;
38	mode_t mode;
39	struct mq_attr *attr;
40
41	va_start(ap, oflags);
42
43	/* Android's NDK's mode_t is smaller than an int, which results in
44	 * SIGILL here when passing the mode_t type.
45	 */
46#ifndef ANDROID
47	mode = va_arg(ap, mode_t);
48#else
49	mode = va_arg(ap, int);
50#endif
51
52	attr = va_arg(ap, struct mq_attr *);
53
54	va_end(ap);
55
56	rval = mq_open(pathname, oflags, mode, attr);
57	if (rval == -1) {
58		tst_brk(TBROK | TERRNO, "%s:%d: mq_open(%s,%d,0%o,%p) failed",
59			 file, lineno, pathname, oflags, mode, attr);
60	}
61
62	return rval;
63}
64
65#endif /* TST_SAFE_POSIX_IPC_H__ */
66