title: Chroot command for a Ruby process
body: <p>Having the following <code>app.rb</code> Ruby application:</p>
  
  <pre><code>#!/usr/local/Cellar/rbenv/0.3.0/versions/1.9.3-p194/bin/ruby
  
  require 'rubygems'
  require 'sinatra'
  
  get '/pwd' do
    `pwd`
  end
  </code></pre>
  
  <p>I'm able to start it as a webserver with a command such as:</p>
  
  <ul>
  <li><code>./Users/doug/code/backdore/app.rb</code></li>
  <li><code>ruby /Users/doug/code/backdore/app.rb</code></li>
  <li><code>/usr/local/Cellar/rbenv/0.3.0/versions/1.9.3-p194/bin/ruby /Users/doug/code/backdore/app.rb</code></li>
  </ul>
  
  <p>Then, using a webclient, a GET on <code>http://localhost:4567/pwd</code> can return <code>/Users/doug/code/backdore</code> which is the current path from where I started the command.</p>
  
  <p>If I stop the server, and execute again the command from <code>Users/doug/code/</code> folder, then the returned path from the server is gonna be: <code>/Users/doug/code</code>.</p>
  
  <p>My question is the following, because I would like to <em>chroot</em> this webserver, I would need to prefix the command with something like:</p>
  
  <pre><code>sudo chroot /Users/doug/code/backdore /usr/local/Cellar/rbenv/0.3.0/versions/1.9.3-p194/bin/ruby "app.rb"
  </code></pre>
  
  <p>However, this donesn't work and I have this error message:</p>
  
  <blockquote>
    <p>chroot: /usr/local/Cellar/rbenv/0.3.0/versions/1.9.3-p194/bin/ruby: No such file or directory</p>
  </blockquote>
  
  <p>I'm working on Mac OS X, but I suppose this should be similar on Ubuntu. How could I chroot this server inside '<code>/Users/doug/code/backdore</code>', in order to see only '<code>/</code>' from the server?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>In order to have a Ruby interpreter inside the chroot, I just installed <a href="http://geekmonkey.org/articles/36-an-introduction-to-mini-ruby" rel="nofollow">mruby</a> and made some tests with this script...</p>
  
  <pre><code>$ cat test/script.rb
  loop do
    puts "hello world"
  end
  </code></pre>
  
  <p>My current location is:</p>
  
  <pre><code>$ pwd
  /Users/doug/code/mruby
  </code></pre>
  
  <p>So, just for testing the script,</p>
  
  <pre><code>$ bin/mruby test/script.rb
  hello world
  hello world
  hell^C
  </code></pre>
  
  <p>It's working fine. But if I try to chroot this like this:</p>
  
  <pre><code>$ chroot /Users/doug/code/mruby bin/mruby test/script.rb
  chroot: /Users/doug/code/mruby: Operation not permitted
  </code></pre>
  
  <p>And if I try like this:</p>
  
  <pre><code>$ sudo chroot /Users/doug/code/mruby bin/mruby test/script.rb
  Password:
  ~/code/mruby ‹1.9.3-p194›  ‹master*› $
  </code></pre>
  
  <p>and nothing happen (it should be an infinit loop).</p>
  
  <p>Thanks for any help.</p>