win32-process 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ = 0.6.1 - 16-Jul-2009
2
+ * Added the Process.uid method. This method returns a user id (really, the RID
3
+ of the SID) by default, but can also take an optional parameter to return
4
+ a binary SID instead at the user's discretion.
5
+ * Added working implementations of Process.getpriority and Process.setpriority.
6
+ Note they they only work for processes, not process groups or users.
7
+ * Set license to Artistic 2.0, and updated the gemspec.
8
+
1
9
  = 0.6.0 - 31-Oct-2008
2
10
  * The mandatory argument for Process.create has been switched from 'app_name'
3
11
  to 'command_line', to be more in line with underlying CreateProcess API.
data/lib/win32/process.rb CHANGED
@@ -8,15 +8,18 @@ require 'windows/console'
8
8
  require 'windows/window'
9
9
  require 'windows/unicode'
10
10
  require 'windows/tool_helper'
11
+ require 'windows/security'
12
+ require 'windows/msvcrt/string'
11
13
 
12
14
  module Process
13
15
  class Error < RuntimeError; end
14
16
 
15
17
  # Eliminates redefinition warnings.
16
- undef_method :kill, :wait, :wait2, :waitpid, :waitpid2, :ppid
18
+ undef_method :getpriority, :kill, :ppid, :setpriority
19
+ undef_method :wait, :wait2, :waitpid, :waitpid2, :uid
17
20
 
18
21
  # The version of this library
19
- WIN32_PROCESS_VERSION = '0.6.0'
22
+ WIN32_PROCESS_VERSION = '0.6.1'
20
23
 
21
24
  include Windows::Process
22
25
  include Windows::Thread
@@ -24,20 +27,24 @@ module Process
24
27
  include Windows::Library
25
28
  include Windows::Console
26
29
  include Windows::Handle
30
+ include Windows::Security
27
31
  include Windows::Synchronize
28
32
  include Windows::Window
29
33
  include Windows::Unicode
30
34
  include Windows::ToolHelper
35
+ include Windows::MSVCRT::String
31
36
 
32
37
  extend Windows::Error
33
38
  extend Windows::Process
34
39
  extend Windows::Thread
40
+ extend Windows::Security
35
41
  extend Windows::Synchronize
36
42
  extend Windows::Handle
37
43
  extend Windows::Library
38
44
  extend Windows::Console
39
45
  extend Windows::Unicode
40
46
  extend Windows::ToolHelper
47
+ extend Windows::MSVCRT::String
41
48
 
42
49
  # Used by Process.create
43
50
  ProcessInfo = Struct.new("ProcessInfo",
@@ -50,6 +57,122 @@ module Process
50
57
  @child_pids = [] # Static variable used for Process.fork
51
58
  @i = -1 # Static variable used for Process.fork
52
59
 
60
+ # Retrieves the priority class for the specified process id +int+. Unlike
61
+ # the default implementation, lower values do not necessarily correspond to
62
+ # higher priority classes.
63
+ #
64
+ # The +kind+ parameter is ignored but present for API compatibility.
65
+ # You can only retrieve process information, not process group or user
66
+ # information, so it is effectively always Process::PRIO_PROCESS.
67
+ #
68
+ # Possible return values are:
69
+ #
70
+ # 32 - Process::NORMAL_PRIORITY_CLASS
71
+ # 64 - Process::IDLE_PRIORITY_CLASS
72
+ # 128 - Process::HIGH_PRIORITY_CLASS
73
+ # 256 - Process::REALTIME_PRIORITY_CLASS
74
+ # 16384 - Process::BELOW_NORMAL_PRIORITY_CLASS
75
+ # 32768 - Process::ABOVE_NORMAL_PRIORITY_CLASS
76
+ #
77
+ def getpriority(kind = Process::PRIO_PROCESS, int = nil)
78
+ raise ArgumentError unless int
79
+
80
+ handle = OpenProcess(PROCESS_QUERY_INFORMATION, 0 , int)
81
+
82
+ if handle == INVALID_HANDLE_VALUE
83
+ raise Error, get_last_error
84
+ end
85
+
86
+ priority_class = GetPriorityClass(handle)
87
+
88
+ if priority_class == 0
89
+ raise Error, get_last_error
90
+ end
91
+
92
+ priority_class
93
+ end
94
+
95
+ # Sets the priority class for the specified process id +int+.
96
+ #
97
+ # The +kind+ parameter is ignored but present for API compatibility.
98
+ # You can only retrieve process information, not process group or user
99
+ # information, so it is effectively always Process::PRIO_PROCESS.
100
+ #
101
+ # Possible +int_priority+ values are:
102
+ #
103
+ # * Process::NORMAL_PRIORITY_CLASS
104
+ # * Process::IDLE_PRIORITY_CLASS
105
+ # * Process::HIGH_PRIORITY_CLASS
106
+ # * Process::REALTIME_PRIORITY_CLASS
107
+ # * Process::BELOW_NORMAL_PRIORITY_CLASS
108
+ # * Process::ABOVE_NORMAL_PRIORITY_CLASS
109
+ #
110
+ def setpriority(kind = nil, int = nil, int_priority = nil)
111
+ raise ArgumentError unless int
112
+ raise ArgumentError unless int_priority
113
+
114
+ handle = OpenProcess(PROCESS_SET_INFORMATION, 0 , int)
115
+
116
+ if handle == INVALID_HANDLE_VALUE
117
+ raise Error, get_last_error
118
+ end
119
+
120
+ unless SetPriorityClass(handle, int_priority)
121
+ raise Error, get_last_error
122
+ end
123
+
124
+ return 0 # Match the spec
125
+ end
126
+
127
+ # Returns the uid of the current process. Specifically, it returns the
128
+ # RID of the SID associated with the owner of the process.
129
+ #
130
+ # If +sid+ is set to true, then a binary sid is returned. Otherwise, a
131
+ # numeric id is returned (the default).
132
+ #--
133
+ # The Process.uid method in core Ruby always returns 0 on MS Windows.
134
+ #
135
+ def uid(sid = false)
136
+ token = 0.chr * 4
137
+
138
+ unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token)
139
+ raise Error, get_last_error
140
+ end
141
+
142
+ token = token.unpack('V')[0]
143
+ rlength = 0.chr * 4
144
+ tuser = 0.chr * 512
145
+
146
+ bool = GetTokenInformation(
147
+ token,
148
+ TokenUser,
149
+ tuser,
150
+ tuser.size,
151
+ rlength
152
+ )
153
+
154
+ unless bool
155
+ raise Error, get_last_error
156
+ end
157
+
158
+ lsid = tuser[8, (rlength.unpack('L').first - 8)]
159
+
160
+ if sid
161
+ lsid
162
+ else
163
+ sid_addr = [lsid].pack('p*').unpack('L')[0]
164
+ sid_buf = 0.chr * 80
165
+ sid_ptr = 0.chr * 4
166
+
167
+ unless ConvertSidToStringSid(sid_addr, sid_ptr)
168
+ raise Error, get_last_error
169
+ end
170
+
171
+ strcpy(sid_buf, sid_ptr.unpack('L')[0])
172
+ sid_buf.strip.split('-').last.to_i
173
+ end
174
+ end
175
+
53
176
  # Waits for the given child process to exit and returns that pid.
