Created
November 23, 2009 21:51
-
-
Save zenchild/241409 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
require 'rubygems' | |
require 'ffi' | |
module Utmp | |
extend FFI::Library | |
UT_HOSTSIZE = 256 | |
UT_LINESIZE = 32 | |
INIT_PROCESS = 5 | |
NEW_TIME = 3 | |
RUN_LVL = 1 | |
EMPTY = 0 | |
UT_NAMESIZE = 32 | |
ACCOUNTING = 9 | |
BOOT_TIME = 2 | |
LOGIN_PROCESS = 6 | |
OLD_TIME = 4 | |
DEAD_PROCESS = 8 | |
USER_PROCESS = 7 | |
class ExitStatus < FFI::Struct | |
layout :e_termination, :short, | |
:e_exit, :short | |
end | |
class UtTv < FFI::Struct | |
layout :tv_sec, :int32, | |
:tv_usec, :int32 | |
end | |
class Utmp < FFI::Struct | |
layout :ut_type, :short, | |
:ut_pid, :int, | |
:ut_line, [:char, UT_LINESIZE], | |
:ut_id, [:char, 4], | |
:ut_user, [:char, UT_NAMESIZE], | |
:ut_host, [:char, UT_HOSTSIZE], | |
:ut_exit, ExitStatus, | |
:ut_session, :int32, | |
:ut_tv, UtTv, | |
:ut_addr_v6, [:int32,4], | |
:__unused, [:char, 20] | |
end | |
attach_function :setutent, [], :void | |
attach_function :endutent, [], :void | |
attach_function :getutent, [], :pointer | |
end | |
begin | |
loop do | |
ptr = Utmp.getutent | |
ut = Utmp::Utmp.new(ptr) | |
puts "User: #{ut[:ut_user]}" | |
puts "Host: #{ut[:ut_host]}" | |
puts "Pid: #{ut[:ut_pid]}" | |
puts "Time: #{Time.at(ut[:ut_tv][:tv_sec]).to_s}" | |
end | |
rescue FFI::NullPointerError | |
nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "generate FFI constants" | |
task :ffi_consts do | |
require 'rubygems' | |
require 'ffi' | |
require 'ffi/tools/const_generator' | |
consts = FFI::ConstGenerator.new("Utmp") do |cg| | |
cg.include 'utmp.h' | |
process_constants = %w{ | |
EMPTY RUN_LVL BOOT_TIME NEW_TIME OLD_TIME INIT_PROCESS | |
LOGIN_PROCESS USER_PROCESS DEAD_PROCESS ACCOUNTING | |
UT_LINESIZE UT_NAMESIZE UT_HOSTSIZE | |
} | |
process_constants.each { |c| | |
cg.const c | |
} | |
end | |
puts "module Utmp" | |
consts.constants.each_pair do |k,v| | |
puts " #{k} = #{v.converted_value}" | |
end | |
puts "end" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment