-
Notifications
You must be signed in to change notification settings - Fork 10
/
gzpipe
executable file
·382 lines (367 loc) · 14.8 KB
/
gzpipe
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
#!/bin/sh
######################################################################
#
# GZPIPE - Making a Named Pipe Behaves as a Gzipping Filter
#
# USAGE: gzpipe [-t timeout] <named_pipe_to_use> [output_file]
#
# 1) create the named pipe <named_pipe_to_use> when unexists
# 2) read stream data from the named pipe
# 3) compress it with gzip
# 4) output to <output_file> (default: STDOUT)
# 5) remove the named pipe if the pipe is made by me
# However, the pipe will be disconnected by itself when the time will
# have been to <timeout>(default:600sec). That is not to forget to disconnect.
#
# Return: ==0 when succeed
# !=0 when failed
#
# HOW TO APPLY THIS:
# * e.g., if you want to write the executing log of a shellscript into
# a file which is gzipped, you can use commands as follow.
# > touch /PATH/TO/LOG/DIR/logfile.gz
# > chmod 600 /PATH/TO/LOG/DIR/logfile.gz
# > gzpipe /tmp/named_pipe.$$ /PATH/TO/LOG/DIR/logfile.gz
# > [ $? -eq 0 ] || { echo 'error!' 1>&2; exit 1; }
# > exec 2>/tmp/named_pipe.$$
# > set -xv
#
# NOTICE:
# * The pipe and my process will be erased when the caller process will
# die.
# * If you want to set permission to the <output_file>, you should
# make the <output_file> beforehand with the demanded permission.
# * The following command, which use redirection instead of the 2nd
# argument works correctly at almost all host.
# > gzpipe "named_pipe_to_use" > "output_file"
# But it should not be written. Beause the redirection from a
# background process will not probably be assumed. And performance
# also will probably worse than "output_file" is as an argument.
# However, the method has GREAT POSSIBILITIES. e.g., you can write
# commands as follow.
# > gzpipe "namedpipe" | zcat | tr 'A-Z' 'a-z' | grep error >error.log &
# > while [ ! -p "namedpipe" ];do sleep 0; done # wait for creating the pipe
# > exec 2>"namedpipe"
# > (various commands which write into stderr)
# That means, you can join various filters to fd=2 (stderr). Although
# it is very tricky. ;-)
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-12-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
readonly DEFAULT_TIMEOUT=600
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage ========================
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [-t timeout] <named_pipe_to_use> [output_file]
* See the document at the source code of this command
Version : 2020-12-06 02:51:43 JST
USAGE
exit 1
}
######################################################################
# Prepare Main Routine
######################################################################
# === Define functions ===============================================
# --- FUNC: Make a Temporary File ------------------------------------
# arg : (none)
# ret : 0 when succeeded
# stdout: path of the generated temporary file
LFs=$(printf '\\\n_');LFs=${LFs%_} # Use as a "\n" in s-command of sed
mktempf0() {
# --- parameters, etc.
num_of_digits=16 # Length of Random String
n=10 # Max Times of Retrying
chrs='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'
# Available letters
# --- call built-in mktemp if exists
type mktemp >/dev/null 2>&1 && {
mktemp -t tmp.${0##*/}.$$.XXXXXXXXXXXXXXXX
return $?
}
# --- set the temporary directory
Dir_tmp=$(set | grep -q ^TMPDIR= | sed 's/^[^=]\{1,\}=//');
case "$Dir_tmp" in
'') Dir_tmp='/tmp' ;;
/) Dir_tmp='' ;;
*) Dir_tmp=${Dir_tmp%/};;
esac
# --- mktemp loop
while [ $n -ge 0 ]; do
# --- Generate Ramdom string
# calculate the number of words which required
nw=$(echo "${num_of_digits}*l(${#chrs})/11.09+1" | # 11.09=ln(65536)
bc -l |
sed 's/\..*$//' )
# make a random hexadecimal digit
if [ -c /dev/urandom ]; then
hstr=$(dd if=/dev/urandom bs=2 count=$nw 2>/dev/null |
od -A n -t x2 -v |
tr 'abcdef ' 'ABCDEF\n' |
tr -Cd 0123456789ABCDEF )
else
hstr=$( (ps -Ao pid,etime,pcpu,vsz; date) |
od -t d4 -A n -v |
sed 's/[^0-9]\{1,\}/'"$LFs"'/g' |
grep '[0-9]' |
tail -n 42 |
sed 's/.*\(.\{8\}\)$/\1/g' |
awk 'BEGIN{a=-2147483648;} #
{a+=$1; } #
END { #
srand(a); #
for(i=0;i<'$nw';i++){ #
printf("%02X",int(rand()*65536)); #
} #
}' )
fi
# make a random string from the hexadecimal digit
s=$(echo "obase=${#chrs};ibase=16;$hstr" |
bc |
tr -d '\\\n' |
tr ' ' '\n' |
awk 'BEGIN {for(i=1;i<'$num_of_digits';i++){print 0;}} #
/[0-9]/{print; }' |
awk 'BEGIN {ORS=""; #
s="'"$chrs"'"; #
for(i=0;i<length(s);i++){ #
c[i]=substr(s,i+1,1); #
} } #
/[0-9]/{print c[$0*1]; }' |
tail -c $num_of_digits )
# --- Making the file path
Path_target="${Dir_tmp}/tmp.${0##*/}.$$.$s"
# --- Making the file
(set -C; umask 177; : > "$Path_target") 2>/dev/null || {
[ -f "$Path_target" ] && { n=$((n-1)); continue; }
n=-1; break;
}
break
done
# --- print error message when failed to make a file
case "$n" in -1) return 1;; esac
# --- print the path of the file
printf '%s\n' "$Path_target"
# --- return successfully
return 0
}
######################################################################
# Main Routine
######################################################################
# === Judge which I have started as a parent or a child ==============
while :; do
case $# in 6) :;; *) check='p'; break;; esac
pid=$$
pid=$(ps -Ao pid,ppid | awk '$1=='$pid'{print $2*1}')
case "$pid" in '') echo '*** Unexpected error (ps command)' 1>&2; break;; esac
pid=$(ps -Ao pid,ppid | awk '$1=='$pid'{print $2*1}')
case "$pid" in "$5") :;; *) check='p'; break;; esac
check='c';break;
done
# ===== ROUTINE AS A PARENT ==========================================
case $check in 'p')
#
# --- make sure that gzip command exists ---------------------------
type gzip >/dev/null 2>&1 || {
printf '%s\n' "${0##*/}: gzip command is not found" 1>&2
exit 1
}
#
# --- exit trap (for parent) ---------------------------------------
exit_trap() {
set -- ${1:-} $? # $? is set as $1 if no argument given
trap '' EXIT HUP INT QUIT PIPE ALRM TERM
rm -f "${namedpipe_to_del_by_myself:-}"
trap - EXIT HUP INT QUIT PIPE ALRM TERM
exit $1
}
#
# --- parse the arguments ------------------------------------------
timeout=$DEFAULT_TIMEOUT
case $# in 0) print_usage_and_exit;; esac
case "$1" in
-t) shift
case $# in 0) print_usage_and_exit;; esac
printf '%s' "$1" | grep -q '[^0-9]' && print_usage_and_exit
timeout=$1
shift
;;
esac
case $# in [12]) :;; *) print_usage_and_exit;; esac
case "$1" in '') print_usage_and_exit;; esac
pipe=$1
outfile=''
case $# in 2) case "$2" in '') :;; *) outfile=$2;; esac;; esac
case "$pipe" in "$outfile") print_usage_and_exit;; esac
case "$pipe" in -|/*|./*|../*) :;; *) pipe="./$pipe" ;; esac
case "$outfile" in -|/*|./*|../*) :;; *) outfile="./$outfile";; esac
#
# --- enable the exit trap -----------------------------------------
trap 'exit_trap' EXIT HUP INT QUIT PIPE ALRM TERM
#
# --- investigate the caller process ID ----------------------------
pid=$$
pid=$(ps -Ao pid,ppid | awk '$1=='$pid'{print $2}')
#
# --- make the named pipe if required ------------------------------
check=$(ls -l "$pipe" 2>/dev/null)
case "$?$check" in
[1-9]|[1-9][0-9]|[012][0-9][0-9]) # $?>0 && $check==''
mkfifo -m 600 "$pipe"
case $? in
0) : ;;
*) printf '%s\n' "${0##*/}: Can't make the named pipe" 1>&2
exit_trap 2 ;;
esac
namedpipe_to_del_by_myself=$pipe
;;
0[!0-9]*) # $?==0 && $check!=''
case "$check" in
p*) : ;;
*) printf '%s\n' "${0##*/}: \"$pipe\" exists as another attribute" 1>&2
exit_trap 3 ;;
esac
;;
*) printf '%s\n' "${0##*/}: An error occured at executing ls command" 1>&2
exit_trap 4
;;
esac
#
# --- make stderr silent -------------------------------------------
#exec 3>&2 2>/dev/null # fd3 is only available for debugging
exec 2>/dev/null >&2 3>&2
#
# --- prepare for locking ------------------------------------------
sleep 100 & # This is to wait that the child process is ready
sleepjob_pid=$!
#
# --- exec the gzip piping script ----------------------------------
case "${namedpipe_to_del_by_myself:-}" in
'') check=0 ;;
*) check=1; namedpipe_to_del_by_myself='';;
esac
"$0" "$pipe" "$outfile" "$check" "$sleepjob_pid" "$pid" "$timeout" &
#
# --- wait for starting of the gzip piping script ------------------
wait "$sleepjob_pid" 2>/dev/null
#
# --- exit normally ------------------------------------------------
exit_trap 0
;;
# ===== ROUTINE AS A CHILD ===========================================
'c')
# This child process must be called with the arguments
# $1:namedpipe to read
# $2:file to write into
# $3:1 is set if the parent wants the child to delete $1 after using,
# otherwise 0
# $4:process ID to kill for resuming the parent process
# $5:process ID which this script waits for termination (caller PID)
# $6:timeout (seconds)
#
# --- validate the arguments ---------------------------------------
case $# in 6) :;; *) echo '*** Invalid arguments' 1>&3; exit 10;; esac
[ -p "$1" ] || { echo '*** Invalid argument #1' 1>&3; exit 11; }
pipe=$1
if [ -n "$2" ] && ([ ! -f "$2" ] || [ ! -w "$2" ]); then
echo '*** Invalid argument #2' 1>&3
exit 12
fi
file=$2
case "$3" in [01]) :;; *) echo '*** Invalid argument #3' 1>&3; exit 13;; esac
del_the_pipe=$3
printf '%s\n' "$4" | grep -q '^[0-9]\+$' ||
{ echo '*** Invalid argument #4' 1>&3; exit 14; }
ppid=$4
printf '%s\n' "$5" | grep -q '^[0-9]\+$' ||
{ echo '*** Invalid argument #5' 1>&3; exit 15; }
cpid=$5
printf '%s\n' "$6" | grep -q '^[0-9]\+$' ||
{ echo '*** Invalid argument #6' 1>&3; exit 16; }
timeout=$6
#
# --- set exiting trap ---------------------------------------------
exit_trap() {
set -- ${1:-} $? # $? is set as $1 if no argument given
trap '' EXIT HUP INT QUIT PIPE ALRM TERM
ret=$1
# --- the gzipped pipe has been used or not? ---
while [ -f "${gz_file:-}" ]; do
check=$(cat "$gz_file")
case "$check" in '') break;; esac
ret=$check
break
done
# --- terminate the remained processes ---
kill -s TERM "${timerjob_pid:-}" "${timersleep_pid:-}" \
"${gzippedpipingjob_pid:-}" "${gzip_pid:-}" \
"${pollingsleep_pid:-}"
# --- delete the unnecessary files ---
rm -f "${gz_file:-}"
case $del_the_pipe in 1) rm -f "$pipe";; esac
# --- finish ---
trap - EXIT HUP INT QUIT PIPE ALRM TERM
exit $ret
}
trap 'exit_trap' EXIT HUP INT QUIT PIPE ALRM TERM
#
# --- tell the parent that I have started up -----------------------
kill -s TERM "$ppid"
#
# --- make a temporary file ----------------------------------------
gz_file=$(mktempf0)
case $? in
0) : ;;
*) printf '%s\n' "${0##*/}: Can't make a tempfile" 1>&3; exit_trap 18;;
esac
#
# --- set my life time ---------------------------------------------
case $timeout in
0) : ;;
*) pid=$$
{ sleep $timeout; kill -s ALRM $pid; } &
timerjob_pid=$!
timersleep_pid=$(ps -Ao pid,ppid | awk '$2=='$timerjob_pid'{print $1}');;
esac
#
# --- do the gzipped piping (hehave as a background job) -----------
pid=$$
case "$file" in
'') { gzip <"$pipe" ; echo $? >"$gz_file"; kill -s ALRM $pid; } & ;;
*) { gzip <"$pipe" >"$file"; echo $? >"$gz_file"; kill -s ALRM $pid; } & ;;
esac
gzippedpipingjob_pid=$!
gzip_pid=$(ps -Ao pid,ppid | awk '$2=='${gzippedpipingjob_pid}'{print $1}')
# TIPS: { gzip < namedpipe_which_is_received_nothing_yet; } &
# You cannot find the process ID of gzip yet. The reason is
# that the namedpipe which is received nothing yet prevent from
# starting the gzip command until the namedpipe get any data.
#
# --- wait for termination of the caller (is not the parent) process
while :; do
kill -0 $cpid >/dev/null 2>&1
case $? in 0) :;; *) break;; esac
sleep 1 &
pollingsleep_pid=$!
wait $pollingsleep_pid
done
exit_trap 0
;; esac