1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_msgget.c,v 1.2 2014/02/27 00:59:50 joerg Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_msgget.c,v 1.2 2014/02/27 00:59:50 joerg Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <sys/msg.h>
3511be35a1SLionel Sambuc #include <sys/stat.h>
3611be35a1SLionel Sambuc #include <sys/sysctl.h>
3711be35a1SLionel Sambuc #include <sys/wait.h>
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc #include <atf-c.h>
4011be35a1SLionel Sambuc #include <errno.h>
4111be35a1SLionel Sambuc #include <pwd.h>
4211be35a1SLionel Sambuc #include <stdio.h>
4311be35a1SLionel Sambuc #include <stdlib.h>
4411be35a1SLionel Sambuc #include <string.h>
4511be35a1SLionel Sambuc #include <sysexits.h>
4611be35a1SLionel Sambuc #include <time.h>
4711be35a1SLionel Sambuc #include <unistd.h>
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc #define MSG_KEY 12345689
5011be35a1SLionel Sambuc
5111be35a1SLionel Sambuc static void clean(void);
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc static void
clean(void)5411be35a1SLionel Sambuc clean(void)
5511be35a1SLionel Sambuc {
5611be35a1SLionel Sambuc int id;
5711be35a1SLionel Sambuc
5811be35a1SLionel Sambuc if ((id = msgget(MSG_KEY, 0)) != -1)
5911be35a1SLionel Sambuc (void)msgctl(id, IPC_RMID, 0);
6011be35a1SLionel Sambuc }
6111be35a1SLionel Sambuc
6211be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(msgget_excl);
ATF_TC_HEAD(msgget_excl,tc)6311be35a1SLionel Sambuc ATF_TC_HEAD(msgget_excl, tc)
6411be35a1SLionel Sambuc {
6511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL");
6611be35a1SLionel Sambuc }
6711be35a1SLionel Sambuc
ATF_TC_BODY(msgget_excl,tc)6811be35a1SLionel Sambuc ATF_TC_BODY(msgget_excl, tc)
6911be35a1SLionel Sambuc {
7011be35a1SLionel Sambuc int id;
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc /*
7311be35a1SLionel Sambuc * Create a message queue and re-open it with
7411be35a1SLionel Sambuc * O_CREAT and IPC_EXCL set. This should fail.
7511be35a1SLionel Sambuc */
7611be35a1SLionel Sambuc id = msgget(MSG_KEY, IPC_CREAT | 0600);
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc if (id < 0)
7911be35a1SLionel Sambuc atf_tc_fail("failed to create message queue");
8011be35a1SLionel Sambuc
8111be35a1SLionel Sambuc errno = 0;
8211be35a1SLionel Sambuc
8311be35a1SLionel Sambuc if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1)
8411be35a1SLionel Sambuc atf_tc_fail("msgget(2) failed for IPC_EXCL");
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc ATF_REQUIRE(errno == EEXIST);
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc /*
8911be35a1SLionel Sambuc * However, the same call should succeed
9011be35a1SLionel Sambuc * when IPC_EXCL is not set in the flags.
9111be35a1SLionel Sambuc */
9211be35a1SLionel Sambuc id = msgget(MSG_KEY, IPC_CREAT | 0600);
9311be35a1SLionel Sambuc
9411be35a1SLionel Sambuc if (id < 0)
9511be35a1SLionel Sambuc atf_tc_fail("msgget(2) failed to re-open");
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
9811be35a1SLionel Sambuc }
9911be35a1SLionel Sambuc
ATF_TC_CLEANUP(msgget_excl,tc)10011be35a1SLionel Sambuc ATF_TC_CLEANUP(msgget_excl, tc)
10111be35a1SLionel Sambuc {
10211be35a1SLionel Sambuc clean();
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(msgget_exit);
ATF_TC_HEAD(msgget_exit,tc)10611be35a1SLionel Sambuc ATF_TC_HEAD(msgget_exit, tc)
10711be35a1SLionel Sambuc {
10811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
10911be35a1SLionel Sambuc "Test that XSI message queues are "
11011be35a1SLionel Sambuc "not removed when the process exits");
11111be35a1SLionel Sambuc }
11211be35a1SLionel Sambuc
ATF_TC_BODY(msgget_exit,tc)11311be35a1SLionel Sambuc ATF_TC_BODY(msgget_exit, tc)
11411be35a1SLionel Sambuc {
11511be35a1SLionel Sambuc int id, sta;
11611be35a1SLionel Sambuc pid_t pid;
11711be35a1SLionel Sambuc
11811be35a1SLionel Sambuc pid = fork();
11911be35a1SLionel Sambuc ATF_REQUIRE(pid >= 0);
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc if (pid == 0) {
12211be35a1SLionel Sambuc
12311be35a1SLionel Sambuc if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1)
12411be35a1SLionel Sambuc _exit(EXIT_FAILURE);
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc _exit(EXIT_SUCCESS);
12711be35a1SLionel Sambuc }
12811be35a1SLionel Sambuc
12911be35a1SLionel Sambuc (void)wait(&sta);
13011be35a1SLionel Sambuc
13111be35a1SLionel Sambuc if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
13211be35a1SLionel Sambuc atf_tc_fail("failed to create message queue");
13311be35a1SLionel Sambuc
13411be35a1SLionel Sambuc id = msgget(MSG_KEY, 0);
13511be35a1SLionel Sambuc
13611be35a1SLionel Sambuc if (id == -1)
13711be35a1SLionel Sambuc atf_tc_fail("message queue was removed on process exit");
13811be35a1SLionel Sambuc
13911be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc
ATF_TC_CLEANUP(msgget_exit,tc)14211be35a1SLionel Sambuc ATF_TC_CLEANUP(msgget_exit, tc)
14311be35a1SLionel Sambuc {
14411be35a1SLionel Sambuc clean();
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(msgget_init);
ATF_TC_HEAD(msgget_init,tc)14811be35a1SLionel Sambuc ATF_TC_HEAD(msgget_init, tc)
14911be35a1SLionel Sambuc {
15011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
15111be35a1SLionel Sambuc "Test that msgget(2) initializes data structures properly");
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
ATF_TC_BODY(msgget_init,tc)15411be35a1SLionel Sambuc ATF_TC_BODY(msgget_init, tc)
15511be35a1SLionel Sambuc {
15611be35a1SLionel Sambuc const uid_t uid = geteuid();
15711be35a1SLionel Sambuc const gid_t gid = getegid();
15811be35a1SLionel Sambuc struct msqid_ds msgds;
15911be35a1SLionel Sambuc time_t t;
16011be35a1SLionel Sambuc int id;
16111be35a1SLionel Sambuc
16211be35a1SLionel Sambuc (void)memset(&msgds, 0x9, sizeof(struct msqid_ds));
16311be35a1SLionel Sambuc
16411be35a1SLionel Sambuc t = time(NULL);
16511be35a1SLionel Sambuc id = msgget(MSG_KEY, IPC_CREAT | 0600);
16611be35a1SLionel Sambuc
16711be35a1SLionel Sambuc ATF_REQUIRE(id !=-1);
16811be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
16911be35a1SLionel Sambuc
17011be35a1SLionel Sambuc ATF_CHECK(msgds.msg_qnum == 0);
17111be35a1SLionel Sambuc ATF_CHECK(msgds.msg_lspid == 0);
17211be35a1SLionel Sambuc ATF_CHECK(msgds.msg_lrpid == 0);
17311be35a1SLionel Sambuc ATF_CHECK(msgds.msg_rtime == 0);
17411be35a1SLionel Sambuc ATF_CHECK(msgds.msg_stime == 0);
17511be35a1SLionel Sambuc ATF_CHECK(msgds.msg_perm.uid == uid);
17611be35a1SLionel Sambuc ATF_CHECK(msgds.msg_perm.gid == gid);
17711be35a1SLionel Sambuc ATF_CHECK(msgds.msg_perm.cuid == uid);
17811be35a1SLionel Sambuc ATF_CHECK(msgds.msg_perm.cgid == gid);
17911be35a1SLionel Sambuc ATF_CHECK(msgds.msg_perm.mode == 0600);
18011be35a1SLionel Sambuc
181*0a6a1f1dSLionel Sambuc if (llabs(t - msgds.msg_ctime) > 5)
18211be35a1SLionel Sambuc atf_tc_fail("msgget(2) initialized current time incorrectly");
18311be35a1SLionel Sambuc
18411be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
18511be35a1SLionel Sambuc }
18611be35a1SLionel Sambuc
ATF_TC_CLEANUP(msgget_init,tc)18711be35a1SLionel Sambuc ATF_TC_CLEANUP(msgget_init, tc)
18811be35a1SLionel Sambuc {
18911be35a1SLionel Sambuc clean();
19011be35a1SLionel Sambuc }
19111be35a1SLionel Sambuc
19211be35a1SLionel Sambuc ATF_TC(msgget_limit);
ATF_TC_HEAD(msgget_limit,tc)19311be35a1SLionel Sambuc ATF_TC_HEAD(msgget_limit, tc)
19411be35a1SLionel Sambuc {
19511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits");
19611be35a1SLionel Sambuc }
19711be35a1SLionel Sambuc
ATF_TC_BODY(msgget_limit,tc)19811be35a1SLionel Sambuc ATF_TC_BODY(msgget_limit, tc)
19911be35a1SLionel Sambuc {
20011be35a1SLionel Sambuc size_t len = sizeof(int);
20111be35a1SLionel Sambuc bool fail = false;
20211be35a1SLionel Sambuc int i, lim = 0;
20311be35a1SLionel Sambuc int *buf;
20411be35a1SLionel Sambuc
20511be35a1SLionel Sambuc if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0)
20611be35a1SLionel Sambuc atf_tc_skip("failed to read kern.ipc.msgmni sysctl");
20711be35a1SLionel Sambuc
20811be35a1SLionel Sambuc buf = calloc(lim + 1, sizeof(*buf));
20911be35a1SLionel Sambuc ATF_REQUIRE(buf != NULL);
21011be35a1SLionel Sambuc
21111be35a1SLionel Sambuc for (i = 0; i < lim; i++) {
21211be35a1SLionel Sambuc
21311be35a1SLionel Sambuc buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
21411be35a1SLionel Sambuc
21511be35a1SLionel Sambuc (void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]);
21611be35a1SLionel Sambuc
21711be35a1SLionel Sambuc /*
21811be35a1SLionel Sambuc * This test only works when there are zero existing
21911be35a1SLionel Sambuc * message queues. Thus, bypass the unit test when
22011be35a1SLionel Sambuc * this precondition is not met, for reason or another.
22111be35a1SLionel Sambuc */
22211be35a1SLionel Sambuc if (buf[i] == -1)
22311be35a1SLionel Sambuc goto out;
22411be35a1SLionel Sambuc }
22511be35a1SLionel Sambuc
22611be35a1SLionel Sambuc i++;
22711be35a1SLionel Sambuc errno = 0;
22811be35a1SLionel Sambuc
22911be35a1SLionel Sambuc buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
23011be35a1SLionel Sambuc
23111be35a1SLionel Sambuc if (buf[i] != -1 || errno != ENOSPC)
23211be35a1SLionel Sambuc fail = true;
23311be35a1SLionel Sambuc
23411be35a1SLionel Sambuc out: /* Remember to clean-up. */
23511be35a1SLionel Sambuc for (i = 0; i < lim; i++)
23611be35a1SLionel Sambuc (void)msgctl(buf[i], IPC_RMID, 0);
23711be35a1SLionel Sambuc
23811be35a1SLionel Sambuc free(buf);
23911be35a1SLionel Sambuc
24011be35a1SLionel Sambuc if (fail != false)
24111be35a1SLionel Sambuc atf_tc_fail("msgget(2) opened more than %d queues", lim);
24211be35a1SLionel Sambuc }
24311be35a1SLionel Sambuc
24411be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(msgget_mode);
ATF_TC_HEAD(msgget_mode,tc)24511be35a1SLionel Sambuc ATF_TC_HEAD(msgget_mode, tc)
24611be35a1SLionel Sambuc {
24711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)");
24811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "require.user", "root");
24911be35a1SLionel Sambuc }
25011be35a1SLionel Sambuc
ATF_TC_BODY(msgget_mode,tc)25111be35a1SLionel Sambuc ATF_TC_BODY(msgget_mode, tc)
25211be35a1SLionel Sambuc {
25311be35a1SLionel Sambuc static const mode_t mode[] = {
25411be35a1SLionel Sambuc S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP,
25511be35a1SLionel Sambuc S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
25611be35a1SLionel Sambuc };
25711be35a1SLionel Sambuc
25811be35a1SLionel Sambuc struct msqid_ds msgds;
25911be35a1SLionel Sambuc size_t i;
26011be35a1SLionel Sambuc int id;
26111be35a1SLionel Sambuc
26211be35a1SLionel Sambuc for (i = 0; i < __arraycount(mode); i++) {
26311be35a1SLionel Sambuc
26411be35a1SLionel Sambuc (void)fprintf(stderr, "testing mode %d\n", mode[i]);
26511be35a1SLionel Sambuc (void)memset(&msgds, 0, sizeof(struct msqid_ds));
26611be35a1SLionel Sambuc
26711be35a1SLionel Sambuc id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]);
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc ATF_REQUIRE(id != -1);
27011be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
27111be35a1SLionel Sambuc ATF_REQUIRE(msgds.msg_perm.mode == mode[i]);
27211be35a1SLionel Sambuc ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
27311be35a1SLionel Sambuc }
27411be35a1SLionel Sambuc }
27511be35a1SLionel Sambuc
ATF_TC_CLEANUP(msgget_mode,tc)27611be35a1SLionel Sambuc ATF_TC_CLEANUP(msgget_mode, tc)
27711be35a1SLionel Sambuc {
27811be35a1SLionel Sambuc clean();
27911be35a1SLionel Sambuc }
28011be35a1SLionel Sambuc
28111be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)28211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
28311be35a1SLionel Sambuc {
28411be35a1SLionel Sambuc
28511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, msgget_excl);
28611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, msgget_exit);
28711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, msgget_init);
28811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, msgget_limit);
28911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, msgget_mode);
29011be35a1SLionel Sambuc
29111be35a1SLionel Sambuc return atf_no_error();
29211be35a1SLionel Sambuc }
293