1 USING: arrays elevate elevate.private io.launcher kernel locals
2 math sequences splitting strings system unix.ffi unix.process prettyprint ;
6 ! https://wiki.sei.cmu.edu/confluence/x/p9YxBQ
7 ! group ID must be lowered before user ID otherwise program may re-gain root!
9 getgid setgid failed-process? [ lowered-failed ] [ ] if
10 getuid setuid failed-process? [ lowered-failed ] [ ] if ;
12 GENERIC: posix-replace-process ( command-list -- code )
13 ! naive split breaks with spaces inside quotes in shell commands
14 M: string posix-replace-process
15 " " split posix-replace-process ;
16 M: array posix-replace-process
17 [ first ] [ rest " " prefix ] bi exec-with-path ;
19 ! if either the real or effective user IDs are 0, we are already elevated
21 getuid geteuid [ zero? ] bi@ or ;
23 :: posix-elevated ( command replace? -- process )
24 command "sudo" prepend-command
25 replace? [ posix-replace-process ] [ run-process ] if
26 dup failed-process? [ drop command { "sudo" } elevated-failed ] [ ] if ;
29 2drop posix-elevated ;