Skip to content
This repository

Multiple groups support #102

Open
wants to merge 4 commits into from

1 participant

Ky-Anh Huynh
Ky-Anh Huynh
icy commented July 20, 2012

Allow a process to run under different groups

God.watch do |w|
  w.gid = "foo"
  w.gids = %w{bar rvm admin}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16  lib/god/process.rb
@@ -4,7 +4,7 @@ class Process
4 4
 
5 5
     attr_accessor :name, :uid, :gid, :log, :log_cmd, :err_log, :err_log_cmd,
6 6
                   :start, :stop, :restart, :unix_socket, :chroot, :env, :dir,
7  
-                  :stop_timeout, :stop_signal, :umask
  7
+                  :stop_timeout, :stop_signal, :umask, :gids
8 8
 
9 9
     def initialize
10 10
       self.log = '/dev/null'
@@ -80,6 +80,18 @@ def valid?
80 80
         end
81 81
       end
82 82
 
  83
+      # if multiple gids is provided
  84
+      if self.gids
  85
+        self.gids.each do |g|
  86
+          begin
  87
+            Etc.getgrnam(g)
  88
+          rescue ArgumentError
  89
+            valid = false
  90
+            applog(self, :error, "GID for '#{g}' does not exist")
  91
+          end
  92
+        end
  93
+      end
  94
+
83 95
       # dir must exist and be a directory if specified
84 96
       if self.dir
85 97
         if !File.exist?(self.dir)
@@ -289,10 +301,12 @@ def spawn(command)
289 301
         File.umask self.umask if self.umask
290 302
         uid_num = Etc.getpwnam(self.uid).uid if self.uid
291 303
         gid_num = Etc.getgrnam(self.gid).gid if self.gid
  304
+        gids_num = (self.gids || []).map{|g| Etc.getgrnam(g).gid}
292 305
 
293 306
         ::Dir.chroot(self.chroot) if self.chroot
294 307
         ::Process.setsid
295 308
         ::Process.groups = [gid_num] if self.gid
  309
+        ::Process.groups |= gids_num unless gids_num.empty?
296 310
         ::Process::Sys.setgid(gid_num) if self.gid
297 311
         ::Process::Sys.setuid(uid_num) if self.uid
298 312
         self.dir ||= '/'
16  test/test_process.rb
@@ -72,6 +72,22 @@ def test_valid_should_return_false_if_gid_does_not_exists
72 72
     assert !@p.valid?
73 73
   end
74 74
 
  75
+  def test_valid_should_return_true_if_gids_exists
  76
+    @p.start = 'qux'
  77
+    @p.log = '/tmp/foo.log'
  78
+    @p.gids = %w{wheel root}
  79
+
  80
+    assert @p.valid?
  81
+  end
  82
+
  83
+  def test_valid_should_return_false_if_gids_does_not_exists
  84
+    @p.start = 'qux'
  85
+    @p.log = '/tmp/foo.log'
  86
+    @p.gids = %w{foobarbaz root}
  87
+
  88
+    assert !@p.valid?
  89
+  end
  90
+
75 91
   def test_valid_should_return_true_if_dir_exists
76 92
     @p.start = 'qux'
77 93
     @p.log = '/tmp/foo.log'
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.