This file is indexed.

/usr/share/virt-manager/virtManager/halhelper.py is in virt-manager 0.9.5-1ubuntu3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#
# Copyright (C) 2007 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#

import logging
import os
import glob

import dbus

from virtManager.baseclass import vmmGObject
from virtManager.netdev import vmmNetDevice
from virtManager.mediadev import vmmMediaDevice

_hal_helper = None

def get_hal_helper(init=True):
    global _hal_helper
    if not _hal_helper and init:
        _hal_helper = vmmHalHelper()
    return _hal_helper

def cleanup():
    global _hal_helper
    if _hal_helper:
        _hal_helper.cleanup()
    _hal_helper = None

def get_net_bridge_owner(name_ignore, sysfspath):
    # Now magic to determine if the device is part of a bridge
    brportpath = os.path.join(sysfspath, "brport")

    try:
        if os.path.exists(brportpath):
            brlinkpath = os.path.join(brportpath, "bridge")
            dest = os.readlink(brlinkpath)
            (ignore, bridge) = os.path.split(dest)
            return bridge
    except:
        logging.exception("Unable to determine if device is shared")

    return None

def get_net_mac_address(name_ignore, sysfspath):
    mac = None
    addrpath = sysfspath + "/address"
    if os.path.exists(addrpath):
        df = open(addrpath, 'r')
        mac = df.readline().strip(" \n\t")
        df.close()
    return mac

def get_bonding_masters():
    masters = []
    if os.path.exists("/sys/class/net/bonding_masters"):
        f = open("/sys/class/net/bonding_masters")
        while True:
            rline = f.readline()
            if not rline:
                break
            if rline == "\x00":
                continue
            rline = rline.strip("\n\t")
            masters = rline[:].split(' ')
    return masters

def is_net_bonding_slave(name_ignore, sysfspath):
    masterpath = sysfspath + "/master"
    if os.path.exists(masterpath):
        return True
    return False

