Do-compile 16.4 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1 2 3 4
#!/usr/bin/perl

use Getopt::Long;
$opt_distribution=$opt_user=$opt_result=$opt_config_options=$opt_config_env="";
5
$opt_dbd_options=$opt_perl_options=$opt_make_options=$opt_suffix="";
6
$opt_tmp=$opt_version_suffix="";
7
$opt_help=$opt_Information=$opt_delete=$opt_debug=$opt_stage=$opt_rsh_mail=$opt_no_test=$opt_no_perl=$opt_with_low_memory=$opt_fast_benchmark=$opt_static_client=$opt_static_server=$opt_static_perl=$opt_sur=$opt_with_small_disk=$opt_local_perl=$opt_tcpip=$opt_build_thread=$opt_no_mysqltest=$opt_use_old_distribution=$opt_enable_shared=$opt_no_crash_me=$opt_no_strip=0;
monty@work.mysql.com's avatar
monty@work.mysql.com committed
8 9
$opt_innodb=$opt_bdb=0;

10
GetOptions("Information","help","distribution=s","user=s","result=s","delete","no-test","no-mysqltest","perl-files=s","debug","config-options=s","config-env=s","stage=i","rsh-mail","with-low-memory","fast-benchmark","tmp=s","static-client","static-server","static-perl","no-perl","local-perl","perl-options=s","make-options=s", "sur","with-small-disk","dbd-options=s","tcpip","suffix=s","build-thread=i","innodb","bdb","use-old-distribution","enable-shared","no-crash-me","no-strip","version-suffix=s", "with-other-libc=s") || usage();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11 12 13 14

usage() if ($opt_help || $opt_Information);
usage() if (!$opt_distribution);

