• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi/native_api.h"
17 #include <cstring>
18 #include <js_native_api_types.h>
19 #include <sys/msg.h>
20 
21 #define PARAM_1 1
22 #define NO_ERR 0
23 #define SUCCESS 1
24 #define FAIL (-1)
25 #define PARAM_0 0
26 #define TEN 10
27 #define PARAM_3 3
28 #define PARAM_5 5
29 #define STATERROR (-100)
30 #define OPENERROR (-99)
31 #define MMAPERROR (-98)
32 #define TEST_SIZE 4096
33 #define FLAG 0666
Msgget(napi_env env,napi_callback_info info)34 static napi_value Msgget(napi_env env, napi_callback_info info)
35 {
36     int msqid;
37     msqid = msgget(IPC_PRIVATE, FLAG);
38     int retVal = FAIL;
39     if (msqid < PARAM_0) {
40         retVal = FAIL;
41     } else {
42         retVal = SUCCESS;
43     }
44     napi_value result = nullptr;
45     napi_create_int32(env, retVal, &result);
46     return result;
47 }
48 
Msgsnd(napi_env env,napi_callback_info info)49 static napi_value Msgsnd(napi_env env, napi_callback_info info)
50 {
51     int msqid;
52     struct msgbuf buf;
53     int sendlength;
54     msqid = msgget(IPC_PRIVATE, FLAG);
55     buf.mtype = PARAM_1;
56     strcpy(buf.mtext, "AA");
57     sendlength = sizeof(struct msgbuf) - sizeof(long);
58     int ret = msgsnd(msqid, &buf, sendlength, PARAM_0);
59     int retVal = FAIL;
60     if (ret < PARAM_0) {
61         retVal = FAIL;
62     } else {
63         retVal = SUCCESS;
64     }
65     napi_value result = nullptr;
66     napi_create_int32(env, retVal, &result);
67     return result;
68 }
69 typedef struct {
70     long mtype;
71     char message[50];
72 } MSG_DATA;
73 
Msgrcv(napi_env env,napi_callback_info info)74 static napi_value Msgrcv(napi_env env, napi_callback_info info)
75 {
76     int msqid;
77     msqid = msgget(IPC_PRIVATE, FLAG);
78     MSG_DATA msginfo;
79     bzero(&msginfo, sizeof(msginfo));
80     int ret = msgrcv(msqid, &msginfo, sizeof(msginfo.message), PARAM_5, IPC_NOWAIT);
81     int retVal = FAIL;
82     if (ret == FAIL) {
83         retVal = FAIL;
84     } else {
85         retVal = SUCCESS;
86     }
87     napi_value result = nullptr;
88     napi_create_int32(env, retVal, &result);
89     return result;
90 }
91 
Msgctl(napi_env env,napi_callback_info info)92 static napi_value Msgctl(napi_env env, napi_callback_info info)
93 {
94     int msqid;
95     struct msqid_ds msgidInfo;
96     struct msgbuf buf;
97     int ret;
98     int sendlength;
99     msqid = msgget(IPC_PRIVATE, FLAG);
100 
101     buf.mtype = PARAM_1;
102     strcpy(buf.mtext, "AS");
103     sendlength = sizeof(struct msgbuf) - sizeof(long);
104     ret = msgsnd(msqid, &buf, sendlength, PARAM_0);
105     buf.mtype = PARAM_3;
106     strcpy(buf.mtext, "SS");
107     sendlength = sizeof(struct msgbuf) - sizeof(long);
108     ret = msgsnd(msqid, &buf, sendlength, PARAM_0);
109     ret = msgctl(msqid, IPC_STAT, &msgidInfo);
110     int retVal = FAIL;
111     if (ret < PARAM_0) {
112         retVal = FAIL;
113     } else {
114         retVal = SUCCESS;
115     }
116     napi_value result = nullptr;
117     napi_create_int32(env, retVal, &result);
118     return result;
119 }
120 
121 EXTERN_C_START
Init(napi_env env,napi_value exports)122 static napi_value Init(napi_env env, napi_value exports)
123 {
124     napi_property_descriptor desc[] = {
125         {"msgget", nullptr, Msgget, nullptr, nullptr, nullptr, napi_default, nullptr},
126         {"msgsnd", nullptr, Msgsnd, nullptr, nullptr, nullptr, napi_default, nullptr},
127         {"msgrcv", nullptr, Msgrcv, nullptr, nullptr, nullptr, napi_default, nullptr},
128         {"msgctl", nullptr, Msgctl, nullptr, nullptr, nullptr, napi_default, nullptr},
129 
130     };
131     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
132     return exports;
133 }
134 EXTERN_C_END
135 
136 static napi_module demoModule = {
137     .nm_version = 1,
138     .nm_flags = 0,
139     .nm_filename = nullptr,
140     .nm_register_func = Init,
141     .nm_modname = "msgndk",
142     .nm_priv = ((void *)0),
143     .reserved = {0},
144 };
145 
RegisterModule(void)146 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
147