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