15
if ($opt_bdb && $opt_version_suffix eq "")
monty@work.mysql.com's avatar
monty@work.mysql.com committed
16
{
17
  $opt_version_suffix="-max";
monty@work.mysql.com's avatar
monty@work.mysql.com committed
18 19
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
20
chomp($host=`hostname`);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
21
$full_host_name=$host;
22
info("Compiling MySQL$opt_version_suffix at $host$suffix, stage: $opt_stage\n");
monty@work.mysql.com's avatar
monty@work.mysql.com committed
23
$connect_option= ($opt_tcpip ? "--host=$host" : "");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
24
$host =~ /^([^.-]*)/;
monty@work.mysql.com's avatar
monty@work.mysql.com committed
25
$host=$1 . $opt_suffix;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
26 27
$email="$opt_user\@mysql.com";
$pwd = `pwd`; chomp($pwd);
28
$log="$pwd/Logs/$host$opt_version_suffix.log";
29
$opt_distribution =~ /(mysql[^\/]*)\.tar/;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
30 31 32 33 34 35 36
$ver=$1;
$gcc_version=which("gcc");
if (defined($gcc_version) && ! $opt_config_env)
{
  $tmp=`$gcc_version -v 2>&1`;
  if ($tmp =~ /version 2\.7\./)
  {
37
    $opt_config_env= 'CC=gcc CFLAGS="-O2 -fno-omit-frame-pointer" CXX=gcc CXXFLAGS="-O2 -fno-omit-frame-pointer"';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
38
  }
39 40 41 42
  elsif ($tmp =~ /version 3\.0\./)
  {
    $opt_config_env= 'CC=gcc CFLAGS="-O3 -fno-omit-frame-pointer" CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti"';
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
43 44
  else
  {
45
    $opt_config_env= 'CC=gcc CFLAGS="-O3 -fno-omit-frame-pointer" CXX=gcc CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti"';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
46 47
  }
}
monty@work.mysql.com's avatar
monty@work.mysql.com committed
48 49

$new_opt_tmp=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
50 51
if ($opt_tmp)
{
monty@work.mysql.com's avatar
monty@work.mysql.com committed
52 53 54 55 56
  if (! -d $opt_tmp)
  {
    safe_system("mkdir $opt_tmp");
    $new_opt_tmp=1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
57 58 59 60 61 62
  $ENV{'TMPDIR'}=$opt_tmp;
}
else
{
  $opt_tmp="/tmp";
}
monty@work.mysql.com's avatar
monty@work.mysql.com committed
63
$bench_tmpdir="$opt_tmp/my_build-$host";
bk@work.mysql.com's avatar
bk@work.mysql.com committed
64 65
$ENV{'PATH'}= "$pwd/$host/bin:" . $ENV{'PATH'}; 
$rsh=which("rcmd","rsh");
66
$make=which("gmake","make");	# Can't use -j here!
bk@work.mysql.com's avatar
bk@work.mysql.com committed
67 68 69 70 71
$tar=which("gtar","tar");
$sendmail=find("/usr/lib/sendmail","/usr/sbin/sendmail");
$sur= $opt_sur ? "/my/local/bin/sur" : "";
delete $ENV{'MYSQL_PWD'};	# Reset possibly password
delete $ENV{'MY_BASEDIR_VERSION'};
72
$ENV{'MYSQL_TCP_PORT'}= $mysql_tcp_port= 3334 + $opt_build_thread*2;
monty@work.mysql.com's avatar
monty@work.mysql.com committed
73
$ENV{'MYSQL_UNIX_PORT'}=$mysql_unix_port="$opt_tmp/mysql$opt_suffix.build";
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
$ENV{"PERL5LIB"}="$pwd/$host/perl5:$pwd/$host/perl5/site_perl";
75
$slave_port=$mysql_tcp_port+16;
76
$manager_port=$mysql_tcp_port+1;
77

78 79 80 81 82 83 84 85 86 87 88 89 90
if ($opt_stage == 0)
{
  system("mkdir Logs") if (! -d "Logs");
  system("mv $log ${log}-old") if (-f $log);
  unlink($log);
}

open(LOG,">>$log") || abort("Can't open log file, error $?");
select LOG;
$|=1;
select STDOUT;
$|=1;

91 92 93 94 95 96 97 98
if (-x "$host/bin/mysqladmin")
{
  log_system("$host/bin/mysqladmin --no-defaults -u root -S $mysql_unix_port -s shutdown");
  log_system("$host/bin/mysqladmin --no-defaults -u root -P $mysql_tcp_port -h $host -s shutdown");
  log_system("$host/bin/mysqladmin --no-defaults -u root -P $slave_port -h $host -s shutdown");
  log_system("$host/bin/mysqladmin --no-defaults -u root -P 9306 -h $host -s shutdown");
  log_system("$host/bin/mysqladmin --no-defaults -u root -P 9307 -h $host -s shutdown");
}
99
kill_all("mysqlmanager");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
100 101 102 103

if ($opt_stage == 0)
{
  print "$host: Removing old distribution\n" if ($opt_debug);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
104 105 106 107
  if (!$opt_use_old_distribution)
  {
    system("mkdir $host") if (! -d $host);
    system("touch $host/mysql-fix-for-glob");
108
    rm_all(<$host/mysql*>);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
109 110
    system("mkdir $host/bin") if (! -d "$host/bin");
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112 113 114
  rm_all("$host/test");
  system("mkdir $host/test") if (! -d "$host/test");
}

115
safe_cd($host);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
116
if ($opt_stage == 0 && ! $opt_use_old_distribution)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
117 118
{
  safe_system("gunzip < $opt_distribution | $tar xf -");
119 120

  # Fix file times; This is needed because the time for files may be
121 122 123 124 125
  # in the future.  The following is done this way to ensure that
  # we don't get any errors from xargs touch
  system("touch timestamp");
  sleep(2);
  system("touch timestamp2");
126
  system("find . -newer timestamp -print | xargs touch; rm -f timestamp timestamp2");
127 128
  sleep(2);
  # Ensure that files we don't want to rebuild are newer than other files
129
  safe_cd($ver);
130 131 132 133 134 135 136
  foreach $name ("configure",
		 "Docs/include.texi",
		 "Docs/*.html", "Docs/manual.txt", "Docs/mysql.info",
		 "sql/sql_yacc.h", "sql/sql_yacc.cc")
  {
    system("touch $name");
  }
137 138
  # Fix some file modes in BDB tables that makes life harder.
  system("chmod -R u+rw .");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
139
}
140

141
safe_cd("$pwd/$host/$ver");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
142
if ($opt_stage <= 1)
monty@work.mysql.com's avatar
monty@work.mysql.com committed
143
{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
144
  $opt_config_options.=" --with-low-memory" if ($opt_with_low_memory);
145
  # Fix files if this is in another timezone than work.mysql.com
bk@work.mysql.com's avatar
bk@work.mysql.com committed
146
  unlink("config.cache");
147 148
  unlink("bdb/build_unix/config.cache");
  unlink("innobase/config.cache");
monty@work.mysql.com's avatar
monty@work.mysql.com committed
149
  log_system("$make clean") if ($opt_use_old_distribution);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
150 151 152 153 154 155 156 157
  if ($opt_static_server)
  {
    $opt_config_options.=" --with-mysqld-ldflags=-all-static";
  }
  if ($opt_static_client)
  {
    $opt_config_options.=" --with-client-ldflags=-all-static";
  }
158 159 160 161
  if ($opt_with_other_libc)
  {
    $opt_with_other_libc = "--with-other-libc=$opt_with_other_libc";    
  }
162 163 164 165
  if (!$opt_enable_shared)
  {
    $opt_config_options.= " --disable-shared"; # Default for binary versions
  }
monty@work.mysql.com's avatar
monty@work.mysql.com committed
166 167 168 169 170 171 172 173
  if ($opt_bdb)
  {
    $opt_config_options.= " --with-berkeley-db"
  }
  if ($opt_innodb)
  {
    $opt_config_options.= " --with-innodb"
  }
174
  check_system("$opt_config_env ./configure --prefix=/usr/local/mysql \"--with-comment=Official MySQL$opt_version_suffix binary\" --with-extra-charsets=complex \"--with-server-suffix=$opt_version_suffix\" --enable-thread-safe-client --enable-local-infile $opt_config_options","Thank you for choosing MySQL");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
175 176 177 178 179 180 181 182
  if (-d "$pwd/$host/include-mysql")
  {
    safe_system("cp -r $pwd/$host/include-mysql/* $pwd/$host/$ver/include");
  }
}

if ($opt_stage <= 2)
{
183
  my ($command);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
184
  unlink($opt_distribution) if ($opt_delete && !$opt_use_old_distribution);
185 186 187
  $command=$make;
  $command.= " $opt_make_options" if (defined($opt_make_options) && $opt_make_options ne "");
  safe_system($command);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
188 189 190 191 192 193 194
}

#
# Create a binary distrubtion
#
if ($opt_stage <= 3)
{
195
  my ($flags);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
196 197
  log_system("rm -fr mysql-3* mysql-4* $pwd/$host/*.tar.gz");
  log_system("nm -n sql/mysqld | gzip -9 -v 2>&1 > sql/mysqld.sym.gz | cat");
198 199 200 201

  $flags="";
  $flags.="--no-strip" if ($opt_no_strip);
  check_system("scripts/make_binary_distribution --tmp=$opt_tmp --suffix=$opt_suffix $flags",".tar.gz created");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
202
  safe_system("mv mysql*.tar.gz $pwd/$host");
203 204 205 206 207 208 209 210
  if (-f "client/.libs/mysqladmin")
  {
    safe_system("cp client/.libs/mysqladmin $pwd/$host/bin");
  }
  else
  {
    safe_system("cp client/mysqladmin $pwd/$host/bin");
  }  
211
  safe_system("$make clean") if ($opt_with_small_disk);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
212 213 214 215 216 217
}

#
# Unpack and test the binary distrubtion
#

monty@work.mysql.com's avatar
monty@work.mysql.com committed
218 219 220 221 222
$tar_file=<$pwd/$host/*.tar.gz>;
if (!defined($tar_file))
{
  $tar_file=<$pwd/$host/*.tgz>;
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
223 224 225 226 227 228 229
if ($opt_stage <= 4 && !$opt_no_test)
{
  rm_all(<$pwd/$host/test/*>);
  safe_cd("$pwd/$host/test");
  safe_system("gunzip < $tar_file | $tar xf -");
}

230
$tar_file =~ /(mysql[^\/]*)\.tar/;
monty@work.mysql.com's avatar
monty@work.mysql.com committed
231 232
$ver=$1;
$test_dir="$pwd/$host/test/$ver";
233
$ENV{"LD_LIBRARY_PATH"}= "$test_dir/lib:" . $ENV{"LD_LIBRARY_PATH"};
234

monty@work.mysql.com's avatar
monty@work.mysql.com committed
235
if ($opt_stage <= 5 && !$opt_no_test && !$opt_no_mysqltest)
236
{
237
  system("mkdir $bench_tmpdir") if (! -d $bench_tmpdir);
monty@work.mysql.com's avatar
monty@work.mysql.com committed
238
  safe_cd("${test_dir}/mysql-test");
239
  check_system("./mysql-test-run --warnings --tmpdir=$bench_tmpdir --master_port=$mysql_tcp_port --slave_port=$slave_port --manager-port=$manager_port --no-manager --sleep=10", "tests were successful");
240 241
}

242 243 244
# Start the server if we are going to run any of the benchmarks

if (!$opt_no_test)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
245
{
monty@work.mysql.com's avatar
monty@work.mysql.com committed
246
  my $extra;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
247 248 249 250
  safe_cd($test_dir);
  log_system("./bin/mysqladmin --no-defaults -u root -S $mysql_unix_port -s shutdown") || info("There was no mysqld running\n");
  sleep(2);
  log_system("rm -f  ./data/mysql/*");
251
  check_system("scripts/mysql_install_db --no-defaults --skip-locking","https://order");
monty@work.mysql.com's avatar
monty@work.mysql.com committed
252 253 254 255 256 257 258 259 260
  $extra="";
  if ($opt_bdb)
  {
    $extra.=" -O bdb_cache_size=16M";
  }
  if ($opt_innodb)
  {
    $extra.=" --innodb_data_file_path=ibdata1:100M";
  }
261
  safe_system("./bin/mysqld --no-defaults --basedir . --datadir ./data --skip-locking $extra >> $log 2>&1 &");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
262 263 264
  sleep(2);
}

265

266
if ($opt_stage <= 7 && $opt_perl_files && !$opt_no_perl && !$opt_no_test)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{
  safe_cd($test_dir);
  rm_all("perl");
  safe_system("mkdir perl");
  $ENV{'IN_MYSQL_DISTRIBUTION'}=1;
  $ENV{'MYSQL_BUILD'}=$test_dir;

  foreach $module (split(/,/,$opt_perl_files))
  {
    my $options;
    safe_cd("$test_dir/perl");
    if ($opt_debug)
    {
      safe_system("gunzip < $pwd/$module | tar xvf -");
    }
    else
    {
      safe_system("gunzip < $pwd/$module | tar xf -");
    }
    $module =~ m|([^/]+)\.tar\.gz|;
    $module = $1;
    safe_cd($module);
    $options="";
    $options= "--mysql-install --noprompt --mysql-incdir=$test_dir/include --mysql-libdir=$test_dir/lib -nomsql-install -nomsql1-install --mysql-test-db=test $opt_dbd_options" if ($module =~ /Msql-Mysql/);
    $options.= " PREFIX=$pwd/$host INSTALLPRIVLIB=$pwd/$host/perl5 INSTALLSCRIPT=$pwd/$host/bin INSTALLSITELIB=$pwd/$host/perl5/site_perl INSTALLBIN=$pwd/$host/bin INSTALLMAN1DIR=$pwd/$host/man INSTALLMAN3DIR=$pwd/$host/man/man3" if ($opt_local_perl);
    $options.= " $opt_perl_options" if (defined($opt_perl_options));
    safe_system($opt_static_perl ? "perl Makefile.PL -static $options" : "perl Makefile.PL $options");
    safe_system("$make ; $sur $make install");
  }
}


299
if ($opt_stage <= 8 && !$opt_no_test && !$opt_no_crash_me)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
300 301 302
{
  safe_cd("$test_dir/sql-bench");
  log_system("rm -f limits/mysql.cfg");
303
  safe_system("perl ./crash-me --force --batch-mode $connect_option");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
304 305
}

306
if ($opt_stage <= 9 && !$opt_no_test)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
307 308 309 310
{
  safe_cd("$test_dir/sql-bench");
  log_system("rm -f output/*");
  $tmp= $opt_fast_benchmark ? "--fast --user root --small-test" : "";
311
  check_system("perl ./run-all-tests --log --die-on-errors $connect_option $tmp","RUN-mysql");
monty@work.mysql.com's avatar
monty@work.mysql.com committed
312 313 314 315 316 317 318 319
  if ($opt_bdb)
  {
    check_system("perl ./run-all-tests --log --suffix=\"_bdb\" --die-on-errors $connect_option $tmp --create-option=\"type=bdb\"","RUN-mysql");
  }
  if ($opt_innodb)
  {
    check_system("perl ./run-all-tests --log --suffix=\"_innodb\" --die-on-errors $connect_option $tmp --create-option=\"type=innodb\"","RUN-mysql");
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
320 321
}

322
if ($opt_stage <= 10 && $opt_result)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
323 324 325 326
{
  safe_system("rcp $pwd/$host/*.tar.gz $opt_result");
}

monty@work.mysql.com's avatar
monty@work.mysql.com committed
327 328 329 330
rm_all($bench_tmpdir);
rm_all("$opt_tmp") if ($new_opt_tmp);

log_system("$pwd/$host/bin/mysqladmin --no-defaults -S $mysql_unix_port -u root shutdown");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
331 332 333 334 335 336 337 338 339 340
print LOG "ok\n";
close LOG;
print "$host: ok\n";

exit 0;


sub usage
{
print <<EOF;
341
$0  version 1.4
bk@work.mysql.com's avatar
bk@work.mysql.com committed
342 343 344 345 346 347

$0 takes the following options:

--help or --Information
Show this help

monty@work.mysql.com's avatar
monty@work.mysql.com committed
348 349 350 351 352 353
--innodb
Compile with support for Innodb tables

--bdb
Compile with support for Berkeley DB tables

bk@work.mysql.com's avatar
bk@work.mysql.com committed
354
--user 'user_name'
355
Mail 'user_name'\@mysql.com if something went wrong.
monty@work.mysql.com's avatar
monty@work.mysql.com committed
356
If user is empty then no mail is sent.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

--distribution 'distribution_file'
Name of the MySQL distribution file.

--result 'result_dir'
Send binary distribution to result_dir

--no-delete
Do not delete the distribution file.

--perl-files=list of files
Compile and install the given perl modules.

--no-test
Don not run crash-me or the benchmarks.

--config-options 'some extra configure options'
To add some extra options, like '--with-perl=yes'

--config-env 'environment for configure'
To set up the environment, like 'CC=cc CXX=gcc CXXFLAGS=-O3'

--dbd-options 'options'
Options for Makefile.PL when configuring msql-mysql-modules.

382 383 384
--make-options 'options'
Options to make after configure. (Like 'CXXLD=gcc')

385
--version-suffix suffix
386
Can be used to set a suffix (normally 'com' or '-max') for a distribution
387

bk@work.mysql.com's avatar
bk@work.mysql.com committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
--with-low-memory
Use less memory when compiling.

-- stage (1-6)
Start script from some specific point.

-- debug
Print all shell commands on stdout.
EOF
exit 1;
} 

sub abort
{
  my($message)=@_;
  my($command,$mail_header_file);
  print LOG "\n$message\n";
  print "$host: $message\n" if ($opt_debug);
  close LOG;

  if ($opt_user)
  {
    $mail_header_file="$opt_tmp/do-command.$$";
    open(TMP,">$mail_header_file");
monty@work.mysql.com's avatar
monty@work.mysql.com committed
412
    print TMP "From: mysql\@$full_host_name\n";
bk@work.mysql.com's avatar
bk@work.mysql.com committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    print TMP "To: $email\n";
    print TMP "Subject: MySQL compilation failed again\n\n";
    close TMP;
    $command=($opt_rsh_mail) ? "$rsh analytik" : "";
    system("tail -30 $log > $log.mail");
    system("cat $mail_header_file $log.mail | $command $sendmail -t -f $email");
    unlink($mail_header_file);
    unlink("$log.mail");
  }
  print LOG "Aborting\n";
  exit 1;
}

sub info
{
  my($message)=@_;
  print LOG "$message\n";
  print "$host: $message\n";
}

sub log_system
{
  my($com)=@_;
  print "$host: $com\n" if ($opt_debug);
  if (defined($log))
  {
    print LOG "$com\n";
    system("$com >> $log 2>&1") &&
      print LOG ("Info: couldn't execute command, error: " . ($? / 256) ."\n");
  }
  else
  {
    system($com) && print "$host: Couldn't execute command, error: " . ($? / 256) ."\n";
  }
}


sub safe_system
{
  my($com,$res)=@_;
  print LOG "$com\n";
  print "$host: $com\n" if ($opt_debug);
  system("$com >> $log 2>&1") && abort("error: Couldn't execute command, error: " . ($? / 256));
}

sub check_system
{
  my($com,$res)=@_;
  my ($error,$found);
  print LOG "$com\n";
  print "$host: $com\n" if ($opt_debug);
  open (COM, "$com 2>&1 < /dev/null|") || abort("Got error " . ($?/256) ." opening pipe");
  $found=0;
  while (<COM>)
  {
    print LOG $_;
    if (index($_,$res) >= 0)
    {
      $found=1;
      last;
    }
  }
  close COM;
  abort("Couldn't find '$res' in the command result") if (!$found);
  print "$host: Command ok\n" if ($opt_debug);
}

sub safe_cd
{
  my($dir)=@_;
  print LOG "cd $dir\n";
  print "$host: cd $dir\n" if ($opt_debug);
  chdir($dir) || abort("Can't cd to $dir");
}

sub which
{
  my(@progs)=@_;
  foreach $prog (@progs)
  {
    chomp($found=`which $prog | head -1`);
    if ($? == 0 && $found ne "" && index($found," ") == -1)
    {
      $found =~ s|/+|/|g;	# Make nicer output
      return $found;
    }
  }
  return undef();
}

sub find
{
  my (@progs)=@_;
  foreach $prog (@progs)
  {
    return $prog if (-x $prog);
  }
  return undef();  
}

#
# Remove recursively all from a directory
# This is needed because problems with NFS and open files
#

sub rm_all
{
  my(@rm_files)=@_;
  my($dir,$current_dir,@files,@dirs);
  $current_dir = `pwd`; chomp($current_dir);

  foreach $dir (@rm_files)
  {
    if (-d $dir)
    {
      chdir($dir) || abort("Can't cd to $dir");
      print "$host: Removing from $dir\n" if ($opt_debug);
      while (<* .*>)
      {
	next if ($_ eq "." x (length($_)));
	if (-d $_)
	{
#	  die "Can't remove directory that starts with ." if ($_ =~ /^\./ && $_ ne ".libs"); # Safety
	  push (@dirs,$_);
	}
	else
	{
	  push (@files,$_);
	}
      }
      if ($#files >= 0)
      {
	system("rm -f " . join(" ",@files)) && abort("Can't remove files from $dir");
      }
      foreach $dir (@dirs)
      {
	rm_all($dir);
      }
      chdir($current_dir) || abort("Can't cd to $current_dir");
      log_system("rmdir $dir");
    }
    else
    {
      system("rm -f $dir") && abort("Can't remove file $dir");
    }
  }
}
560 561 562 563

sub kill_all
{
  my ($pattern) = @_;
564
  my ($USER,$BSD,$LINUX, $pscmd, $user, $pid);
565
  $user=$ENV{'USER'};
566
  $BSD = -f '/vmunix' || $ENV{"OS"} eq "SunOS4" || $^O eq 'darwin';
567 568 569
  $LINUX = $^O eq 'linux';
  $pscmd = $BSD ? "/bin/ps -auxww" : $LINUX ? "/bin/ps axuw" : "/bin/ps -ef";

570 571 572 573 574
  if (!open(PS, "$pscmd|"))
  {
    print "Warning: Can't run $pscmd: $!\n";
    exit;
  }
575 576 577 578 579 580 581 582

  # Catch any errors with eval.  A bad pattern, for instance.
 process:
  while ($cand = <PS>)
  {
    chop($cand);
    ($pid_user, $pid) = split(' ', $cand);
    next if $pid == $$;
583 584
    next process if (! ($cand =~ $pattern) || $pid_user ne $user);
    print LOG "Killing $_\n";
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    &killpid($pid);
  }
}

sub killpid
{
  local($pid) = @_;
  kill 15, $pid;
  for (1..5)
  {
    sleep 2;
    return if kill(0, $pid) == 0;
  }
  kill 9, $pid;
  for (1..5) {
    sleep 2;
    return if kill(0, $pid) == 0;
  }
  print LOG "$pid will not die!\n";
}