| 1 |
#script executed on the first reserved node |
| 2 |
# Exit codes are: |
| 3 |
# - 0: everything worked normally |
| 4 |
# - 1: prologue error (exit value not equal to 0 or timeout exceeded) |
| 5 |
# - 2: epilogue error (exit value not equal to 0 or timeout exceeded) |
| 6 |
# - 3: this script was killed by Leon normally |
| 7 |
# - 4: this script was killed by Leon normally and epilogue was in error (like for 2) |
| 8 |
# - 5: this script was not able to create the node file |
| 9 |
# - 6: this script was not able to create the pid file |
| 10 |
# - 7: cannot get the shell value for the user |
| 11 |
# - 8: this script was not able to create tmp directory |
| 12 |
# - 12: cannot go into the launching directory of the job and epilogue was in error |
| 13 |
# - 10: cannot go into the launching directory of the job |
| 14 |
# - 20: cannot create STDOUT and STDERR files |
| 15 |
# - 22: cannot create STDOUT and STDERR files and epilogue was in error |
| 16 |
# - 30: timeout of the bipbip hashtable send |
| 17 |
# - 33: oarexec was stopped normally via USR1 signal but there was an epilogue error |
| 18 |
# - 34: all worked normally and this is the result of an USR1 signal |
| 19 |
# - 40: all worked normally and the user process received the checkpoint signal |
| 20 |
# - 41: oarexec has sent the checkpoint signal but there was an epilogue error |
| 21 |
# - 42: oarexec has sent the user defined signal (oardel -s) |
| 22 |
# |
| 23 |
# The exit code is sent to the Almighty via its TCP socket |
| 24 |
# |
| 25 |
# oarexec read a hashtable from bipbip, execute the prologue and then close STD file descriptor + fork a child and kill itself. |
| 26 |
# So the SSH return just after job initialisation and prologue |
| 27 |
|
| 28 |
BEGIN{ |
| 29 |
if (!defined(&OAR::Tools::get_all_process_children())){ |
| 30 |
require(OAR::Tools); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
use strict; |
| 35 |
use Sys::Hostname; |
| 36 |
use POSIX qw(:signal_h :errno_h :sys_wait_h strftime); |
| 37 |
|
| 38 |
my $Old_umask = sprintf("%lo",umask()); |
| 39 |
umask(oct("022")); |
| 40 |
|
| 41 |
$SIG{PIPE} = 'IGNORE'; |
| 42 |
$SIG{HUP} = 'IGNORE'; |
| 43 |
# To deblock sshd father when oardel in suspended state |
| 44 |
$SIG{CONT} = sub{ |
| 45 |
kill('CONT',getppid()); |
| 46 |
}; |
| 47 |
|
| 48 |
my $Job_id = shift; |
| 49 |
|
| 50 |
$| = 1; |
| 51 |
# block signals until we define the handlers |
| 52 |
my $sigset = POSIX::SigSet->new; |
| 53 |
my $Blockset = POSIX::SigSet->new(SIGINT, SIGTERM, SIGQUIT, SIGCHLD, SIGUSR2, SIGUSR1); |
| 54 |
sigprocmask(SIG_BLOCK, $Blockset, $sigset); |
| 55 |
|
| 56 |
my $Bin_Path = $ENV{OARDIR}."/"; |
| 57 |
my $Tmp_file_prefix; |
| 58 |
|
| 59 |
my $Debug_mode; |
| 60 |
my $Node_file; |
| 61 |
my $Res_file; |
| 62 |
my $Pid_file; |
| 63 |
my $Kill_myself = 0; |
| 64 |
my $Oarexec_exit_code = 0; |
| 65 |
my $Exit_script_code = 'N'; |
| 66 |
|
| 67 |
# Run script: prologue, epilogue or passive job hook |
| 68 |
sub run_script($$$) { |
| 69 |
my $script_name = shift; |
| 70 |
my $script_cmd = shift; |
| 71 |
my $script_timeout = shift; |
| 72 |
my $script_error = -1; |
| 73 |
eval { |
| 74 |
my $pid; |
| 75 |
$SIG{ALRM} = sub { kill(9,$pid); die "alarm\n" }; |
| 76 |
print("[oarexec] LAUNCH $script_name: $script_cmd\n"); |
| 77 |
alarm($script_timeout); |
| 78 |
my $sigchld_handler = $SIG{CHLD}; |
| 79 |
$SIG{CHLD} = 'DEFAULT'; |
| 80 |
$pid = fork(); |
| 81 |
if($pid == 0){ |
| 82 |
#CHILD |
| 83 |
$SIG{ALRM} = 'DEFAULT'; |
| 84 |
exec($script_cmd); |
| 85 |
warn("[ERROR] cannot execute $script_name\n"); |
| 86 |
exit(-1); |
| 87 |
} |
| 88 |
my $wait_res = -1; |
| 89 |
# Avoid being disturbed by a signal |
| 90 |
while ((defined($pid)) and ($wait_res != $pid)){ |
| 91 |
$wait_res = waitpid($pid,0); |
| 92 |
$script_error = $?; |
| 93 |
} |
| 94 |
$SIG{CHLD} = $sigchld_handler; |
| 95 |
alarm(0); |
| 96 |
print("[oarexec] END $script_name\n"); |
| 97 |
}; |
| 98 |
return ($script_error, $@); |
| 99 |
} |
| 100 |
|
| 101 |
#Notify Almighty. |
| 102 |
#arg1 --> exit value |
| 103 |
sub quit_oarexec($$){ |
| 104 |
my $exit_value = shift; |
| 105 |
my $job_data = shift; |
| 106 |
|
| 107 |
if ($job_data->{detach_oarexec} == 1){ |
| 108 |
# We must contact Almighty |
| 109 |
my $max_wait_time = 900; |
| 110 |
my $wait_time = 30; |
| 111 |
my $max_retry = 48; |
| 112 |
my $nb_retry = 0; |
| 113 |
|
| 114 |
while (1){ |
| 115 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); |
| 116 |
my $string = sprintf("%02d:%02d:%02d", $hour,$min,$sec); |
| 117 |
if (!defined(OAR::Tools::notify_tcp_socket($job_data->{almighty_hostname},$job_data->{almighty_port},"OAREXEC_$job_data->{job_id}"."_$exit_value"."_$Exit_script_code"."_$job_data->{challenge}"))){ |
| 118 |
if ((defined($job_data->{cpuset_full_path})) and ($nb_retry < $max_retry)){ |
| 119 |
print("[oarexec] [$string] notified Almighty with exit value $exit_value and retry in $max_wait_time seconds if not killed by the cpuset_manager before\n"); |
| 120 |
$wait_time = $max_wait_time; |
| 121 |
}else{ |
| 122 |
print("[oarexec] [$string] notified Almighty with exit value $exit_value and exit\n"); |
| 123 |
exit($exit_value); |
| 124 |
} |
| 125 |
$nb_retry++; |
| 126 |
}else{ |
| 127 |
print("[oarexec] cannot notify Almighty: server down or network error\n"); |
| 128 |
} |
| 129 |
sleep($wait_time); |
| 130 |
if ($wait_time < $max_wait_time){ |
| 131 |
$wait_time = 2 * $wait_time; |
| 132 |
if ($wait_time > $max_wait_time){ |
| 133 |
$wait_time = $max_wait_time; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
exit($exit_value); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#delete temporary file |
| 145 |
sub clean_all(){ |
| 146 |
#system("rm -f $OAR_FILE_NODES $oarPidFile"); |
| 147 |
unlink($Node_file,$Res_file,$Pid_file,OAR::Tools::get_oar_user_signal_file_name($Job_id)); |
| 148 |
# Kill oarsub connections |
| 149 |
my $file = OAR::Tools::get_oarsub_connections_file_name($Job_id); |
| 150 |
if (open(OARSUBS,$file)){ |
| 151 |
while ($_ = <OARSUBS>){ |
| 152 |
if ($_ =~ m/^(\d+)$/m){ |
| 153 |
my $ppid = $1; |
| 154 |
my ($children,$cmd_name) = OAR::Tools::get_one_process_children($ppid); |
| 155 |
my @pid_list = @{$children}; |
| 156 |
if ($cmd_name =~ /OAR_JOBID=$Job_id\D/m){ |
| 157 |
# remove ppid |
| 158 |
print("[oarexec] kill oarsub processes: @pid_list\n"); |
| 159 |
system({"oardodo"} "oardodo","kill","-9",@pid_list) if ($#pid_list >= 0); |
| 160 |
}else{ |
| 161 |
print("[oarexec] want to kill oarsub connections @pid_list BUT it seems there is no OAR oarsub processes\n"); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
close(OARSUBS); |
| 166 |
} |
| 167 |
unlink($file); |
| 168 |
|
| 169 |
# Delete unwanted children |
| 170 |
my ($children,$cmd_name) = OAR::Tools::get_one_process_children($$); |
| 171 |
shift(@{$children}); |
| 172 |
kill(9,@{$children}); |
| 173 |
} |
| 174 |
|
| 175 |
#kill all child of the pid |
| 176 |
# arg1 --> child pid |
| 177 |
sub kill_children($){ |
| 178 |
my $child_pid = shift; |
| 179 |
|
| 180 |
my ($children, $cmd_name) = OAR::Tools::get_one_process_children($child_pid); |
| 181 |
system({"oardodo"} "oardodo","kill","-9",@{$children}); |
| 182 |
} |
| 183 |
|
| 184 |
sub send_kill_signal_to_myself($){ |
| 185 |
my $signal_to_send = shift; |
| 186 |
|
| 187 |
my $father = $$; |
| 188 |
my $pid=fork; |
| 189 |
if($pid==0){ |
| 190 |
sleep(5); |
| 191 |
kill($signal_to_send, $father); |
| 192 |
exit(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
# Append a string to a file owned by a user |
| 197 |
sub append_string_to_user_file($$$$){ |
| 198 |
my ($user, $file, $dir, $str) = @_; |
| 199 |
|
| 200 |
$str = "## OAR [".strftime("%F %T",localtime())."] $str ##"; |
| 201 |
system({"bash"} "bash","-c","echo '$str' | OARDO_BECOME_USER=$user oardodo bash --noprofile --norc -c \"cd '$dir' && tee -a '$file'\""); |
| 202 |
} |
| 203 |
|
| 204 |
# Retrieve job information from bipbip |
| 205 |
my $Job; |
| 206 |
my $tmp = ""; |
| 207 |
eval { |
| 208 |
$SIG{ALRM} = sub { die "alarm\n" }; |
| 209 |
alarm(OAR::Tools::get_bipbip_ssh_hashtable_send_timeout()); |
| 210 |
while (<STDIN>){ |
| 211 |
$tmp .= $_; |
| 212 |
} |
| 213 |
$Job = eval($tmp); |
| 214 |
alarm(0); |
| 215 |
}; |
| 216 |
if( $@ ){ |
| 217 |
print("[oarexec] timeout in the job hashtable retrieval\n"); |
| 218 |
exit(30); |
| 219 |
} |
| 220 |
if (!defined($Job->{job_id})){ |
| 221 |
print("[oarexec] received dump\n"); |
| 222 |
print("[oarexec] $tmp\n"); |
| 223 |
print("[oarexec] end of dump\n"); |
| 224 |
print("[oarexec] invalid job hashtable transfered\n"); |
| 225 |
exit(31); |
| 226 |
} |
| 227 |
|
| 228 |
my $host = hostname(); |
| 229 |
|
| 230 |
print("[oarexec] user: $Job->{job_user}, launch directory: $Job->{launching_directory}\n"); |
| 231 |
|
| 232 |
OAR::Tools::set_default_oarexec_directory($Job->{tmp_directory}); |
| 233 |
$Tmp_file_prefix = OAR::Tools::get_default_oarexec_directory(); |
| 234 |
# Test if OAR directory exists and is owned by oar or if we can create it |
| 235 |
if (!(((-d $Tmp_file_prefix) and (-O $Tmp_file_prefix)) or (mkdir($Tmp_file_prefix)))){ |
| 236 |
print("[oarexec] directory $Tmp_file_prefix does not exist, cannot create it\n"); |
| 237 |
exit(8); |
| 238 |
} |
| 239 |
chdir($Tmp_file_prefix); |
| 240 |
|
| 241 |
#create node set file |
| 242 |
$Node_file = "$Tmp_file_prefix/$Job_id"; |
| 243 |
if (! open(FILE,">$Node_file")){ |
| 244 |
print("[oarexec] cannot write file $Node_file\n"); |
| 245 |
exit(5); |
| 246 |
} |
| 247 |
#Feed the node file |
| 248 |
my @tmp_res; |
| 249 |
my %tmp_already_there; |
| 250 |
foreach my $r (@{$Job->{resources}}){ |
| 251 |
if (($r->{$Job->{node_file_db_fields}} ne "") and ($r->{type} eq "default")){ |
| 252 |
if (($r->{$Job->{node_file_db_fields_distinct_values}} ne "") and (!defined($tmp_already_there{$r->{$Job->{node_file_db_fields_distinct_values}}}))){ |
| 253 |
push(@tmp_res, $r->{$Job->{node_file_db_fields}}); |
| 254 |
$tmp_already_there{$r->{$Job->{node_file_db_fields_distinct_values}}} = 1; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
foreach my $f (sort(@tmp_res)){ |
| 259 |
print(FILE "$f\n") or exit(5); |
| 260 |
} |
| 261 |
close(FILE); |
| 262 |
|
| 263 |
#create resource set file |
| 264 |
$Res_file = "$Tmp_file_prefix/$Job_id"."_resources"; |
| 265 |
if (! open(FILE,">$Res_file")){ |
| 266 |
print("[oarexec] cannot write file $Res_file\n"); |
| 267 |
exit(5); |
| 268 |
} |
| 269 |
#Feed the resource file |
| 270 |
foreach my $r (@{$Job->{resources}}){ |
| 271 |
my $line = ""; |
| 272 |
foreach my $p (keys(%{$r})){ |
| 273 |
if(OAR::Tools::check_resource_system_property($p) != 1){ |
| 274 |
$r->{$p} = "" if (!defined($r->{$p})); |
| 275 |
$line .= " $p = '$r->{$p}' ," |
| 276 |
} |
| 277 |
} |
| 278 |
chop($line); |
| 279 |
print(FILE "$line\n") or exit(5); |
| 280 |
} |
| 281 |
close(FILE); |
| 282 |
|
| 283 |
print("[oarexec] job command name: [$Job->{command}]\n"); |
| 284 |
|
| 285 |
if (defined($Job->{prologue})){ |
| 286 |
# Launch prologue script |
| 287 |
my ($script_error, $eval_error) = run_script("prologue", "$Job->{prologue} $Job_id $Job->{job_user} $Node_file $Job->{walltime_seconds}", $Job->{pro_epi_timeout}); |
| 288 |
if( $eval_error || ($script_error != 0)){ |
| 289 |
print("[oarexec] prologue error: $eval_error; return code = $script_error\n"); |
| 290 |
clean_all(); |
| 291 |
exit(1); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
# Get user shell |
| 296 |
my @pass_info = getpwnam($Job->{job_user}); |
| 297 |
my $shell = $pass_info[8]; |
| 298 |
if (!defined($shell)){ |
| 299 |
clean_all(); |
| 300 |
print("[oarexec] error: user $Job->{job_user} does not exist on this node, $host\n"); |
| 301 |
exit(7); |
| 302 |
} |
| 303 |
my $uid = $pass_info[2]; |
| 304 |
|
| 305 |
my @cmd; |
| 306 |
my $stdin_script_to_send; |
| 307 |
my $oardo_become_user = $Job->{job_user}; |
| 308 |
if ( $Job->{mode} eq "PASSIVE" ){ |
| 309 |
my $str; |
| 310 |
($str, $stdin_script_to_send) = OAR::Tools::get_oarexecuser_perl_script_for_oarexec($Node_file,$Job_id,$Job->{array_id},$Job->{array_index},$Job->{user},$shell,$Job->{launching_directory},$Job->{stdout_file},$Job->{stderr_file},$Res_file,$Job->{name},$Job->{project},$Job->{walltime},$Job->{walltime_seconds},$Job->{job_env},$Job->{command}); |
| 311 |
@cmd = ("oardodo","perl","-e",$str); |
| 312 |
if ((defined($Job->{types}->{deploy}) or defined($Job->{types}->{cosystem})) and defined($Job->{deploy_cosystem_job_exec_system})) { |
| 313 |
if ($Job->{deploy_cosystem_job_exec_system} eq "systemd-run") { |
| 314 |
$oardo_become_user = ""; |
| 315 |
@cmd = ("oardodo","systemd-run","-q","--uid=$uid","--scope","--collect","--slice=user-$uid.slice","perl","-e",$str); |
| 316 |
} elsif ($Job->{deploy_cosystem_job_exec_system} eq "none") { |
| 317 |
# Use regular command by default |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#resolve terminal type problems |
| 323 |
my $terminal = $ENV{TERM}; |
| 324 |
if (($terminal ne "") and ($terminal ne "unknown")){ |
| 325 |
$ENV{TERM} = $terminal; |
| 326 |
}else{ |
| 327 |
$ENV{TERM} = "xterm"; |
| 328 |
} |
| 329 |
|
| 330 |
#oar own the tty |
| 331 |
#so we must change owner for the user |
| 332 |
system({"bash"} "bash","-c","TTY=\$(tty) && test -e \$TTY && oardodo chown $Job->{job_user}:oar \$TTY && oardodo chmod 660 \$TTY"); |
| 333 |
|
| 334 |
# pipe for notify the end of a child process |
| 335 |
pipe(pipe_child_read,pipe_child_write); |
| 336 |
autoflush pipe_child_write 1; |
| 337 |
autoflush pipe_child_read 1; |
| 338 |
|
| 339 |
sub child_signal_handler { |
| 340 |
$SIG{CHLD} = \&child_signal_handler; |
| 341 |
|
| 342 |
my $wait_pid_ret ; |
| 343 |
while (($wait_pid_ret = waitpid(-1,WNOHANG)) > 0){ |
| 344 |
my $exit_value = $? >> 8; |
| 345 |
print(pipe_child_write "$wait_pid_ret $exit_value\n"); |
| 346 |
} |
| 347 |
} |
| 348 |
$SIG{CHLD} = \&child_signal_handler; |
| 349 |
|
| 350 |
#For kill signal |
| 351 |
pipe(pipe_kill_read,pipe_kill_write); |
| 352 |
autoflush pipe_kill_write 1; |
| 353 |
autoflush pipe_kill_read 1; |
| 354 |
|
| 355 |
sub user_defined_signal_handler { |
| 356 |
$SIG{URG} = \&user_defined_signal_handler; |
| 357 |
|
| 358 |
print("[oarexec] user defined signal handler of @_\n"); |
| 359 |
my $signal; |
| 360 |
open(FILE, OAR::Tools::get_oar_user_signal_file_name($Job_id)); |
| 361 |
while (<FILE>) { |
| 362 |
$signal = $_; |
| 363 |
} |
| 364 |
close(FILE); |
| 365 |
print(pipe_kill_write "SIGNAL_$signal\n"); |
| 366 |
} |
| 367 |
$SIG{URG} = \&user_defined_signal_handler; |
| 368 |
|
| 369 |
sub kill_signal_handler { |
| 370 |
$SIG{TERM} = \&kill_signal_handler; |
| 371 |
$SIG{INT} = \&kill_signal_handler; |
| 372 |
$SIG{QUIT} = \&kill_signal_handler; |
| 373 |
|
| 374 |
print("[oarexec] kill signal handler of @_\n"); |
| 375 |
print(pipe_kill_write "KILL\n"); |
| 376 |
} |
| 377 |
|
| 378 |
$SIG{TERM} = \&kill_signal_handler; |
| 379 |
$SIG{INT} = \&kill_signal_handler; |
| 380 |
$SIG{QUIT} = \&kill_signal_handler; |
| 381 |
|
| 382 |
sub stop_signal_handler { |
| 383 |
$SIG{USR1} = \&stop_signal_handler; |
| 384 |
|
| 385 |
print("[oarexec] stop signal handler of @_\n"); |
| 386 |
print(pipe_kill_write "STOP\n"); |
| 387 |
} |
| 388 |
|
| 389 |
$SIG{USR1} = \&stop_signal_handler; |
| 390 |
|
| 391 |
sub checkpoint_signal_handler { |
| 392 |
$SIG{USR2} = \&checkpoint_signal_handler; |
| 393 |
|
| 394 |
print("[oarexec] checkpoint signal handler of @_\n"); |
| 395 |
print(pipe_kill_write "CHECKPOINT\n"); |
| 396 |
} |
| 397 |
|
| 398 |
$SIG{USR2} = \&checkpoint_signal_handler; |
| 399 |
|
| 400 |
if ($Job->{detach_oarexec} == 1){ |
| 401 |
#Detach process from bipbip SSH |
| 402 |
print("[oarexec] detaching the oarexec process\n"); |
| 403 |
|
| 404 |
if(fork() != 0){ |
| 405 |
print(OAR::Tools::get_bipbip_oarexec_rendez_vous()); |
| 406 |
# Exit from main oarexec |
| 407 |
exit(0); |
| 408 |
} |
| 409 |
# We must redirect STD to close SSH |
| 410 |
open(STDIN, "/dev/zero"); |
| 411 |
if ($Job->{debug_mode} > 0){ |
| 412 |
open(STDOUT, ">>$Tmp_file_prefix/oar.log"); |
| 413 |
open(STDERR, ">>$Tmp_file_prefix/oar.log"); |
| 414 |
}else{ |
| 415 |
open(STDOUT, ">/dev/null"); |
| 416 |
open(STDERR, ">/dev/null"); |
| 417 |
} |
| 418 |
}else{ |
| 419 |
print(OAR::Tools::get_bipbip_oarexec_rendez_vous()); |
| 420 |
} |
| 421 |
|
| 422 |
#Write file with this oarexec pid |
| 423 |
$Pid_file = OAR::Tools::get_oar_pid_file_name($Job_id); |
| 424 |
if ((!open(FILEPID,">$Pid_file")) or (!print(FILEPID "$$"))){ |
| 425 |
print("[oarexec] cannot write file $Pid_file\n"); |
| 426 |
clean_all(); |
| 427 |
quit_oarexec(6,$Job); |
| 428 |
} |
| 429 |
close(FILEPID); |
| 430 |
|
| 431 |
sigprocmask(SIG_UNBLOCK, $Blockset); |
| 432 |
|
| 433 |
my $pid = $$; |
| 434 |
my $rin_script = ''; |
| 435 |
if ($Job->{mode} eq "PASSIVE"){ |
| 436 |
# For parsing script outputs |
| 437 |
pipe(pipe_script_read,pipe_script_write); |
| 438 |
autoflush pipe_script_write 1; |
| 439 |
autoflush pipe_script_read 1; |
| 440 |
vec($rin_script,fileno(pipe_script_read),1) = 1; |
| 441 |
# To send command line |
| 442 |
pipe(pipe_stdin_read,pipe_stdin_write); |
| 443 |
autoflush pipe_stdin_write 1; |
| 444 |
autoflush pipe_stdin_read 1; |
| 445 |
#print("[oarexec] Launch the command: $cmd\n"); |
| 446 |
$pid = fork; |
| 447 |
if($pid == 0){ |
| 448 |
#CHILD |
| 449 |
# Run the job script hook if any |
| 450 |
if (defined($Job->{passive_job_hook}) && -x $Job->{passive_job_hook}) { |
| 451 |
my ($script_error, $eval_error) = run_script("passive job hook", "oardodo $Job->{passive_job_hook} \$PPID,\$\$:$Job->{user}:$Job_id:$Job->{name}:$Job->{project}:$Job->{walltime_seconds}", $Job->{pro_epi_timeout}); |
| 452 |
if ($eval_error || ($script_error != 0)){ |
| 453 |
warn("[oarexec] passive job hook error: $eval_error; return code = $script_error\n"); |
| 454 |
exit(-1); |
| 455 |
} |
| 456 |
} |
| 457 |
# Setup the environment for the job script execution |
| 458 |
$SIG{CHLD} = 'DEFAULT'; |
| 459 |
$SIG{TERM} = 'DEFAULT'; |
| 460 |
$SIG{INT} = 'DEFAULT'; |
| 461 |
$SIG{QUIT} = 'DEFAULT'; |
| 462 |
$SIG{USR1} = 'DEFAULT'; |
| 463 |
$SIG{USR2} = 'DEFAULT'; |
| 464 |
$SIG{PIPE} = 'DEFAULT'; |
| 465 |
$SIG{HUP} = 'DEFAULT'; |
| 466 |
|
| 467 |
print("[oarexec] child exec: @cmd\n"); |
| 468 |
close(pipe_script_read); |
| 469 |
close(STDOUT); |
| 470 |
# Redirect script output into the pipe |
| 471 |
open(STDOUT, ">& pipe_script_write"); |
| 472 |
|
| 473 |
# Use the child STDIN to send the user command |
| 474 |
close(pipe_stdin_write); |
| 475 |
close(STDIN); |
| 476 |
open(STDIN, "<& pipe_stdin_read"); |
| 477 |
|
| 478 |
$ENV{OARDO_BECOME_USER} = $oardo_become_user; |
| 479 |
umask(oct($Old_umask)); |
| 480 |
exec(@cmd); |
| 481 |
warn("[ERROR] Cannot find @cmd\n"); |
| 482 |
exit(-1); |
| 483 |
} |
| 484 |
close(pipe_script_write); |
| 485 |
close(pipe_stdin_read); |
| 486 |
# Send user command without any interpretation security isssue!!! |
| 487 |
#print("[oarexec] ########### $stdin_script_to_send\n"); |
| 488 |
# This is the most secure manner that I find |
| 489 |
print(pipe_stdin_write $stdin_script_to_send."\n"); |
| 490 |
open(pipe_stdin_write,"<& /dev/null"); |
| 491 |
} |
| 492 |
|
| 493 |
print("[oarexec] child pid = $pid\n"); |
| 494 |
|
| 495 |
my $res_read; |
| 496 |
my $line_read; |
| 497 |
my $rin = ''; |
| 498 |
my $rin_sig = ''; |
| 499 |
my $rin_pipe = ''; |
| 500 |
vec($rin_sig,fileno(pipe_kill_read),1) = 1; |
| 501 |
vec($rin_pipe,fileno(pipe_child_read),1) = 1; |
| 502 |
$rin = $rin_sig | $rin_pipe | $rin_script; |
| 503 |
my $rin_tmp; |
| 504 |
my $Stop_signal = 0; |
| 505 |
my $Checkpoint_signal = 0; |
| 506 |
my $user_signal = 0; |
| 507 |
my $stop_loop = 0; |
| 508 |
my $user_cmd_pid = -1; |
| 509 |
# wait end of the child process or KILL notification |
| 510 |
while (($line_read != $pid) and ($Stop_signal == 0) and ($stop_loop == 0)){ |
| 511 |
print("[oarexec] wait end of child process or kill notification\n"); |
| 512 |
select($rin_tmp = $rin, undef, undef, undef); |
| 513 |
|
| 514 |
if (vec($rin_tmp, fileno(pipe_script_read), 1)){ |
| 515 |
($res_read,$line_read) = OAR::Tools::read_socket_line(\*pipe_script_read,1); |
| 516 |
if (($res_read == 1) and ($line_read eq "")){ |
| 517 |
# Script is finished |
| 518 |
close(pipe_script_read); |
| 519 |
close(pipe_stdin_write); |
| 520 |
}else{ |
| 521 |
print("[oarexec] PIPE writes: $res_read,$line_read\n"); |
| 522 |
if ($line_read =~ /^EXIT_CODE\s(\d+)$/){ |
| 523 |
$Exit_script_code = $1; |
| 524 |
print("OAREXEC_SCRIPT_EXIT_VALUE $Exit_script_code\n"); |
| 525 |
}elsif ($line_read =~ /^USER_CMD_PID\s(\d+)$/){ |
| 526 |
$user_cmd_pid = $1; |
| 527 |
print("[oarexec] user command PID = $user_cmd_pid\n"); |
| 528 |
} |
| 529 |
} |
| 530 |
}elsif (vec($rin_tmp, fileno(pipe_child_read), 1)){ |
| 531 |
($res_read,$line_read) = OAR::Tools::read_socket_line(\*pipe_child_read,1); |
| 532 |
print("[oarexec] PIPE reads: $res_read,$line_read\n"); |
| 533 |
if ($line_read =~ m/(\d+) (\d+)/m){ |
| 534 |
if ($1 == $pid){ |
| 535 |
print("[oarexec] reset CHLD signal handler\n"); |
| 536 |
$line_read = $pid; |
| 537 |
$Oarexec_exit_code = $2 * 10; |
| 538 |
} |
| 539 |
} |
| 540 |
}elsif (vec($rin_tmp, fileno(pipe_kill_read), 1)){ |
| 541 |
($res_read,$line_read) = OAR::Tools::read_socket_line(\*pipe_kill_read,1); |
| 542 |
print("[oarexec] pipe kill signal: $res_read,$line_read\n"); |
| 543 |
if ($line_read eq "KILL"){ |
| 544 |
print("[oarexec] kill children\n"); |
| 545 |
if ($Job->{mode} eq "INTERACTIVE"){ |
| 546 |
$stop_loop = 1; |
| 547 |
}else{ |
| 548 |
kill_children($pid); |
| 549 |
if (defined($Job->{stderr_file}) and ($Job->{stderr_file} ne "")){ |
| 550 |
append_string_to_user_file($Job->{job_user}, $Job->{stderr_file}, $Job->{launching_directory}, "Job $Job_id KILLED"); |
| 551 |
} |
| 552 |
} |
| 553 |
$Kill_myself = 1; |
| 554 |
}elsif ($line_read eq "STOP"){ |
| 555 |
print("[oarexec] received STOP notification\n"); |
| 556 |
if ($Job->{mode} eq "INTERACTIVE"){ |
| 557 |
$Stop_signal = 1; |
| 558 |
}else{ |
| 559 |
print("[oarexec] Received USR1 signal: someone wants to finish the job but it is not INTERACTIVE\n"); |
| 560 |
} |
| 561 |
}elsif ($line_read eq "CHECKPOINT"){ |
| 562 |
#We must send the signal defined by oarsub to the child of $pid |
| 563 |
if ($user_cmd_pid >= 0){ |
| 564 |
print("[oarexec] checkpoint received, send signal $Job->{checkpoint_signal} to the pid $user_cmd_pid\n"); |
| 565 |
system({"oardodo"} "oardodo","kill","-s",$Job->{checkpoint_signal},$user_cmd_pid); |
| 566 |
if (defined($Job->{stderr_file}) and ($Job->{stderr_file} ne "")){ |
| 567 |
append_string_to_user_file($Job->{job_user}, $Job->{stderr_file}, $Job->{launching_directory}, "Job $Job_id CHECKPOINTED with signal $Job->{checkpoint_signal}"); |
| 568 |
} |
| 569 |
}else{ |
| 570 |
print("[oarexec] cannot find pid of user process, retry in 5 seconds\n"); |
| 571 |
send_kill_signal_to_myself('SIGUSR2'); |
| 572 |
} |
| 573 |
$Checkpoint_signal = 1; |
| 574 |
}elsif ($line_read =~ m/^SIGNAL_(.*)/){ |
| 575 |
my $signal = $1; |
| 576 |
#We must send $signal to the child of $pid |
| 577 |
if ($user_cmd_pid >= 0){ |
| 578 |
print("[oarexec] signal URG received, send signal $signal to the pid $user_cmd_pid\n"); |
| 579 |
system({"oardodo"} "oardodo","kill","-s",$signal,$user_cmd_pid); |
| 580 |
if (defined($Job->{stderr_file}) and ($Job->{stderr_file} ne "")){ |
| 581 |
append_string_to_user_file($Job->{job_user}, $Job->{stderr_file}, $Job->{launching_directory}, "Job $Job_id SIGNALED with $signal"); |
| 582 |
} |
| 583 |
}else{ |
| 584 |
print("[oarexec] cannot find pid of user process, will retry in 5 seconds\n"); |
| 585 |
send_kill_signal_to_myself('URG'); |
| 586 |
} |
| 587 |
$user_signal = 1; |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
$SIG{CHLD} = 'DEFAULT'; |
| 593 |
$SIG{TERM} = 'IGNORE'; |
| 594 |
$SIG{INT} = 'IGNORE'; |
| 595 |
$SIG{QUIT} = 'IGNORE'; |
| 596 |
$SIG{USR1} = 'DEFAULT'; |
| 597 |
$SIG{USR2} = 'DEFAULT'; |
| 598 |
close(pipe_script_read) if ($rin_script ne ''); |
| 599 |
close(pipe_child_write); |
| 600 |
close(pipe_child_read); |
| 601 |
close(pipe_kill_write); |
| 602 |
close(pipe_kill_read); |
| 603 |
|
| 604 |
print("[oarexec] job terminated\n"); |
| 605 |
|
| 606 |
if (defined($Job->{epilogue})){ |
| 607 |
# Launch epilogue script |
| 608 |
my ($script_error, $eval_error) = run_script("epilogue", "$Job->{epilogue} $Job_id $Job->{job_user} $Node_file", $Job->{pro_epi_timeout}); |
| 609 |
if( $eval_error || ($script_error != 0)){ |
| 610 |
print("[oarexec] epilogue error: $eval_error; return code = $script_error\n"); |
| 611 |
clean_all(); |
| 612 |
if ($Kill_myself == 1){ |
| 613 |
quit_oarexec(4,$Job); |
| 614 |
}elsif($Stop_signal == 1){ |
| 615 |
quit_oarexec(33,$Job); |
| 616 |
}elsif($Checkpoint_signal == 1){ |
| 617 |
quit_oarexec(41,$Job); |
| 618 |
}else{ |
| 619 |
quit_oarexec(2+$Oarexec_exit_code,$Job); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
clean_all(); |
| 625 |
if ($Kill_myself == 1){ |
| 626 |
quit_oarexec(3,$Job); |
| 627 |
}elsif($Stop_signal == 1){ |
| 628 |
quit_oarexec(34,$Job); |
| 629 |
}elsif($Checkpoint_signal == 1){ |
| 630 |
quit_oarexec(40,$Job); |
| 631 |
}elsif($user_signal == 1){ |
| 632 |
quit_oarexec(42,$Job); |
| 633 |
}else{ |
| 634 |
quit_oarexec(0+$Oarexec_exit_code,$Job); |
| 635 |
} |
| 636 |
|