imcplugin demo: Extend to support stat() call
[nativeclient.git] / tests / srpc / srpc_shm.c
blob5dbecf715b22d792b8f7db5e19bdac0b1c192ca8
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 /* NativeClient shared memory handle return test. */
35 #include <errno.h>
36 #include <nacl/nacl_srpc.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/mman.h>
40 #include <sys/nacl_syscalls.h>
43 * GetShmHandle creates and returns a shared memory region.
45 int GetShmHandle(NaClSrpcChannel *channel,
46 NaClSrpcArg **in_args,
47 NaClSrpcArg **out_args);
49 NACL_SRPC_METHOD("get_shm_handle:i:h", GetShmHandle);
51 int GetShmHandle(NaClSrpcChannel *channel,
52 NaClSrpcArg **in_args,
53 NaClSrpcArg **out_args) {
54 int desc;
55 size_t size = in_args[0]->u.ival;
56 /* Start up banner, clear errors. */
57 printf("GetShmHandle: size %d\n", size);
58 /* Create an IMC shared memory region. */
59 desc = imc_mem_obj_create(size);
60 if (desc >= 0) {
61 char* map_addr;
62 map_addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, desc, 0);
63 if (MAP_FAILED == map_addr) {
64 printf("GetShmHandle: mmap failed\n");
65 /* TODO: close handle */
66 desc = -1;
68 } else {
69 printf("GetShmHandle: imc_mem_obj_create failed %d\n", desc);
71 /* Return the shm descriptor. */
72 out_args[0]->u.hval = desc;
73 return NACL_SRPC_RESULT_OK;
76 * GetInvalidHandle creates and returns a shared memory region.
78 int GetInvalidHandle(NaClSrpcChannel *channel,
79 NaClSrpcArg **in_args,
80 NaClSrpcArg **out_args);
82 NACL_SRPC_METHOD("get_invalid_handle::h", GetInvalidHandle);
84 int GetInvalidHandle(NaClSrpcChannel *channel,
85 NaClSrpcArg **in_args,
86 NaClSrpcArg **out_args) {
87 /* Return an invalid shm descriptor. */
88 out_args[0]->u.ival = -1;
89 return NACL_SRPC_RESULT_OK;