関数順 インデックス
目的別 インデックス

構文
 
 chroot ルート・ディレクトリにするディレクトリ名

返り値
 
 実行結果

説明

  • 指定したディレクトリをルート・ディレクトリとして扱う。使用できるのはスーパー・ユーザのみです。

使用例

ルート・ディレクトリを/home/user1に変更する
#!/usr/bin/perl
use strict;
use warnings;

my $root_dir = '/home/user1';
my $search_dir = '/';

chroot $root_dir or die $!;

opendir my $dh, $search_dir or die "$!";
foreach my $file (readdir $dh) {
  print $search_dir, $file, "\n";
}
closedir $dh;
子プロセスのルート・ディレクトリを/home/user1に変更する
#!/usr/bin/perl
use strict;
use warnings;

my $dir = '/home/user1';

my $pid;
if ($pid = fork()) {
  wait();
  print "----- parent process -----\n";
  show_files();
} elsif (defined $pid) {
  chroot $dir or die $!;
  print "----- child process -----\n";
  show_files();
} else {
  print "Error:$!\n";
}
print "End.\n";

sub show_files {
  my $search_dir = '/';
  opendir my $dh, $search_dir or die "$!";
  foreach my $file (readdir $dh) {
    print $search_dir, $file, "\n";
  }
  closedir $dh;
}