1 /* $NetBSD: t_sysv.c,v 1.4 2014/03/02 20:13:12 jmmv Exp $ */
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Test the SVID-compatible Message Queue facility.
51 #include <sys/param.h>
56 volatile int did_sigsys, did_sigchild;
57 volatile int child_status, child_count;
59 void sigsys_handler(int);
60 void sigchld_handler(int);
64 void print_msqid_ds(struct msqid_ds *, mode_t);
67 void print_semid_ds(struct semid_ds *, mode_t);
70 void print_shmid_ds(struct shmid_ds *, mode_t);
73 #define MESSAGE_TEXT_LEN 256
77 char mtext[MESSAGE_TEXT_LEN];
80 const char *m1_str = "California is overrated.";
81 const char *m2_str = "The quick brown fox jumped over the lazy dog.";
93 key_t msgkey, semkey, shmkey;
98 int val; /* value for SETVAL */
99 struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */
100 u_short *array; /* array for GETALL & SETALL */
104 /* Writes an integer to a file. To be used from the body of the test
105 * cases below to pass any global identifiers to the cleanup routine. */
107 write_int(const char *path, const int value)
111 output = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
112 ATF_REQUIRE_MSG(output != -1, "Failed to create %s", path);
113 write(output, &value, sizeof(value));
118 /* Reads an integer from a file. To be used from the cleanup routines
119 * of the test cases below. */
121 read_int(const char *path)
125 input = open(path, O_RDONLY);
130 ATF_REQUIRE_EQ(read(input, &value, sizeof(value)), sizeof(value));
138 sigsys_handler(int signo)
145 sigchld_handler(int signo)
151 * Reap the child and return its status
153 if (wait(&c_status) == -1)
154 child_status = -errno;
156 child_status = c_status;
161 key_t get_ftok(int id)
164 char token_key[64], token_dir[64];
168 strlcpy(token_key, "/tmp/t_sysv.XXXXXX", sizeof(token_key));
169 tmpdir = mkdtemp(token_key);
170 ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp() failed: %d", errno);
172 strlcpy(token_dir, tmpdir, sizeof(token_dir));
173 strlcpy(token_key, tmpdir, sizeof(token_key));
174 strlcat(token_key, "/token_key", sizeof(token_key));
176 /* Create the file, since ftok() requires it to exist! */
178 fd = open(token_key, O_RDWR | O_CREAT | O_EXCL, 0600);
181 atf_tc_fail("open() of temp file failed: %d", errno);
186 key = ftok(token_key, id);
188 ATF_REQUIRE_MSG(unlink(token_key) != -1, "unlink() failed: %d", errno);
189 ATF_REQUIRE_MSG(rmdir(token_dir) != -1, "rmdir() failed: %d", errno);
194 ATF_TC_WITH_CLEANUP(msg);
198 atf_tc_set_md_var(tc, "timeout", "3");
199 atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
205 struct msqid_ds m_ds;
213 * Install a SIGSYS handler so that we can exit gracefully if
214 * System V Message Queue support isn't in the kernel.
217 sa.sa_handler = sigsys_handler;
218 sigemptyset(&sa.sa_mask);
220 ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
221 "sigaction SIGSYS: %d", errno);
224 * Install a SIGCHLD handler to deal with all possible exit
225 * conditions of the receiver.
229 sa.sa_handler = sigchld_handler;
230 sigemptyset(&sa.sa_mask);
232 ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
233 "sigaction SIGCHLD: %d", errno);
235 msgkey = get_ftok(4160);
236 ATF_REQUIRE_MSG(msgkey != (key_t)-1, "get_ftok failed");
238 sender_msqid = msgget(msgkey, IPC_CREAT | 0640);
239 ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno);
240 write_int("sender_msqid", sender_msqid);
243 atf_tc_skip("SYSV Message Queue not supported");
247 ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
248 "msgctl IPC_STAT 1: %d", errno);
250 print_msqid_ds(&m_ds, 0640);
252 m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
254 ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_SET, &m_ds) != -1,
255 "msgctl IPC_SET: %d", errno);
257 memset(&m_ds, 0, sizeof(m_ds));
259 ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
260 "msgctl IPC_STAT 2: %d", errno);
262 ATF_REQUIRE_MSG((m_ds.msg_perm.mode & 0777) == 0600,
263 "IPC_SET of mode didn't hold");
265 print_msqid_ds(&m_ds, 0600);
267 switch ((child_pid = fork())) {
269 atf_tc_fail("fork: %d", errno);
281 for (loop = 0; loop < maxloop; loop++) {
283 * Send the first message to the receiver and wait for the ACK.
286 strlcpy(m.mtext, m1_str, sizeof(m.mtext));
287 ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN,
288 0) != -1, "sender: msgsnd 1: %d", errno);
290 ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
291 MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
292 "sender: msgrcv 1 ack: %d", errno);
294 print_msqid_ds(&m_ds, 0600);
297 * Send the second message to the receiver and wait for the ACK.
300 strlcpy(m.mtext, m2_str, sizeof(m.mtext));
301 ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0) != -1,
302 "sender: msgsnd 2: %d", errno);
304 ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
305 MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
306 "sender: msgrcv 2 ack: %d", errno);
310 * Wait for child to finish
312 sigemptyset(&sigmask);
313 (void) sigsuspend(&sigmask);
316 * ...and any other signal is an unexpected error.
319 c_status = child_status;
321 atf_tc_fail("waitpid: %d", -c_status);
322 else if (WIFEXITED(c_status) == 0)
323 atf_tc_fail("child abnormal exit: %d", c_status);
324 else if (WEXITSTATUS(c_status) != 0)
325 atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
327 ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds)
328 != -1, "msgctl IPC_STAT: %d", errno);
330 print_msqid_ds(&m_ds, 0600);
334 atf_tc_fail("sender: received unexpected signal");
337 ATF_TC_CLEANUP(msg, tc)
342 * Remove the message queue if it exists.
344 sender_msqid = read_int("sender_msqid");
345 if (sender_msqid != -1)
346 if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
347 err(1, "msgctl IPC_RMID");
351 print_msqid_ds(struct msqid_ds *mp, mode_t mode)
353 uid_t uid = geteuid();
354 gid_t gid = getegid();
356 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
357 mp->msg_perm.uid, mp->msg_perm.gid,
358 mp->msg_perm.cuid, mp->msg_perm.cgid,
359 mp->msg_perm.mode & 0777);
361 printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
362 mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
365 printf("stime: %s", ctime(&mp->msg_stime));
366 printf("rtime: %s", ctime(&mp->msg_rtime));
367 printf("ctime: %s", ctime(&mp->msg_ctime));
370 * Sanity check a few things.
373 ATF_REQUIRE_MSG(mp->msg_perm.uid == uid && mp->msg_perm.cuid == uid,
376 ATF_REQUIRE_MSG(mp->msg_perm.gid == gid && mp->msg_perm.cgid == gid,
379 ATF_REQUIRE_MSG((mp->msg_perm.mode & 0777) == mode, "mode mismatch");
388 if ((msqid = msgget(msgkey, 0)) == -1)
389 err(1, "receiver: msgget");
391 for (loop = 0; loop < maxloop; loop++) {
393 * Receive the first message, print it, and send an ACK.
395 if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0) != MESSAGE_TEXT_LEN)
396 err(1, "receiver: msgrcv 1");
398 printf("%s\n", m.mtext);
399 if (strcmp(m.mtext, m1_str) != 0)
400 err(1, "receiver: message 1 data isn't correct");
402 m.mtype = MTYPE_1_ACK;
404 if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
405 err(1, "receiver: msgsnd ack 1");
408 * Receive the second message, print it, and send an ACK.
411 if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0) != MESSAGE_TEXT_LEN)
412 err(1, "receiver: msgrcv 2");
414 printf("%s\n", m.mtext);
415 if (strcmp(m.mtext, m2_str) != 0)
416 err(1, "receiver: message 2 data isn't correct");
418 m.mtype = MTYPE_2_ACK;
420 if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
421 err(1, "receiver: msgsnd ack 2");
428 * Test the SVID-compatible Semaphore facility.
431 ATF_TC_WITH_CLEANUP(sem);
435 atf_tc_set_md_var(tc, "timeout", "3");
436 atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
443 struct semid_ds s_ds;
450 * Install a SIGSYS handler so that we can exit gracefully if
451 * System V Semaphore support isn't in the kernel.
454 sa.sa_handler = sigsys_handler;
455 sigemptyset(&sa.sa_mask);
457 ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
458 "sigaction SIGSYS: %d", errno);
461 * Install a SIGCHLD handler to deal with all possible exit
462 * conditions of the receiver.
466 sa.sa_handler = sigchld_handler;
467 sigemptyset(&sa.sa_mask);
469 ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
470 "sigaction SIGCHLD: %d", errno);
472 semkey = get_ftok(4160);
473 ATF_REQUIRE_MSG(semkey != (key_t)-1, "get_ftok failed");
475 sender_semid = semget(semkey, 1, IPC_CREAT | 0640);
476 ATF_REQUIRE_MSG(sender_semid != -1, "semget: %d", errno);
477 write_int("sender_semid", sender_semid);
480 atf_tc_skip("SYSV Semaphore not supported");
485 ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
486 "semctl IPC_STAT: %d", errno);
488 print_semid_ds(&s_ds, 0640);
490 s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
493 ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_SET, sun) != -1,
494 "semctl IPC_SET: %d", errno);
496 memset(&s_ds, 0, sizeof(s_ds));
499 ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
500 "semctl IPC_STAT: %d", errno);
502 ATF_REQUIRE_MSG((s_ds.sem_perm.mode & 0777) == 0600,
503 "IPC_SET of mode didn't hold");
505 print_semid_ds(&s_ds, 0600);
507 for (child_count = 0; child_count < 5; child_count++) {
508 switch ((child_pid = fork())) {
510 atf_tc_fail("fork: %d", errno);
523 * Wait for all of the waiters to be attempting to acquire the
527 i = semctl(sender_semid, 0, GETNCNT);
529 atf_tc_fail("semctl GETNCNT: %d", i);
535 * Now set the thundering herd in motion by initializing the
536 * semaphore to the value 1.
539 ATF_REQUIRE_MSG(semctl(sender_semid, 0, SETVAL, sun) != -1,
540 "sender: semctl SETVAL to 1: %d", errno);
543 * Wait for all children to finish
545 sigemptyset(&sigmask);
547 (void) sigsuspend(&sigmask);
549 c_status = child_status;
551 atf_tc_fail("waitpid: %d", -c_status);
552 else if (WIFEXITED(c_status) == 0)
553 atf_tc_fail("c abnormal exit: %d", c_status);
554 else if (WEXITSTATUS(c_status) != 0)
555 atf_tc_fail("c status: %d",
556 WEXITSTATUS(c_status));
559 ATF_REQUIRE_MSG(semctl(sender_semid, 0,
560 IPC_STAT, sun) != -1,
561 "semctl IPC_STAT: %d", errno);
563 print_semid_ds(&s_ds, 0600);
566 if (child_count <= 0)
570 atf_tc_fail("sender: received unexpected signal");
576 ATF_TC_CLEANUP(sem, tc)
581 * Remove the semaphore if it exists
583 sender_semid = read_int("sender_semid");
584 if (sender_semid != -1)
585 if (semctl(sender_semid, 0, IPC_RMID) == -1)
586 err(1, "semctl IPC_RMID");
590 print_semid_ds(struct semid_ds *sp, mode_t mode)
592 uid_t uid = geteuid();
593 gid_t gid = getegid();
595 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
596 sp->sem_perm.uid, sp->sem_perm.gid,
597 sp->sem_perm.cuid, sp->sem_perm.cgid,
598 sp->sem_perm.mode & 0777);
600 printf("nsems %u\n", sp->sem_nsems);
602 printf("otime: %s", ctime(&sp->sem_otime));
603 printf("ctime: %s", ctime(&sp->sem_ctime));
606 * Sanity check a few things.
609 ATF_REQUIRE_MSG(sp->sem_perm.uid == uid && sp->sem_perm.cuid == uid,
612 ATF_REQUIRE_MSG(sp->sem_perm.gid == gid && sp->sem_perm.cgid == gid,
615 ATF_REQUIRE_MSG((sp->sem_perm.mode & 0777) == mode,
616 "mode mismatch %o != %o", (sp->sem_perm.mode & 0777), mode);
625 if ((semid = semget(semkey, 1, 0)) == -1)
626 err(1, "waiter: semget");
629 * Attempt to acquire the semaphore.
633 s.sem_flg = SEM_UNDO;
635 if (semop(semid, &s, 1) == -1)
636 err(1, "waiter: semop -1");
638 printf("WOO! GOT THE SEMAPHORE!\n");
642 * Release the semaphore and exit.
646 s.sem_flg = SEM_UNDO;
648 if (semop(semid, &s, 1) == -1)
649 err(1, "waiter: semop +1");
655 * Test the SVID-compatible Shared Memory facility.
658 ATF_TC_WITH_CLEANUP(shm);
662 atf_tc_set_md_var(tc, "timeout", "3");
663 atf_tc_set_md_var(tc, "descr", "Checks sysv shared memory");
669 struct shmid_ds s_ds;
676 * Install a SIGSYS handler so that we can exit gracefully if
677 * System V Shared Memory support isn't in the kernel.
680 sa.sa_handler = sigsys_handler;
681 sigemptyset(&sa.sa_mask);
683 ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
684 "sigaction SIGSYS: %d", errno);
687 * Install a SIGCHLD handler to deal with all possible exit
688 * conditions of the sharer.
692 sa.sa_handler = sigchld_handler;
693 sigemptyset(&sa.sa_mask);
695 ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
696 "sigaction SIGCHLD: %d", errno);
698 pgsize = sysconf(_SC_PAGESIZE);
700 shmkey = get_ftok(4160);
701 ATF_REQUIRE_MSG(shmkey != (key_t)-1, "get_ftok failed");
703 ATF_REQUIRE_MSG((sender_shmid = shmget(shmkey, pgsize,
704 IPC_CREAT | 0640)) != -1,
705 "shmget: %d", errno);
706 write_int("sender_shmid", sender_shmid);
708 ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
709 "shmctl IPC_STAT: %d", errno);
711 print_shmid_ds(&s_ds, 0640);
713 s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
715 ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_SET, &s_ds) != -1,
716 "shmctl IPC_SET: %d", errno);
718 memset(&s_ds, 0, sizeof(s_ds));
720 ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
721 "shmctl IPC_STAT: %d", errno);
723 ATF_REQUIRE_MSG((s_ds.shm_perm.mode & 0777) == 0600,
724 "IPC_SET of mode didn't hold");
726 print_shmid_ds(&s_ds, 0600);
728 shm_buf = shmat(sender_shmid, NULL, 0);
729 ATF_REQUIRE_MSG(shm_buf != (void *) -1, "sender: shmat: %d", errno);
732 * Write the test pattern into the shared memory buffer.
734 strcpy(shm_buf, m2_str);
736 switch ((child_pid = fork())) {
738 atf_tc_fail("fork: %d", errno);
750 * Wait for child to finish
752 sigemptyset(&sigmask);
753 (void) sigsuspend(&sigmask);
756 c_status = child_status;
758 atf_tc_fail("waitpid: %d", -c_status);
759 else if (WIFEXITED(c_status) == 0)
760 atf_tc_fail("c abnormal exit: %d", c_status);
761 else if (WEXITSTATUS(c_status) != 0)
762 atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
764 ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT,
766 "shmctl IPC_STAT: %d", errno);
768 print_shmid_ds(&s_ds, 0600);
772 atf_tc_fail("sender: received unexpected signal");
775 ATF_TC_CLEANUP(shm, tc)
780 * Remove the shared memory area if it exists.
782 sender_shmid = read_int("sender_shmid");
783 if (sender_shmid != -1)
784 if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
785 err(1, "shmctl IPC_RMID");
789 print_shmid_ds(struct shmid_ds *sp, mode_t mode)
791 uid_t uid = geteuid();
792 gid_t gid = getegid();
794 printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
795 sp->shm_perm.uid, sp->shm_perm.gid,
796 sp->shm_perm.cuid, sp->shm_perm.cgid,
797 sp->shm_perm.mode & 0777);
799 printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
800 (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
803 printf("atime: %s", ctime(&sp->shm_atime));
804 printf("dtime: %s", ctime(&sp->shm_dtime));
805 printf("ctime: %s", ctime(&sp->shm_ctime));
808 * Sanity check a few things.
811 ATF_REQUIRE_MSG(sp->shm_perm.uid == uid && sp->shm_perm.cuid == uid,
814 ATF_REQUIRE_MSG(sp->shm_perm.gid == gid && sp->shm_perm.cgid == gid,
817 ATF_REQUIRE_MSG((sp->shm_perm.mode & 0777) == mode, "mode mismatch");
826 shmid = shmget(shmkey, pgsize, 0);
827 ATF_REQUIRE_MSG(shmid != -1, "receiver: shmget:%d", errno);
829 shm_buf = shmat(shmid, NULL, 0);
830 ATF_REQUIRE_MSG(shm_buf != (void *) -1, "receiver: shmat: %d", errno);
832 printf("%s\n", (const char *)shm_buf);
834 ATF_REQUIRE_MSG(strcmp((const char *)shm_buf, m2_str) == 0,
835 "receiver: data isn't correct");
843 ATF_TP_ADD_TC(tp, msg);
844 ATF_TP_ADD_TC(tp, sem);
845 ATF_TP_ADD_TC(tp, shm);
847 return atf_no_error();