Class: MiqWin32::Services

Inherits:
Object
  • Object
show all
Defined in:
lib/metadata/util/win32/Win32Services.rb

Constant Summary collapse

SERVICE_MAPPING =
[
  'Type', :svc_type,
  'Start', :start,
  'ImagePath', :image_path,
  'DisplayName', :display_name,
  'ObjectName', :object_name,
  'Description', :description,
  'Type', :type,
  'DependOnService', :depend_on_service,
  'DependOnGroup', :depend_on_group,
]
SERVICE_VALUE_MAP =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_c, fs) ⇒ Services

Returns a new instance of Services.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/metadata/util/win32/Win32Services.rb', line 24

def initialize(_c, fs)
  @services = []

  regHnd = RemoteRegistry.new(fs, true)
  reg_doc = regHnd.loadHive("system", [{:key => 'CurrentControlSet/Services', :depth => 0, :value => SERVICE_VALUE_MAP}])
  regHnd.close

  reg_node = MIQRexml.findRegElement("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services", reg_doc.root)
  if reg_node
    reg_node.each_element do |e|
      next if e.name != :key

      # Remove child elements's that have children.  This data is not being processed on the server
      # and adds a lot of extract size to the xml and time for tagging.
      e.each_element { |e1| e1.remove! if e1.name == :key }
    end

    reg_node.each_element_with_attribute(:keyname) do |e|
      attrs = XmlFind.decode(e, SERVICE_MAPPING)
      attrs[:name] = e.attributes[:keyname]
      attrs[:typename] = service_type_to_string(attrs.delete(:type))
      attrs[:depend_on_service] = attrs[:depend_on_service].split(' ').collect { |d| {:name => d} } if attrs[:depend_on_service]
      attrs[:depend_on_group] = attrs[:depend_on_group].split(' ').collect { |d| {:name => d} } if attrs[:depend_on_group]

      @services << attrs
    end
  end

  # Force memory cleanup
  reg_doc = nil; GC.start
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



7
8
9
# File 'lib/metadata/util/win32/Win32Services.rb', line 7

def services
  @services
end

Instance Method Details

#service_type_to_string(type) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/metadata/util/win32/Win32Services.rb', line 56

def service_type_to_string(type)
  type = type.nil? ? 1 : type.to_i
  if (type & 0x00000001) > 0
    "kernel"
  elsif (type & 0x00000002) > 0
    "filesystem"
  elsif ((type & 0x00000010) > 0) || ((type & 0x00000020) > 0)
    "win32_service"
  else
    "misc"
  end
end

#to_xml(doc = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/metadata/util/win32/Win32Services.rb', line 69

def to_xml(doc = nil)
  doc = MiqXml.createDoc(nil) unless doc

  @services.each do |s|
    depends_service = s.delete(:depend_on_service)
    depends_group = s.delete(:depend_on_group)

    node = doc.add_element("service", XmlHelpers.stringify_keys(s))

    depends_service.each { |d| node.add_element("depend_on_service", XmlHelpers.stringify_keys(d)) } if depends_service
    depends_group.each { |d| node.add_element("depend_on_group", XmlHelpers.stringify_keys(d)) } if depends_group
  end

  doc
end