src/solaris/classes/java/lang/ProcessImpl.java

Print this page




  48         if (s == null)
  49             return null;
  50         byte[] bytes = s.getBytes();
  51         byte[] result = new byte[bytes.length + 1];
  52         System.arraycopy(bytes, 0,
  53                          result, 0,
  54                          bytes.length);
  55         result[result.length-1] = (byte)0;
  56         return result;
  57     }
  58 
  59     // Only for use by ProcessBuilder.start()
  60     static Process start(String[] cmdarray,
  61                          java.util.Map<String,String> environment,
  62                          String dir,
  63                          ProcessBuilder.Redirect[] redirects,
  64                          boolean redirectErrorStream)
  65         throws IOException
  66     {
  67         assert cmdarray != null && cmdarray.length > 0;

  68 

  69         // Convert arguments to a contiguous block; it's easier to do
  70         // memory management in Java than in C.
  71         byte[][] args = new byte[cmdarray.length-1][];
  72         int size = args.length; // For added NUL bytes
  73         for (int i = 0; i < args.length; i++) {
  74             args[i] = cmdarray[i+1].getBytes();
  75             size += args[i].length;
  76         }
  77         byte[] argBlock = new byte[size];
  78         int i = 0;
  79         for (byte[] arg : args) {
  80             System.arraycopy(arg, 0, argBlock, i, arg.length);
  81             i += arg.length + 1;
  82             // No need to write NUL bytes explicitly
  83         }
  84 
  85         int[] envc = new int[1];
  86         byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);
  87 
  88         int[] std_fds;


 110                     std_fds[1] = -1;
 111                 else if (redirects[1] == Redirect.INHERIT)
 112                     std_fds[1] = 1;
 113                 else {
 114                     f1 = new FileOutputStream(redirects[1].file(),
 115                                               redirects[1].append());
 116                     std_fds[1] = fdAccess.get(f1.getFD());
 117                 }
 118 
 119                 if (redirects[2] == Redirect.PIPE)
 120                     std_fds[2] = -1;
 121                 else if (redirects[2] == Redirect.INHERIT)
 122                     std_fds[2] = 2;
 123                 else {
 124                     f2 = new FileOutputStream(redirects[2].file(),
 125                                               redirects[2].append());
 126                     std_fds[2] = fdAccess.get(f2.getFD());
 127                 }
 128             }
 129 




 130         return new UNIXProcess
 131             (toCString(cmdarray[0]),
 132              argBlock, args.length,
 133              envBlock, envc[0],
 134              toCString(dir),
 135                  std_fds,
 136              redirectErrorStream);
 137         } finally {
 138             // In theory, close() can throw IOException
 139             // (although it is rather unlikely to happen here)
 140             try { if (f0 != null) f0.close(); }
 141             finally {
 142                 try { if (f1 != null) f1.close(); }
 143                 finally { if (f2 != null) f2.close(); }
 144             }
 145         }





 146     }

 147 }


  48         if (s == null)
  49             return null;
  50         byte[] bytes = s.getBytes();
  51         byte[] result = new byte[bytes.length + 1];
  52         System.arraycopy(bytes, 0,
  53                          result, 0,
  54                          bytes.length);
  55         result[result.length-1] = (byte)0;
  56         return result;
  57     }
  58 
  59     // Only for use by ProcessBuilder.start()
  60     static Process start(String[] cmdarray,
  61                          java.util.Map<String,String> environment,
  62                          String dir,
  63                          ProcessBuilder.Redirect[] redirects,
  64                          boolean redirectErrorStream)
  65         throws IOException
  66     {
  67         assert cmdarray != null && cmdarray.length > 0;
  68         String prog = cmdarray[0];
  69 
  70         try {
  71             // Convert arguments to a contiguous block; it's easier to do
  72             // memory management in Java than in C.
  73             byte[][] args = new byte[cmdarray.length-1][];
  74             int size = args.length; // For added NUL bytes
  75             for (int i = 0; i < args.length; i++) {
  76                 args[i] = cmdarray[i+1].getBytes();
  77                 size += args[i].length;
  78             }
  79             byte[] argBlock = new byte[size];
  80             int i = 0;
  81             for (byte[] arg : args) {
  82                 System.arraycopy(arg, 0, argBlock, i, arg.length);
  83                 i += arg.length + 1;
  84                 // No need to write NUL bytes explicitly
  85             }
  86 
  87             int[] envc = new int[1];
  88             byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);
  89 
  90             int[] std_fds;


 112                         std_fds[1] = -1;
 113                     else if (redirects[1] == Redirect.INHERIT)
 114                         std_fds[1] = 1;
 115                     else {
 116                         f1 = new FileOutputStream(redirects[1].file(),
 117                                               redirects[1].append());
 118                         std_fds[1] = fdAccess.get(f1.getFD());
 119                     }
 120 
 121                     if (redirects[2] == Redirect.PIPE)
 122                         std_fds[2] = -1;
 123                     else if (redirects[2] == Redirect.INHERIT)
 124                         std_fds[2] = 2;
 125                     else {
 126                         f2 = new FileOutputStream(redirects[2].file(),
 127                                                   redirects[2].append());
 128                         std_fds[2] = fdAccess.get(f2.getFD());
 129                     }
 130                 }
 131 
 132             SecurityManager security = System.getSecurityManager();
 133             if (security != null) 
 134                 security.checkExec(prog);
 135 
 136             return new UNIXProcess
 137                 (toCString(prog),
 138                  argBlock, args.length,
 139                  envBlock, envc[0],
 140                  toCString(dir),
 141                      std_fds,
 142                  redirectErrorStream);
 143             } finally {
 144                 // In theory, close() can throw IOException
 145                 // (although it is rather unlikely to happen here)
 146                 try { if (f0 != null) f0.close(); }
 147                 finally {
 148                     try { if (f1 != null) f1.close(); }
 149                     finally { if (f2 != null) f2.close(); }
 150                 }
 151             }
 152         } catch (IOException e) {
 153             throw new IOException (
 154                 "Cannot run program \"" + prog + "\""
 155                 + (dir == null ? "" : " (in directory \"" + dir + "\")")
 156                 + ": " + e.getMessage(), e);
 157         }
 158     }
 159 }