Home | History | Annotate | Line # | Download | only in kernel
t_sysv.c revision 1.5
      1  1.5  pgoyette /*	$NetBSD: t_sysv.c,v 1.5 2018/02/03 02:57:15 pgoyette Exp $	*/
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pgoyette  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  pgoyette  * NASA Ames Research Center, and by Andrew Doran.
     10  1.1  pgoyette  *
     11  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     12  1.1  pgoyette  * modification, are permitted provided that the following conditions
     13  1.1  pgoyette  * are met:
     14  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     15  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     16  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     19  1.1  pgoyette  *
     20  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  pgoyette  */
     32  1.1  pgoyette 
     33  1.1  pgoyette /*
     34  1.1  pgoyette  * Test the SVID-compatible Message Queue facility.
     35  1.1  pgoyette  */
     36  1.1  pgoyette 
     37  1.1  pgoyette #include <atf-c.h>
     38  1.1  pgoyette 
     39  1.1  pgoyette #include <err.h>
     40  1.1  pgoyette #include <errno.h>
     41  1.1  pgoyette #include <fcntl.h>
     42  1.1  pgoyette #include <signal.h>
     43  1.1  pgoyette #include <stdio.h>
     44  1.1  pgoyette #include <stdlib.h>
     45  1.1  pgoyette #include <string.h>
     46  1.1  pgoyette #include <time.h>
     47  1.1  pgoyette #include <unistd.h>
     48  1.1  pgoyette 
     49  1.1  pgoyette #include <sys/ipc.h>
     50  1.1  pgoyette #include <sys/msg.h>
     51  1.1  pgoyette #include <sys/param.h>
     52  1.1  pgoyette #include <sys/sem.h>
     53  1.1  pgoyette #include <sys/shm.h>
     54  1.1  pgoyette #include <sys/wait.h>
     55  1.1  pgoyette 
     56  1.1  pgoyette volatile int did_sigsys, did_sigchild;
     57  1.1  pgoyette volatile int child_status, child_count;
     58  1.1  pgoyette 
     59  1.1  pgoyette void	sigsys_handler(int);
     60  1.1  pgoyette void	sigchld_handler(int);
     61  1.1  pgoyette 
     62  1.2  pgoyette key_t	get_ftok(int);
     63  1.2  pgoyette 
     64  1.1  pgoyette void	print_msqid_ds(struct msqid_ds *, mode_t);
     65  1.1  pgoyette void	receiver(void);
     66  1.1  pgoyette 
     67  1.1  pgoyette void	print_semid_ds(struct semid_ds *, mode_t);
     68  1.1  pgoyette void	waiter(void);
     69  1.1  pgoyette 
     70  1.1  pgoyette void	print_shmid_ds(struct shmid_ds *, mode_t);
     71  1.1  pgoyette void	sharer(void);
     72  1.1  pgoyette 
     73  1.1  pgoyette #define	MESSAGE_TEXT_LEN	256
     74  1.1  pgoyette 
     75  1.5  pgoyette struct testmsg {
     76  1.1  pgoyette 	long	mtype;
     77  1.1  pgoyette 	char	mtext[MESSAGE_TEXT_LEN];
     78  1.1  pgoyette };
     79  1.1  pgoyette 
     80  1.1  pgoyette const char *m1_str = "California is overrated.";
     81  1.1  pgoyette const char *m2_str = "The quick brown fox jumped over the lazy dog.";
     82  1.1  pgoyette 
     83  1.1  pgoyette size_t	pgsize;
     84  1.1  pgoyette 
     85  1.1  pgoyette #define	MTYPE_1		1
     86  1.1  pgoyette #define	MTYPE_1_ACK	2
     87  1.1  pgoyette 
     88  1.1  pgoyette #define	MTYPE_2		3
     89  1.1  pgoyette #define	MTYPE_2_ACK	4
     90  1.1  pgoyette 
     91  1.1  pgoyette pid_t	child_pid;
     92  1.1  pgoyette 
     93  1.1  pgoyette key_t	msgkey, semkey, shmkey;
     94  1.1  pgoyette 
     95  1.1  pgoyette int	maxloop = 1;
     96  1.1  pgoyette 
     97  1.1  pgoyette union semun {
     98  1.1  pgoyette 	int	val;		/* value for SETVAL */
     99  1.1  pgoyette 	struct	semid_ds *buf;	/* buffer for IPC_{STAT,SET} */
    100  1.1  pgoyette 	u_short	*array;		/* array for GETALL & SETALL */
    101  1.1  pgoyette };
    102  1.1  pgoyette 
    103  1.1  pgoyette 
    104  1.4      jmmv /* Writes an integer to a file.  To be used from the body of the test
    105  1.4      jmmv  * cases below to pass any global identifiers to the cleanup routine. */
    106  1.4      jmmv static void
    107  1.4      jmmv write_int(const char *path, const int value)
    108  1.4      jmmv {
    109  1.4      jmmv 	int output;
    110  1.4      jmmv 
    111  1.4      jmmv 	output = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    112  1.4      jmmv 	ATF_REQUIRE_MSG(output != -1, "Failed to create %s", path);
    113  1.4      jmmv 	write(output, &value, sizeof(value));
    114  1.4      jmmv 	close(output);
    115  1.4      jmmv }
    116  1.4      jmmv 
    117  1.4      jmmv 
    118  1.4      jmmv /* Reads an integer from a file.  To be used from the cleanup routines
    119  1.4      jmmv  * of the test cases below. */
    120  1.4      jmmv static int
    121  1.4      jmmv read_int(const char *path)
    122  1.4      jmmv {
    123  1.4      jmmv 	int input;
    124  1.4      jmmv 
    125  1.4      jmmv 	input = open(path, O_RDONLY);
    126  1.4      jmmv 	if (input == -1)
    127  1.4      jmmv 		return -1;
    128  1.4      jmmv 	else {
    129  1.4      jmmv 		int value;
    130  1.4      jmmv 		read(input, &value, sizeof(value));
    131  1.4      jmmv 		return value;
    132  1.4      jmmv 	}
    133  1.4      jmmv }
    134  1.4      jmmv 
    135  1.4      jmmv 
    136  1.1  pgoyette void
    137  1.1  pgoyette sigsys_handler(int signo)
    138  1.1  pgoyette {
    139  1.1  pgoyette 
    140  1.1  pgoyette 	did_sigsys = 1;
    141  1.1  pgoyette }
    142  1.1  pgoyette 
    143  1.1  pgoyette void
    144  1.1  pgoyette sigchld_handler(int signo)
    145  1.1  pgoyette {
    146  1.1  pgoyette 	int c_status;
    147  1.1  pgoyette 
    148  1.1  pgoyette 	did_sigchild = 1;
    149  1.1  pgoyette 	/*
    150  1.1  pgoyette 	 * Reap the child and return its status
    151  1.1  pgoyette 	 */
    152  1.1  pgoyette 	if (wait(&c_status) == -1)
    153  1.1  pgoyette 		child_status = -errno;
    154  1.1  pgoyette 	else
    155  1.1  pgoyette 		child_status = c_status;
    156  1.1  pgoyette 
    157  1.1  pgoyette 	child_count--;
    158  1.1  pgoyette }
    159  1.1  pgoyette 
    160  1.2  pgoyette key_t get_ftok(int id)
    161  1.2  pgoyette {
    162  1.2  pgoyette 	int fd;
    163  1.2  pgoyette 	char token_key[64], token_dir[64];
    164  1.2  pgoyette 	char *tmpdir;
    165  1.2  pgoyette 	key_t key;
    166  1.2  pgoyette 
    167  1.2  pgoyette 	strlcpy(token_key, "/tmp/t_sysv.XXXXXX", sizeof(token_key));
    168  1.2  pgoyette 	tmpdir = mkdtemp(token_key);
    169  1.2  pgoyette 	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp() failed: %d", errno);
    170  1.2  pgoyette 
    171  1.2  pgoyette 	strlcpy(token_dir, tmpdir, sizeof(token_dir));
    172  1.2  pgoyette 	strlcpy(token_key, tmpdir, sizeof(token_key));
    173  1.2  pgoyette 	strlcat(token_key, "/token_key", sizeof(token_key));
    174  1.2  pgoyette 
    175  1.2  pgoyette 	/* Create the file, since ftok() requires it to exist! */
    176  1.2  pgoyette 
    177  1.5  pgoyette 	fd = open(token_key, O_RDWR | O_CREAT | O_EXCL, 0600);
    178  1.2  pgoyette 	if (fd == -1) {
    179  1.2  pgoyette 		rmdir(tmpdir);
    180  1.2  pgoyette 		atf_tc_fail("open() of temp file failed: %d", errno);
    181  1.2  pgoyette 		return (key_t)-1;
    182  1.2  pgoyette 	} else
    183  1.2  pgoyette 		close(fd);
    184  1.2  pgoyette 
    185  1.2  pgoyette 	key = ftok(token_key, id);
    186  1.5  pgoyette 	ATF_REQUIRE_MSG(key != (key_t)-1, "ftok() failed");
    187  1.2  pgoyette 
    188  1.2  pgoyette 	ATF_REQUIRE_MSG(unlink(token_key) != -1, "unlink() failed: %d", errno);
    189  1.2  pgoyette 	ATF_REQUIRE_MSG(rmdir(token_dir) != -1, "rmdir() failed: %d", errno);
    190  1.2  pgoyette 
    191  1.2  pgoyette 	return key;
    192  1.2  pgoyette }
    193  1.2  pgoyette 
    194  1.2  pgoyette ATF_TC_WITH_CLEANUP(msg);
    195  1.1  pgoyette ATF_TC_HEAD(msg, tc)
    196  1.1  pgoyette {
    197  1.1  pgoyette 
    198  1.1  pgoyette 	atf_tc_set_md_var(tc, "timeout", "3");
    199  1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
    200  1.1  pgoyette }
    201  1.1  pgoyette 
    202  1.1  pgoyette ATF_TC_BODY(msg, tc)
    203  1.1  pgoyette {
    204  1.1  pgoyette 	struct sigaction sa;
    205  1.1  pgoyette 	struct msqid_ds m_ds;
    206  1.5  pgoyette 	struct testmsg m;
    207  1.1  pgoyette 	sigset_t sigmask;
    208  1.4      jmmv 	int sender_msqid;
    209  1.1  pgoyette 	int loop;
    210  1.1  pgoyette 	int c_status;
    211  1.1  pgoyette 
    212  1.1  pgoyette 	/*
    213  1.1  pgoyette 	 * Install a SIGSYS handler so that we can exit gracefully if
    214  1.1  pgoyette 	 * System V Message Queue support isn't in the kernel.
    215  1.1  pgoyette 	 */
    216  1.1  pgoyette 	did_sigsys = 0;
    217  1.1  pgoyette 	sa.sa_handler = sigsys_handler;
    218  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    219  1.1  pgoyette 	sa.sa_flags = 0;
    220  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
    221  1.1  pgoyette 	    "sigaction SIGSYS: %d", errno);
    222  1.1  pgoyette 
    223  1.1  pgoyette 	/*
    224  1.1  pgoyette 	 * Install a SIGCHLD handler to deal with all possible exit
    225  1.1  pgoyette 	 * conditions of the receiver.
    226  1.1  pgoyette 	 */
    227  1.1  pgoyette 	did_sigchild = 0;
    228  1.1  pgoyette 	child_count = 0;
    229  1.1  pgoyette 	sa.sa_handler = sigchld_handler;
    230  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    231  1.1  pgoyette 	sa.sa_flags = 0;
    232  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
    233  1.1  pgoyette 	    "sigaction SIGCHLD: %d", errno);
    234  1.1  pgoyette 
    235  1.2  pgoyette 	msgkey = get_ftok(4160);
    236  1.2  pgoyette 	ATF_REQUIRE_MSG(msgkey != (key_t)-1, "get_ftok failed");
    237  1.1  pgoyette 
    238  1.1  pgoyette 	sender_msqid = msgget(msgkey, IPC_CREAT | 0640);
    239  1.1  pgoyette 	ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno);
    240  1.4      jmmv 	write_int("sender_msqid", sender_msqid);
    241  1.1  pgoyette 
    242  1.1  pgoyette 	if (did_sigsys) {
    243  1.1  pgoyette 		atf_tc_skip("SYSV Message Queue not supported");
    244  1.1  pgoyette 		return;
    245  1.1  pgoyette 	}
    246  1.1  pgoyette 
    247  1.1  pgoyette 	ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
    248  1.1  pgoyette 	"msgctl IPC_STAT 1: %d", errno);
    249  1.1  pgoyette 
    250  1.1  pgoyette 	print_msqid_ds(&m_ds, 0640);
    251  1.1  pgoyette 
    252  1.1  pgoyette 	m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
    253  1.1  pgoyette 
    254  1.1  pgoyette 	ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_SET, &m_ds) != -1,
    255  1.1  pgoyette 	    "msgctl IPC_SET: %d", errno);
    256  1.1  pgoyette 
    257  1.1  pgoyette 	memset(&m_ds, 0, sizeof(m_ds));
    258  1.1  pgoyette 
    259  1.1  pgoyette 	ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
    260  1.1  pgoyette 	    "msgctl IPC_STAT 2: %d", errno);
    261  1.1  pgoyette 
    262  1.1  pgoyette 	ATF_REQUIRE_MSG((m_ds.msg_perm.mode & 0777) == 0600,
    263  1.1  pgoyette 	    "IPC_SET of mode didn't hold");
    264  1.1  pgoyette 
    265  1.1  pgoyette 	print_msqid_ds(&m_ds, 0600);
    266  1.1  pgoyette 
    267  1.1  pgoyette 	switch ((child_pid = fork())) {
    268  1.1  pgoyette 	case -1:
    269  1.1  pgoyette 		atf_tc_fail("fork: %d", errno);
    270  1.1  pgoyette 		return;
    271  1.1  pgoyette 
    272  1.1  pgoyette 	case 0:
    273  1.1  pgoyette 		child_count++;
    274  1.1  pgoyette 		receiver();
    275  1.1  pgoyette 		break;
    276  1.1  pgoyette 
    277  1.1  pgoyette 	default:
    278  1.1  pgoyette 		break;
    279  1.1  pgoyette 	}
    280  1.1  pgoyette 
    281  1.1  pgoyette 	for (loop = 0; loop < maxloop; loop++) {
    282  1.1  pgoyette 		/*
    283  1.1  pgoyette 		 * Send the first message to the receiver and wait for the ACK.
    284  1.1  pgoyette 		 */
    285  1.1  pgoyette 		m.mtype = MTYPE_1;
    286  1.1  pgoyette 		strcpy(m.mtext, m1_str);
    287  1.3     skrll 		ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN,
    288  1.3     skrll 		    0) != -1, "sender: msgsnd 1: %d", errno);
    289  1.1  pgoyette 
    290  1.3     skrll 		ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
    291  1.3     skrll 				       MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
    292  1.1  pgoyette 		    "sender: msgrcv 1 ack: %d", errno);
    293  1.1  pgoyette 
    294  1.1  pgoyette 		print_msqid_ds(&m_ds, 0600);
    295  1.1  pgoyette 
    296  1.1  pgoyette 		/*
    297  1.1  pgoyette 		 * Send the second message to the receiver and wait for the ACK.
    298  1.1  pgoyette 		 */
    299  1.1  pgoyette 		m.mtype = MTYPE_2;
    300  1.1  pgoyette 		strcpy(m.mtext, m2_str);
    301  1.3     skrll 		ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0) != -1,
    302  1.1  pgoyette 		    "sender: msgsnd 2: %d", errno);
    303  1.1  pgoyette 
    304  1.3     skrll 		ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
    305  1.3     skrll 				       MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
    306  1.1  pgoyette 		    "sender: msgrcv 2 ack: %d", errno);
    307  1.1  pgoyette 	}
    308  1.1  pgoyette 
    309  1.1  pgoyette 	/*
    310  1.1  pgoyette 	 * Wait for child to finish
    311  1.1  pgoyette 	 */
    312  1.1  pgoyette 	sigemptyset(&sigmask);
    313  1.1  pgoyette 	(void) sigsuspend(&sigmask);
    314  1.1  pgoyette 
    315  1.1  pgoyette 	/*
    316  1.1  pgoyette 	 * ...and any other signal is an unexpected error.
    317  1.1  pgoyette 	 */
    318  1.1  pgoyette 	if (did_sigchild) {
    319  1.1  pgoyette 		c_status = child_status;
    320  1.1  pgoyette 		if (c_status < 0)
    321  1.1  pgoyette 			atf_tc_fail("waitpid: %d", -c_status);
    322  1.1  pgoyette 		else if (WIFEXITED(c_status) == 0)
    323  1.1  pgoyette 			atf_tc_fail("child abnormal exit: %d", c_status);
    324  1.1  pgoyette 		else if (WEXITSTATUS(c_status) != 0)
    325  1.1  pgoyette 			atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
    326  1.1  pgoyette 		else {
    327  1.1  pgoyette 			ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds)
    328  1.1  pgoyette 			    != -1, "msgctl IPC_STAT: %d", errno);
    329  1.1  pgoyette 
    330  1.1  pgoyette 			print_msqid_ds(&m_ds, 0600);
    331  1.1  pgoyette 			atf_tc_pass();
    332  1.1  pgoyette 		}
    333  1.1  pgoyette 	} else
    334  1.1  pgoyette 		atf_tc_fail("sender: received unexpected signal");
    335  1.1  pgoyette }
    336  1.1  pgoyette 
    337  1.2  pgoyette ATF_TC_CLEANUP(msg, tc)
    338  1.1  pgoyette {
    339  1.4      jmmv 	int sender_msqid;
    340  1.1  pgoyette 
    341  1.1  pgoyette 	/*
    342  1.2  pgoyette 	 * Remove the message queue if it exists.
    343  1.1  pgoyette 	 */
    344  1.4      jmmv 	sender_msqid = read_int("sender_msqid");
    345  1.2  pgoyette 	if (sender_msqid != -1)
    346  1.4      jmmv 		if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
    347  1.4      jmmv 			err(1, "msgctl IPC_RMID");
    348  1.1  pgoyette }
    349  1.1  pgoyette 
    350  1.1  pgoyette void
    351  1.5  pgoyette print_msqid_ds(struct msqid_ds *mp, mode_t mode)
    352  1.1  pgoyette {
    353  1.1  pgoyette 	uid_t uid = geteuid();
    354  1.1  pgoyette 	gid_t gid = getegid();
    355  1.1  pgoyette 
    356  1.1  pgoyette 	printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
    357  1.1  pgoyette 	    mp->msg_perm.uid, mp->msg_perm.gid,
    358  1.1  pgoyette 	    mp->msg_perm.cuid, mp->msg_perm.cgid,
    359  1.1  pgoyette 	    mp->msg_perm.mode & 0777);
    360  1.1  pgoyette 
    361  1.1  pgoyette 	printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
    362  1.1  pgoyette 	    mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
    363  1.1  pgoyette 	    mp->msg_lrpid);
    364  1.1  pgoyette 
    365  1.1  pgoyette 	printf("stime: %s", ctime(&mp->msg_stime));
    366  1.1  pgoyette 	printf("rtime: %s", ctime(&mp->msg_rtime));
    367  1.1  pgoyette 	printf("ctime: %s", ctime(&mp->msg_ctime));
    368  1.1  pgoyette 
    369  1.1  pgoyette 	/*
    370  1.1  pgoyette 	 * Sanity check a few things.
    371  1.1  pgoyette 	 */
    372  1.1  pgoyette 
    373  1.1  pgoyette 	ATF_REQUIRE_MSG(mp->msg_perm.uid == uid && mp->msg_perm.cuid == uid,
    374  1.1  pgoyette 	    "uid mismatch");
    375  1.1  pgoyette 
    376  1.1  pgoyette 	ATF_REQUIRE_MSG(mp->msg_perm.gid == gid && mp->msg_perm.cgid == gid,
    377  1.1  pgoyette 	    "gid mismatch");
    378  1.1  pgoyette 
    379  1.1  pgoyette 	ATF_REQUIRE_MSG((mp->msg_perm.mode & 0777) == mode, "mode mismatch");
    380  1.1  pgoyette }
    381  1.1  pgoyette 
    382  1.1  pgoyette void
    383  1.5  pgoyette receiver(void)
    384  1.1  pgoyette {
    385  1.5  pgoyette 	struct testmsg m;
    386  1.1  pgoyette 	int msqid, loop;
    387  1.1  pgoyette 
    388  1.1  pgoyette 	if ((msqid = msgget(msgkey, 0)) == -1)
    389  1.1  pgoyette 		err(1, "receiver: msgget");
    390  1.1  pgoyette 
    391  1.1  pgoyette 	for (loop = 0; loop < maxloop; loop++) {
    392  1.1  pgoyette 		/*
    393  1.1  pgoyette 		 * Receive the first message, print it, and send an ACK.
    394  1.1  pgoyette 		 */
    395  1.3     skrll 		if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0) != MESSAGE_TEXT_LEN)
    396  1.1  pgoyette 			err(1, "receiver: msgrcv 1");
    397  1.1  pgoyette 
    398  1.1  pgoyette 		printf("%s\n", m.mtext);
    399  1.1  pgoyette 		if (strcmp(m.mtext, m1_str) != 0)
    400  1.1  pgoyette 			err(1, "receiver: message 1 data isn't correct");
    401  1.1  pgoyette 
    402  1.1  pgoyette 		m.mtype = MTYPE_1_ACK;
    403  1.1  pgoyette 
    404  1.3     skrll 		if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
    405  1.1  pgoyette 			err(1, "receiver: msgsnd ack 1");
    406  1.1  pgoyette 
    407  1.1  pgoyette 		/*
    408  1.1  pgoyette 		 * Receive the second message, print it, and send an ACK.
    409  1.1  pgoyette 		 */
    410  1.1  pgoyette 
    411  1.3     skrll 		if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0) != MESSAGE_TEXT_LEN)
    412  1.1  pgoyette 			err(1, "receiver: msgrcv 2");
    413  1.1  pgoyette 
    414  1.1  pgoyette 		printf("%s\n", m.mtext);
    415  1.1  pgoyette 		if (strcmp(m.mtext, m2_str) != 0)
    416  1.1  pgoyette 			err(1, "receiver: message 2 data isn't correct");
    417  1.1  pgoyette 
    418  1.1  pgoyette 		m.mtype = MTYPE_2_ACK;
    419  1.1  pgoyette 
    420  1.3     skrll 		if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
    421  1.1  pgoyette 			err(1, "receiver: msgsnd ack 2");
    422  1.1  pgoyette 	}
    423  1.1  pgoyette 
    424  1.1  pgoyette 	exit(0);
    425  1.1  pgoyette }
    426  1.1  pgoyette 
    427  1.1  pgoyette /*
    428  1.1  pgoyette  * Test the SVID-compatible Semaphore facility.
    429  1.1  pgoyette  */
    430  1.1  pgoyette 
    431  1.2  pgoyette ATF_TC_WITH_CLEANUP(sem);
    432  1.1  pgoyette ATF_TC_HEAD(sem, tc)
    433  1.1  pgoyette {
    434  1.1  pgoyette 
    435  1.1  pgoyette 	atf_tc_set_md_var(tc, "timeout", "3");
    436  1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
    437  1.1  pgoyette }
    438  1.1  pgoyette 
    439  1.1  pgoyette ATF_TC_BODY(sem, tc)
    440  1.1  pgoyette {
    441  1.1  pgoyette 	struct sigaction sa;
    442  1.1  pgoyette 	union semun sun;
    443  1.1  pgoyette 	struct semid_ds s_ds;
    444  1.1  pgoyette 	sigset_t sigmask;
    445  1.4      jmmv 	int sender_semid;
    446  1.1  pgoyette 	int i;
    447  1.1  pgoyette 	int c_status;
    448  1.1  pgoyette 
    449  1.1  pgoyette 	/*
    450  1.1  pgoyette 	 * Install a SIGSYS handler so that we can exit gracefully if
    451  1.1  pgoyette 	 * System V Semaphore support isn't in the kernel.
    452  1.1  pgoyette 	 */
    453  1.1  pgoyette 	did_sigsys = 0;
    454  1.1  pgoyette 	sa.sa_handler = sigsys_handler;
    455  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    456  1.1  pgoyette 	sa.sa_flags = 0;
    457  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
    458  1.1  pgoyette 	    "sigaction SIGSYS: %d", errno);
    459  1.1  pgoyette 
    460  1.1  pgoyette 	/*
    461  1.1  pgoyette 	 * Install a SIGCHLD handler to deal with all possible exit
    462  1.1  pgoyette 	 * conditions of the receiver.
    463  1.1  pgoyette 	 */
    464  1.1  pgoyette 	did_sigchild = 0;
    465  1.1  pgoyette 	child_count = 0;
    466  1.1  pgoyette 	sa.sa_handler = sigchld_handler;
    467  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    468  1.1  pgoyette 	sa.sa_flags = 0;
    469  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
    470  1.1  pgoyette 	    "sigaction SIGCHLD: %d", errno);
    471  1.1  pgoyette 
    472  1.2  pgoyette 	semkey = get_ftok(4160);
    473  1.2  pgoyette 	ATF_REQUIRE_MSG(semkey != (key_t)-1, "get_ftok failed");
    474  1.1  pgoyette 
    475  1.1  pgoyette 	sender_semid = semget(semkey, 1, IPC_CREAT | 0640);
    476  1.1  pgoyette 	ATF_REQUIRE_MSG(sender_semid != -1, "semget: %d", errno);
    477  1.4      jmmv 	write_int("sender_semid", sender_semid);
    478  1.1  pgoyette 
    479  1.1  pgoyette 	if (did_sigsys) {
    480  1.1  pgoyette 		atf_tc_skip("SYSV Semaphore not supported");
    481  1.1  pgoyette 		return;
    482  1.1  pgoyette 	}
    483  1.1  pgoyette 
    484  1.1  pgoyette 	sun.buf = &s_ds;
    485  1.1  pgoyette 	ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
    486  1.1  pgoyette 	    "semctl IPC_STAT: %d", errno);
    487  1.1  pgoyette 
    488  1.1  pgoyette 	print_semid_ds(&s_ds, 0640);
    489  1.1  pgoyette 
    490  1.1  pgoyette 	s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
    491  1.1  pgoyette 
    492  1.1  pgoyette 	sun.buf = &s_ds;
    493  1.1  pgoyette 	ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_SET, sun) != -1,
    494  1.1  pgoyette 	    "semctl IPC_SET: %d", errno);
    495  1.1  pgoyette 
    496  1.1  pgoyette 	memset(&s_ds, 0, sizeof(s_ds));
    497  1.1  pgoyette 
    498  1.1  pgoyette 	sun.buf = &s_ds;
    499  1.1  pgoyette 	ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
    500  1.1  pgoyette 	    "semctl IPC_STAT: %d", errno);
    501  1.1  pgoyette 
    502  1.1  pgoyette 	ATF_REQUIRE_MSG((s_ds.sem_perm.mode & 0777) == 0600,
    503  1.1  pgoyette 	    "IPC_SET of mode didn't hold");
    504  1.1  pgoyette 
    505  1.1  pgoyette 	print_semid_ds(&s_ds, 0600);
    506  1.1  pgoyette 
    507  1.1  pgoyette 	for (child_count = 0; child_count < 5; child_count++) {
    508  1.1  pgoyette 		switch ((child_pid = fork())) {
    509  1.1  pgoyette 		case -1:
    510  1.1  pgoyette 			atf_tc_fail("fork: %d", errno);
    511  1.1  pgoyette 			return;
    512  1.1  pgoyette 
    513  1.1  pgoyette 		case 0:
    514  1.1  pgoyette 			waiter();
    515  1.1  pgoyette 			break;
    516  1.1  pgoyette 
    517  1.1  pgoyette 		default:
    518  1.1  pgoyette 			break;
    519  1.1  pgoyette 		}
    520  1.1  pgoyette 	}
    521  1.1  pgoyette 
    522  1.1  pgoyette 	/*
    523  1.1  pgoyette 	 * Wait for all of the waiters to be attempting to acquire the
    524  1.1  pgoyette 	 * semaphore.
    525  1.1  pgoyette 	 */
    526  1.1  pgoyette 	for (;;) {
    527  1.1  pgoyette 		i = semctl(sender_semid, 0, GETNCNT);
    528  1.1  pgoyette 		if (i == -1)
    529  1.1  pgoyette 			atf_tc_fail("semctl GETNCNT: %d", i);
    530  1.1  pgoyette 		if (i == 5)
    531  1.1  pgoyette 			break;
    532  1.1  pgoyette 	}
    533  1.1  pgoyette 
    534  1.1  pgoyette 	/*
    535  1.1  pgoyette 	 * Now set the thundering herd in motion by initializing the
    536  1.1  pgoyette 	 * semaphore to the value 1.
    537  1.1  pgoyette 	 */
    538  1.1  pgoyette 	sun.val = 1;
    539  1.1  pgoyette 	ATF_REQUIRE_MSG(semctl(sender_semid, 0, SETVAL, sun) != -1,
    540  1.1  pgoyette 	    "sender: semctl SETVAL to 1: %d", errno);
    541  1.1  pgoyette 
    542  1.1  pgoyette 	/*
    543  1.1  pgoyette 	 * Wait for all children to finish
    544  1.1  pgoyette 	 */
    545  1.1  pgoyette 	sigemptyset(&sigmask);
    546  1.1  pgoyette 	for (;;) {
    547  1.1  pgoyette 		(void) sigsuspend(&sigmask);
    548  1.1  pgoyette 		if (did_sigchild) {
    549  1.1  pgoyette 			c_status = child_status;
    550  1.1  pgoyette 			if (c_status < 0)
    551  1.1  pgoyette 				atf_tc_fail("waitpid: %d", -c_status);
    552  1.1  pgoyette 			else if (WIFEXITED(c_status) == 0)
    553  1.1  pgoyette 				atf_tc_fail("c abnormal exit: %d", c_status);
    554  1.1  pgoyette 			else if (WEXITSTATUS(c_status) != 0)
    555  1.1  pgoyette 				atf_tc_fail("c status: %d",
    556  1.1  pgoyette 				    WEXITSTATUS(c_status));
    557  1.1  pgoyette 			else {
    558  1.1  pgoyette 				sun.buf = &s_ds;
    559  1.1  pgoyette 				ATF_REQUIRE_MSG(semctl(sender_semid, 0,
    560  1.1  pgoyette 						    IPC_STAT, sun) != -1,
    561  1.1  pgoyette 				    "semctl IPC_STAT: %d", errno);
    562  1.1  pgoyette 
    563  1.1  pgoyette 				print_semid_ds(&s_ds, 0600);
    564  1.1  pgoyette 				atf_tc_pass();
    565  1.1  pgoyette 			}
    566  1.1  pgoyette 			if (child_count <= 0)
    567  1.1  pgoyette 				break;
    568  1.1  pgoyette 			did_sigchild = 0;
    569  1.1  pgoyette 		} else {
    570  1.1  pgoyette 			atf_tc_fail("sender: received unexpected signal");
    571  1.1  pgoyette 			break;
    572  1.1  pgoyette 		}
    573  1.1  pgoyette 	}
    574  1.1  pgoyette }
    575  1.1  pgoyette 
    576  1.2  pgoyette ATF_TC_CLEANUP(sem, tc)
    577  1.1  pgoyette {
    578  1.4      jmmv 	int sender_semid;
    579  1.1  pgoyette 
    580  1.1  pgoyette 	/*
    581  1.2  pgoyette 	 * Remove the semaphore if it exists
    582  1.1  pgoyette 	 */
    583  1.4      jmmv 	sender_semid = read_int("sender_semid");
    584  1.2  pgoyette 	if (sender_semid != -1)
    585  1.4      jmmv 		if (semctl(sender_semid, 0, IPC_RMID) == -1)
    586  1.4      jmmv 			err(1, "semctl IPC_RMID");
    587  1.1  pgoyette }
    588  1.1  pgoyette 
    589  1.1  pgoyette void
    590  1.5  pgoyette print_semid_ds(struct semid_ds *sp, mode_t mode)
    591  1.1  pgoyette {
    592  1.1  pgoyette 	uid_t uid = geteuid();
    593  1.1  pgoyette 	gid_t gid = getegid();
    594  1.1  pgoyette 
    595  1.1  pgoyette 	printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
    596  1.1  pgoyette 	    sp->sem_perm.uid, sp->sem_perm.gid,
    597  1.1  pgoyette 	    sp->sem_perm.cuid, sp->sem_perm.cgid,
    598  1.1  pgoyette 	    sp->sem_perm.mode & 0777);
    599  1.1  pgoyette 
    600  1.1  pgoyette 	printf("nsems %u\n", sp->sem_nsems);
    601  1.1  pgoyette 
    602  1.1  pgoyette 	printf("otime: %s", ctime(&sp->sem_otime));
    603  1.1  pgoyette 	printf("ctime: %s", ctime(&sp->sem_ctime));
    604  1.1  pgoyette 
    605  1.1  pgoyette 	/*
    606  1.1  pgoyette 	 * Sanity check a few things.
    607  1.1  pgoyette 	 */
    608  1.1  pgoyette 
    609  1.1  pgoyette 	ATF_REQUIRE_MSG(sp->sem_perm.uid == uid && sp->sem_perm.cuid == uid,
    610  1.1  pgoyette 	    "uid mismatch");
    611  1.1  pgoyette 
    612  1.1  pgoyette 	ATF_REQUIRE_MSG(sp->sem_perm.gid == gid && sp->sem_perm.cgid == gid,
    613  1.1  pgoyette 	    "gid mismatch");
    614  1.1  pgoyette 
    615  1.1  pgoyette 	ATF_REQUIRE_MSG((sp->sem_perm.mode & 0777) == mode,
    616  1.1  pgoyette 	    "mode mismatch %o != %o", (sp->sem_perm.mode & 0777), mode);
    617  1.1  pgoyette }
    618  1.1  pgoyette 
    619  1.1  pgoyette void
    620  1.5  pgoyette waiter(void)
    621  1.1  pgoyette {
    622  1.1  pgoyette 	struct sembuf s;
    623  1.1  pgoyette 	int semid;
    624  1.1  pgoyette 
    625  1.1  pgoyette 	if ((semid = semget(semkey, 1, 0)) == -1)
    626  1.1  pgoyette 		err(1, "waiter: semget");
    627  1.1  pgoyette 
    628  1.1  pgoyette 	/*
    629  1.1  pgoyette 	 * Attempt to acquire the semaphore.
    630  1.1  pgoyette 	 */
    631  1.1  pgoyette 	s.sem_num = 0;
    632  1.1  pgoyette 	s.sem_op = -1;
    633  1.1  pgoyette 	s.sem_flg = SEM_UNDO;
    634  1.1  pgoyette 
    635  1.1  pgoyette 	if (semop(semid, &s, 1) == -1)
    636  1.1  pgoyette 		err(1, "waiter: semop -1");
    637  1.1  pgoyette 
    638  1.1  pgoyette 	printf("WOO!  GOT THE SEMAPHORE!\n");
    639  1.1  pgoyette 	sleep(1);
    640  1.1  pgoyette 
    641  1.1  pgoyette 	/*
    642  1.1  pgoyette 	 * Release the semaphore and exit.
    643  1.1  pgoyette 	 */
    644  1.1  pgoyette 	s.sem_num = 0;
    645  1.1  pgoyette 	s.sem_op = 1;
    646  1.1  pgoyette 	s.sem_flg = SEM_UNDO;
    647  1.1  pgoyette 
    648  1.1  pgoyette 	if (semop(semid, &s, 1) == -1)
    649  1.1  pgoyette 		err(1, "waiter: semop +1");
    650  1.1  pgoyette 
    651  1.1  pgoyette 	exit(0);
    652  1.1  pgoyette }
    653  1.1  pgoyette 
    654  1.1  pgoyette /*
    655  1.1  pgoyette  * Test the SVID-compatible Shared Memory facility.
    656  1.1  pgoyette  */
    657  1.1  pgoyette 
    658  1.2  pgoyette ATF_TC_WITH_CLEANUP(shm);
    659  1.1  pgoyette ATF_TC_HEAD(shm, tc)
    660  1.1  pgoyette {
    661  1.1  pgoyette 
    662  1.1  pgoyette 	atf_tc_set_md_var(tc, "timeout", "3");
    663  1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Checks sysv shared memory");
    664  1.1  pgoyette }
    665  1.1  pgoyette 
    666  1.1  pgoyette ATF_TC_BODY(shm, tc)
    667  1.1  pgoyette {
    668  1.1  pgoyette 	struct sigaction sa;
    669  1.1  pgoyette 	struct shmid_ds s_ds;
    670  1.1  pgoyette 	sigset_t sigmask;
    671  1.1  pgoyette 	char *shm_buf;
    672  1.4      jmmv 	int sender_shmid;
    673  1.1  pgoyette 	int c_status;
    674  1.1  pgoyette 
    675  1.1  pgoyette 	/*
    676  1.1  pgoyette 	 * Install a SIGSYS handler so that we can exit gracefully if
    677  1.1  pgoyette 	 * System V Shared Memory support isn't in the kernel.
    678  1.1  pgoyette 	 */
    679  1.1  pgoyette 	did_sigsys = 0;
    680  1.1  pgoyette 	sa.sa_handler = sigsys_handler;
    681  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    682  1.1  pgoyette 	sa.sa_flags = 0;
    683  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
    684  1.1  pgoyette 	    "sigaction SIGSYS: %d", errno);
    685  1.1  pgoyette 
    686  1.1  pgoyette 	/*
    687  1.1  pgoyette 	 * Install a SIGCHLD handler to deal with all possible exit
    688  1.1  pgoyette 	 * conditions of the sharer.
    689  1.1  pgoyette 	 */
    690  1.1  pgoyette 	did_sigchild = 0;
    691  1.1  pgoyette 	child_count = 0;
    692  1.1  pgoyette 	sa.sa_handler = sigchld_handler;
    693  1.1  pgoyette 	sigemptyset(&sa.sa_mask);
    694  1.1  pgoyette 	sa.sa_flags = 0;
    695  1.1  pgoyette 	ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
    696  1.1  pgoyette 	    "sigaction SIGCHLD: %d", errno);
    697  1.1  pgoyette 
    698  1.1  pgoyette 	pgsize = sysconf(_SC_PAGESIZE);
    699  1.1  pgoyette 
    700  1.2  pgoyette 	shmkey = get_ftok(4160);
    701  1.2  pgoyette 	ATF_REQUIRE_MSG(shmkey != (key_t)-1, "get_ftok failed");
    702  1.1  pgoyette 
    703  1.1  pgoyette 	ATF_REQUIRE_MSG((sender_shmid = shmget(shmkey, pgsize,
    704  1.1  pgoyette 					       IPC_CREAT | 0640)) != -1,
    705  1.1  pgoyette 	    "shmget: %d", errno);
    706  1.4      jmmv 	write_int("sender_shmid", sender_shmid);
    707  1.1  pgoyette 
    708  1.1  pgoyette 	ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
    709  1.1  pgoyette 	    "shmctl IPC_STAT: %d", errno);
    710  1.1  pgoyette 
    711  1.1  pgoyette 	print_shmid_ds(&s_ds, 0640);
    712  1.1  pgoyette 
    713  1.1  pgoyette 	s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
    714  1.1  pgoyette 
    715  1.1  pgoyette 	ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_SET, &s_ds) != -1,
    716  1.1  pgoyette 	    "shmctl IPC_SET: %d", errno);
    717  1.1  pgoyette 
    718  1.1  pgoyette 	memset(&s_ds, 0, sizeof(s_ds));
    719  1.1  pgoyette 
    720  1.1  pgoyette 	ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
    721  1.1  pgoyette 	    "shmctl IPC_STAT: %d", errno);
    722  1.1  pgoyette 
    723  1.1  pgoyette 	ATF_REQUIRE_MSG((s_ds.shm_perm.mode & 0777) == 0600,
    724  1.1  pgoyette 	    "IPC_SET of mode didn't hold");
    725  1.1  pgoyette 
    726  1.1  pgoyette 	print_shmid_ds(&s_ds, 0600);
    727  1.1  pgoyette 
    728  1.1  pgoyette 	shm_buf = shmat(sender_shmid, NULL, 0);
    729  1.1  pgoyette 	ATF_REQUIRE_MSG(shm_buf != (void *) -1, "sender: shmat: %d", errno);
    730  1.1  pgoyette 
    731  1.1  pgoyette 	/*
    732  1.1  pgoyette 	 * Write the test pattern into the shared memory buffer.
    733  1.1  pgoyette 	 */
    734  1.1  pgoyette 	strcpy(shm_buf, m2_str);
    735  1.1  pgoyette 
    736  1.1  pgoyette 	switch ((child_pid = fork())) {
    737  1.1  pgoyette 	case -1:
    738  1.1  pgoyette 		atf_tc_fail("fork: %d", errno);
    739  1.1  pgoyette 		return;
    740  1.1  pgoyette 
    741  1.1  pgoyette 	case 0:
    742  1.1  pgoyette 		sharer();
    743  1.1  pgoyette 		break;
    744  1.1  pgoyette 
    745  1.1  pgoyette 	default:
    746  1.1  pgoyette 		break;
    747  1.1  pgoyette 	}
    748  1.1  pgoyette 
    749  1.1  pgoyette 	/*
    750  1.1  pgoyette 	 * Wait for child to finish
    751  1.1  pgoyette 	 */
    752  1.1  pgoyette 	sigemptyset(&sigmask);
    753  1.1  pgoyette 	(void) sigsuspend(&sigmask);
    754  1.1  pgoyette 
    755  1.1  pgoyette 	if (did_sigchild) {
    756  1.1  pgoyette 		c_status = child_status;
    757  1.1  pgoyette 		if (c_status < 0)
    758  1.1  pgoyette 			atf_tc_fail("waitpid: %d", -c_status);
    759  1.1  pgoyette 		else if (WIFEXITED(c_status) == 0)
    760  1.1  pgoyette 			atf_tc_fail("c abnormal exit: %d", c_status);
    761  1.1  pgoyette 		else if (WEXITSTATUS(c_status) != 0)
    762  1.1  pgoyette 			atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
    763  1.1  pgoyette 		else {
    764  1.1  pgoyette 			ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT,
    765  1.1  pgoyette 					       &s_ds) != -1,
    766  1.1  pgoyette 			    "shmctl IPC_STAT: %d", errno);
    767  1.1  pgoyette 
    768  1.1  pgoyette 			print_shmid_ds(&s_ds, 0600);
    769  1.1  pgoyette 			atf_tc_pass();
    770  1.1  pgoyette 		}
    771  1.1  pgoyette 	} else
    772  1.1  pgoyette 		atf_tc_fail("sender: received unexpected signal");
    773  1.1  pgoyette }
    774  1.1  pgoyette 
    775  1.2  pgoyette ATF_TC_CLEANUP(shm, tc)
    776  1.1  pgoyette {
    777  1.4      jmmv 	int sender_shmid;
    778  1.1  pgoyette 
    779  1.1  pgoyette 	/*
    780  1.2  pgoyette 	 * Remove the shared memory area if it exists.
    781  1.1  pgoyette 	 */
    782  1.4      jmmv 	sender_shmid = read_int("sender_shmid");
    783  1.2  pgoyette 	if (sender_shmid != -1)
    784  1.4      jmmv 		if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
    785  1.4      jmmv 			err(1, "shmctl IPC_RMID");
    786  1.1  pgoyette }
    787  1.1  pgoyette 
    788  1.1  pgoyette void
    789  1.5  pgoyette print_shmid_ds(struct shmid_ds *sp, mode_t mode)
    790  1.1  pgoyette {
    791  1.1  pgoyette 	uid_t uid = geteuid();
    792  1.1  pgoyette 	gid_t gid = getegid();
    793  1.1  pgoyette 
    794  1.1  pgoyette 	printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
    795  1.1  pgoyette 	    sp->shm_perm.uid, sp->shm_perm.gid,
    796  1.1  pgoyette 	    sp->shm_perm.cuid, sp->shm_perm.cgid,
    797  1.1  pgoyette 	    sp->shm_perm.mode & 0777);
    798  1.1  pgoyette 
    799  1.1  pgoyette 	printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
    800  1.1  pgoyette 	    (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
    801  1.1  pgoyette 	    sp->shm_nattch);
    802  1.1  pgoyette 
    803  1.1  pgoyette 	printf("atime: %s", ctime(&sp->shm_atime));
    804  1.1  pgoyette 	printf("dtime: %s", ctime(&sp->shm_dtime));
    805  1.1  pgoyette 	printf("ctime: %s", ctime(&sp->shm_ctime));
    806  1.1  pgoyette 
    807  1.1  pgoyette 	/*
    808  1.1  pgoyette 	 * Sanity check a few things.
    809  1.1  pgoyette 	 */
    810  1.1  pgoyette 
    811  1.1  pgoyette 	ATF_REQUIRE_MSG(sp->shm_perm.uid == uid && sp->shm_perm.cuid == uid,
    812  1.1  pgoyette 	    "uid mismatch");
    813  1.1  pgoyette 
    814  1.1  pgoyette 	ATF_REQUIRE_MSG(sp->shm_perm.gid == gid && sp->shm_perm.cgid == gid,
    815  1.1  pgoyette 	    "gid mismatch");
    816  1.1  pgoyette 
    817  1.5  pgoyette 	ATF_REQUIRE_MSG((sp->shm_perm.mode & 0777) == mode,
    818  1.5  pgoyette 	    "mode mismatch %o != %o", sp->shm_perm.mode & 0777, mode);
    819  1.1  pgoyette }
    820  1.1  pgoyette 
    821  1.1  pgoyette void
    822  1.5  pgoyette sharer(void)
    823  1.1  pgoyette {
    824  1.1  pgoyette 	int shmid;
    825  1.1  pgoyette 	void *shm_buf;
    826  1.1  pgoyette 
    827  1.1  pgoyette 	shmid = shmget(shmkey, pgsize, 0);
    828  1.1  pgoyette 	ATF_REQUIRE_MSG(shmid != -1, "receiver: shmget:%d", errno);
    829  1.1  pgoyette 
    830  1.1  pgoyette 	shm_buf = shmat(shmid, NULL, 0);
    831  1.1  pgoyette 	ATF_REQUIRE_MSG(shm_buf != (void *) -1, "receiver: shmat: %d", errno);
    832  1.1  pgoyette 
    833  1.1  pgoyette 	printf("%s\n", (const char *)shm_buf);
    834  1.1  pgoyette 
    835  1.1  pgoyette 	ATF_REQUIRE_MSG(strcmp((const char *)shm_buf, m2_str) == 0,
    836  1.1  pgoyette 	    "receiver: data isn't correct");
    837  1.1  pgoyette 
    838  1.1  pgoyette 	exit(0);
    839  1.1  pgoyette }
    840  1.1  pgoyette 
    841  1.1  pgoyette ATF_TP_ADD_TCS(tp)
    842  1.1  pgoyette {
    843  1.1  pgoyette 
    844  1.1  pgoyette 	ATF_TP_ADD_TC(tp, msg);
    845  1.1  pgoyette 	ATF_TP_ADD_TC(tp, sem);
    846  1.1  pgoyette 	ATF_TP_ADD_TC(tp, shm);
    847  1.1  pgoyette 
    848  1.1  pgoyette 	return atf_no_error();
    849  1.1  pgoyette }
    850  1.1  pgoyette 
    851