54
177
  #
55
178
  # Note that the $? (Process::Status) global variable is NOT set. This
@@ -699,8 +822,8 @@ module Process
699
822
  pid
700
823
  end
701
824
 
702
- module_function :kill, :wait, :wait2, :waitpid, :waitpid2, :create, :fork
703
- module_function :ppid
825
+ module_function :create, :fork, :getpriority, :kill, :ppid, :setpriority
826
+ module_function :wait, :wait2, :waitpid, :waitpid2, :uid
704
827
  end
705
828
 
706
829
  # Create a global fork method
@@ -37,10 +37,11 @@ class TC_Win32Process < Test::Unit::TestCase
37
37
 
38
38
  def setup
39
39
  @pid = nil
40
+ @pri_class = Process::NORMAL_PRIORITY_CLASS
40
41
  end
41
42
 
42
43
  def test_version
43
- assert_equal('0.6.0', Process::WIN32_PROCESS_VERSION)
44
+ assert_equal('0.6.1', Process::WIN32_PROCESS_VERSION)
44
45
  end
45
46
 
46
47
  def test_kill
@@ -117,6 +118,35 @@ class TC_Win32Process < Test::Unit::TestCase
117
118
  assert_equal(true, Process.ppid > 0)
118
119
  assert_equal(false, Process.pid == Process.ppid)
119
120
  end
121
+
122
+ def test_uid
123
+ assert_respond_to(Process, :uid)
124
+ assert_kind_of(Fixnum, Process.uid)
125
+ assert_kind_of(String, Process.uid(true))
126
+ end
127
+
128
+ def test_getpriority
129
+ assert_respond_to(Process, :getpriority)
130
+ assert_nothing_raised{ Process.getpriority(nil, Process.pid) }
131
+ assert_kind_of(Fixnum, Process.getpriority(nil, Process.pid))
132
+ end
133
+
134
+ def test_getpriority_expected_errors
135
+ assert_raise{ Process.getpriority }
136
+ assert_raise{ Process.getpriority(nil) }
137
+ end
138
+
139
+ def test_setpriority
140
+ assert_respond_to(Process, :setpriority)
141
+ assert_nothing_raised{ Process.setpriority(nil, Process.pid, @pri_class) }
142
+ assert_equal(0, Process.setpriority(nil, Process.pid, @pri_class))
143
+ end
144
+
145
+ def test_setpriority_expected_errors
146
+ assert_raise{ Process.setpriority }
147
+ assert_raise{ Process.setpriority(nil) }
148
+ assert_raise{ Process.setpriority(nil, Process.pid) }
149
+ end
120
150
 
