1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * A simplified Unix domain socket which can read and write bytes at a time
  26  * used for simulating external launchers which use UNIX sockets to talk
  27  * the VM.
  28  */
  29 
  30 import java.io.IOException;
  31 
  32 public class UnixDomainSocket {
  33 
  34     static {
  35         System.loadLibrary("InheritedChannel");
  36         init();
  37     }
  38 
  39     private final int fd;
  40     private volatile String name;
  41 
  42     public UnixDomainSocket() throws IOException {
  43         this.fd = create();
  44     }
  45 
  46     public void bind(String name) throws IOException {
  47         bind0(fd, name);
  48         this.name = name;
  49     }
  50 
  51     public UnixDomainSocket accept() throws IOException {
  52         int newsock = accept0(fd);
  53         return new UnixDomainSocket(newsock);
  54     }
  55 
  56     public UnixDomainSocket(int fd) {
  57         this.fd = fd;
  58     }
  59 
  60     public void connect(String dest) throws IOException {
  61         connect0(fd, dest);
  62     }
  63 
  64     public int read() throws IOException {
  65         return read0(fd);
  66     }
  67 
  68     public String name() {
  69         return name;
  70     }
  71 
  72     public void write(int w) throws IOException {
  73         write0(fd, w);
  74     }
  75 
  76     public void close() {
  77         close0(fd, name); // close0 will unlink name if non-null
  78     }
  79 
  80     public int fd() {
  81         return fd;
  82     }
  83 
  84     public String toString() {
  85         return "UnixDomainSocket: fd=" + Integer.toString(fd);
  86     }
  87 
  88     private static native int create() throws IOException;
  89     private static native void bind0(int fd, String name) throws IOException;
  90     private static native int accept0(int fd) throws IOException;
  91     private static native int connect0(int fd, String name) throws IOException;
  92 
  93     /* read and write bytes with UNIX domain sockets */
  94 
  95     private static native int read0(int fd) throws IOException;
  96     private static native void write0(int fd, int w) throws IOException;
  97     private static native void close0(int fd, String name);
  98     private static native void init();
  99     public static native UnixDomainSocket[] socketpair();
 100 }
 101