1/*
2 *  This program is free software; you can redistribute it and/or modify
3 *  it under the terms of the GNU General Public License version 2.
4 *
5 *  This program is distributed in the hope that it will be useful,
6 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
7 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8 *  GNU General Public License for more details.
9 *
10 * Test that the shm_open() function sets errno = EACCES if the shared memory
11 * object exists and the permissions specified by oflag are denied
12 *
13 * Create a shared memory object with no read or write permission and try to
14 * open it.
15 */
16
17#define _XOPEN_SOURCE 600
18#include <stdio.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <unistd.h>
24#include <sys/types.h>
25#include <pwd.h>
26#include <string.h>
27#include "posixtest.h"
28
29#define SHM_NAME "posixtest_32-1"
30
31/** Set the euid of this process to a non-root uid */
32int set_nonroot()
33{
34	struct passwd *pw;
35	setpwent();
36	/* search for the first user which is non root */
37	while ((pw = getpwent()) != NULL)
38		if (strcmp(pw->pw_name, "root"))
39			break;
40	endpwent();
41	if (pw == NULL) {
42		printf("There is no other user than current and root.\n");
43		return 1;
44	}
45
46	if (seteuid(pw->pw_uid) != 0) {
47		if (errno == EPERM) {
48			printf
49			    ("You don't have permission to change your UID.\n");
50			return 1;
51		}
52		perror("An error occurs when calling seteuid()");
53		return 1;
54	}
55
56	printf("Testing with user '%s' (uid: %d)\n",
57	       pw->pw_name, (int)geteuid());
58	return 0;
59}
60
61int main(void)
62{
63	int fd;
64
65	/* This test should be run under standard user permissions */
66	if (getuid() == 0) {
67		if (set_nonroot() != 0) {
68			printf("Cannot run this test as non-root user\n");
69			return PTS_UNTESTED;
70		}
71	}
72
73	fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, 0);
74	if (fd == -1) {
75		perror("An error occurs when calling shm_open()");
76		return PTS_UNRESOLVED;
77	}
78
79	fd = shm_open(SHM_NAME, O_RDWR, 0);
80
81	if (fd == -1 && errno == EACCES) {
82		printf("Test PASSED\n");
83		shm_unlink(SHM_NAME);
84		return PTS_PASS;
85	} else if (fd != -1) {
86		printf("shm_open success.\n");
87		shm_unlink(SHM_NAME);
88		return PTS_FAIL;
89	}
90
91	perror("shm_open");
92	return PTS_FAIL;
93}
94