class vmmHalHelper(vmmGObject):
    def __init__(self):
        vmmGObject.__init__(self)

        self.bus = None
        self.hal_iface = None
        self.sigs = []

        # Error message we encountered when initializing
        self.startup_error = None

        self._dbus_connect()

    def _cleanup(self):
        self.bus = None
        self.hal_iface = None

        for sig in self.sigs:
            sig.remove()
        self.sigs = []

    def get_init_error(self):
        return self.startup_error

    def _dbus_connect(self):
        # Probe for network devices
        try:
            # Get a connection to the SYSTEM bus
            self.bus = dbus.SystemBus()
            # Get a handle to the HAL service
            hal_object = self.bus.get_object('org.freedesktop.Hal',
                                             '/org/freedesktop/Hal/Manager')
            self.hal_iface = dbus.Interface(hal_object,
                                            'org.freedesktop.Hal.Manager')


            # Track device add/removes so we can detect newly inserted CD media
            self.sigs.append(
                self.hal_iface.connect_to_signal("DeviceAdded",
                                                 self._device_added))
            self.sigs.append(
                self.hal_iface.connect_to_signal("DeviceRemoved",
                                                 self._device_removed))
        except Exception, e:
            logging.error("Unable to connect to HAL to list network "
                          "devices: " + str(e))
            self.startup_error = str(e)

    def connect(self, name, callback, *args):
        handle_id = vmmGObject.connect(self, name, callback, *args)

        if name == "netdev-added":
            self.populate_netdevs()

        elif name == "optical-added":
            self.populate_opt_media()

        return handle_id


    ##################
    # Helper methods #
    ##################

    def dbus_dev_lookup(self, halpath):
        obj = self.bus.get_object("org.freedesktop.Hal", halpath)
        objif = dbus.Interface(obj, "org.freedesktop.Hal.Device")
        return objif

    def is_cdrom_media(self, halpath):
        obj = self.dbus_dev_lookup(halpath)
        return bool(obj.QueryCapability("volume") and
                    obj.GetPropertyBoolean("volume.is_disc") and
                    obj.GetPropertyBoolean("volume.disc.has_data"))

    def is_cdrom(self, halpath):
        obj = self.dbus_dev_lookup(halpath)
        return bool(obj.QueryCapability("storage.cdrom"))

    def is_netdev(self, halpath):
        obj = self.dbus_dev_lookup(halpath)
        return bool(obj.QueryCapability("net"))


    #############################
    # Initial device population #
    #############################

    def populate_opt_media(self):
        for path in self.hal_iface.FindDeviceByCapability("storage.cdrom"):
            # Make sure we only populate CDROM devs
            if not self.is_cdrom(path):
                continue

            devnode, media_label, media_hal_path = self._fetch_cdrom_info(path)
            self.add_optical_dev(str(devnode), str(path), media_label,
                                 media_hal_path)


    def populate_netdevs(self):
        bondMasters = get_bonding_masters()

        for bond in bondMasters:
            sysfspath = "/sys/class/net/" + bond
            mac = get_net_mac_address(bond, sysfspath)
            if mac:
                self._net_device_added(bond, mac, sysfspath)

                # Add any associated VLANs
                self._net_tag_device_added(bond, sysfspath)

        # Find info about all current present physical net devices
        for path in self.hal_iface.FindDeviceByCapability("net"):
            self._net_phys_device_added(path)


    #############################
    # Device callback listeners #
    #############################

    def _device_added(self, path):
        if self.is_cdrom_media(path):
            self._optical_media_added(path)
        elif self.is_cdrom(path):
            self._optical_added(path)
        elif self.is_netdev(path):
            self._net_phys_device_added(path)

    def _device_removed(self, path):
        self.emit("device-removed", str(path))

    def add_optical_dev(self, devpath, halpath, media_label, media_hal_path):
        obj = vmmMediaDevice(devpath, halpath, bool(media_label),
                             media_label, media_hal_path)
        obj.set_hal_media_signals(self)
        self.emit("optical-added", obj)

    def _optical_added(self, halpath):
        devpath, media_label, media_hal_path = self._fetch_cdrom_info(halpath)
        self.add_optical_dev(devpath, halpath, media_label, media_hal_path)

    def _optical_media_added(self, media_hal_path):
        media_label, devpath = self._fetch_media_info(media_hal_path)

        self.emit("optical-media-added", devpath, media_label, media_hal_path)

    def _net_phys_device_added(self, halpath):
        dbusobj = self.dbus_dev_lookup(halpath)

        name = dbusobj.GetPropertyString("net.interface")
        mac = dbusobj.GetPropertyString("net.address")

        # HAL gives back paths to like:
        # /sys/devices/pci0000:00/0000:00:1e.0/0000:01:00.0/net/eth0
        # which doesn't work so well - we want this:
        sysfspath = "/sys/class/net/" + name

        # If running a device in bridged mode, there's a reasonable
        # chance that the actual ethernet device has been renamed to
        # something else. ethN -> pethN
        psysfspath = sysfspath[0:len(sysfspath) - len(name)] + "p" + name
        if os.path.exists(psysfspath):
            logging.debug("Device %s named to p%s", name, name)
            name = "p" + name
            sysfspath = psysfspath

        # Ignore devices that are slaves of a bond
        if is_net_bonding_slave(name, sysfspath):
            logging.debug("Skipping device %s in bonding slave", name)
            return

        # Add the main NIC
        self._net_device_added(name, mac, sysfspath, halpath)

        # Add any associated VLANs
        self._net_tag_device_added(name, sysfspath)

    def _net_tag_device_added(self, name, sysfspath):
        for vlanpath in glob.glob(sysfspath + ".*"):
            if os.path.exists(vlanpath):
                logging.debug("Process VLAN %s", vlanpath)
                vlanmac = get_net_mac_address(name, vlanpath)
                if vlanmac:
                    (ignore, vlanname) = os.path.split(vlanpath)

                    # If running a device in bridged mode, there's areasonable
                    # chance that the actual ethernet device has beenrenamed to
                    # something else. ethN -> pethN
                    pvlanpath = (vlanpath[0:len(vlanpath) - len(vlanname)] +
                                 "p" + vlanname)
                    if os.path.exists(pvlanpath):
                        logging.debug("Device %s named to p%s",
                                      vlanname, vlanname)
                        vlanname = "p" + vlanname
                        vlanpath = pvlanpath
                    self._net_device_added(vlanname, vlanmac, vlanpath)

    def _net_device_added(self, name, mac, sysfspath, halpath=None):
        bridge = get_net_bridge_owner(name, sysfspath)
        shared = False
        if bridge is not None:
            shared = True

        dev = vmmNetDevice(name, mac, shared, bridge, halpath)
        self.emit("netdev-added", dev)


    ######################
    # CDROM info methods #
    ######################

    def _fetch_media_info(self, halpath):
        label = None
        devnode = None

        volif = self.dbus_dev_lookup(halpath)

        devnode = volif.GetProperty("block.device")
        label = volif.GetProperty("volume.label")
        if not label:
            label = devnode

        return (label and str(label), devnode and str(devnode))

    def _fetch_cdrom_info(self, halpath):
        devif = self.dbus_dev_lookup(halpath)

        devnode = devif.GetProperty("block.device")
        media_label = None
        media_hal_path = None

        if devnode:
            media_label, media_hal_path = self._find_media_for_devpath(devnode)

        return (devnode and str(devnode), media_label, media_hal_path)

    def _find_media_for_devpath(self, devpath):
        for path in self.hal_iface.FindDeviceByCapability("volume"):
            if not self.is_cdrom_media(path):
                continue

            label, devnode = self._fetch_media_info(path)

            if devnode == devpath:
                return (label, path)

        return None, None

vmmHalHelper.type_register(vmmHalHelper)
vmmHalHelper.signal_new(vmmHalHelper, "netdev-added", [object])
vmmHalHelper.signal_new(vmmHalHelper, "optical-added", [object])
vmmHalHelper.signal_new(vmmHalHelper, "optical-media-added", [str, str, str])
vmmHalHelper.signal_new(vmmHalHelper, "device-removed", [str])