1 # Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved
2 # Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
3 # Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 # its contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # Module to share code to start and stop the Apache daemon.
36 use File::Spec::Functions;
43 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
46 @EXPORT = qw(&getHTTPDPath &getDefaultConfigForTestDirectory &openHTTPD &closeHTTPD &getHTTPDPid &setShouldWaitForUserInterrupt);
53 my $httpdPidDir = File::Spec->catfile($tmpDir, "WebKit");
54 my $httpdPidFile = File::Spec->catfile($httpdPidDir, "httpd.pid");
56 my $waitForUserInterrupt = 0;
58 $SIG{'INT'} = 'cleanup';
59 $SIG{'TERM'} = 'cleanup';
63 if (isDebianBased()) {
64 $httpdPath = "/usr/sbin/apache2";
66 $httpdPath = "/usr/sbin/httpd";
71 sub getDefaultConfigForTestDirectory
73 my ($testDirectory) = @_;
74 die "No test directory has been specified." unless ($testDirectory);
79 my $windowsConfDirectory = "$testDirectory/http/conf/";
80 unless (-x "/usr/lib/apache/libphp4.dll") {
81 copy("$windowsConfDirectory/libphp4.dll", "/usr/lib/apache/libphp4.dll");
82 chmod(0755, "/usr/lib/apache/libphp4.dll");
84 $httpdConfig = "$windowsConfDirectory/cygwin-httpd.conf";
85 } elsif (isDebianBased()) {
86 $httpdConfig = "$testDirectory/http/conf/apache2-debian-httpd.conf";
87 } elsif (isFedoraBased()) {
88 $httpdConfig = "$testDirectory/http/conf/fedora-httpd.conf";
90 $httpdConfig = "$testDirectory/http/conf/httpd.conf";
91 $httpdConfig = "$testDirectory/http/conf/apache2-httpd.conf" if `$httpdPath -v` =~ m|Apache/2|;
94 my $documentRoot = "$testDirectory/http/tests";
95 my $jsTestResourcesDirectory = $testDirectory . "/fast/js/resources";
96 my $typesConfig = "$testDirectory/http/conf/mime.types";
97 my $httpdLockFile = File::Spec->catfile($httpdPidDir, "httpd.lock");
98 my $httpdScoreBoardFile = File::Spec->catfile($httpdPidDir, "httpd.scoreboard");
101 "-f", "$httpdConfig",
102 "-C", "DocumentRoot \"$documentRoot\"",
103 # Setup a link to where the js test templates are stored, use -c so that mod_alias will already be loaded.
104 "-c", "Alias /js-test-resources \"$jsTestResourcesDirectory\"",
105 "-c", "TypesConfig \"$typesConfig\"",
106 # Apache wouldn't run CGIs with permissions==700 otherwise
107 "-c", "User \"#$<\"",
108 "-c", "LockFile \"$httpdLockFile\"",
109 "-c", "PidFile \"$httpdPidFile\"",
110 "-c", "ScoreBoardFile \"$httpdScoreBoardFile\"",
113 # FIXME: Enable this on Windows once <rdar://problem/5345985> is fixed
114 # The version of Apache we use with Cygwin does not support SSL
115 my $sslCertificate = "$testDirectory/http/conf/webkit-httpd.pem";
116 push(@httpdArgs, "-c", "SSLCertificateFile \"$sslCertificate\"") unless isCygwin();
125 die "No HTTPD configuration has been specified" unless (@args);
126 mkdir($httpdPidDir, 0755);
127 die "No write permissions to $httpdPidDir" unless (-w $httpdPidDir);
129 if (-f $httpdPidFile) {
130 open (PIDFILE, $httpdPidFile);
131 my $oldPid = <PIDFILE>;
134 if (0 != kill 0, $oldPid) {
135 print "\nhttpd is already running: pid $oldPid, killing...\n";
139 while ((kill(0, $oldPid) != 0) && $retryCount) {
144 die "Timed out waiting for httpd to quit" unless $retryCount;
148 $httpdPath = "/usr/sbin/httpd" unless ($httpdPath);
150 open2(\*HTTPDIN, \*HTTPDOUT, $httpdPath, @args);
153 while (!-f $httpdPidFile && $retryCount) {
160 die "Timed out waiting for httpd to start";
163 $httpdPid = <PIDFILE> if open(PIDFILE, $httpdPidFile);
164 chomp $httpdPid if $httpdPid;
167 waitpid($httpdPid, 0) if ($waitForUserInterrupt && $httpdPid);
180 while (-f $httpdPidFile && $retryCount) {
186 print STDERR "Timed out waiting for httpd to terminate!\n";
194 sub setShouldWaitForUserInterrupt
196 $waitForUserInterrupt = 1;