This file is indexed.

/usr/share/arc/submit_common.sh is in nordugrid-arc-arex 5.4.2-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
######################################################
#Common functions for submit scripts
######################################################

# This script should not be executed directly but is sourced in
# from various backend scripts that itself are called from the
# grid manager. Its purpose is to prepare the runtime environments,
# which is almost the same procedure invariant of the backend
# used.
#
# To test the functionality of this script, you may source
# this script from the bourne command shell, i.e. 
# 
#	. /opt/nordugrid/libexec/submit_common.sh
# 
# and it should give no error. Then call the methods defined in
# this script directly. Also, you can test the parse_arg_file
# by executing
#
#	/opt/nordugrid/libexec/submit_common.sh <args>
#
# directly. More tests still require to be added.

sourcewithargs ()
{
script=$1
shift
. $script
}

init () {

   parse_arg_file $1

   if [ -z "$pkgdatadir" ]; then echo 'pkgdatadir must be set' 1>&2; exit 1; fi
   if [ -z "$joboption_lrms" ]; then echo 'joboption_lrms must be set' 1>&2; exit 1; fi

   # May use joboption_queue (set by parse_arg_file)
   . ${pkgdatadir}/configure-${joboption_lrms}-env.sh

   # Where runtime scripts can be found on computing nodes (empty if does not exist)
   RUNTIME_CONFIG_DIR=$CONFIG_runtimedir
   export RUNTIME_CONFIG_DIR
   # Description of (cross-)mounted disc space on cluster
   RUNTIME_FRONTEND_SEES_NODE=$CONFIG_shared_scratch
   RUNTIME_NODE_SEES_FRONTEND=$CONFIG_shared_filesystem
   RUNTIME_LOCAL_SCRATCH_DIR=$CONFIG_scratchdir
   
   #default is NFS
   if [ -z "${RUNTIME_NODE_SEES_FRONTEND}" ] ; then
     RUNTIME_NODE_SEES_FRONTEND=yes
   fi
   # locally empty means no
   if [ "${RUNTIME_NODE_SEES_FRONTEND}" = 'no' ] ; then
     RUNTIME_NODE_SEES_FRONTEND=
   fi

   # Only CPU time specified in job limits, rough limit for wall time
   walltime_ratio='1'
   # Use specified CPU time as soft limit, allow to run a bit longer before hard limit
   time_hardlimit_ratio='1/1'
   # Use specified memory requirement as soft limit, allow a bit more before hard limit
   memory_hardlimit_ratio='1/1'
   # Where to store temporary files on gatekeeper
   TMPDIR=${TMPDIR:-/tmp}
   # Where GNU time utility is located on computing nodes (empty if does not exist)
   GNU_TIME=${CONFIG_gnu_time:-/usr/bin/time}
   # Command to get name of executing node
   NODENAME=${CONFIG_nodename:-"/bin/hostname -f"}
} 

read_arc_conf () {
   ARC_CONFIG=${ARC_CONFIG:-/etc/arc.conf}
   . $pkgdatadir/config_parser_compat.sh || exit $?

   config_parse_file $ARC_CONFIG 1>&2 || exit $?
   config_import_section "common"
   config_import_section "cluster"

   # Also read queue section
   if [ ! -z "$joboption_queue" ]; then
     if ! config_import_section "queue/$joboption_queue"; then
         echo "ERROR: Job requested invalid share: $joboption_queue" 1>&2
         exit 1
     fi
   fi
}


usage="usage: `basename $0` (<arguments file>|-h|--help)"

parse_arg_file () {
   arg_file=$1
   if [ -z "$arg_file" ] ; then
      echo "Arguments file should be specified" 1>&2
      echo "$usage" 1>&2
      exit 1
   fi
   if [ "--help" = "$1" -o "-h" = "$1" ]; then
      echo "$usage" 1>&2
      cat <<EOHELP 1>&2

This script should not be executed directly but it is
called from the grid manager.
EOHELP
      exit 1
   fi
   if [ ! -f $arg_file ] ; then
      echo "No such arguments file at '$arg_file'" 1>&2
      echo "$usage" 1>&2
      exit 1
   fi
   . $arg_file

   if [ -z "$joboption_controldir" ] ; then
     joboption_controldir=`dirname "$arg_file"`
     if [ "$joboption_controldir" = '.' ] ; then
        joboption_controldir="$PWD"
     fi
   fi
   if [ -z "$joboption_gridid" ] ; then
     joboption_gridid=`basename "$arg_file" | sed 's/^job\.\(.*\)\.grami$/\1/'`
   fi

   ##############################################################
   # combine arguments to command -  easier to use
   ##############################################################
   i=0
   joboption_args=
   eval "var_is_set=\${joboption_arg_$i+yes}"
   while [ ! -z "${var_is_set}" ] ; do
      eval "var_value=\${joboption_arg_$i}"
      # Use -- to avoid echo eating arguments it understands
      var_value=`echo -- "$var_value" |cut -f2- -d' '| sed 's/\\\\/\\\\\\\\/g' | sed 's/"/\\\"/g'`
      joboption_args="$joboption_args \"${var_value}\""
      i=$(( $i + 1 ))
      eval "var_is_set=\${joboption_arg_$i+yes}"
   done
}

