Changeset 25374

Show
Ignore:
Timestamp:
05/08/08 08:42:33 (1 week ago)
Author:
bonefish
Message:
Implement shm_open() and shm_unlink(). The shared memory objects are
simply created as files in /boot/var/shared_memory/. The Bootscript
clears the directory.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • haiku/trunk/data/system/boot/Bootscript

    r25335 r25374  
    5757SCRIPTS=beos/system/boot 
    5858SERVERS=beos/system/servers 
     59 
     60# clean the shared memory dir 
     61shmDir=/boot/var/shared_memory 
     62rm -rf $shmDir 
     63mkdir $shmDir 
     64chmod 777 $shmDir 
    5965 
    6066# Set up the environment 
  • haiku/trunk/headers/posix/sys/mman.h

    r24964 r25374  
    66#define _SYS_MMAN_H 
    77 
     8#include <sys/cdefs.h> 
    89#include <sys/types.h> 
    910 
     
    2526#define MAP_FAILED              ((void*)-1) 
    2627 
    27 #ifdef __cplusplus 
    28 extern "C" { 
    29 #endif 
    3028 
     29__BEGIN_DECLS 
    3130 
    32 extern void*  mmap(void* address, size_t length, int protection, int flags, 
    33                                        int fd, off_t offset); 
    34 extern int            munmap(void* address, size_t length); 
     31void* mmap(void* address, size_t length, int protection, int flags, 
     32                        int fd, off_t offset); 
     33int           munmap(void* address, size_t length); 
    3534 
     35int             shm_open(const char* name, int openMode, mode_t permissions); 
     36int             shm_unlink(const char* name); 
    3637 
    37 #ifdef __cplusplus 
    38 
    39 #endif 
     38__END_DECLS 
    4039 
    4140 
  • haiku/trunk/src/system/libroot/posix/sys/mman.cpp

    r24967 r25374  
    77 
    88#include <errno.h> 
     9#include <fcntl.h> 
     10#include <string.h> 
    911 
    1012#include <OS.h> 
     
    1315#include <syscalls.h> 
    1416#include <vm.h> 
     17 
     18 
     19static const char* kSharedMemoryDir = "/boot/var/shared_memory/"; 
     20 
     21 
     22static bool 
     23append_string(char*& path, size_t& bytesLeft, const char* toAppend, size_t size) 
     24{ 
     25        if (bytesLeft <= size) 
     26                return false; 
     27 
     28        memcpy(path, toAppend, size); 
     29        path += size; 
     30        path[0] = '\0'; 
     31        bytesLeft -= size; 
     32 
     33        return true; 
     34} 
     35 
     36 
     37static bool 
     38append_string(char*& path, size_t& bytesLeft, const char* toAppend) 
     39{ 
     40        return append_string(path, bytesLeft, toAppend, strlen(toAppend)); 
     41} 
     42 
     43 
     44static status_t 
     45shm_name_to_path(const char* name, char* path, size_t pathSize) 
     46{ 
     47        if (name == NULL) 
     48                return B_BAD_VALUE; 
     49 
     50        // skip leading slashes 
     51        while (*name == '/') 
     52                name++; 
     53 
     54        if (*name == '\0') 
     55                return B_BAD_VALUE; 
     56 
     57        // create the path; replace occurrences of '/' by "%s" and '%' by "%%" 
     58        if (!append_string(path, pathSize, kSharedMemoryDir)) 
     59                return ENAMETOOLONG; 
     60 
     61        while (const char* found = strpbrk(name, "%/")) { 
     62                // append section that doesn't need escaping 
     63                if (found != name) { 
     64                        if (!append_string(path, pathSize, name, found - name)) 
     65                                return ENAMETOOLONG; 
     66                } 
     67 
     68                // append escaped char 
     69                const char* append = (*found == '%' ? "%%" : "%s"); 
     70                if (!append_string(path, pathSize, append, 2)) 
     71                        return ENAMETOOLONG; 
     72                name = found + 1; 
     73        } 
     74 
     75        // append remaining string 
     76        if (!append_string(path, pathSize, name)) 
     77                return ENAMETOOLONG; 
     78 
     79        return B_OK; 
     80} 
     81 
     82 
     83// #pragma mark - 
    1584 
    1685 
     
    72141        RETURN_AND_SET_ERRNO(_kern_unmap_memory(address, length)); 
    73142} 
     143 
     144 
     145int 
     146shm_open(const char* name, int openMode, mode_t permissions) 
     147{ 
     148        char path[PATH_MAX]; 
     149        status_t error = shm_name_to_path(name, path, sizeof(path)); 
     150        if (error != B_OK) 
     151                RETURN_AND_SET_ERRNO(error); 
     152 
     153        return open(path, openMode, permissions); 
     154} 
     155 
     156 
     157int 
     158shm_unlink(const char* name) 
     159{ 
     160        char path[PATH_MAX]; 
     161        status_t error = shm_name_to_path(name, path, sizeof(path)); 
     162        if (error != B_OK) 
     163                RETURN_AND_SET_ERRNO(error); 
     164 
     165        return unlink(path); 
     166}