Created
May 5, 2011 18:20
-
-
Save tylerflint/957578 to your computer and use it in GitHub Desktop.
exec functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def spawn(command) | |
| fork do | |
| File.umask self.umask if self.umask | |
| uid_num = Etc.getpwnam(self.uid).uid if self.uid | |
| gid_num = Etc.getgrnam(self.gid).gid if self.gid | |
| ::Dir.chroot(self.chroot) if self.chroot | |
| ::Process.setsid | |
| ::Process.groups = [gid_num] if self.gid | |
| ::Process::Sys.setgid(gid_num) if self.gid | |
| ::Process::Sys.setuid(uid_num) if self.uid | |
| self.dir ||= '/' | |
| Dir.chdir self.dir | |
| $0 = command | |
| STDIN.reopen "/dev/null" | |
| if self.log_cmd | |
| STDOUT.reopen IO.popen(self.log_cmd, "a") | |
| else | |
| STDOUT.reopen file_in_chroot(self.log), "a" | |
| end | |
| if err_log_cmd | |
| STDERR.reopen IO.popen(err_log_cmd, "a") | |
| elsif err_log && (log_cmd || err_log != log) | |
| STDERR.reopen file_in_chroot(err_log), "a" | |
| else | |
| STDERR.reopen STDOUT | |
| end | |
| # close any other file descriptors | |
| 3.upto(256){|fd| IO::new(fd).close rescue nil} | |
| if self.env && self.env.is_a?(Hash) | |
| self.env.each do |(key, value)| | |
| ENV[key] = value | |
| end | |
| end | |
| exec command unless command.empty? | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment