xref: /netbsd-src/lib/librt/shm.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: shm.c,v 1.1 2013/12/19 19:11:50 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Mindaugas Rasiukevicius.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Interface for POSIX shared memory objects.
34  */
35 
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: shm.c,v 1.1 2013/12/19 19:11:50 rmind Exp $");
38 
39 #include <sys/mman.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 
43 #include <stdio.h>
44 #include <stdbool.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <limits.h>
50 
51 /*
52  * Shared memory objects are supported using tmpfs.
53  */
54 #define	SHMFS_DIR_PATH		"/var/shm"
55 #define	SHMFS_DIR_MODE		(S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
56 #define	SHMFS_OBJ_PREFIX	".shmobj_"
57 
58 #define	MOUNT_SHMFS		MOUNT_TMPFS
59 
60 static const char *		_shmfs_path = NULL;
61 
62 static bool
63 _shm_check_fs(void)
64 {
65 	const char *shmfs = SHMFS_DIR_PATH;
66 	struct statvfs sv;
67 	struct stat st;
68 
69 	if (statvfs1(shmfs, &sv, ST_NOWAIT) == -1) {
70 		return false;
71 	}
72 	if (strncmp(sv.f_fstypename, MOUNT_SHMFS, sizeof(sv.f_fstypename))) {
73 		return false;
74 	}
75 
76 	if (lstat(shmfs, &st) == -1) {
77 		return false;
78 	}
79 	if ((st.st_mode & SHMFS_DIR_MODE) != SHMFS_DIR_MODE) {
80 		return false;
81 	}
82 
83 	_shmfs_path = shmfs;
84 	return true;
85 }
86 
87 static bool
88 _shm_get_path(char *buf, size_t len, const char *name)
89 {
90 	int ret;
91 
92 	if (__predict_false(!_shmfs_path) && !_shm_check_fs()) {
93 		errno = ENOTSUP;
94 		return false;
95 	}
96 
97 	/*
98 	 * As per POSIX: the name should begin with a slash character.
99 	 * We may disallow other slashes (implementation-defined behaviour).
100 	 */
101 	if (*name++ != '/' || strchr(name, '/') != NULL) {
102 		errno = EINVAL;
103 		return false;
104 	}
105 
106 	ret = snprintf(buf, len, "%s/%s%s",
107 	    _shmfs_path, SHMFS_OBJ_PREFIX, name);
108 
109 	if ((size_t)ret >= PATH_MAX) {
110 		errno = ENAMETOOLONG;
111 		return false;
112 	}
113 	return ret != -1;
114 }
115 
116 int
117 shm_open(const char *name, int oflag, mode_t mode)
118 {
119 	char path[PATH_MAX + 1];
120 
121 	if (!_shm_get_path(path, sizeof(path), name)) {
122 		return -1;
123 	}
124 	return open(path, oflag | O_CLOEXEC | O_NOFOLLOW, mode);
125 }
126 
127 int
128 shm_unlink(const char *name)
129 {
130 	char path[PATH_MAX + 1];
131 
132 	if (!_shm_get_path(path, sizeof(path), name)) {
133 		return -1;
134 	}
135 	return unlink(path);
136 }
137