Tidy: remove some unused definitions
[nativeclient.git] / intermodule_comm / nacl_imc_unistd.cc
blob63b32c6545d9a3fa73f89a5e3e181a0591fb113a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // NaCl inter-module communication primitives.
35 // This file implements common parts of IMC for "unix like systems" (i.e. not
36 // used on Windows).
38 // TODO: Perhaps this file should go into a platform-specific directory
39 // (posix? unixlike?) We have a little convention going where mac/linux stuff
40 // goes in the linux directory and is referenced by the mac build but that's a
41 // little sloppy.
43 #include "native_client/intermodule_comm/nacl_imc.h"
45 #include <assert.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <sys/mman.h>
54 #include <sys/types.h>
56 #include <algorithm>
58 #include "native_client/include/atomic_ops.h"
60 namespace nacl {
62 namespace {
64 // The pathname prefix for memory objects created by CreateMemoryObject().
65 const char kShmPrefix[] = "/google-nacl-shm-";
67 } // namespace
69 bool WouldBlock() {
70 return (errno == EAGAIN) ? true : false;
73 int GetLastErrorString(char* buffer, size_t length) {
74 #if NACL_LINUX
75 // Note some Linux distributions provide only GNU version of strerror_r().
76 if (buffer == NULL || length == 0) {
77 errno = ERANGE;
78 return -1;
80 char* message = strerror_r(errno, buffer, length);
81 if (message != buffer) {
82 size_t message_bytes = strlen(message) + 1;
83 length = std::min(message_bytes, length);
84 memmove(buffer, message, length);
85 buffer[length - 1] = '\0';
87 return 0;
88 #else
89 return strerror_r(errno, buffer, length);
90 #endif
93 Handle CreateMemoryObject(size_t length) {
94 static AtomicWord memory_object_count;
96 char name[PATH_MAX];
97 for (;;) {
98 snprintf(name, sizeof name, "%s-%u.%u", kShmPrefix,
99 getpid(),
100 static_cast<uint32_t>(AtomicIncrement(&memory_object_count, 1)));
101 int m = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0666);
102 if (0 <= m) {
103 if (ftruncate(m, length) == -1) {
104 close(m);
105 m = -1;
107 shm_unlink(name);
108 return m;
110 if (errno != EEXIST) {
111 return -1;
116 void* Map(void* start, size_t length, int prot, int flags,
117 Handle memory, off_t offset) {
118 static int posix_prot[4] = {
119 PROT_NONE,
120 PROT_READ,
121 PROT_WRITE,
122 PROT_READ | PROT_WRITE
125 int adjusted = 0;
126 if (flags & kMapShared) {
127 adjusted |= MAP_SHARED;
129 if (flags & kMapPrivate) {
130 adjusted |= MAP_PRIVATE;
132 if (flags & kMapFixed) {
133 adjusted |= MAP_FIXED;
135 return mmap(start, length, posix_prot[prot & 3], adjusted, memory, offset);
138 int Unmap(void* start, size_t length) {
139 return munmap(start, length);
142 } // namespace nacl