shm.c revision 1.2 1 /* $NetBSD: shm.c,v 1.2 2015/06/30 11:46:47 martin 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.2 2015/06/30 11:46:47 martin 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 static char _shmfs_path_buf[PATH_MAX];
62
63 static bool
64 _shm_check_fs(void)
65 {
66 const char *shmfs = SHMFS_DIR_PATH;
67 struct statvfs sv;
68 struct stat st;
69 char buf[PATH_MAX];
70 ssize_t cnt;
71
72 if ((cnt = readlink(shmfs, buf, sizeof(buf))) > 0) {
73 if ((size_t)cnt >= sizeof(buf))
74 return false;
75 buf[cnt] = 0;
76 shmfs = buf;
77 }
78 if (statvfs1(shmfs, &sv, ST_NOWAIT) == -1) {
79 return false;
80 }
81 if (strncmp(sv.f_fstypename, MOUNT_SHMFS, sizeof(sv.f_fstypename))) {
82 return false;
83 }
84
85 if (lstat(shmfs, &st) == -1) {
86 return false;
87 }
88 if ((st.st_mode & SHMFS_DIR_MODE) != SHMFS_DIR_MODE) {
89 return false;
90 }
91
92 if (shmfs == buf) {
93 strcpy(_shmfs_path_buf, buf);
94 _shmfs_path = _shmfs_path_buf;
95 } else {
96 _shmfs_path = shmfs;
97 }
98 return true;
99 }
100
101 static bool
102 _shm_get_path(char *buf, size_t len, const char *name)
103 {
104 int ret;
105
106 if (__predict_false(!_shmfs_path) && !_shm_check_fs()) {
107 errno = ENOTSUP;
108 return false;
109 }
110
111 /*
112 * As per POSIX: the name should begin with a slash character.
113 * We may disallow other slashes (implementation-defined behaviour).
114 */
115 if (*name++ != '/' || strchr(name, '/') != NULL) {
116 errno = EINVAL;
117 return false;
118 }
119
120 ret = snprintf(buf, len, "%s/%s%s",
121 _shmfs_path, SHMFS_OBJ_PREFIX, name);
122
123 if ((size_t)ret >= PATH_MAX) {
124 errno = ENAMETOOLONG;
125 return false;
126 }
127 return ret != -1;
128 }
129
130 int
131 shm_open(const char *name, int oflag, mode_t mode)
132 {
133 char path[PATH_MAX + 1];
134
135 if (!_shm_get_path(path, sizeof(path), name)) {
136 return -1;
137 }
138 return open(path, oflag | O_CLOEXEC | O_NOFOLLOW, mode);
139 }
140
141 int
142 shm_unlink(const char *name)
143 {
144 char path[PATH_MAX + 1];
145
146 if (!_shm_get_path(path, sizeof(path), name)) {
147 return -1;
148 }
149 return unlink(path);
150 }
151