Feature #9770


Added by akr (Akira Tanaka) almost 7 years ago. Updated over 6 years ago.

Target version:


How about Etc.uname method to call the uname(2) system call?

% ./ruby -rpp -retc -e 'pp Etc.uname'
 :version=>"#1 SMP Thu Nov 5 19:54:42 UTC 2009",

Sometimes we need to obtain OS (kernel) version or similar system information.

For example, there are several tests which invokes uname command (and function via Win32API):

test/ruby/test_io.rb:        return false if (`/bin/uname -r`.split('.') <=> %w[3 8]) < 0
test/ruby/test_io.rb:        return false if (`/bin/uname -r`.split('.') <=> %w[3 5]) < 0
test/ruby/test_io.rb:        return false if (`/bin/uname -r`.split('.') <=> %w[3 8]) < 0
test/ruby/test_sleep.rb:        4.98 if /Linux ([\d.]+)/ =~ `uname -sr` && ($1.split('.')<=>%w/2 6 18/)<1
test/socket/test_socket.rb:            (`uname -r`[/[0-9.]+/].split('.').map(&:to_i) <=> [2,6,18]) <= 0
test/dbm/test_dbm.rb:        uname ='cygwin1', 'uname', 'P', 'I')

Etc.uname provides clean replacement for them.
uname -r can be changed to Etc.uname[:release].

uname function is defined by POSIX.
So it is pretty portable.
If the function is not available, NotImplementedError is raised.

I chose Etc module because uname() returns system wide information.

The return value is a plain hash because:

  • Struct is marshal-incompatible if some OS add fields. (glibc has "domainname", for example. Current implementation doesn't support it, though.)
  • No inspect method required to view contents.

Any idea?


etc-uname.patch (3.16 KB) etc-uname.patch akr (Akira Tanaka), 04/23/2014 02:12 PM

Updated by usa (Usaku NAKAMURA) almost 7 years ago

Should I implement an emulation for Windows?
Which of an emulation or NotImplementedError do you think as desiable, akr-san?

Updated by akr (Akira Tanaka) almost 7 years ago

As far as the current usages are guraded by platform test such as /linux/ =~ RUBY_PLATFORM,
it seems that emulation on Windows is not so important for non-Windows platforms.

If someone want to check Windows version, Etc.uname may be a good method to provide the information.

Updated by djberg96 (Daniel Berger) almost 7 years ago

Already available in the sys-uname gem if anyone is interested. On Windows I return much more information using WMI.

Updated by djberg96 (Daniel Berger) almost 7 years ago

If you don't want to use WMI, you could get the same type of information using a combination of GetVersionEx and GetSystemInfo.

Updated by akr (Akira Tanaka) over 6 years ago

sys-uname gem is not usable for tests bundled in ruby.

Updated by djberg96 (Daniel Berger) over 6 years ago

I wasn't suggesting that you use the gem, just use it as a guide.

Updated by akr (Akira Tanaka) over 6 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r45983.

  • ext/etc/etc.c: Etc.uname method implemented.

  • ext/etc/extconf.rb: Check uname() function.

[ruby-core:62139] [Feature #9770]

Also available in: Atom PDF