#
# Exits with 0 if the argument is all digits
#
is_number () {
    /usr/bin/perl -e 'exit 1 if $ARGV[0] !~ m/^\d+$/' "$1"
}

#
# Sets a default memory limit for jobs that don't have one
#
set_req_mem () {
    if ! is_number "$joboption_memory"; then

        echo "---------------------------------------------------------------------" 1>&2
        echo "WARNING: The job description contains no explicit memory requirement." 1>&2

        if is_number "$CONFIG_defaultmemory"; then
            joboption_memory=$CONFIG_defaultmemory

            echo "         A default memory limit taken from 'defaultmemory' in        " 1>&2
            echo "         arc.conf will apply.                                        " 1>&2
            echo "         Limit is: $CONFIG_defaultmemory mb.                         " 1>&2
	elif is_number "$CONFIG_nodememory"; then
            joboption_memory=$CONFIG_nodememory

            echo "         A default memory limit taken from 'nodememory' in arc.conf  " 1>&2
            echo "         will apply. You may want to set 'defaultmemory' to something" 1>&2
            echo "         else in arc.conf to better handle jobs with no memory       " 1>&2
            echo "         specified.                                                  " 1>&2
            echo "         Limit is: $CONFIG_nodememory mb.                            " 1>&2
        else
            joboption_memory=1000

            echo "         nodememory is not specified in arc.conf. A default      " 1>&2
            echo "         memory limit of 1GB will apply.                             " 1>&2
        fi
        echo "---------------------------------------------------------------------" 1>&2
    fi

    if [ "$joboption_localtransfer" = 'yes' ] && [ "$joboption_memory" -lt 1000 ]; then
        echo "---------------------------------------------------------------------" 1>&2
        echo "WARNING: localtransfers are enabled and job has less than 1GB of  " 1>&2
        echo "         ram. up- and downloaders take up a lot of ram, this can give" 1>&2
        echo "         you problems.                                               " 1>&2
        echo "---------------------------------------------------------------------" 1>&2
    fi

}

set_count () {
  if [ -z "$joboption_count" ] || [ "$joboption_count" -le 1 ] ; then
    joboption_count=1
    joboption_countpernode=-1
    joboption_numnodes=-1
  fi
}

##############################################################
# Zero stage of runtime environments
##############################################################
# should we write failures to job.111.failed like the old pbs?

RTE_stage0 () {
   joboption_num=0
   eval "var_is_set=\${joboption_runtime_$joboption_num+yes}"
   while [ ! -z "${var_is_set}" ] ; do
      eval "var_value=\${joboption_runtime_$joboption_num}"
      if [ -r "$RUNTIME_CONFIG_DIR/${var_value}" ] ; then
         arg_num=1
         args_value="0 "
         eval "arg_is_set=\${joboption_runtime_${joboption_num}_${arg_num}+yes}"
         while [ ! -z "${arg_is_set}" ] ; do
            eval "arg_value=\${joboption_runtime_${joboption_num}_${arg_num}}"
            # Use -- to avoid echo eating arguments it understands
            arg_value=`echo -- "$arg_value" |cut -f2- -d' '| sed 's/\\\\/\\\\\\\\/g' | sed 's/"/\\\"/g'`
            args_value="$args_value \"${arg_value}\""
            arg_num=$(( $arg_num + 1 ))
            eval "arg_is_set=\${joboption_runtime_${joboption_num}_${arg_num}+yes}"
         done
         #eval ". \"$RUNTIME_CONFIG_DIR/${var_value}\" $args_value" 1>&2
         cmdl="$RUNTIME_CONFIG_DIR/${var_value}"
         sourcewithargs $cmdl $args_value 1>&2
	 if [ $? -ne '0' ] ; then
            echo "ERROR: runtime script ${var_value} failed" 1>&2
            exit 1
         fi
      else
         echo "Warning: runtime script ${var_value} is missing" 1>&2
         #should we exit here?
      fi
      joboption_num=$(( $joboption_num + 1 ))
      eval "var_is_set=\${joboption_runtime_$joboption_num+yes}"
   done

  # joboption_count might have been changed by an RTE. Save it for accounting purposes.
  if [ -n "$joboption_count" ]; then
    diagfile="${joboption_controldir}/job.${joboption_gridid}.diag"
    echo "Processors=$joboption_count" >> "$diagfile"
    if [ -n "$joboption_numnodes" ]; then
      echo "Nodecount=$joboption_numnodes" >> "$diagfile"
    fi
  fi
}

