2024-04-15 22:15:07 +02:00
|
|
|
(local { : assoc : system : dirname } (require :anoia))
|
|
|
|
(local { : mktree : rmtree : symlink } (require :anoia.fs))
|
|
|
|
|
|
|
|
(fn parse-match [s] (string.match s "(.-)=(.+)"))
|
|
|
|
|
|
|
|
(fn parse-args [args]
|
|
|
|
(match args
|
|
|
|
["-s" service & rest] (assoc (parse-args rest) :service service)
|
|
|
|
["-n" path & rest] (assoc (parse-args rest) :linkname path)
|
|
|
|
matches { :matches (collect [_ m (ipairs matches)] (parse-match m)) }
|
|
|
|
_ nil))
|
|
|
|
|
|
|
|
(fn %% [fmt ...] (string.format fmt ...))
|
|
|
|
|
|
|
|
(fn event-matches? [params e]
|
|
|
|
(and
|
|
|
|
e
|
|
|
|
(accumulate [match? true
|
|
|
|
name value (pairs params.matches)]
|
|
|
|
(and match? (= value (. e name))))))
|
|
|
|
|
|
|
|
|
|
|
|
(var up :unknown)
|
|
|
|
|
|
|
|
(fn start-service [devname linkname service]
|
|
|
|
(match (symlink (.. "/dev/" devname ) linkname)
|
|
|
|
ok (pcall system (%% "s6-rc -b -u change %q" service))
|
|
|
|
(nil err) false))
|
|
|
|
|
|
|
|
(fn stop-service [linkname service]
|
|
|
|
(match (pcall system (%% "s6-rc -b -d change %q" linkname service))
|
|
|
|
ok (os.remove linkname)
|
|
|
|
(nil err) false))
|
|
|
|
|
|
|
|
(fn toggle-service [devname linkname service wanted?]
|
|
|
|
(when (not (= up wanted?))
|
|
|
|
(set up
|
|
|
|
(if wanted?
|
|
|
|
(start-service devname linkname service)
|
|
|
|
(not (stop-service linkname service))))))
|
|
|
|
|
|
|
|
(fn parse-uevent [s]
|
|
|
|
(when s
|
|
|
|
(let [(nl nxt) (string.find s "\0" 1 true)]
|
|
|
|
(collect [k v (string.gmatch
|
|
|
|
(string.sub s (+ 1 nxt))
|
|
|
|
"(%g-)=(%g+)")]
|
|
|
|
(k:lower) v))))
|
|
|
|
|
2024-04-20 17:53:43 +02:00
|
|
|
(fn run-with-fh [fh args]
|
2024-04-15 22:15:07 +02:00
|
|
|
(set up :unknown)
|
|
|
|
(let [parameters
|
|
|
|
(assert (parse-args args) (.. "can't parse args: " (table.concat args " ")))]
|
|
|
|
(mktree (dirname parameters.linkname))
|
|
|
|
(var finished? false)
|
|
|
|
|
|
|
|
(while (not finished?)
|
|
|
|
(let [e (parse-uevent (fh:read 5000))]
|
|
|
|
(when (event-matches? parameters e)
|
|
|
|
(let [wanted? (. {:add true :change true} e.action)]
|
|
|
|
(toggle-service e.devname parameters.linkname parameters.service wanted?)))
|
|
|
|
(set finished? (= e nil))
|
|
|
|
))))
|
|
|
|
|
2024-04-20 17:53:43 +02:00
|
|
|
(fn run [args]
|
2024-04-15 22:15:07 +02:00
|
|
|
(let [nellie (require :nellie)
|
|
|
|
netlink (nellie.open 4)]
|
2024-04-20 17:53:43 +02:00
|
|
|
(run-with-fh netlink arg)))
|
2024-04-15 22:15:07 +02:00
|
|
|
|
2024-04-20 17:53:43 +02:00
|
|
|
{ : run : run-with-fh : event-matches? }
|