Article View: fa.netbsd.tech.kern
Article #46posix shared memory
From: YAMAMOTO Takashi
Date: Fri, 29 Mar 2013 14:27
Date: Fri, 29 Mar 2013 14:27
166 lines
6248 bytes
6248 bytes
--Boundary-20130329170018-1693901
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 8bit
i have userland implementation of posix shared memory.
(attached)
rmind proposed a kernel implementation while ago.
http://mail-index.netbsd.org/tech-kern/2009/07/30/msg005599.html
(thus cc:)
which way should we go?
IMO userland implementation is better because:
- simpler
- smaller
- a bug would have less impact
- "locking object in memory" functionality is better to be
implemented with more generic api like fcntl if necessary
YAMAMOTO Takashi
--Boundary-20130329170018-1693901
Content-Type: Text/Plain; charset=us-ascii
Content-Disposition: attachment; filename="shm.c"
Content-Transfer-Encoding: 8bit
/* $NetBSD$ */
/*-
* Copyright (c)2013 YAMAMOTO Takashi,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* POSIX shared memory
*
* this is a pure userland implementation.
*
* in this implementation, shared memory objects are regular files in
* _PATH_SHM directory.
* the directory should have mode 1333. (or 1777. this implmentation
* doesn't need directory read access)
*
* for performance reasons, it might be desirable to mount a special
* filesystem like tmpfs on the directory.
* requirements for the filesystem:
* - it should support create, open, unlink, mmap of regular files.
* - it should support access controls with mode bits.
* - it should support directory sticky bit.
* - it doesn't need to support sub directories, symlinks, hardlinks,
* or special files.
* - it doesn't need to support persistency across a system reboot.
* POSIX allows either behaviours.
*
* why not implemented in kernel?
* in addition to usual reasons to prefer userland implementations in
* general, we can benefit from existing filesystem functionalities
* for free. eg. quota.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD$");
#include "namespace.h"
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define _PATH_SHM "/var/shm/"
#ifdef __weak_alias
__weak_alias(shm_open,_shm_open)
__weak_alias(shm_unlink,_shm_unlink)
#endif
static int
_mkname(const char *name, char buf[PATH_MAX])
{
const char *p;
int ret;
/*
* check the given name.
*
* SUSv3:
* If name begins with the slash character, then
* calling shm_open() with the same value of name refer to the same
* shared memory object, as long as that name has not been removed.
* If name does not begin with the slash character, the effect is
* implementation-defined. The interpretation of slash characters
* other than the leading slash character in name is implementation-
* defined.
*/
p = name + strspn(name, "/"); /* skip leading slashes */
if (name[0] != '/' || strchr(p, '/')) {
/*
* implementation-defined behaviour.
*/
errno = ENOTSUP;
return -1;
}
/*
* construct the absolute path of the backing file.
*/
ret = snprintf(buf, PATH_MAX, "%s%s", _PATH_SHM, p);
if (ret == -1) {
return -1;
} else if ((size_t)ret >= PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
return 0;
}
int
shm_open(const char *name, int oflags, mode_t mode)
{
char real_name[PATH_MAX];
if (_mkname(name, real_name)) {
return -1;
}
return open(real_name, oflags, mode);
}
int
shm_unlink(const char *name)
{
char real_name[PATH_MAX];
if (_mkname(name, real_name)) {
return -1;
}
return unlink(real_name);
}
--Boundary-20130329170018-1693901--
Technical Details
Message-ID:
<fa.NBppblL+1aBqokYkfcBQZSUIQ6k@ifi.uio.no>Path:
arc1-ca.pugleaf.net!ex12ni10502wid.1!nntp.google.com!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10c6f4,c8504c1ed5e12aa5,start
X-Google-NewGroupId: yes
X-Google-Attributes: gidb7e13b6095,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
X-Received: by 10.180.106.73 with SMTP id gs9mr4485905wib.2.1364567250016;
Fri, 29 Mar 2013 07:27:30 -0700 (PDT)
Path: arc1-ca.pugleaf.net!ex12ni10502wid.1!nntp.google.com!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mail
From: yamt@mwd.biglobe.ne.jp (YAMAMOTO Takashi)
Newsgroups: fa.netbsd.tech.kern
Subject: posix shared memory
Date: Fri, 29 Mar 2013 14:27:29 UTC
Organization: Internet mailing list
Lines: 167
Sender: tech-kern-owner@NetBSD.org
Message-ID: <fa.NBppblL+1aBqokYkfcBQZSUIQ6k@ifi.uio.no>
NNTP-Posting-Host: mail-jess.uio.no
Mime-Version: 1.0
X-Trace: readme.uio.no 1364567249 6385 129.240.7.9 (29 Mar 2013 14:27:29 GMT)
X-Complaints-To: abuse@uio.no
NNTP-Posting-Date: Fri, 29 Mar 2013 14:27:29 +0000 (UTC)
Cc: tech-kern@netbsd.org, rmind@netbsd.org
To: tech-userlevel@netbsd.org
Delivered-To: tech-kern@netbsd.org
X-Mailer: Cue version 0.8 (111031-0948/takashi)
List-Id: tech-kern.NetBSD.org
Original-Date: Fri, 29 Mar 2013 14:27:06 +0000 (UTC)
Original-Message-Id: <20130329142706.30B3A14A293@mail.netbsd.org>
Content-Type: Multipart/Mixed; boundary="Boundary-20130329170018-1693901"