Module: RExec

Defined in:
lib/rexec/version.rb,
lib/rexec/task.rb,
lib/rexec/daemon.rb,
lib/rexec/server.rb,
lib/rexec/connection.rb,
lib/rexec/daemon/base.rb,
lib/rexec/environment.rb,
lib/rexec/priviledges.rb,
lib/rexec/daemon/pidfile.rb,
lib/rexec/daemon/controller.rb

Overview

Copyright © 2007, 2009 Samuel Williams. Released under the GNU GPLv3.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Daemon, VERSION Classes: Connection, InvalidConnectionError, Task

Constant Summary collapse

RD =
0
WR =
1
@@connection_code =
(Pathname.new(__FILE__).dirname + "connection.rb").read
@@client_code =
(Pathname.new(__FILE__).dirname + "client.rb").read

Class Method Summary collapse

Class Method Details

.change_user(user) ⇒ Object

Set the user of the current process. Supply either a user ID or a user name.

Be aware that on Mac OS X / Ruby 1.8 there are bugs when the user id is negative (i.e. it doesn’t work). For example “nobody” with uid -2 won’t work.



12
13
14
15
16
17
18
# File 'lib/rexec/priviledges.rb', line 12

def self.change_user(user)
  if user.kind_of?(String)
    user = Etc.getpwnam(user).uid
  end

  Process::Sys.setuid(user)
end

.close_io(except = [$stdin, $stdout, $stderr]) ⇒ Object

This function closes all IO other than $stdin, $stdout, $stderr



47
48
49
50
51
52
53
54
# File 'lib/rexec/task.rb', line 47

def self.close_io(except = [$stdin, $stdout, $stderr])
  # Make sure all file descriptors are closed
  ObjectSpace.each_object(IO) do |io|
    unless except.include?(io)
      io.close rescue nil
    end
  end
end

.current_userObject

Get the user of the current process. Returns the user name.



22
23
24
25
26
# File 'lib/rexec/priviledges.rb', line 22

def self.current_user
  uid = Process::Sys.getuid
  
  Etc.getpwuid(uid).name
end

.env(new_env = nil, &block) ⇒ Object

Updates the global ENV for the duration of block. Not multi-thread safe.



4
5
6
7
8
9
10
11
12
13
# File 'lib/rexec/environment.rb', line 4

def self.env (new_env = nil, &block)
  old_env = ENV.to_hash

  ENV.update(new_env) if new_env

  yield

  ENV.clear
  ENV.update(old_env)
end

.start_server(code, command, options = {}, &block) ⇒ Object

Start a remote ruby server. This function is a structural cornerstone. This code runs the command you supply (this command should start an instance of ruby somewhere), sends it the code in connection.rb and client.rb as well as the code you supply.

Once the remote ruby instance is set up and ready to go, this code will return (or yield) the connection and pid of the executed command.

From this point, you can send and receive objects, and interact with the code you provided within a remote ruby instance.

If command is a shell such as “/bin/sh”, and we need to start ruby separately, you can supply options[:ruby] = "/usr/bin/ruby" to explicitly start the ruby command.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rexec/server.rb', line 40

def self.start_server(code, command, options = {}, &block)
  options[:passthrough] = :err unless options[:passthrough]
  
  send_code = Proc.new do |cin|
    cin.puts(@@connection_code)
    cin.puts(@@client_code)
    cin.puts(code)
  end
  
  if block_given?
    Task.open(command, options) do |process|
      conn = Connection.build(process, options, &send_code)
      
      yield conn, process.pid
    end
  else
    process = Task.open(command, options)
    conn = Connection.build(process, options, &send_code)
    
    return conn, process.pid
  end
end