1d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes/*
2d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
3d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * All rights reserved.
5d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
6d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Redistribution and use in source and binary forms, with or without
7d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * modification, are permitted provided that the following conditions
8d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * are met:
9d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 1. Redistributions of source code must retain the above copyright
10d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer.
11d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
12d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
13d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    documentation and/or other materials provided with the distribution.
14d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 3. The name of the author may not be used to endorse or promote products
15d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    derived from this software without specific prior written permission.
16d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
17d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes */
28d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
29d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include "tests.h"
30d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <errno.h>
31d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <stdio.h>
32d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <stdlib.h>
33d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <sys/shm.h>
34d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
35d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include "xlat.h"
36d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include "xlat/shm_resource_flags.h"
37d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
38d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesstatic int id = -1;
39d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
40d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesstatic void
41d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescleanup(void)
42d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{
43d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	shmctl(id, IPC_RMID, NULL);
44d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id);
45d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	id = -1;
46d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes}
47d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
48d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesint
49d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesmain(void)
50d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{
51d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const key_t private_key =
52d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
53d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
54d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const int bogus_id = 0xdefaced1;
55d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const int bogus_cmd = 0xdefaced2;
56d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static void * const bogus_addr = (void *) -1L;
57d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const size_t bogus_size =
58d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	/*
59d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	 * musl sets size to SIZE_MAX if size argument is greater than
60d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	 * PTRDIFF_MAX - musl/src/ipc/shmget.c
61d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	 */
62d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	#ifdef __GLIBC__
63d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(size_t) 0xdec0ded1dec0ded2ULL;
64d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	#else
65d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(size_t) 0x1e55c0de5dec0dedULL;
66d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	#endif
67d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	static const int bogus_flags = 0xface1e55;
68d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
69d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	int rc;
70d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	struct shmid_ds ds;
71d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
72d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	rc = shmget(bogus_key, bogus_size, bogus_flags);
73d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmget\\(%#llx, %zu, %s%s%s%#x\\|%#04o\\) += %s\n",
74d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       zero_extend_signed_to_ull(bogus_key), bogus_size,
75d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
76d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
77d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       SHM_HUGETLB & bogus_flags ? "SHM_HUGETLB\\|" : "",
78d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | SHM_HUGETLB),
79d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       bogus_flags & 0777, sprintrc_grep(rc));
80d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
81d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	id = shmget(private_key, 1, 0600);
82d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (id < 0)
83d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_skip("shmget");
84d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
85d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	atexit(cleanup);
86d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
87d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	rc = shmctl(bogus_id, bogus_cmd, NULL);
88d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?%#x /\\* SHM_\\?\\?\\? \\*/, NULL\\)"
89d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       " += %s\n", bogus_id, bogus_cmd, sprintrc_grep(rc));
90d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
91d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	rc = shmctl(bogus_id, IPC_STAT, bogus_addr);
92d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, %p\\) += %s\n",
93d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       bogus_id, bogus_addr, sprintrc_grep(rc));
94d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
95d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (shmctl(id, IPC_STAT, &ds))
96d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_skip("shmctl IPC_STAT");
97d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{shm_perm=\\{uid=%u, gid=%u, "
98d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		"mode=%#o, key=%u, cuid=%u, cgid=%u\\}, shm_segsz=%u, shm_cpid=%u, "
99d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		"shm_lpid=%u, shm_nattch=%u, shm_atime=%u, shm_dtime=%u, "
100d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		"shm_ctime=%u\\}\\) += 0\n",
101d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid,
102d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds.shm_perm.mode, (unsigned) ds.shm_perm.__key,
103d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds.shm_perm.cuid, (unsigned) ds.shm_perm.cgid,
104d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds.shm_segsz, (unsigned) ds.shm_cpid,
105d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds.shm_lpid, (unsigned) ds.shm_nattch,
106d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds.shm_atime, (unsigned) ds.shm_dtime,
107d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		(unsigned) ds. shm_ctime);
108d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
109d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (shmctl(id, IPC_SET, &ds))
110d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_skip("shmctl IPC_SET");
111d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?IPC_SET, \\{shm_perm=\\{uid=%u, gid=%u"
112d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       ", mode=%#o\\}, ...\\}\\) += 0\n",
113d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid,
114d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       (unsigned) ds.shm_perm.mode);
115d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
116d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	rc = shmctl(0, SHM_INFO, &ds);
117d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(0, (IPC_64\\|)?SHM_INFO, %p\\) += %s\n",
118d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       &ds, sprintrc_grep(rc));
119d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
120d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	rc = shmctl(id, SHM_STAT, &ds);
121d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("shmctl\\(%d, (IPC_64\\|)?SHM_STAT, %p\\) += %s\n",
122d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       id, &ds, sprintrc_grep(rc));
123d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
124d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	return 0;
125d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes}
126