File: unix-java.c

package info (click to toggle)
libmatthew-java 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 432 kB
  • sloc: java: 2,076; ansic: 361; makefile: 141
file content (464 lines) | stat: -rw-r--r-- 13,099 bytes parent folder | download | duplicates (2)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Java Unix Sockets Library
 *
 * Copyright (c) Matthew Johnson 2005
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * To Contact the author, please email src@matthew.ath.cx
 *
 */


/* _GNU_SOURCE is required to use struct ucred in glibc 2.8 */
#define _GNU_SOURCE

#include "unix-java.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>

#ifndef IOV_MAX
#define IOV_MAX 1024
#endif


#ifdef __cplusplus
extern "C" {
#endif

void throw(JNIEnv* env, int err, const char* msg)
{
   jstring jmsg = (*env)->NewStringUTF(env, msg);
   jclass exc = (*env)->FindClass(env, "cx/ath/matthew/unix/UnixIOException");
   jmethodID cons = (*env)->GetMethodID(env, exc, "<init>", "(ILjava/lang/String;)V");
   jobject exo = (*env)->NewObject(env, exc, cons, err, jmsg);
   (*env)->DeleteLocalRef(env, exc);
   (*env)->DeleteLocalRef(env, jmsg);
   (*env)->Throw(env, exo);
   (*env)->DeleteLocalRef(env, exo);
}

void handleerrno(JNIEnv *env)
{
   if (0 == errno) return;
   int err = errno;
   if (EAGAIN == err) return; // we read 0 bytes due to a timeout
   const char* msg = strerror(err);
   throw(env, err, msg);
}
   
/*
 * Class:     cx_ath_matthew_unix_UnixServerSocket
 * Method:    native_bind
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixServerSocket_native_1bind
  (JNIEnv *env, jobject o, jstring address, jboolean abstract)
{
   int sock = socket(PF_UNIX, SOCK_STREAM, 0);
   if (-1 == sock) { handleerrno(env); return -1; }
   const char* caddr = (*env)->GetStringUTFChars(env, address, 0);
   int slen = (*env)->GetStringUTFLength(env, address)+1;
   struct sockaddr_un *sad = malloc(sizeof(sa_family_t)+slen);
   if (abstract)  {
      char* shifted = sad->sun_path+1;
      strncpy(shifted, caddr, slen-1);
      sad->sun_path[0] = 0;
   } else
      strncpy(sad->sun_path, caddr, slen);
   (*env)->ReleaseStringUTFChars(env, address, caddr);
   sad->sun_family = AF_UNIX;
   int rv = bind(sock, (const  struct  sockaddr*) sad, sizeof(sa_family_t)+slen);
   free(sad);
   if (-1 == rv) { handleerrno(env); return -1; }
   rv = listen(sock, 10);
   if (-1 == rv) { handleerrno(env); return -1; }
   return sock;
}

/*
 * Class:     cx_ath_matthew_unix_UnixServerSocket
 * Method:    native_close
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_cx_ath_matthew_unix_UnixServerSocket_native_1close
  (JNIEnv * env, jobject o, jint sock)
{
   if (0 == sock) return;
   int rv = shutdown(sock, SHUT_RDWR);
   if (-1 == rv) { handleerrno(env); }
   else {
      rv = close(sock);
      if (-1 == rv) { handleerrno(env); }
   }
}

/*
 * Class:     cx_ath_matthew_unix_UnixServerSocket
 * Method:    native_accept
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixServerSocket_native_1accept
  (JNIEnv * env, jobject o, jint sock)
{
   int newsock = accept(sock, NULL, NULL);
   if (-1 == newsock) handleerrno(env);
   return newsock;
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_set_pass_cred
 * Signature: (IZ)V
 */
JNIEXPORT void JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1set_1pass_1cred
  (JNIEnv *env, jobject o, jint sock, jboolean enable)
{
#ifdef SO_PASSCRED
   int opt = enable;
   int rv = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(int));
   if (-1 == rv) { handleerrno(env);}
#endif
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_connect
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1connect
  (JNIEnv *env, jobject o, jstring address, jboolean abstract)
{
   int sock = socket(PF_UNIX, SOCK_STREAM, 0);
   if (-1 == sock) { handleerrno(env); return -1; }
   const char* caddr = (*env)->GetStringUTFChars(env, address, 0);
   int slen = (*env)->GetStringUTFLength(env, address)+1;
   struct sockaddr_un *sad = malloc(sizeof(sa_family_t)+slen);
   if (abstract)  {
      char* shifted = sad->sun_path+1;
      strncpy(shifted, caddr, slen-1);
      sad->sun_path[0] = 0;
   } else
      strncpy(sad->sun_path, caddr, slen);
   (*env)->ReleaseStringUTFChars(env, address, caddr);
   sad->sun_family = AF_UNIX;
   int rv = connect(sock, (const struct sockaddr*) sad, sizeof(sa_family_t)+slen);
   free(sad);
   if (-1 == rv) { handleerrno(env); return -1; }
   return sock;
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_close
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1close
  (JNIEnv *env, jobject o, jint sock)
{
   if (0 == sock) return;
   int rv = shutdown(sock, SHUT_RDWR);
   if (-1 == rv) { handleerrno(env); }
   else {
      rv = close(sock);
      if (-1 == rv) { handleerrno(env); }
   }
}

/*
 * Class:     cx_ath_matthew_unix_USInputStream
 * Method:    native_recv
 * Signature: ([BII)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_USInputStream_native_1recv
  (JNIEnv *env, jobject o, jint sock, jbyteArray buf, jint offs, jint len, jint flags, jint timeout)
{
   fd_set rfds;
   struct timeval tv;
   jbyte* cbuf = (*env)->GetByteArrayElements(env, buf, NULL);
   void* recvb = cbuf + offs;
   int rv;

   if (timeout > 0) {
      FD_ZERO(&rfds);
      FD_SET(sock, &rfds);
      tv.tv_sec = 0;
      tv.tv_usec = timeout;
      rv = select(sock+1, &rfds, NULL, NULL, &tv);
      rv = recv(sock, recvb, len, flags);
      if (-1 == rv) { handleerrno(env); rv = -1; }
      (*env)->ReleaseByteArrayElements(env, buf, cbuf, 0);
      return rv;
   } else  {
      rv = recv(sock, recvb, len, flags);
      (*env)->ReleaseByteArrayElements(env, buf, cbuf, 0);
      if (-1 == rv) { handleerrno(env); return -1; }
      return rv;
   }
}

/*
 * Class:     cx_ath_matthew_unix_USOutputStream
 * Method:    native_send
 * Signature: (I[BII)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_USOutputStream_native_1send__I_3BII
  (JNIEnv *env, jobject o, jint sock, jbyteArray buf, jint offs, jint len)
{
   jbyte* cbuf = (*env)->GetByteArrayElements(env, buf, NULL);
   void* sendb = cbuf + offs;
   int rv = send(sock, sendb, len, 0);
   (*env)->ReleaseByteArrayElements(env, buf, cbuf, 0);
   if (-1 == rv) { handleerrno(env); return -1; }
   return rv;
}

/*
 * Class:     cx_ath_matthew_unix_USOutputStream
 * Method:    native_send
 * Signature: (I[[B)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_USOutputStream_native_1send__I_3_3B
  (JNIEnv *env, jobject o, jint sock, jobjectArray bufs)
{
   size_t sblen = 1;
   socklen_t sblen_size = sizeof(sblen);
   getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sblen, &sblen_size);

   struct msghdr msg;
   struct iovec *iov;
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   msg.msg_control = NULL;
   msg.msg_controllen = 0;
   msg.msg_flags = 0;
   size_t els = (*env)->GetArrayLength(env, bufs);
   iov = (struct iovec*) malloc((els<IOV_MAX?els:IOV_MAX) * sizeof(struct iovec));
   msg.msg_iov = iov;
   jbyteArray *b = (jbyteArray*) malloc(els * sizeof(jbyteArray));
   int rv = 0;
   
   for (int i = 0, j = 0, s = 0; i <= els; i++, j++) {
      if (i == els) {
         msg.msg_iovlen = j;
         rv = sendmsg(sock, &msg, 0);
         for (int k = i-1, l = j-1; l >= 0; k--, l--)
            (*env)->ReleaseByteArrayElements(env, b[k], iov[l].iov_base, 0);
         if (-1 == rv) { handleerrno(env); return -1; }
         break;
      }
      b[i] = (*env)->GetObjectArrayElement(env, bufs, i);
      if (NULL == b[i]) {
         msg.msg_iovlen = j;
         rv = sendmsg(sock, &msg, 0);
         for (int k = i-1, l = j-1; l >= 0; k--, l--)
            (*env)->ReleaseByteArrayElements(env, b[k], iov[l].iov_base, 0);
         if (-1 == rv) { handleerrno(env); return -1; }
         break;
      }
      size_t l = (*env)->GetArrayLength(env, b[i]);
      if (s+l > sblen || j == IOV_MAX) {
         msg.msg_iovlen = j;
         rv = sendmsg(sock, &msg, 0);
         s = 0;
         for (int k = i-1, l = j-1; l >= 0; k--, l--)
            (*env)->ReleaseByteArrayElements(env, b[k], iov[l].iov_base, 0);
         j = 0;
         if (-1 == rv) { handleerrno(env); return -1; }
      }
      iov[j].iov_base = (*env)->GetByteArrayElements(env, b[i], NULL);
      iov[j].iov_len = l;
      s += l;
   }
   
   free(iov);
   free(b);
   return rv;
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_getPID
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1getPID
  (JNIEnv * env, jobject o, jint sock)
{
#ifdef SO_PEERCRED
   struct ucred cr;
   socklen_t cl=sizeof(cr);

   if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cr, &cl)==0) 
      return cr.pid;
   else
      return -1;
#else
   return -1;
#endif
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_getUID
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1getUID
  (JNIEnv * env, jobject o, jint sock)
{
#ifdef SO_PEERCRED
   struct ucred cr;
   socklen_t cl=sizeof(cr);

   if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cr, &cl)==0) 
      return cr.uid;
   else
      return -1;
#else
   return -1;
#endif
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_getGID
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1getGID
  (JNIEnv * env, jobject o, jint sock)
{
#ifdef SO_PEERCRED
   struct ucred cr;
   socklen_t cl=sizeof(cr);

   if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &cr, &cl)==0) 
      return cr.gid;
   else
      return -1;
#else
   return -1;
#endif
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_send_creds
 * Signature: (B)V
 */
JNIEXPORT void JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1send_1creds
  (JNIEnv * env, jobject o, jint sock, jbyte data)
{
   struct msghdr msg;
   struct iovec iov;
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   msg.msg_flags = 0;
   msg.msg_iov = &iov;
   msg.msg_iovlen = 1;
   msg.msg_control = NULL;
   msg.msg_controllen = 0;
   iov.iov_base = &data;
   iov.iov_len = 1;

#ifdef SCM_CREDENTIALS
   char buf[CMSG_SPACE(sizeof(struct ucred))];
   msg.msg_control = buf;
   msg.msg_controllen = sizeof buf;
   struct cmsghdr *cmsg;
   struct ucred *creds;

   cmsg = CMSG_FIRSTHDR(&msg);
   cmsg->cmsg_level = SOL_SOCKET;
   cmsg->cmsg_type = SCM_CREDENTIALS;
   cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
   /* Initialize the payload: */
   creds = (struct ucred *)CMSG_DATA(cmsg);
   creds->pid = getpid();
   creds->uid = getuid();
   creds->gid = getgid();
#endif

   int rv = sendmsg(sock, &msg, 0);
   if (-1 == rv) { handleerrno(env); }
}

/*
 * Class:     cx_ath_matthew_unix_UnixSocket
 * Method:    native_recv_creds
 * Signature: ([I)B
 */
JNIEXPORT jbyte JNICALL Java_cx_ath_matthew_unix_UnixSocket_native_1recv_1creds
  (JNIEnv *env, jobject o, jint sock, jintArray jcreds)
{
   struct msghdr msg;
   char iov_buf = 0;
   struct iovec iov;
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   msg.msg_flags = 0;
   msg.msg_iov = &iov;
   msg.msg_iovlen = 1;
   msg.msg_control = NULL;
   msg.msg_controllen = 0;
   iov.iov_base = &iov_buf;
   iov.iov_len = 1;

#ifdef SCM_CREDENTIALS
   char buf[CMSG_SPACE(sizeof(struct ucred))];
   msg.msg_control = buf;
   msg.msg_controllen = sizeof buf;
   struct cmsghdr *cmsg;
   struct ucred *creds = NULL;
#endif

   recvmsg(sock, &msg, 0);

#ifdef SCM_CREDENTIALS
   for (cmsg = CMSG_FIRSTHDR(&msg);
         cmsg != NULL;
         cmsg = CMSG_NXTHDR(&msg,cmsg)) {
      if (cmsg->cmsg_level == SOL_SOCKET
            && cmsg->cmsg_type == SCM_CREDENTIALS) {
         creds = (struct ucred *) CMSG_DATA(cmsg);        
         break;
      }
   }
   if (NULL != creds) {
      jint cred_array[3];
      cred_array[0] = creds->pid;
      cred_array[1] = creds->uid;
      cred_array[2] = creds->gid;
      (*env)->SetIntArrayRegion(env, jcreds, 0, 3, &cred_array[0]);
   }
#endif

   return iov_buf;
}


#ifdef __cplusplus
}
#endif