1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <cstring>
21 #include <ctime>
22 #include <sys/ipc.h>
23 #include <sys/shm.h>
24 
25 #define SHM_RDONLY 010000
26 #define PARAM_0 0
27 #define ONEVAL 1
28 #define MINUSONE (-1)
29 #define TEST_MODE 0666
30 #define VALUE100 100
31 #define ERRON_0 0
32 
Shmget(napi_env env, napi_callback_info info)33 static napi_value Shmget(napi_env env, napi_callback_info info)
34 {
35     errno = ERRON_0;
36     static const char path[] = ".";
37     static const int id = 'h';
38     int shmid;
39     shmid = shmget(ftok(path, id), VALUE100, IPC_CREAT | TEST_MODE);
40     napi_value result;
41     napi_create_int32(env, shmid, &result);
42     return result;
43 }
44 
Shmdt(napi_env env, napi_callback_info info)45 static napi_value Shmdt(napi_env env, napi_callback_info info)
46 {
47     static const char path[] = ".";
48     static const int id = 'h';
49     key_t k;
50     int shmid;
51     void *p;
52     k = ftok(path, id);
53     shmid = shmget(k, PARAM_0, PARAM_0);
54     napi_value result = nullptr;
55     p = shmat(shmid, PARAM_0, SHM_RDONLY);
56     int shmval = shmdt(p);
57     napi_create_int32(env, shmval, &result);
58     return result;
59 }
Shmctl(napi_env env, napi_callback_info info)60 static napi_value Shmctl(napi_env env, napi_callback_info info)
61 {
62     static const char path[] = ".";
63     static const int id = 'h';
64     int shmid = shmget(ftok(path, id), VALUE100, IPC_CREAT | TEST_MODE);
65     int shmval = shmctl(shmid, IPC_RMID, PARAM_0);
66     napi_value result = nullptr;
67     napi_create_int32(env, shmval, &result);
68     return result;
69 }
Shmat(napi_env env, napi_callback_info info)70 static napi_value Shmat(napi_env env, napi_callback_info info)
71 {
72     static const char path[] = ".";
73     static const int id = 'h';
74     key_t k;
75     int shmid;
76     void *p;
77     k = ftok(path, id);
78     shmid = shmget(k, PARAM_0, PARAM_0);
79     napi_value result = nullptr;
80     if ((p = shmat(shmid, PARAM_0, SHM_RDONLY)) == PARAM_0) {
81         napi_create_int32(env, MINUSONE, &result);
82     } else {
83         napi_create_int32(env, PARAM_0, &result);
84     }
85     return result;
86 }
87 EXTERN_C_START
Init(napi_env env, napi_value exports)88 static napi_value Init(napi_env env, napi_value exports)
89 {
90     napi_property_descriptor desc[] = {
91         {"shmat", nullptr, Shmat, nullptr, nullptr, nullptr, napi_default, nullptr},
92         {"shmget", nullptr, Shmget, nullptr, nullptr, nullptr, napi_default, nullptr},
93         {"shmdt", nullptr, Shmdt, nullptr, nullptr, nullptr, napi_default, nullptr},
94         {"shmctl", nullptr, Shmctl, nullptr, nullptr, nullptr, napi_default, nullptr},
95 
96     };
97     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
98     return exports;
99 }
100 
101 EXTERN_C_END
102 
103 static napi_module demoModule = {
104     .nm_version = 1,
105     .nm_flags = 0,
106     .nm_filename = nullptr,
107     .nm_register_func = Init,
108     .nm_modname = "shm",
109     .nm_priv = ((void *)0),
110     .reserved = {0},
111 };
112 
RegisterModule(void)113 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
114