121
151
  def test_creation_constants
122
152
  assert_not_nil(Process::CREATE_DEFAULT_ERROR_MODE)
@@ -1,26 +1,30 @@
1
- require "rubygems"
1
+ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |gem|
4
- desc = "Adds create, fork, wait, wait2, waitpid, and a special kill method"
5
- gem.name = "win32-process"
6
- gem.version = "0.6.0"
7
- gem.authors = ['Daniel Berger', 'Park Heesob']
8
- gem.email = "djberg96@gmail.com"
4
+ gem.name = 'win32-process'
5
+ gem.version = '0.6.1'
6
+ gem.license = 'Artistic 2.0'
7
+ gem.authors = ['Daniel Berger', 'Park Heesob']
8
+ gem.email = 'djberg96@gmail.com'
9
+ gem.homepage = 'http://www.rubyforge.org/projects/win32utils'
10
+ gem.platform = Gem::Platform::RUBY
11
+ gem.summary = 'Adds and redefines several Process methods for MS Windows'
12
+ gem.test_files = Dir['test/*.rb']
13
+ gem.has_rdoc = true
14
+ gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
15
+
16
+ gem.description = <<-EOF
17
+ The win32-process library implements several Process methods that are
18
+ either unimplemented or dysfunctional in some way in the default Ruby
19
+ implementation. Examples include Process.kill, Process.waitpid,
20
+ Process.create and an experimental Process.fork method.
21
+ EOF
22
+
9
23
  gem.rubyforge_project = 'win32utils'
10
- gem.homepage = "http://www.rubyforge.org/projects/win32utils"
11
- gem.platform = Gem::Platform::RUBY
12
- gem.summary = desc
13
- gem.description = desc
14
- gem.test_files = Dir["test/*.rb"]
15
- gem.has_rdoc = true
16
- gem.files = Dir["lib/win32/*.rb"] + Dir["test/*"] + Dir["[A-Z]*"] + Dir["examples/*"]
17
- gem.files.reject! { |fn| fn.include? "CVS" }
18
- gem.require_path = "lib"
19
- gem.extra_rdoc_files = ["README", "CHANGES", "MANIFEST"]
20
- gem.add_dependency("windows-pr", ">= 0.8.6")
21
- end
24
+ gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
22
25
 
23
- if $0 == __FILE__
24
- Gem.manage_gems if Gem::RubyGemsVersion.to_f < 1.3
25
- Gem::Builder.new(spec).build
26
+ gem.add_dependency('windows-pr', '>= 1.0.5')
26
27
  end
28
+
29
+ Gem.manage_gems if Gem::RubyGemsVersion.to_f < 1.3
30
+ Gem::Builder.new(spec).build
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: win32-process
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Berger
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-10-31 00:00:00 -06:00
13
+ date: 2009-07-16 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -21,9 +21,9 @@ dependencies:
21
21
  requirements:
22
22
  - - ">="
23
23
  - !ruby/object:Gem::Version
24
- version: 0.8.6
24
+ version: 1.0.5
25
25
  version:
26
- description: Adds create, fork, wait, wait2, waitpid, and a special kill method
26
+ description: " The win32-process library implements several Process methods that are\n either unimplemented or dysfunctional in some way in the default Ruby\n implementation. Examples include Process.kill, Process.waitpid,\n Process.create and an experimental Process.fork method.\n"
27
27
  email: djberg96@gmail.com
28
28
  executables: []
29
29
 
@@ -34,22 +34,21 @@ extra_rdoc_files:
34
34
  - CHANGES
35
35
  - MANIFEST
36
36
  files:
37
- - lib/win32/process.rb
38
- - test/test_win32_process.rb
39
37
  - CHANGES
40
- - examples
41
- - lib
42
- - MANIFEST
43
- - Rakefile
44
- - README
45
- - test
46
- - win32-process.gemspec
47
38
  - examples/test_create.rb
48
39
  - examples/test_fork_wait.rb
49
40
  - examples/test_fork_waitpid.rb
50
41
  - examples/test_kill.rb
42
+ - lib/win32/process.rb
43
+ - MANIFEST
44
+ - Rakefile
45
+ - README
46
+ - test/test_win32_process.rb
47
+ - win32-process.gemspec
51
48
  has_rdoc: true
52
49
  homepage: http://www.rubyforge.org/projects/win32utils
50
+ licenses:
51
+ - Artistic 2.0
53
52
  post_install_message:
54
53
  rdoc_options: []
55
54
 
@@ -70,9 +69,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
69
  requirements: []
71
70
 
72
71
  rubyforge_project: win32utils
73
- rubygems_version: 1.3.1
72
+ rubygems_version: 1.3.4
74
73
  signing_key:
75
- specification_version: 2
76
- summary: Adds create, fork, wait, wait2, waitpid, and a special kill method
74
+ specification_version: 3
75
+ summary: Adds and redefines several Process methods for MS Windows
77
76
  test_files:
78
77
  - test/test_win32_process.rb