From cfafc97fc084964a5cd6feea98ef898f7ab71611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robbert=20M=C3=BCller?= Date: Fri, 6 Oct 2017 19:57:30 +0200 Subject: [PATCH] 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 --- src/Provider/Node/SystemNodeProvider.php | 43 +++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Provider/Node/SystemNodeProvider.php b/src/Provider/Node/SystemNodeProvider.php index 216d739..507948c 100644 --- a/src/Provider/Node/SystemNodeProvider.php +++ b/src/Provider/Node/SystemNodeProvider.php @@ -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; + } }