##############################################################
# create temp job script
##############################################################

mktempscript () {
   # File name to be used for temporary job script
   LRMS_JOB_SCRIPT=`mktemp ${TMPDIR}/${joboption_lrms}_job_script.XXXXXX`
   echo "Created file $LRMS_JOB_SCRIPT"
   if [ -z "$LRMS_JOB_SCRIPT" ] ; then
      echo "Creation of temporary file failed"
      exit 1
   fi

   LRMS_JOB_OUT="${LRMS_JOB_SCRIPT}.out"
   touch $LRMS_JOB_OUT
   LRMS_JOB_ERR="${LRMS_JOB_SCRIPT}.err"
   touch $LRMS_JOB_ERR
   if [ ! -f "$LRMS_JOB_SCRIPT" ] || [ ! -f "$LRMS_JOB_OUT" ] || [ ! -f "$LRMS_JOB_ERR" ] ; then
      echo "Something is wrong. Either somebody is deleting files or I cannot write to ${TMPDIR}"
      rm -f "$LRMS_JOB_SCRIPT" "$LRMS_JOB_OUT" "$LRMS_JOB_ERR"
      exit 1
   fi
}

##############################################################
# Execution times (obtained in seconds)
##############################################################

##############################################################
# Add environment variables
##############################################################

add_user_env () {
   echo "# Setting environment variables as specified by user" >> $LRMS_JOB_SCRIPT
   has_gridglobalid=''
   i=0
   eval "var_is_set=\${joboption_env_$i+yes}"
   while [ ! -z "${var_is_set}" ] ; do
      eval "var_value=\${joboption_env_$i}"
      if [ "$var_value" ] && [ -z "${var_value##GRID_GLOBAL_JOBID=*}" ]; then
          has_gridglobalid=yes
      fi
      var_escaped=`echo "$var_value" | sed "s/'/'\\\\\''/g"`
      echo "export '${var_escaped}'" >> $LRMS_JOB_SCRIPT
      i=$(( $i + 1 ))
      eval "var_is_set=\${joboption_env_$i+yes}"
   done

   # guess globalid in case not already provided
   if [ -z "$has_gridglobalid" ]; then
      hostname=`/usr/bin/perl -MSys::Hostname -we 'print hostname'`
      hostname=${CONFIG_hostname:-$hostname}
      gm_port=${CONFIG_gm_port:-2811}
      gm_mount_point=${CONFIG_gm_mount_point:-/jobs}
      echo "export GRID_GLOBAL_JOBID='gsiftp://$hostname:$gm_port$gm_mount_point/$joboption_gridid'" >> $LRMS_JOB_SCRIPT
   fi

   echo "" >> $LRMS_JOB_SCRIPT
}

sourcewithargs_jobscript () {
   echo "# source with arguments for DASH shells" >> $LRMS_JOB_SCRIPT
   echo "sourcewithargs() {" >> $LRMS_JOB_SCRIPT
   echo "script=\$1" >> $LRMS_JOB_SCRIPT
   echo "shift" >> $LRMS_JOB_SCRIPT
   echo ". \$script" >> $LRMS_JOB_SCRIPT
   echo "}" >> $LRMS_JOB_SCRIPT
}

##############################################################
#  Runtime configuration
##############################################################
RTE_stage1 () {
   echo "# Running runtime scripts" >> $LRMS_JOB_SCRIPT
   
   echo "export RUNTIME_CONFIG_DIR=\${RUNTIME_CONFIG_DIR:-$RUNTIME_CONFIG_DIR}" >> $LRMS_JOB_SCRIPT
   i=0
   eval "var_is_set=\${joboption_runtime_$i+yes}"
   echo "runtimeenvironments=" >> $LRMS_JOB_SCRIPT
   while [ ! -z "${var_is_set}" ] ; do
      if [ "$i" = '0' ] ; then
         echo "if [ ! -z \"\$RUNTIME_CONFIG_DIR\" ] ; then" >> $LRMS_JOB_SCRIPT
      fi
      eval "var_value=\"\${joboption_runtime_$i}\""
      echo "  if [ -r \"\${RUNTIME_CONFIG_DIR}/${var_value}\" ] ; then" >> $LRMS_JOB_SCRIPT
      echo "    runtimeenvironments=\"\${runtimeenvironments}${var_value};\"" >> $LRMS_JOB_SCRIPT
      arg_num=1
      args_value="1 "
      eval "arg_is_set=\${joboption_runtime_${i}_${arg_num}+yes}"
      while [ ! -z "${arg_is_set}" ] ; do
         eval "arg_value=\${joboption_runtime_${i}_${arg_num}}"
         # Use -- to avoid echo eating arguments it understands
         arg_value=`printf "%s" "$arg_value" | sed 's/"/\\\\\\\"/g'`
         args_value="$args_value \"${arg_value}\""
         arg_num=$(( $arg_num + 1 ))
         eval "arg_is_set=\${joboption_runtime_${i}_${arg_num}+yes}"
      done
      #echo "    . \${RUNTIME_CONFIG_DIR}/${var_value} $args_value " >> $LRMS_JOB_SCRIPT
      echo "    cmdl=\${RUNTIME_CONFIG_DIR}/${var_value} " >> $LRMS_JOB_SCRIPT
      printf "    sourcewithargs \$cmdl $args_value \n" >> $LRMS_JOB_SCRIPT

      echo "    if [ \$? -ne '0' ] ; then " >> $LRMS_JOB_SCRIPT
      echo "      echo \"Runtime ${var_value} script failed \" 1>&2 " >> $LRMS_JOB_SCRIPT
      echo "      echo \"Runtime ${var_value} script failed \" 1>\"\$RUNTIME_JOB_DIAG\" " >> $LRMS_JOB_SCRIPT
      echo "      exit 1" >> $LRMS_JOB_SCRIPT
      echo "    fi " >> $LRMS_JOB_SCRIPT
      echo "  fi" >> $LRMS_JOB_SCRIPT
      i=$(( $i + 1 ))
      eval "var_is_set=\${joboption_runtime_$i+yes}"
   done

   if [ ! "$i" = '0' ] ; then
     echo "fi" >> $LRMS_JOB_SCRIPT
   fi

   echo "" >> $LRMS_JOB_SCRIPT
}


