-
Notifications
You must be signed in to change notification settings - Fork 3
/
jtux_posixipc.c
executable file
·415 lines (364 loc) · 12.1 KB
/
jtux_posixipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Copyright 2003 by Marc J. Rochkind. All rights reserved.
May be copied only for purposes and under conditions described
on the Web page www.basepath.com/aup/copyright.htm.
The Example Files are provided "as is," without any warranty;
without even the implied warranty of merchantability or fitness
for a particular purpose. The author and his publisher are not
responsible for any damages, direct or incidental, resulting
from the use or non-use of these Example Files.
The Example Files may contain defects, and some contain deliberate
coding mistakes that were included for educational reasons.
You are responsible for determining if and how the Example Files
are to be used.
*/
#include "defs.h"
#include <mqueue.h>
#include <semaphore.h>
#include <sys/mman.h>
#include "JtuxPosixIPC.h" // generated by javah
#include "jtux_util.h"
#include "JNI_macros.h"
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_mmap(JNIEnv *env, jclass obj,
jlong addr, jint len, jint prot, jint flags, jint fd, jlong off)
{
void *p;
if ((p = mmap((void *)(intptr_t)addr, len, prot, flags, fd, off)) == MAP_FAILED)
JTHROW_null(NULL)
return (intptr_t)p;
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1close(JNIEnv *env, jclass obj,
jlong mqd)
{
JTHROW_neg1(mq_close((mqd_t)(intptr_t)mqd))
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1getattr(JNIEnv *env, jclass obj,
jlong mqd, jobject attr)
{
jclass cls = (*env)->FindClass(env, "jtux/UPosixIPC$s_mq_attr");
struct mq_attr attr_c;
int r;
JTHROW_neg1(r = mq_getattr((mqd_t)(intptr_t)mqd, &attr_c))
if (r != -1) {
if (!field_ctoj_long(env, cls, "mq_flags", attr, attr_c.mq_flags))
return;
if (!field_ctoj_long(env, cls, "mq_maxmsg", attr, attr_c.mq_maxmsg))
return;
if (!field_ctoj_long(env, cls, "mq_msgsize", attr, attr_c.mq_msgsize))
return;
if (!field_ctoj_long(env, cls, "mq_curmsgs", attr, attr_c.mq_curmsgs))
return;
}
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1notify(JNIEnv *env, jclass obj,
jlong mqd, jobject ep)
{
jclass cls = (*env)->FindClass(env, "jtux/UProcess$s_sigevent");
jclass cls_u_sigval_int = (*env)->FindClass(env, "jtux/UProcess$u_sigval_int");
jclass cls_u_sigval_ptr = (*env)->FindClass(env, "jtux/UProcess$u_sigval_ptr");
jfieldID fid;
jobject sigev_value;
struct sigevent sigevent_buf, *ep_c = &sigevent_buf;
long sival_ptr;
if (ep == NULL)
ep_c = NULL;
else {
if (!field_jtoc_int(env, cls, "sigev_notify", ep, &ep_c->sigev_notify))
return;
if (!field_jtoc_int(env, cls, "sigev_signo", ep, &ep_c->sigev_signo))
return;
if ((fid = (*env)->GetFieldID(env, cls, "sigev_value", "Ljtux/UProcess$u_sigval;")) == NULL)
return;
if ((sigev_value = (*env)->GetObjectField(env, ep, fid)) == NULL) {
JNU_ThrowByName(env, "NullPointerException", "sigev_value field not initialized");
return;
}
if ((*env)->IsInstanceOf(env, sigev_value, cls_u_sigval_int)) {
if (!field_jtoc_int(env, cls_u_sigval_int, "sival_int", sigev_value,
&ep_c->sigev_value.sival_int))
return;
}
else if ((*env)->IsInstanceOf(env, sigev_value, cls_u_sigval_ptr)) {
if (!field_jtoc_long(env, cls_u_sigval_ptr, "sival_ptr", sigev_value,
&sival_ptr))
return;
ep_c->sigev_value.sival_ptr = (void *)(intptr_t)sival_ptr;
}
else {
(void)setup_throw_errno(env, EINVAL);
return;
}
}
JTHROW_neg1(mq_notify((mqd_t)(intptr_t)mqd, ep_c))
}
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_mq_1open__Ljava_lang_String_2I(JNIEnv *env, jclass obj,
jstring name, jint flags)
{
JSTR_GET_DECL(name_c, name)
mqd_t mqd;
JSTR_NULLTEST_V(name_c, -1)
JTHROW_neg1(mqd = mq_open(name_c, flags))
JSTR_REL(name_c, name)
return (intptr_t)mqd;
}
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_mq_1open__Ljava_lang_String_2IILjtux_UPosixIPC_00024s_1mq_1attr_2(JNIEnv *env, jclass obj,
jstring name, jint flags, jint perms, jobject attr)
{
JSTR_GET_DECL(name_c, name)
mqd_t mqd;
struct mq_attr attr_c_buf, *attr_c = &attr_c_buf;
jclass cls = (*env)->FindClass(env, "jtux/UPosixIPC$s_mq_attr");
JSTR_NULLTEST_V(name_c, -1)
if (attr == NULL)
attr_c = NULL;
else {
if (!field_jtoc_long(env, cls, "mq_flags", attr, &attr_c->mq_flags))
return -1;
if (!field_jtoc_long(env, cls, "mq_maxmsg", attr, &attr_c->mq_maxmsg))
return -1;
if (!field_jtoc_long(env, cls, "mq_msgsize", attr, &attr_c->mq_msgsize))
return -1;
if (!field_jtoc_long(env, cls, "mq_curmsgs", attr, &attr_c->mq_curmsgs))
return -1;
}
JTHROW_neg1(mqd = mq_open(name_c, flags, perms, attr_c))
JSTR_REL(name_c, name)
return (intptr_t)mqd;
}
JNIEXPORT jint JNICALL Java_jtux_UPosixIPC_mq_1receive(JNIEnv *env, jclass obj,
jlong mqd, jbyteArray msg, jint msgsize, jobject priority)
{
ssize_t n;
void *msgp_c;
int priority_c;
jclass cls = (*env)->FindClass(env, "jtux/UUtil$IntHolder");
if ((msgp_c = (*env)->GetByteArrayElements(env, msg, NULL)) == NULL)
return -1;
JTHROW_neg1(n = mq_receive((mqd_t)(intptr_t)mqd, msgp_c, msgsize, &priority_c))
(*env)->ReleaseByteArrayElements(env, msg, msgp_c, 0);
if (priority != NULL && n != -1) {
if (!field_ctoj_int(env, cls, "value", priority, priority_c))
return -1;
}
return n;
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1send(JNIEnv *env, jclass obj,
jlong mqd, jbyteArray msg, jint msgsize, jint priority)
{
void *msgp_c;
if ((msgp_c = (*env)->GetByteArrayElements(env, msg, NULL)) == NULL)
return;
JTHROW_neg1(mq_send((mqd_t)(intptr_t)mqd, msgp_c, msgsize, priority))
(*env)->ReleaseByteArrayElements(env, msg, msgp_c, JNI_ABORT);
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1setattr(JNIEnv *env, jclass obj,
jlong mqd, jobject attr, jobject oattr)
{
jclass cls = (*env)->FindClass(env, "jtux/UPosixIPC$s_mq_attr");
struct mq_attr attr_c, oattr_buf, *oattr_c = &oattr_buf;
int r;
if (oattr == NULL)
oattr_c = NULL;
if (!field_jtoc_long(env, cls, "mq_flags", attr, &attr_c.mq_flags))
return;
if (!field_jtoc_long(env, cls, "mq_maxmsg", attr, &attr_c.mq_maxmsg))
return;
if (!field_jtoc_long(env, cls, "mq_msgsize", attr, &attr_c.mq_msgsize))
return;
if (!field_jtoc_long(env, cls, "mq_curmsgs", attr, &attr_c.mq_curmsgs))
return;
JTHROW_neg1(r = mq_setattr((mqd_t)(intptr_t)mqd, &attr_c, oattr_c))
if (oattr != NULL && r != -1) {
if (!field_ctoj_long(env, cls, "mq_flags", oattr, oattr_c->mq_flags))
return;
if (!field_ctoj_long(env, cls, "mq_maxmsg", oattr, oattr_c->mq_maxmsg))
return;
if (!field_ctoj_long(env, cls, "mq_msgsize", oattr, oattr_c->mq_msgsize))
return;
if (!field_ctoj_long(env, cls, "mq_curmsgs", oattr, oattr_c->mq_curmsgs))
return;
}
}
JNIEXPORT jint JNICALL Java_jtux_UPosixIPC_mq_1timedreceive(JNIEnv *env, jclass obj,
jlong mqd, jbyteArray msg, jint msgsize, jobject priority, jobject tmout)
{
#if _XOPEN_VERSION >= 600
// Following compiled but not tested
ssize_t n;
void *msgp_c;
int priority_c;
jclass cls_IntHolder = (*env)->FindClass(env, "jtux/UUtil$IntHolder");
jclass cls_timespec = (*env)->FindClass(env, "jtux/UProcess$s_timespec");
struct timespec tmout_c;
long sec;
if ((msgp_c = (*env)->GetByteArrayElements(env, msg, NULL)) == NULL)
return -1;
if (!field_jtoc_long(env, cls_timespec, "tv_sec", tmout, &sec))
return -1;
tmout_c.tv_sec = (time_t)sec;
if (!field_jtoc_long(env, cls_timespec, "tv_nsec", tmout, &tmout_c.tv_nsec))
return -1;
JTHROW_neg1(n = mq_timedreceive((mqd_t)(intptr_t)mqd, msgp_c, msgsize, &priority_c,
&tmout_c))
(*env)->ReleaseByteArrayElements(env, msg, msgp_c, 0);
if (priority != NULL && n != -1) {
if (!field_ctoj_int(env, cls_IntHolder, "value", priority, priority_c))
return -1;
}
return n;
#else
(void)setup_throw_errno(env, ENOSYS);
return -1;
#endif
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1timedsend(JNIEnv *env, jclass obj,
jlong mqd, jbyteArray msg, jint msgsize, jint priority, jobject tmout)
{
#if _XOPEN_VERSION >= 600
// Following compiled but not tested
void *msgp_c;
jclass cls_timespec = (*env)->FindClass(env, "jtux/UProcess$s_timespec");
struct timespec tmout_c;
long sec;
if ((msgp_c = (*env)->GetByteArrayElements(env, msg, NULL)) == NULL)
return;
if (!field_jtoc_long(env, cls_timespec, "tv_sec", tmout, &sec))
return;
tmout_c.tv_sec = (time_t)sec;
if (!field_jtoc_long(env, cls_timespec, "tv_nsec", tmout, &tmout_c.tv_nsec))
return;
JTHROW_neg1(mq_timedsend((mqd_t)(intptr_t)mqd, msgp_c, msgsize, priority,
&tmout_c))
(*env)->ReleaseByteArrayElements(env, msg, msgp_c, JNI_ABORT);
#else
(void)setup_throw_errno(env, ENOSYS);
#endif
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_mq_1unlink(JNIEnv *env, jclass obj,
jstring name)
{
JSTR_GET_DECL(name_c, name)
JSTR_NULLTEST(name_c)
JTHROW_neg1(mq_unlink(name_c))
JSTR_REL(name_c, name)
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_munmap(JNIEnv *env, jclass obj,
jlong addr, jint len)
{
JTHROW_neg1(munmap((void *)(intptr_t)addr, len))
}
/* Not needed... yet!
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_sem_1alloc(JNIEnv *env, jclass obj)
{
sem_t *sem;
JTHROW_null(sem = malloc(sizeof(sem_t)))
return (intptr_t)sem;
}
*/
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1close(JNIEnv *env, jclass obj,
jlong sem)
{
JTHROW_neg1(sem_close((sem_t *)(intptr_t)sem))
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1destroy(JNIEnv *env, jclass obj,
jlong sem)
{
JTHROW_neg1(sem_destroy((sem_t *)(intptr_t)sem))
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1getvalue(JNIEnv *env, jclass obj,
jlong sem, jobject valuep)
{
int value_c, r;
jclass cls = (*env)->FindClass(env, "jtux/UUtil$IntHolder");
JTHROW_neg1(r = sem_getvalue((sem_t *)(intptr_t)sem, &value_c))
if (valuep != NULL && r != -1) {
if (!field_ctoj_int(env, cls, "value", valuep, value_c))
return;
}
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1init(JNIEnv *env, jclass obj,
jlong sem, jint pshared, jint value)
{
JTHROW_neg1(sem_init((sem_t *)(intptr_t)sem, pshared, value))
}
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_sem_1open__Ljava_lang_String_2I(JNIEnv *env, jclass obj,
jstring name, jint flags)
{
JSTR_GET_DECL(name_c, name)
sem_t *sem;
JSTR_NULLTEST_V(name_c, 0)
if ((sem = sem_open(name_c, flags)) == SEM_FAILED)
JTHROW_null(NULL)
JSTR_REL(name_c, name)
return (intptr_t)sem;
}
JNIEXPORT jlong JNICALL Java_jtux_UPosixIPC_sem_1open__Ljava_lang_String_2III(JNIEnv *env, jclass obj,
jstring name, jint flags, jint perms, jint value)
{
JSTR_GET_DECL(name_c, name)
sem_t *sem;
JSTR_NULLTEST_V(name_c, 0)
if ((sem = sem_open(name_c, flags, perms, value)) == SEM_FAILED)
JTHROW_null(NULL)
JSTR_REL(name_c, name)
return (intptr_t)sem;
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1post(JNIEnv *env, jclass obj,
jlong sem)
{
JTHROW_neg1(sem_post((sem_t *)(intptr_t)sem))
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1timedwait(JNIEnv *env, jclass obj,
jlong sem, jobject tmout)
{
#if _XOPEN_VERSION >= 600 || true
// Following compiled but not tested
jclass cls_timespec = (*env)->FindClass(env, "jtux/UProcess$s_timespec");
struct timespec tmout_c;
long sec;
if (!field_jtoc_long(env, cls_timespec, "tv_sec", tmout, &sec))
return;
tmout_c.tv_sec = (time_t)sec;
if (!field_jtoc_long(env, cls_timespec, "tv_nsec", tmout, &tmout_c.tv_nsec))
return;
JTHROW_neg1(sem_timedwait((sem_t *)(intptr_t)sem, &tmout_c))
#else
(void)setup_throw_errno(env, ENOSYS);
#endif
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1trywait(JNIEnv *env, jclass obj,
jlong sem)
{
JTHROW_neg1(sem_trywait((sem_t *)(intptr_t)sem))
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1unlink(JNIEnv *env, jclass obj,
jstring name)
{
JSTR_GET_DECL(name_c, name)
JSTR_NULLTEST(name_c)
JTHROW_neg1(sem_unlink(name_c))
JSTR_REL(name_c, name)
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_sem_1wait(JNIEnv *env, jclass obj,
jlong sem)
{
JTHROW_neg1(sem_wait((sem_t *)(intptr_t)sem))
}
JNIEXPORT jint JNICALL Java_jtux_UPosixIPC_shm_1open(JNIEnv *env, jclass obj,
jstring name, jint flags, jint perms)
{
JSTR_GET_DECL(name_c, name)
int fd;
JSTR_NULLTEST_V(name_c, -1)
JTHROW_neg1(fd = shm_open(name_c, flags, perms))
JSTR_REL(name_c, name)
return fd;
}
JNIEXPORT void JNICALL Java_jtux_UPosixIPC_shm_1unlink(JNIEnv *env, jclass obj,
jstring name)
{
JSTR_GET_DECL(name_c, name)
JSTR_NULLTEST(name_c)
JTHROW_neg1(shm_unlink(name_c))
JSTR_REL(name_c, name)
}