Module: OsCtld::UtmpReader

Defined in:
lib/osctld/utmp_reader.rb

Overview

Read utmp files, see man utmp(5)

Defined Under Namespace

Classes: Entry, ExitStatus, TimeValue

Constant Summary collapse

MAX_ENTRIES =
128

Class Method Summary collapse

Class Method Details

.read(_path, max_entries: MAX_ENTRIES) {|entry| ... } ⇒ Array<Entry>?

Parameters:

  • path (String)

    utmp file to parse

  • max_entries (Integer) (defaults to: MAX_ENTRIES)

    maximum number of entries read from the file

Yield Parameters:

Returns:

Raises:

  • (IOError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/osctld/utmp_reader.rb', line 67

def self.read(_path, max_entries: MAX_ENTRIES)
  ret = []
  i = 0

  File.open('/run/utmp', 'rb') do |f|
    until f.eof?
      e = Entry.read(f)

      if block_given?
        yield(e)
      else
        ret << e
      end

      i += 1
      break if i >= max_entries
    end
  end

  block_given? ? nil : ret
end

.read_utmp_fhs(max_entries: MAX_ENTRIES) {|entry| ... } ⇒ Array<Entry>?

Find utmp file in standard locations and read it

Parameters:

  • max_entries (Integer) (defaults to: MAX_ENTRIES)

    maximum number of entries read from the file

Yield Parameters:

Returns:

Raises:

  • (IOError, Errno::ENOENT)


94
95
96
97
98
99
100
101
102
# File 'lib/osctld/utmp_reader.rb', line 94

def self.read_utmp_fhs(max_entries: MAX_ENTRIES, &block)
  %w[/run/utmp /var/log/utmp].each do |path|
    return read(path, max_entries:, &block)
  rescue Errno::ENOENT
    next
  end

  raise Errno::ENOENT, 'utmp file not found'
end