summaryrefslogtreecommitdiff
path: root/lib/Apache/Config/Preproc/include.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Apache/Config/Preproc/include.pm')
-rw-r--r--lib/Apache/Config/Preproc/include.pm124
1 files changed, 94 insertions, 30 deletions
diff --git a/lib/Apache/Config/Preproc/include.pm b/lib/Apache/Config/Preproc/include.pm
index 55b3bd2..f77436e 100644
--- a/lib/Apache/Config/Preproc/include.pm
+++ b/lib/Apache/Config/Preproc/include.pm
@@ -5,6 +5,7 @@ use Apache::Admin::Config;
use Apache::Config::Preproc;
use File::Spec;
use Cwd 'abs_path';
+use IPC::Open3;
use Carp;
our $VERSION = '1.01';
@@ -15,6 +16,13 @@ sub new {
my $self = bless { included => {}, conf => $conf }, $class;
local %_ = @_;
$self->{server_root} = delete $_{server_root};
+ if (my $v = delete $_{probe}) {
+ if (ref($v) eq 'ARRAY') {
+ $self->probe(@$v);
+ } else {
+ $self->probe;
+ }
+ }
croak "unrecognized arguments" if keys(%_);
$self->{check_included} = $^O eq 'MSWin32'
? \&_check_included_path
@@ -26,49 +34,79 @@ sub conf { shift->{conf} }
sub server_root {
my $self = shift;
- unless ($self->{server_root}) {
- if (my $d = $self->conf->directive('ServerRoot')) {
- $self->{server_root} = $self->conf->dequote($d->value);
- }
+ if (my $v = shift) {
+ $self->{server_root} = $self->conf->dequote($v);
}
return $self->{server_root};
}
-
sub expand {
my ($self, $d, $repl) = @_;
- if ($d->type eq 'directive' && $d->name =~ /^include(optional)?$/i) {
- my $optional = $1;
+ if ($d->type eq 'directive') {
+ if (lc($d->name) eq 'serverroot') {
+ $self->server_root($d->value);
+ } elsif ($d->name =~ /^include(optional)?$/i) {
+ my $optional = $1;
- my $pat = $self->conf->dequote($d->value);
- unless (File::Spec->file_name_is_absolute($pat)) {
- if (my $d = $self->server_root) {
- $pat = File::Spec->catfile($d, $pat);
+ my $pat = $self->conf->dequote($d->value);
+ unless (File::Spec->file_name_is_absolute($pat)) {
+ if (my $d = $self->server_root) {
+ $pat = File::Spec->catfile($d, $pat);
+ }
}
- }
- my @filelist = glob $pat;
- if (@filelist) {
- foreach my $file (@filelist) {
- if ($self->check_included($file)) {
- croak "file $file already included";
- }
- if (my $inc = new Apache::Admin::Config($file,
- @{$self->conf->options})) {
- # NOTE: make sure each item is cloned
- push @$repl, map { $_->clone } $inc->select;
- } else {
- croak $Apache::Admin::Config::ERROR;
+ my @filelist = glob $pat;
+ if (@filelist) {
+ foreach my $file (@filelist) {
+ if ($self->check_included($file)) {
+ croak "file $file already included";
+ }
+ if (my $inc = new Apache::Admin::Config($file,
+ @{$self->conf->options})) {
+ # NOTE: make sure each item is cloned
+ push @$repl, map { $_->clone } $inc->select;
+ } else {
+ croak $Apache::Admin::Config::ERROR;
+ }
}
}
+ return 1;
}
- return 1;
}
return 0;
}
+sub probe {
+ my ($self, @servlist) = @_;
+ unless (@servlist) {
+ @servlist = qw(/usr/sbin/httpd /usr/sbin/apache2);
+ }
+
+ open(my $nullout, '>', File::Spec->devnull);
+ open(my $nullin, '<', File::Spec->devnull);
+ foreach my $serv (@servlist) {
+ use Symbol 'gensym';
+ my $fd = gensym;
+ eval {
+ if (my $pid = open3($nullin, $fd, $nullout, $serv, '-V')) {
+ while (<$fd>) {
+ chomp;
+ if (/^\s+-D\s+HTTPD_ROOT=(.+)\s*$/) {
+ $self->server_root($1);
+ last;
+ }
+ }
+ }
+ };
+ close $fd;
+ last unless ($@)
+ }
+ close $nullin;
+ close $nullout;
+}
+
sub check_included {
my ($self, $file) = @_;
return $self->${ \ $self->{check_included} }($file);
@@ -107,7 +145,12 @@ Apache::Config::Preproc::include - expand Include statements
$x = new Apache::Config::Preproc '/path/to/httpd.conf',
-expand => [
- { include => { -server_root => $dir } }
+ { include => [ server_root => $dir ] }
+ ];
+
+ $x = new Apache::Config::Preproc '/path/to/httpd.conf',
+ -expand => [
+ { include => [ probe => [ '/usr/sbin/httpd' ] ] }
];
=head1 DESCRIPTION
@@ -115,10 +158,31 @@ Apache::Config::Preproc::include - expand Include statements
Processes B<Include> and B<IncludeOptional> statements and replaces them
with the contents of the files supplied in their arguments. If the argument
is not an absolute file name, it is searched in the server root directory.
-The latter is determined inspecting the B<ServerRoot> statement. If this
-statement is absent, the current working directory is used. The
-B<-server_root> constructor argument can be used to enforce a specific
-server root directory.
+
+Keyword arguments:
+
+=over 4
+
+=item B<server_root =E<gt>> I<DIR>
+
+Sets default server root value to I<DIR>.
+
+=item B<probe =E<gt>> I<LISTREF> | B<1>
+
+Determine the default server root value by analyzing the output of
+B<httpd -V>. If I<LISTREF> is given, it contains alternative pathnames
+of the Apache B<httpd> binary. Otherwise,
+
+ probe => 1
+
+is a shorthand for
+
+ probe => [qw(/usr/sbin/httpd /usr/sbin/apache2)]
+
+=back
+
+When the B<ServerRoot> statement is seen, its value overwrites any
+previously set server root directory.
=cut

Return to:

Send suggestions and report system problems to the System administrator.