summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-02-22 12:54:30 +0200
committerSergey Poznyakoff <gray@gnu.org>2018-02-22 12:54:30 +0200
commitc16f2363476167d59a2eaad38ef9e98f032ebdd1 (patch)
tree9cac41ac5cd55e76d872c9a977187484a76092f0
parent3c71a4b3c78a8b25258e858135cd239d22bd83c9 (diff)
downloadapache-defaults-c16f2363476167d59a2eaad38ef9e98f032ebdd1.tar.gz
apache-defaults-c16f2363476167d59a2eaad38ef9e98f032ebdd1.tar.bz2
Improve error reporting; Add documentation and test suite.
-rw-r--r--lib/Apache/Defaults.pm226
-rw-r--r--t/01version.t40
-rw-r--r--t/02modules.t15
-rw-r--r--t/03env.t14
-rw-r--r--t/MockHttpd.pm67
-rw-r--r--t/TestHttpd.pm25
6 files changed, 373 insertions, 14 deletions
diff --git a/lib/Apache/Defaults.pm b/lib/Apache/Defaults.pm
index 13c729e..08c4865 100644
--- a/lib/Apache/Defaults.pm
+++ b/lib/Apache/Defaults.pm
@@ -7,2 +7,3 @@ use Shell::GetEnv;
7use DateTime::Format::Strptime; 7use DateTime::Format::Strptime;
8use Text::ParseWords;
8use Symbol 'gensym'; 9use Symbol 'gensym';
@@ -28,3 +29,4 @@ sub new {
28 29
29 if (my @select = grep { -x $_ } @servlist) { 30 if (my @select = grep { -x $_->[0] }
31 map { [ shellwords($_) ] } @servlist) {
30 $self->{server} = shift @select; 32 $self->{server} = shift @select;
@@ -35,5 +37,9 @@ sub new {
35 if ($v = delete $_{environ}) { 37 if ($v = delete $_{environ}) {
36 $self->{environ} = Shell::GetEnv->new('sh', ". $v", startup => 0) 38 my $env = Shell::GetEnv->new('sh', ". $v",
37 ->envs; 39 { startup => 0 });
38 } 40 if ($env->status) {
41 croak "Got status ".$env->status." trying to inherit environment";
42 }
43 $self->{environ} = $env->envs;
44 }
39 45
@@ -43,3 +49,4 @@ sub new {
43 49
44sub server { shift->{server} } 50sub server { shift->{server}[0] }
51sub server_command { @{shift->{server}} }
45sub environ { shift->{environ} } 52sub environ { shift->{environ} }
@@ -52,6 +59,8 @@ sub probe {
52 59
53 my $fd = gensym; 60 my $out = gensym;
61 my $err = gensym;
54 local %ENV = %{$self->{environ}} if $self->{environ}; 62 local %ENV = %{$self->{environ}} if $self->{environ};
55 if (my $pid = open3($nullin, $fd, $nullout, $self->server, @opt)) { 63 if (my $pid = open3($nullin, $out, $err,
56 while (<$fd>) { 64 $self->server_command, @opt)) {
65 while (<$out>) {
57 chomp; 66 chomp;
@@ -59,6 +68,18 @@ sub probe {
59 } 68 }
69 waitpid($pid, 0);
70 if ($? == -1) {
71 croak "failed to execute " .$self->server . ": $!";
72 } elsif ($? & 127) {
73 croak sprintf("%s died with signal %d%s",
74 $self->server, $? & 127,
75 ($? & 128) ? ' (core dumped)' : '');
76 } elsif (my $code = $? >> 8) {
77 local $/ = undef;
78 croak sprintf("%s terminated with status %d; error message: %s",
79 $self->server, $code, <$err>);
80 }
60 } 81 }
61 close $fd;
62 close $nullin; 82 close $nullin;
63 close $nullout; 83 close $out;
84 close $err;
64} 85}
@@ -86,3 +107,2 @@ sub _get_version_info {
86 pattern => '%b %d %Y %H:%M%S', 107 pattern => '%b %d %Y %H:%M%S',
87 strict => 1,
88 locale => 'en_US', 108 locale => 'en_US',
@@ -106,3 +126,3 @@ sub _get_version_info {
106 $self->{defines}{$+{name}} = $self->dequote($+{val}); 126 $self->{defines}{$+{name}} = $self->dequote($+{val});
107 } elsif (/^\s+-D\s+(?<name>.+?)(?:\s*(?<com>.+))?$/) { 127 } elsif (/^\s+-D\s+(?<name>\S+)(?:\s*(?<com>.+))?$/) {
108 $self->{defines}{$+{name}} = 1; 128 $self->{defines}{$+{name}} = 1;
@@ -141,3 +161,6 @@ sub defines {
141 $self->_get_version_info; 161 $self->_get_version_info;
142 return @{$self->{defines}}{@_}; 162 if (@_) {
163 return @{$self->{defines}}{@_};
164 }
165 return sort keys %{$self->{defines}};
143} 166}
@@ -273,3 +296,3 @@ sub preloaded {
273 } 296 }
274 return keys %{$self->{preloaded}}; 297 return sort keys %{$self->{preloaded}};
275} 298}
@@ -292 +315,176 @@ sub _get_module_info {
2921; 3151;
316__END__
317=head1 NAME
318
319Apache::Defaults - Get default settings for Apache httpd daemon
320
321=head1 SYNOPSIS
322
323 $x = new Apache::Defaults;
324 print $x->name;
325 print $x->version;
326 print $x->server_root;
327 print $x->built;
328 print $x->architecture;
329 print $x->MPM;
330 print $x->defines('DYNAMIC_MODULE_LIMIT');
331 print $x->preloaded('cgi_module');
332
333=head1 DESCRIPTION
334
335Detects the default settings of the Apache httpd daemon by invoking
336it with appropriate options and analyzing its output.
337
338=head1 METHODS
339
340=head2 new
341
342 $x = new Apache::Defaults(%attrs);
343
344Detects the settings of the apache server and returns the object representing
345them. Attributes (I<%attrs>) are:
346
347=over 4
348
349=item C<server>
350
351Full pathname of the B<httpd> binary to inspect. The argument can also be
352a reference to the list of possible pathnames. In this case, the first of
353them that exists on disk and has executable privileges will be used. Full
354command line can also be used, e.g.:
355
356 server => '/usr/sbin/httpd -d /etc/httpd'
357
358The default used in the absense of this attribute is:
359
360 [ '/usr/sbin/httpd', '/usr/sbin/apache2' ]
361
362=item C<environ>
363
364Name of the shell script that sets the environment for B<httpd> invocation.
365For some obscure reason, B<httpd> attempts to read its configuration file
366even if invoked with the B<-V> option (which is intended to print the version
367and build parameters of the daemon). It will fail if its configuration refers
368to the environment variables, defined elsewhere. This situation is quite
369common in Debian-based distributions, which define the environment variables
370in file F</etc/apache2/envvars>. This attribute is intended to cope with
371such problems, e.g.:
372
373 $x = new Apache::Defaults(environ => /etc/apache2/envvars)
374
375=back
376
377The method will I<croak> if an error occurs (e.g. the server binary
378is not found or exits with failure).
379
380=head2 server
381
382 $s = $x->server;
383
384Returns the pathname of the B<httpd> binary.
385
386=head2 server_command
387
388 @cmd = $x->server_command;
389
390Returns full command line of the B<httpd> binary.
391
392=head2 environ
393
394 $hashref = $x->environ
395
396Returns a reference to the environment used when invoking the server.
397
398=head2 name
399
400 $s = $x->name;
401
402Returns server implementation name (normally C<Apache>).
403
404=head2 version
405
406 $v = $x->version;
407
408Returns server version (as string).
409
410=head2 platform
411
412 $s = $x->platform;
413
414Platform (distribution) on which the binary is compiled.
415
416=head2 architecture
417
418Architecture for which the server is built.
419
420=head2 built
421
422 $d = $x->built;
423
424Returns a B<DateTime> object, representing the time when the server
425was built.
426
427=head2 loaded_with
428
429APR tools with which the server is loaded.
430
431=head2 compiled_with
432
433APR tools with which the server is compiled.
434
435=head2 MPM
436
437MPM module loaded in the configuration.
438
439=head2 MPM_threaded
440
441True if the MPM is threaded.
442
443=head2 MPM_forked
444
445True if the MPM is forked.
446
447=head2 defines