Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (63 sloc) 1.864 kb
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
(ns leiningen.daemon.runtime
  (:import java.io.FileOutputStream
           (org.jruby.ext.posix POSIXFactory
                                POSIXHandler))
  (:require [clojure.java.io :refer (reader)]
            [clojure.java.shell :as sh]))

(defn throwf [& message]
  (throw (Exception. (apply format message))))

(def handler
  (proxy [POSIXHandler]
      []
    (error [error extra]
      (println "error:" error extra))
    (unimplementedError [methodname]
      (throwf "unimplemented method %s" methodname))
    (warn [warn-id message & data]
      (println "warning:" warn-id message data))
    (isVerbose []
      false)
    (getCurrentWorkingDirectory []
      (System/getProperty "user.dir"))
    (getEnv []
      (map str (System/getenv)))
    (getInputStream []
      System/in)
    (getOutputStream []
      System/out)
    (getErrorStream []
      System/err)
    (getPID []
      (rand-int 65536))))

(def C (POSIXFactory/getPOSIX handler true))

(defn closeDescriptors []
  (.close System/out)
  (.close System/err)
  (.close System/in))

(defn is-daemon? []
  (System/getProperty "leiningen.daemon"))

(defn chdirToRoot []
  (.chdir C "/")
  (System/setProperty "user.dir" "/"))

(defn get-current-pid []
  (.getpid C))

(defn write-pid-file
  "Write the pid of the current process to pid-path"
  [pid-path]
  (let [pid (str (get-current-pid))]
    (printf "writing pid %s to %s\n" pid pid-path)
    (spit pid-path pid)))

(defn abort
  "Abort, once we're in the user's project, so leiningen.core.main/abort isn't available"
  [message]
  (println message)
  (System/exit 1))

(defn init
  "do all the post-fork setup. set session id, close file descriptors, write pid file"
  [pid-path & {:keys [debug]}]
  ;; (.setsid C)
  ;; (when (not debug)
  ;; (closeDescriptors))
  (write-pid-file pid-path))

(defn sigterm [pid]
  (.kill C pid 15))
Something went wrong with that request. Please try again.