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 = ENAMETOOLONG if the length
11 * of the name argument exceeds {PATH_MAX} (including the terminating null).
12 *
13 * The used name follow the scheme:
14 * aaaaaa/aaaaaa/aaaaaa/aaa ...
15 */
16
17#include <stdio.h>
18#include <sys/mman.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <errno.h>
22#include <limits.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include "posixtest.h"
26
27/* Ensure that each component length is short enough */
28#define COMPONENT_SIZE _POSIX_NAME_MAX
29
30int main(void)
31{
32	int fd, i, path_max;
33	char *shm_name;
34
35	path_max = pathconf("/", _PC_PATH_MAX);
36	if (path_max == -1) {
37		perror("An error occurs when calling pathconf()");
38		return PTS_UNRESOLVED;
39	}
40	shm_name = malloc(path_max + 1);
41
42	for (i = 0; i < path_max; i++)
43		shm_name[i] = (i + 1) % COMPONENT_SIZE ? 'a' : '/';
44	shm_name[path_max] = 0;
45
46	fd = shm_open(shm_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
47
48	if (fd == -1 && errno == ENAMETOOLONG) {
49		printf("Test PASSED\n");
50		return PTS_PASS;
51	} else if (fd != -1) {
52		printf("FAILED: shm_open() succeeded.\n");
53		shm_unlink(shm_name);
54		return PTS_FAIL;
55	}
56
57	if (sysconf(_SC_VERSION) >= 200800L) {
58		printf("UNTESTED: shm_open() did not fail with ENAMETOLONG\n");
59		return PTS_UNTESTED;
60	}
61
62	perror("FAILED: shm_open");
63	return PTS_FAIL;
64}
65