#######################################################################
# copy information useful for transfering files to/from node directly
#######################################################################

setup_local_transfer () {
  RUNTIME_CONTROL_DIR=`mktemp ${joboption_directory}/control.XXXXXX`
  if [ -z "$RUNTIME_CONTROL_DIR" ] ; then
    echo 'Failed to choose name for temporary control directory' 1>&2
    rm -f "$LRMS_JOB_SCRIPT" "$LRMS_JOB_OUT" "$LRMS_JOB_ERR"
    exit 1
  fi
  rm -f "$RUNTIME_CONTROL_DIR"
  mkdir "$RUNTIME_CONTROL_DIR"
  if [ $? -ne '0' ] ; then
    echo 'Failed to create temporary control directory' 1>&2
    rm -f "$LRMS_JOB_SCRIPT" "$LRMS_JOB_OUT" "$LRMS_JOB_ERR"
    exit 1
  fi
  chmod go-rwx,u+rwx "${RUNTIME_CONTROL_DIR}"
  echo '' >"${RUNTIME_CONTROL_DIR}/job.local.proxy"
  chmod go-rw,u+r,a-x "${RUNTIME_CONTROL_DIR}/job.local.proxy"
  cat "${joboption_controldir}/job.${joboption_gridid}.proxy" >"${RUNTIME_CONTROL_DIR}/job.local.proxy"
  cat "${joboption_controldir}/job.${joboption_gridid}.input" >"${RUNTIME_CONTROL_DIR}/job.local.input"
  cat "${joboption_controldir}/job.${joboption_gridid}.output">"${RUNTIME_CONTROL_DIR}/job.local.output"
  cat "${joboption_controldir}/job.${joboption_gridid}.local">"${RUNTIME_CONTROL_DIR}/job.local.local"
  RUNTIME_CONTROL_DIR_REL=`basename "$RUNTIME_CONTROL_DIR"`
  echo "$RUNTIME_CONTROL_DIR_REL *.*" >>"${RUNTIME_CONTROL_DIR}/job.local.input"
  echo "$RUNTIME_CONTROL_DIR_REL" >>"${RUNTIME_CONTROL_DIR}/job.local.output"
  echo "$RUNTIME_CONTROL_DIR_REL" >>"${joboption_controldir}/job.${joboption_gridid}.output"
  RUNTIME_STDOUT_REL=`echo "${joboption_stdout}" | sed "s#^${joboption_directory}##"`
  RUNTIME_STDERR_REL=`echo "${joboption_stderr}" | sed "s#^${joboption_directory}##"`
  echo "$RUNTIME_STDOUT_REL *.*" >>"${RUNTIME_CONTROL_DIR}/job.local.input"
  echo "$RUNTIME_STDERR_REL *.*" >>"${RUNTIME_CONTROL_DIR}/job.local.input"
  echo "RUNTIME_CONTROL_DIR=$RUNTIME_CONTROL_DIR" >> $LRMS_JOB_SCRIPT
}


#######################################################################
#uploading output files
#######################################################################

upload_output_files () {
   if [ "$joboption_localtransfer" = 'yes' ] ; then
     echo "UPLOADER=\${UPLOADER:-$ARC_LOCATION/lib/arc/uploader}" >> $LRMS_JOB_SCRIPT
     cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
     if [ "$RESULT" = '0' ] ; then
       $UPLOADER -p -c 'local' "$RUNTIME_CONTROL_DIR" "$RUNTIME_JOB_DIR" 2>>${RUNTIME_CONTROL_DIR}/job.local.errors
       if [ $? -ne '0' ] ; then
         echo 'ERROR: Uploader failed.' 1>&2
         if [ "$RESULT" = '0' ] ; then RESULT=1 ; fi
       fi
     fi
     rm -f "${RUNTIME_CONTROL_DIR}/job.local.proxy"
EOSCR
   fi
}

