Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.96k stars 555 forks source link

Fix t/op/getppid.t in linux docker container #18618

Open kocoureasy opened 3 years ago

kocoureasy commented 3 years ago

Hi, its similar of issue RT #130143, where test t/op/getppid.t fail in docker container. Problem is in function is_linux_container() which should properly return if perl is called in container, but not properly check new docker environment.

File proc_1.txt contain /proc/1/cgroup under Docker 20.10.3. File diff_getppid.txt contain fix for latest perl

Regards Igor

proc_1.txt diff_getppid.txt

Leont commented 3 years ago

This fix looks very Docker specific. Ideally we would deal with this in a way that detects all containerization, not just docker.

kocoureasy commented 3 years ago

Nice code which detect all of this is in detect_container() or detect_container_files() function from systemd here https://github.com/systemd/systemd/blob/main/src/basic/virt.c

Leont commented 3 years ago

I would propose to check the process tree to see if the current process' ultimate ancestor is process=1.

sub is_in_container {
    return unless -e "/proc/$$/stat";
    my $pid = $$;
    while ($pid > 1) {
        open my $fh, '<', "/proc/$pid/stat";
        ($pid) = <$fh> =~ /\S+ \S+ \S+ (\S+)/;
    }
    return $pid != 1;
}