Add sysfs option for linux system

Linux offers a sysfs interface to the macaddresses of the system network
interfaces without calling exec.

This enables stable system based UUID generation even on docker images
like php:7
This commit is contained in:
Robbert Müller
2017-10-06 19:57:30 +02:00
parent 0ef23d1b10
commit cfafc97fc0
+39 -4
View File
@@ -38,14 +38,19 @@ class SystemNodeProvider implements NodeProviderInterface
$pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/';
$matches = array();
// first try a linux specific way
$node = $this->getsysfs();
// Search the ifconfig output for all MAC addresses and return
// the first one found
$node = false;
if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) {
$node = $matches[1][0];
if ($node !== false) {
if (preg_match_all($pattern, $this->getIfconfig(), $matches, PREG_PATTERN_ORDER)) {
$node = $matches[1][0];
}
}
if ($node !== false) {
$node = str_replace([':', '-'], '', $node);
}
return $node;
}
@@ -73,4 +78,34 @@ class SystemNodeProvider implements NodeProviderInterface
return ob_get_clean();
}
/**
* Returns mac address from the first system interface via the sysfs interface
*
* @return string|bool
*/
protected function getsysfs()
{
if (strtoupper(php_uname('s')) === "LINUX") {
// get all the macadresses of all systems
$macs = array_map(
'file_get_contents',
glob('/sys/class/net/*/address', GLOB_NOSORT)
);
$macs = array_map('trim', $macs);
// remove invalid entries
$macs = array_filter($macs, function ($mac) {
return
// localhost adapter
$mac !== '00:00:00:00:00:00' &&
// must match mac adress
preg_match('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i', $mac);
});
$mac = reset($macs);
}
return $mac;
}
}