tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
posix shared memory
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
/* $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);
}
Home |
Main Index |
Thread Index |
Old Index