diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2018-02-22 12:54:30 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2018-02-22 12:54:30 +0200 |
commit | c16f2363476167d59a2eaad38ef9e98f032ebdd1 (patch) | |
tree | 9cac41ac5cd55e76d872c9a977187484a76092f0 | |
parent | 3c71a4b3c78a8b25258e858135cd239d22bd83c9 (diff) | |
download | apache-defaults-c16f2363476167d59a2eaad38ef9e98f032ebdd1.tar.gz apache-defaults-c16f2363476167d59a2eaad38ef9e98f032ebdd1.tar.bz2 |
Improve error reporting; Add documentation and test suite.
-rw-r--r-- | lib/Apache/Defaults.pm | 226 | ||||
-rw-r--r-- | t/01version.t | 40 | ||||
-rw-r--r-- | t/02modules.t | 15 | ||||
-rw-r--r-- | t/03env.t | 14 | ||||
-rw-r--r-- | t/MockHttpd.pm | 67 | ||||
-rw-r--r-- | t/TestHttpd.pm | 25 |
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; | |||
7 | use DateTime::Format::Strptime; | 7 | use DateTime::Format::Strptime; |
8 | use Text::ParseWords; | ||
8 | use Symbol 'gensym'; | 9 | use 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 | ||
44 | sub server { shift->{server} } | 50 | sub server { shift->{server}[0] } |
51 | sub server_command { @{shift->{server}} } | ||
45 | sub environ { shift->{environ} } | 52 | sub 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 { | |||
292 | 1; | 315 | 1; |
316 | __END__ | ||
317 | =head1 NAME | ||
318 | |||
319 | Apache::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 | |||
335 | Detects the default settings of the Apache httpd daemon by invoking | ||
336 | it with appropriate options and analyzing its output. | ||
337 | |||
338 | =head1 METHODS | ||
339 | |||
340 | =head2 new | ||
341 | |||
342 | $x = new Apache::Defaults(%attrs); | ||
343 | |||
344 | Detects the settings of the apache server and returns the object representing | ||
345 | them. Attributes (I<%attrs>) are: | ||
346 | |||
347 | =over 4 | ||
348 | |||
349 | =item C<server> | ||
350 | |||
351 | Full pathname of the B<httpd> binary to inspect. The argument can also be | ||
352 | a reference to the list of possible pathnames. In this case, the first of | ||
353 | them that exists on disk and has executable privileges will be used. Full | ||
354 | command line can also be used, e.g.: | ||
355 | |||
356 | server => '/usr/sbin/httpd -d /etc/httpd' | ||
357 | |||
358 | The default used in the absense of this attribute is: | ||
359 | |||
360 | [ '/usr/sbin/httpd', '/usr/sbin/apache2' ] | ||
361 | |||
362 | =item C<environ> | ||
363 | |||
364 | Name of the shell script that sets the environment for B<httpd> invocation. | ||
365 | For some obscure reason, B<httpd> attempts to read its configuration file | ||
366 | even if invoked with the B<-V> option (which is intended to print the version | ||
367 | and build parameters of the daemon). It will fail if its configuration refers | ||
368 | to the environment variables, defined elsewhere. This situation is quite | ||
369 | common in Debian-based distributions, which define the environment variables | ||
370 | in file F</etc/apache2/envvars>. This attribute is intended to cope with | ||
371 | such problems, e.g.: | ||
372 | |||
373 | $x = new Apache::Defaults(environ => /etc/apache2/envvars) | ||
374 | |||
375 | =back | ||
376 | |||
377 | The method will I<croak> if an error occurs (e.g. the server binary | ||
378 | is not found or exits with failure). | ||
379 | |||
380 | =head2 server | ||
381 | |||
382 | $s = $x->server; | ||
383 | |||
384 | Returns the pathname of the B<httpd> binary. | ||
385 | |||
386 | =head2 server_command | ||
387 | |||
388 | @cmd = $x->server_command; | ||
389 | |||
390 | Returns full command line of the B<httpd> binary. | ||
391 | |||
392 | =head2 environ | ||
393 | |||
394 | $hashref = $x->environ | ||
395 | |||
396 | Returns a reference to the environment used when invoking the server. | ||
397 | |||
398 | =head2 name | ||
399 | |||
400 | $s = $x->name; | ||
401 | |||
402 | Returns server implementation name (normally C<Apache>). | ||
403 | |||
404 | =head2 version | ||
405 | |||
406 | $v = $x->version; | ||
407 | |||
408 | Returns server version (as string). | ||
409 | |||
410 | =head2 platform | ||
411 | |||
412 | $s = $x->platform; | ||
413 | |||
414 | Platform (distribution) on which the binary is compiled. | ||
415 | |||
416 | =head2 architecture | ||
417 | |||
418 | Architecture for which the server is built. | ||
419 | |||
420 | =head2 built | ||
421 | |||
422 | $d = $x->built; | ||
423 | |||
424 | Returns a B<DateTime> object, representing the time when the server | ||
425 | was built. | ||
426 | |||
427 | =head2 loaded_with | ||
428 | |||
429 | APR tools with which the server is loaded. | ||
430 | |||
431 | =head2 compiled_with | ||
432 | |||
433 | APR tools with which the server is compiled. | ||
434 | |||
435 | =head2 MPM | ||
436 | |||
437 | MPM module loaded in the configuration. | ||
438 | |||
439 | =head2 MPM_threaded | ||
440 | |||
441 | True if the MPM is threaded. | ||
442 | |||
443 | =head2 MPM_forked | ||
444 | |||
445 | True if the MPM is forked. | ||
446 | |||
447 | =head2 defines | ||