Exemplo n.º 1
0
  public List<String> buildARGV(MicroC posix) {
    List<String> argv;
    String os = System.getProperty("os.name");
    if (this.programArgs != null) {
      // if we had args passed to us, don't mess around, just use them
      argv = new JvmBasedArgvFinder(this.programArgs).getArgv();
    } else if ("Linux".equals(os)) {
      argv = new LinuxArgvFinder(posix.getpid()).getArgv();
    }
    //        else if ("Mac OS X".equals(os)) {
    //            argv = new MacARGVFinder().getArgv();
    //            if (!argv.get(0).endsWith("java")) {
    //                this works sometimes, not others, needs debugging. For now this heuristic
    // seems to work
    //                argv = new JvmBasedArgvFinder(this.programArgs).getArgv();
    //            }
    //        }
    else {
      argv = new JvmBasedArgvFinder(this.programArgs).getArgv();
    }

    if (this.extraVmArgs.size() > 0) {
      List<String> new_argv = new ArrayList<String>(argv.size() + extraVmArgs.size());
      new_argv.add(argv.get(0));
      new_argv.addAll(extraVmArgs);
      new_argv.addAll(argv.subList(1, argv.size()));
      argv = new_argv;
    }

    if (this.extraProgramArgs.size() > 0) {
      argv.addAll(extraProgramArgs);
    }

    return argv;
  }
Exemplo n.º 2
0
  public Status forkish() throws IOException {
    if (isDaemon()) {
      posix.setsid();

      OutputStream old_out = System.out;
      OutputStream old_err = System.err;

      System.setOut(new PrintStream(new FileOutputStream(out, true)));
      System.setErr(new PrintStream(new FileOutputStream(err, true)));
      old_err.close();
      old_out.close();

      if (pidfile != null) {
        FileOutputStream p_out = new FileOutputStream(pidfile);
        p_out.write(String.valueOf(posix.getpid()).getBytes());
        p_out.close();
      }

      return Status.child(posix.getpid());
    } else {
      String[] envp = getEnv(Daemon.class.getName() + "=daemon");
      List<String> argv = buildARGV(posix);

      jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime();
      Pointer NULL = Pointer.wrap(runtime, 0L);
      IntByReference child_pid = new IntByReference();

      int rs =
          posix.posix_spawnp(
              child_pid, argv.get(0), NULL, NULL, argv.toArray(new String[argv.size()]), envp);
      if (rs != 0) {
        throw new RuntimeException(posix.strerror(rs));
      }
      return Status.parent(child_pid.getValue());
    }
  }