#######################################################################
# downloading input files (this might fail for fork)
#######################################################################
download_input_files () {
  if [ "$joboption_localtransfer" = 'yes' ] ; then
    RUNTIME_ARC_LOCATION="$ARC_LOCATION"
    RUNTIME_GLOBUS_LOCATION="$GLOBUS_LOCATION"
    echo "ARC_LOCATION=\${ARC_LOCATION:-$RUNTIME_ARC_LOCATION}" >> $LRMS_JOB_SCRIPT
    echo "GLOBUS_LOCATION=\${GLOBUS_LOCATION:-$RUNTIME_GLOBUS_LOCATION}" >> $LRMS_JOB_SCRIPT
    echo "DOWNLOADER=\${DOWNLOADER:-$ARC_LOCATION/lib/arc/downloader}" >> $LRMS_JOB_SCRIPT
    cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
    if [ -z "$ARC_LOCATION" ] ; then
      echo 'Variable ARC_LOCATION is not set' 1>&2
      exit 1
    fi
    if [ -z "$GLOBUS_LOCATION" ] ; then
      echo 'Variable GLOBUS_LOCATION is not set' 1>&2
      exit 1
    fi
    export GLOBUS_LOCATION
    export ARC_LOCATION
    if [ "x$LD_LIBRARY_PATH" = "x" ]; then
      export LD_LIBRARY_PATH="$GLOBUS_LOCATION/lib"
    else
      export LD_LIBRARY_PATH="$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH"
    fi
    export SASL_PATH="$GLOBUS_LOCATION/lib/sasl"
    export X509_USER_KEY="${RUNTIME_CONTROL_DIR}/job.local.proxy"
    export X509_USER_CERT="${RUNTIME_CONTROL_DIR}/job.local.proxy"
    export X509_USER_PROXY="${RUNTIME_CONTROL_DIR}/job.local.proxy"
    unset X509_RUN_AS_SERVER
    $DOWNLOADER -p -c 'local' "$RUNTIME_CONTROL_DIR" "$RUNTIME_JOB_DIR" 2>>${RUNTIME_CONTROL_DIR}/job.local.errors
    if [ $? -ne '0' ] ; then
      echo 'ERROR: Downloader failed.' 1>&2
      exit 1
    fi
EOSCR
  fi
}

##############################################################
# Add std... to job arguments
##############################################################
include_std_streams () {
  input_redirect=
  output_redirect=
  if [ ! -z "$joboption_stdin" ] ; then
    input_redirect="<\$RUNTIME_JOB_STDIN"
  fi
  if [ ! -z "$joboption_stdout" ] ; then
    output_redirect="1>\$RUNTIME_JOB_STDOUT"
  fi
  if [ ! -z "$joboption_stderr" ] ; then
    if [ "$joboption_stderr" = "$joboption_stdout" ] ; then
      output_redirect="$output_redirect 2>&1"
    else
      output_redirect="$output_redirect 2>\$RUNTIME_JOB_STDERR"
    fi
  fi
}

##############################################################
# Runtime configuration
##############################################################
configure_runtime () {
  i=0
  eval "var_is_set=\${joboption_runtime_$i+yes}"
  while [ ! -z "${var_is_set}" ] ; do
    if [ "$i" = '0' ] ; then
      echo "if [ ! -z \"\$RUNTIME_CONFIG_DIR\" ] ; then" >> $LRMS_JOB_SCRIPT
    fi
    eval "var_value=\"\${joboption_runtime_$i}\""
    echo "  if [ -r \"\${RUNTIME_CONFIG_DIR}/${var_value}\" ] ; then" >> $LRMS_JOB_SCRIPT
    arg_num=1
    args_value="2 "
    eval "arg_is_set=\${joboption_runtime_${i}_${arg_num}+yes}"
    while [ ! -z "${arg_is_set}" ] ; do
      eval "arg_value=\${joboption_runtime_${i}_${arg_num}}"
      # Use -- to avoid echo eating arguments it understands
      arg_value=`printf -- "$arg_value" | sed 's/\\\\/\\\\\\\\/g' | sed 's/"/\\\\\\\"/g'`
      args_value="$args_value \"${arg_value}\""
      arg_num=$(( $arg_num + 1 ))
      eval "arg_is_set=\${joboption_runtime_${i}_${arg_num}+yes}"
    done
    echo "    cmdl=\${RUNTIME_CONFIG_DIR}/${var_value}" >> $LRMS_JOB_SCRIPT
    printf "    sourcewithargs \$cmdl $args_value \n" >> $LRMS_JOB_SCRIPT
    echo "  fi" >> $LRMS_JOB_SCRIPT
    i=$(( $i + 1 ))
    eval "var_is_set=\${joboption_runtime_$i+yes}"
  done
  if [ ! "$i" = '0' ] ; then
    echo "fi" >> $LRMS_JOB_SCRIPT
  fi
  echo "" >> $LRMS_JOB_SCRIPT
}

