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;
44 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
47 @EXPORT = qw(&getHTTPDPath &getDefaultConfigForTestDirectory &openHTTPD &closeHTTPD &setShouldWaitForUserInterrupt &waitForHTTPDLock &getWaitTime);
53 my $httpdLockPrefix = "WebKitHttpd.lock.";
55 my $exclusiveLockFile = File::Spec->catfile($tmpDir, "WebKit.lock");
57 my $httpdPidDir = File::Spec->catfile($tmpDir, "WebKit");
58 my $httpdPidFile = File::Spec->catfile($httpdPidDir, "httpd.pid");
60 my $waitForUserInterrupt = 0;
64 $SIG{'INT'} = 'handleInterrupt';
65 $SIG{'TERM'} = 'handleInterrupt';
69 if (isDebianBased()) {
70 $httpdPath = "/usr/sbin/apache2";
72 $httpdPath = "/usr/sbin/httpd";
77 sub getDefaultConfigForTestDirectory
79 my ($testDirectory) = @_;
80 die "No test directory has been specified." unless ($testDirectory);
85 my $windowsConfDirectory = "$testDirectory/http/conf/";
86 unless (-x "/usr/lib/apache/libphp4.dll") {
87 copy("$windowsConfDirectory/libphp4.dll", "/usr/lib/apache/libphp4.dll");
88 chmod(0755, "/usr/lib/apache/libphp4.dll");
90 $httpdConfig = "$windowsConfDirectory/cygwin-httpd.conf";
91 } elsif (isDebianBased()) {
92 $httpdConfig = "$testDirectory/http/conf/apache2-debian-httpd.conf";
93 } elsif (isFedoraBased()) {
94 $httpdConfig = "$testDirectory/http/conf/fedora-httpd.conf";
96 $httpdConfig = "$testDirectory/http/conf/httpd.conf";
97 $httpdConfig = "$testDirectory/http/conf/apache2-httpd.conf" if `$httpdPath -v` =~ m|Apache/2|;
100 my $documentRoot = "$testDirectory/http/tests";
101 my $jsTestResourcesDirectory = $testDirectory . "/fast/js/resources";
102 my $typesConfig = "$testDirectory/http/conf/mime.types";
103 my $httpdLockFile = File::Spec->catfile($httpdPidDir, "httpd.lock");
104 my $httpdScoreBoardFile = File::Spec->catfile($httpdPidDir, "httpd.scoreboard");
107 "-f", "$httpdConfig",
108 "-C", "DocumentRoot \"$documentRoot\"",
109 # Setup a link to where the js test templates are stored, use -c so that mod_alias will already be loaded.
110 "-c", "Alias /js-test-resources \"$jsTestResourcesDirectory\"",
111 "-c", "TypesConfig \"$typesConfig\"",
112 # Apache wouldn't run CGIs with permissions==700 otherwise
113 "-c", "User \"#$<\"",
114 "-c", "LockFile \"$httpdLockFile\"",
115 "-c", "PidFile \"$httpdPidFile\"",
116 "-c", "ScoreBoardFile \"$httpdScoreBoardFile\"",
119 # FIXME: Enable this on Windows once <rdar://problem/5345985> is fixed
120 # The version of Apache we use with Cygwin does not support SSL
121 my $sslCertificate = "$testDirectory/http/conf/webkit-httpd.pem";
122 push(@httpdArgs, "-c", "SSLCertificateFile \"$sslCertificate\"") unless isCygwin();
131 die "No HTTPD configuration has been specified" unless (@args);
132 mkdir($httpdPidDir, 0755);
133 die "No write permissions to $httpdPidDir" unless (-w $httpdPidDir);
135 if (-f $httpdPidFile) {
136 open (PIDFILE, $httpdPidFile);
137 my $oldPid = <PIDFILE>;
140 if (0 != kill 0, $oldPid) {
141 print "\nhttpd is already running: pid $oldPid, killing...\n";
145 while ((kill(0, $oldPid) != 0) && $retryCount) {
152 die "Timed out waiting for httpd to quit";
157 $httpdPath = "/usr/sbin/httpd" unless ($httpdPath);
159 open2(">&1", \*HTTPDIN, $httpdPath, @args);
162 while (!-f $httpdPidFile && $retryCount) {
169 die "Timed out waiting for httpd to start";
172 $httpdPid = <PIDFILE> if open(PIDFILE, $httpdPidFile);
173 chomp $httpdPid if $httpdPid;
176 waitpid($httpdPid, 0) if ($waitForUserInterrupt && $httpdPid);
187 while (-f $httpdPidFile && $retryCount) {
194 print STDERR "Timed out waiting for httpd to terminate!\n";
200 sub setShouldWaitForUserInterrupt
202 $waitForUserInterrupt = 1;
215 unlink $exclusiveLockFile;
216 unlink $myLockFile if $myLockFile;
219 sub extractLockNumber
222 return -1 unless $lockFile;
223 return substr($lockFile, length($httpdLockPrefix));
228 opendir(TMPDIR, $tmpDir) or die "Could not open " . $tmpDir . ".";
229 my @lockFiles = grep {m/^$httpdLockPrefix\d+$/} readdir(TMPDIR);
230 @lockFiles = sort { extractLockNumber($a) <=> extractLockNumber($b) } @lockFiles;
235 sub getNextAvailableLockNumber
237 my @lockFiles = getLockFiles();
238 return 0 unless @lockFiles;
239 return extractLockNumber($lockFiles[-1]) + 1;
242 sub getLockNumberForCurrentRunning
244 my @lockFiles = getLockFiles();
245 return 0 unless @lockFiles;
246 return extractLockNumber($lockFiles[0]);
251 $waitBeginTime = time;
252 scheduleHttpTesting();
253 # If we are the only one waiting for Apache just run the tests without any further checking
254 if (scalar getLockFiles() > 1) {
255 my $currentLockFile = File::Spec->catfile($tmpDir, "$httpdLockPrefix" . getLockNumberForCurrentRunning());
256 my $currentLockPid = <SCHEDULER_LOCK> if (-f $currentLockFile && open(SCHEDULER_LOCK, "<$currentLockFile"));
257 # Wait until we are allowed to run the http tests
258 while ($currentLockPid && $currentLockPid != $$) {
259 $currentLockFile = File::Spec->catfile($tmpDir, "$httpdLockPrefix" . getLockNumberForCurrentRunning());
260 if ($currentLockFile eq $myLockFile) {
261 $currentLockPid = <SCHEDULER_LOCK> if open(SCHEDULER_LOCK, "<$currentLockFile");
262 if ($currentLockPid != $$) {
263 print STDERR "\nPID mismatch.\n";
274 sub scheduleHttpTesting
276 # We need an exclusive lock file to avoid deadlocks and starvation and ensure that the scheduler lock numbers are sequential.
277 # The scheduler locks are used to schedule the running test sessions in first come first served order.
278 while (!(open(SEQUENTIAL_GUARD_LOCK, ">$exclusiveLockFile") && flock(SEQUENTIAL_GUARD_LOCK, LOCK_EX|LOCK_NB))) {}
279 $myLockFile = File::Spec->catfile($tmpDir, "$httpdLockPrefix" . getNextAvailableLockNumber());
280 open(SCHEDULER_LOCK, ">$myLockFile");
281 print SCHEDULER_LOCK "$$";
282 print SEQUENTIAL_GUARD_LOCK "$$";
283 close(SCHEDULER_LOCK);
284 close(SEQUENTIAL_GUARD_LOCK);
285 unlink $exclusiveLockFile;
291 if ($waitBeginTime && $waitEndTime) {
292 $waitTime = $waitEndTime - $waitBeginTime;