Detect if piwigo is running in a container

Append info on PHP_OS in the template of `admin/maintenance_env.php` and in `include/functions.inc.php` -> `send_piwigo_infos()`

Detection works by checking if PHP is running on Linux then check if PID2 is kthreadd
Kthreadd is useless in a container so PID2 should not exist of be another process
If unable to read /proc/2/sched for some reason, assume a SELinux restriction and that PHP is not running in a container

`is_in_container()` doesn't differentiate between VMs or bare metal, it only check if PHP is running in a containerized environement via tools like docker or podman
This commit is contained in:
Renarde-dev
2025-09-05 13:32:34 +02:00
parent 2bd5751e8b
commit 21e77002bc
2 changed files with 41 additions and 3 deletions

View File

@@ -288,7 +288,7 @@ $template->assign(
'PHPWG_URL' => PHPWG_URL,
'PWG_VERSION' => PHPWG_VERSION,
'U_CHECK_UPGRADE' => sprintf($url_format, 'check_upgrade'),
'OS' => PHP_OS,
'OS' => PHP_OS.((is_in_container()) ? ' (container)' : ''),
'PHP_VERSION' => phpversion(),
'DB_ENGINE' => 'MySQL',
'DB_VERSION' => $db_version,

View File

@@ -2540,7 +2540,7 @@ function send_piwigo_infos()
'technical' => array(
'php_version' => PHP_VERSION,
'piwigo_version' => PHPWG_VERSION,
'os_version' => PHP_OS,
'os_version' => PHP_OS.((is_in_container()) ? ' (container)' : ''),
'db_version' => pwg_get_db_version(),
'php_datetime' => date("Y-m-d H:i:s"),
'db_datetime' => $db_current_date,
@@ -2955,4 +2955,42 @@ function pwg_unique_exec_ends($token_name)
conf_delete_param($token_name.'_running');
}
?>
/**
*
* Detect if Piwigo is running in a containerized environment
* Assume all containers are Linux based
* Doesn't differentiate between VMs and bare metal installs
*
* @since 16
*
* @return bool
*/
function is_in_container()
{
if (strtoupper(substr(PHP_OS, 0, 5)) === 'LINUX')
{
if (file_exists('/proc/2/sched')) // Check if PID2 exist
{
$line = file_get_contents('/proc/2/sched'); // Read PID2 name
if (false == $line )
{
return false;
}
else
{
// If PID2 name is not kthreadd, piwigo is running in a container
return !('kthreadd' === substr( $line, 0, 8 ));
}
}
else
{
return true;
}
}
else
{
return false;
}
}
?>