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