1 1.3 martin /* $NetBSD: shm.c,v 1.3 2015/07/08 07:14:38 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.3 martin __RCSID("$NetBSD: shm.c,v 1.3 2015/07/08 07:14:38 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.3 martin static bool shm_ok = false; 61 1.1 rmind 62 1.1 rmind static bool 63 1.1 rmind _shm_check_fs(void) 64 1.1 rmind { 65 1.3 martin int fd; 66 1.1 rmind struct statvfs sv; 67 1.1 rmind struct stat st; 68 1.1 rmind 69 1.3 martin fd = open(SHMFS_DIR_PATH, O_DIRECTORY|O_RDONLY); 70 1.3 martin if (fd == -1) 71 1.1 rmind return false; 72 1.1 rmind 73 1.3 martin if (fstatvfs1(fd, &sv, ST_NOWAIT) == -1) 74 1.3 martin goto out; 75 1.3 martin 76 1.3 martin if (strncmp(sv.f_fstypename, MOUNT_SHMFS, sizeof(sv.f_fstypename))) 77 1.3 martin goto out; 78 1.3 martin 79 1.3 martin if (fstat(fd, &st) == -1) 80 1.3 martin goto out; 81 1.3 martin 82 1.3 martin if ((st.st_mode & SHMFS_DIR_MODE) != SHMFS_DIR_MODE) 83 1.3 martin goto out; 84 1.3 martin 85 1.3 martin shm_ok = true; 86 1.1 rmind 87 1.3 martin out: 88 1.3 martin close(fd); 89 1.3 martin return shm_ok; 90 1.1 rmind } 91 1.1 rmind 92 1.1 rmind static bool 93 1.1 rmind _shm_get_path(char *buf, size_t len, const char *name) 94 1.1 rmind { 95 1.1 rmind int ret; 96 1.1 rmind 97 1.3 martin if (__predict_false(!shm_ok) && !_shm_check_fs()) { 98 1.1 rmind errno = ENOTSUP; 99 1.1 rmind return false; 100 1.1 rmind } 101 1.1 rmind 102 1.1 rmind /* 103 1.1 rmind * As per POSIX: the name should begin with a slash character. 104 1.1 rmind * We may disallow other slashes (implementation-defined behaviour). 105 1.1 rmind */ 106 1.1 rmind if (*name++ != '/' || strchr(name, '/') != NULL) { 107 1.1 rmind errno = EINVAL; 108 1.1 rmind return false; 109 1.1 rmind } 110 1.1 rmind 111 1.3 martin ret = snprintf(buf, len, SHMFS_DIR_PATH "/" SHMFS_OBJ_PREFIX "%s", 112 1.3 martin name); 113 1.1 rmind 114 1.3 martin if ((size_t)ret >= len) { 115 1.1 rmind errno = ENAMETOOLONG; 116 1.1 rmind return false; 117 1.1 rmind } 118 1.1 rmind return ret != -1; 119 1.1 rmind } 120 1.1 rmind 121 1.1 rmind int 122 1.1 rmind shm_open(const char *name, int oflag, mode_t mode) 123 1.1 rmind { 124 1.3 martin char path[PATH_MAX]; 125 1.1 rmind 126 1.1 rmind if (!_shm_get_path(path, sizeof(path), name)) { 127 1.1 rmind return -1; 128 1.1 rmind } 129 1.1 rmind return open(path, oflag | O_CLOEXEC | O_NOFOLLOW, mode); 130 1.1 rmind } 131 1.1 rmind 132 1.1 rmind int 133 1.1 rmind shm_unlink(const char *name) 134 1.1 rmind { 135 1.3 martin char path[PATH_MAX]; 136 1.1 rmind 137 1.1 rmind if (!_shm_get_path(path, sizeof(path), name)) { 138 1.1 rmind return -1; 139 1.1 rmind } 140 1.1 rmind return unlink(path); 141 1.1 rmind } 142