make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / queue-mgmt / qadd-common
blob86374057a31a674acd14f8121c9c91b02d338e00
1 #!/bin/bash
3 qtask_state()
5 local state taskid
6 taskid=$1
7 state=queued
8 if [ -e "$queue_dir/$taskid.end" ]
9 then
10 state=ended
11 else
12 pidfile=$queue_dir/$taskid.pid
13 if [ -e "$pidfile" ]
14 then
15 state=gone
16 exec {task_lock_fd}>>"$pidfile"
17 flock --shared --nonblock "$task_lock_fd"
18 lock_err=$?
19 if [ $lock_err != 0 ]
20 then
21 state=running
25 echo "$state"
28 load_command_global()
30 # load the NUL-delimited command and its arguments from the given file
31 # and put in bash global $command[@] array.
33 local command_file=$1
34 local fd arg
35 declare -g -a command=()
36 exec {fd}<"$command_file"
37 while read -r -d $'\0' -u $fd arg
39 command+=("$arg")
40 done
41 exec {fd}>&-
45 queue_dir=${DEFAULT_QUEUE_DIR:-}
46 quiet=''
47 very_quiet=''
48 dryrun=''
49 force=''
51 while [ $# != 0 ]
53 case "$1" in
54 --help)
55 echo "Usage: $usage_help_text"
56 exit;;
57 -d|--queue-dir)
58 shift
59 queue_dir=$1
61 -e|--show-exit-code)
62 qls_show_exit_status=yes
64 -E|--no-task-exit-code)
65 qrun_return_task_status=''
67 -f|--force)
68 force=yes
70 -h|--hide|--side-state)
71 shift
72 qls_hide_states="$qls_hide_states $1"
74 -H|--no-header)
75 qls_header=''
77 --multiline)
78 qls_multiline=yes
80 -M|--no-multiline|--escape-command)
81 qls_multiline=''
83 -n|--dry-run)
84 dryrun=yes
86 -q|--quiet)
87 quiet=yes
89 -qq|--very-quiet)
90 quiet=yes
91 very_quiet=yes
93 -s|--suppress-output)
94 qrun_suppress_output=yes
96 -x|--if-not-queued)
97 qadd_no_dup=yes
99 -X|--if-not-running)
100 qadd_no_duprun=yes
103 shift
104 break;;
106 echo "${0##*/}: unknwon option: $1" >&2
107 exit -1;;
109 break;;
110 esac
111 shift
112 done
114 if [ -z "$queue_dir" ]
115 then
116 # load default queue dir
117 if [ -e ~/.config/qadd/default-queue-dir ]
118 then
119 queue_dir=`head -n 1 ~/.config/qadd/default-queue-dir`
120 queue_dir=${queue_dir/#~\//$HOME/}
121 mkdir -p "$queue_dir"
125 if [ -z "$queue_dir" ]
126 then
127 echo "${0##*/}: specify the queue control directory." >&2
128 exit -2
132 set -e
133 set -o pipefail
134 set -u
136 exec {lock_fd}>>"$queue_dir/.lock"
137 flock --exclusive $lock_fd