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