##############################################################
# move files to node
##############################################################
move_files_to_node () {
  if [ "$joboption_count" -eq 1 ] || [ ! -z "$RUNTIME_ENABLE_MULTICORE_SCRATCH" ] || [ "$joboption_count" -eq "$joboption_countpernode" ]; then
    echo "RUNTIME_LOCAL_SCRATCH_DIR=\${RUNTIME_LOCAL_SCRATCH_DIR:-$RUNTIME_LOCAL_SCRATCH_DIR}" >> $LRMS_JOB_SCRIPT
  else
    echo "RUNTIME_LOCAL_SCRATCH_DIR=\${RUNTIME_LOCAL_SCRATCH_DIR:-}" >> $LRMS_JOB_SCRIPT
  fi
  echo "RUNTIME_FRONTEND_SEES_NODE=\${RUNTIME_FRONTEND_SEES_NODE:-$RUNTIME_FRONTEND_SEES_NODE}" >> $LRMS_JOB_SCRIPT
  echo "RUNTIME_NODE_SEES_FRONTEND=\${RUNTIME_NODE_SEES_FRONTEND:-$RUNTIME_NODE_SEES_FRONTEND}" >> $LRMS_JOB_SCRIPT
  cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
  if [ ! -z "$RUNTIME_LOCAL_SCRATCH_DIR" ] && [ ! -z "$RUNTIME_NODE_SEES_FRONTEND" ]; then
    RUNTIME_NODE_JOB_DIR="$RUNTIME_LOCAL_SCRATCH_DIR"/`basename "$RUNTIME_JOB_DIR"`
    rm -rf "$RUNTIME_NODE_JOB_DIR"
    mkdir -p "$RUNTIME_NODE_JOB_DIR"
    # move directory contents
    for f in "$RUNTIME_JOB_DIR"/.* "$RUNTIME_JOB_DIR"/*; do 
      [ "$f" = "$RUNTIME_JOB_DIR/*" ] && continue # glob failed, no files
      [ "$f" = "$RUNTIME_JOB_DIR/." ] && continue
      [ "$f" = "$RUNTIME_JOB_DIR/.." ] && continue
      [ "$f" = "$RUNTIME_JOB_DIR/.diag" ] && continue
      [ "$f" = "$RUNTIME_JOB_DIR/.comment" ] && continue
      if ! mv "$f" "$RUNTIME_NODE_JOB_DIR"; then
        echo "Failed to move '$f' to '$RUNTIME_NODE_JOB_DIR'" 1>&2
        exit 1
      fi
    done
    if [ ! -z "$RUNTIME_FRONTEND_SEES_NODE" ] ; then
      # creating link for whole directory
       ln -s "$RUNTIME_FRONTEND_SEES_NODE"/`basename "$RUNTIME_JOB_DIR"` "$RUNTIME_JOB_DIR"
    else
      # keep stdout, stderr and control directory on frontend
      # recreate job directory
      mkdir -p "$RUNTIME_JOB_DIR"
      # make those files
      mkdir -p `dirname "$RUNTIME_JOB_STDOUT"`
      mkdir -p `dirname "$RUNTIME_JOB_STDERR"`
      touch "$RUNTIME_JOB_STDOUT"
      touch "$RUNTIME_JOB_STDERR"
      RUNTIME_JOB_STDOUT__=`echo "$RUNTIME_JOB_STDOUT" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
      RUNTIME_JOB_STDERR__=`echo "$RUNTIME_JOB_STDERR" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
      rm "$RUNTIME_JOB_STDOUT__" 2>/dev/null
      rm "$RUNTIME_JOB_STDERR__" 2>/dev/null
      if [ ! -z "$RUNTIME_JOB_STDOUT__" ] && [ "$RUNTIME_JOB_STDOUT" != "$RUNTIME_JOB_STDOUT__" ]; then
        ln -s "$RUNTIME_JOB_STDOUT" "$RUNTIME_JOB_STDOUT__"
      fi
      if [ "$RUNTIME_JOB_STDOUT__" != "$RUNTIME_JOB_STDERR__" ] ; then
        if [ ! -z "$RUNTIME_JOB_STDERR__" ] && [ "$RUNTIME_JOB_STDERR" != "$RUNTIME_JOB_STDERR__" ]; then
          ln -s "$RUNTIME_JOB_STDERR" "$RUNTIME_JOB_STDERR__"
        fi
      fi
      if [ ! -z "$RUNTIME_CONTROL_DIR" ] ; then
        # move control directory back to frontend
        RUNTIME_CONTROL_DIR__=`echo "$RUNTIME_CONTROL_DIR" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
        mv "$RUNTIME_CONTROL_DIR__" "$RUNTIME_CONTROL_DIR"
      fi
    fi
    # adjust stdin,stdout & stderr pointers
    RUNTIME_JOB_STDIN=`echo "$RUNTIME_JOB_STDIN" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
    RUNTIME_JOB_STDOUT=`echo "$RUNTIME_JOB_STDOUT" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
    RUNTIME_JOB_STDERR=`echo "$RUNTIME_JOB_STDERR" | sed "s#^${RUNTIME_JOB_DIR}#${RUNTIME_NODE_JOB_DIR}#"`
    RUNTIME_FRONTEND_JOB_DIR="$RUNTIME_JOB_DIR"
    RUNTIME_JOB_DIR="$RUNTIME_NODE_JOB_DIR"
  fi
  if [ -z "$RUNTIME_NODE_SEES_FRONTEND" ] ; then
    mkdir -p "$RUNTIME_JOB_DIR"
  fi
EOSCR
}

##############################################################
# move files back to frontend
##############################################################
move_files_to_frontend () {
  cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
  if [ ! -z "$RUNTIME_LOCAL_SCRATCH_DIR" ] && [ ! -z "$RUNTIME_NODE_SEES_FRONTEND" ]; then 
    if [ ! -z "$RUNTIME_FRONTEND_SEES_NODE" ] ; then
      # just move it
      rm -rf "$RUNTIME_FRONTEND_JOB_DIR"
      destdir=`dirname "$RUNTIME_FRONTEND_JOB_DIR"`
      if ! mv "$RUNTIME_NODE_JOB_DIR" "$destdir"; then
        echo "Failed to move '$RUNTIME_NODE_JOB_DIR' to '$destdir'" 1>&2
        RESULT=1
      fi
    else
      # remove links
      rm -f "$RUNTIME_JOB_STDOUT" 2>/dev/null
      rm -f "$RUNTIME_JOB_STDERR" 2>/dev/null
      # move directory contents
      for f in "$RUNTIME_NODE_JOB_DIR"/.* "$RUNTIME_NODE_JOB_DIR"/*; do 
        [ "$f" = "$RUNTIME_NODE_JOB_DIR/*" ] && continue # glob failed, no files
        [ "$f" = "$RUNTIME_NODE_JOB_DIR/." ] && continue
        [ "$f" = "$RUNTIME_NODE_JOB_DIR/.." ] && continue
        [ "$f" = "$RUNTIME_NODE_JOB_DIR/.diag" ] && continue
        [ "$f" = "$RUNTIME_NODE_JOB_DIR/.comment" ] && continue
        if ! mv "$f" "$RUNTIME_FRONTEND_JOB_DIR"; then
          echo "Failed to move '$f' to '$RUNTIME_FRONTEND_JOB_DIR'" 1>&2
          RESULT=1
        fi
      done
      rm -rf "$RUNTIME_NODE_JOB_DIR"
    fi
  fi
  echo "exitcode=$RESULT" >> "$RUNTIME_JOB_DIAG"
  exit $RESULT
EOSCR
}

##############################################################
# copy runtime settings to jobscript
##############################################################
setup_runtime_env () {
  echo "RUNTIME_JOB_DIR=$joboption_directory" >> $LRMS_JOB_SCRIPT
  echo "RUNTIME_JOB_STDIN=$joboption_stdin" >> $LRMS_JOB_SCRIPT
  echo "RUNTIME_JOB_STDOUT=$joboption_stdout" >> $LRMS_JOB_SCRIPT
  echo "RUNTIME_JOB_STDERR=$joboption_stderr" >> $LRMS_JOB_SCRIPT
  echo "RUNTIME_JOB_DIAG=${joboption_directory}.diag" >> $LRMS_JOB_SCRIPT
  # Adjust working directory for tweaky nodes
  # RUNTIME_GRIDAREA_DIR should be defined by external means on nodes
  echo "if [ ! -z \"\$RUNTIME_GRIDAREA_DIR\" ] ; then" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_JOB_DIR=\$RUNTIME_GRIDAREA_DIR/\`basename \$RUNTIME_JOB_DIR\`" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_JOB_STDIN=\`echo \"\$RUNTIME_JOB_STDIN\" | sed \"s#^\$RUNTIME_JOB_DIR#\$RUNTIME_GRIDAREA_DIR#\"\`" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_JOB_STDOUT=\`echo \"\$RUNTIME_JOB_STDOUT\" | sed \"s#^\$RUNTIME_JOB_DIR#\$RUNTIME_GRIDAREA_DIR#\"\`" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_JOB_STDERR=\`echo \"\$RUNTIME_JOB_STDERR\" | sed \"s#^\$RUNTIME_JOB_DIR#\$RUNTIME_GRIDAREA_DIR#\"\`" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_JOB_DIAG=\`echo \"\$RUNTIME_JOB_DIAG\" | sed \"s#^\$RUNTIME_JOB_DIR#\$RUNTIME_GRIDAREA_DIR#\"\`" >> $LRMS_JOB_SCRIPT
  echo "  RUNTIME_CONTROL_DIR=\`echo \"\$RUNTIME_CONTROL_DIR\" | sed \"s#^\$RUNTIME_JOB_DIR#\$RUNTIME_GRIDAREA_DIR#\"\`" >> $LRMS_JOB_SCRIPT
  echo "fi" >> $LRMS_JOB_SCRIPT
}


##############################################################
# change to runtime dir and setup timed run
##############################################################
cd_and_run () {

  cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
  # Changing to session directory
  HOME=$RUNTIME_JOB_DIR
  export HOME
  if ! cd "$RUNTIME_JOB_DIR"; then
    echo "Failed to switch to '$RUNTIME_JOB_DIR'" 1>&2
    RESULT=1
  fi
  if [ ! -z "$RESULT" ] && [ "$RESULT" != 0 ]; then
    exit $RESULT
  fi
EOSCR

  if [ ! -z "$NODENAME" ] ; then
    if [ -z "$NODENAME_WRITTEN" ] ; then
      echo "nodename=\`$NODENAME\`" >> $LRMS_JOB_SCRIPT
      echo "echo \"nodename=\$nodename\" >> \"\$RUNTIME_JOB_DIAG\"" >> $LRMS_JOB_SCRIPT
    fi
  fi
  #TODO this should probably be done on headnode instead
  echo "echo \"Processors=${joboption_count}\" >> \"\$RUNTIME_JOB_DIAG\"" >> $LRMS_JOB_SCRIPT

  # In case the job executable does not exist the error message might be
  # printed by GNU_TIME, which can be confusing for the user.  
  # This will print more appropriate error message.
  echo "executable='$joboption_arg_0'" >> $LRMS_JOB_SCRIPT
  cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
# Check if executable exists
if [ ! -f "$executable" ]; 
then 
  echo "Path \"$executable\" does not seem to exist" 1>$RUNTIME_JOB_STDOUT 2>$RUNTIME_JOB_STDERR 1>&2
  exit 1
fi
EOSCR

  # In case the job executable is written in a scripting language and the
  # interpreter is not found, the error message printed by GNU_TIME is
  # misleading.  This will print a more appropriate error message.
  cat >> $LRMS_JOB_SCRIPT <<'EOSCR'
# See if executable is a script, and extract the name of the interpreter
line1=`dd if="$executable" count=1 2>/dev/null | head -n 1`
command=`echo $line1 | sed -n 's/^#! *//p'`
interpreter=`echo $command | awk '{print $1}'`
if [ "$interpreter" = /usr/bin/env ]; then interpreter=`echo $command | awk '{print $2}'`; fi
# If it's a script and the interpreter is not found ...
[ "x$interpreter" = x ] || type "$interpreter" > /dev/null 2>&1 || {

  echo "Cannot run $executable: $interpreter: not found" 1>$RUNTIME_JOB_STDOUT 2>$RUNTIME_JOB_STDERR 1>&2
  exit 1; }
EOSCR
    # Check that gnu_time works
    cat >> $LRMS_JOB_SCRIPT <<EOSCR
GNU_TIME='$GNU_TIME'
if [ ! -z "\$GNU_TIME" ] && ! "\$GNU_TIME" --version >/dev/null 2>&1; then
  echo "WARNING: GNU time not found at: \$GNU_TIME" 2>&1;
  GNU_TIME=
fi 

if [ -z "\$GNU_TIME" ] ; then
  $joboption_args $input_redirect $output_redirect
else
  \$GNU_TIME -o "\$RUNTIME_JOB_DIAG" -a -f '\
WallTime=%es\nKernelTime=%Ss\nUserTime=%Us\nCPUUsage=%P\n\
MaxResidentMemory=%MkB\nAverageResidentMemory=%tkB\n\
AverageTotalMemory=%KkB\nAverageUnsharedMemory=%DkB\n\
AverageUnsharedStack=%pkB\nAverageSharedMemory=%XkB\n\
PageSize=%ZB\nMajorPageFaults=%F\nMinorPageFaults=%R\n\
Swaps=%W\nForcedSwitches=%c\nWaitSwitches=%w\n\
Inputs=%I\nOutputs=%O\nSocketReceived=%r\nSocketSent=%s\n\
Signals=%k\n' \
$joboption_args $input_redirect $output_redirect

fi
RESULT=\$?

EOSCR
}

if [ "submit_common.sh" = `basename $0` ]; then
  parse_arg_file $*
fi