aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-09-15 16:05:09 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2016-09-15 16:05:09 +0300
commitaaab4be1de451b5b86ee013a7a7f6055befbc354 (patch)
tree9501d2ecb10be3388f5261a768a14d68c7be5bc8
parent53059b7fb9101091eaf67974862820461765710c (diff)
downloadrex-aaab4be1de451b5b86ee013a7a7f6055befbc354.tar.gz
rex-aaab4be1de451b5b86ee013a7a7f6055befbc354.tar.bz2
Improve tempfile handling
* rex (maketempfile): Rewrite. Return {fd name}. All callers changed. (forgettempfile): New function. (rex_command,rex_copy_to) (rex_login): Remove calls to cleanup. (MAIN): Call cleanup here
-rwxr-xr-xrex48
1 files changed, 30 insertions, 18 deletions
diff --git a/rex b/rex
index b2e0ff9..d2106b2 100755
--- a/rex
+++ b/rex
@@ -962,10 +962,23 @@ proc echo {a} {
proc maketempfile {} {
global cleanup_files
- set tempfile ".rex.[pid].tmp"
- exec "/bin/sh" -c "umask 077; touch $tempfile"
+ set tempfile ".rex.[pid].[incr ::tempfile_number]"
+ if {[catch {open $tempfile {RDWR CREAT EXCL TRUNC} 0600} fd]} {
+ terror "can't create temporary $tempfile: $fd"
+ exit 2
+ }
lappend cleanup_files $tempfile
- return $tempfile
+ return [list $fd $tempfile]
+}
+
+proc forgettempfile {file} {
+ global cleanup_files
+ if {[info exist cleanup_files]} {
+ set n [lsearch $cleanup_files $file]
+ if {$n != -1} {
+ set cleanup_files [lreplace $cleanup_files $n $n]
+ }
+ }
}
# Print program version and copyleft info.
@@ -1076,12 +1089,14 @@ proc writedb {file var} {
debug 2 writing database file $file
set temp [maketempfile]
- set fd [open $temp "w"]
+ set fd [lindex $temp 0]
+ set tempname [lindex $temp 1]
foreach key [lsort -command keycmp [array names x]] {
puts $fd "$key $x($key)"
}
close $fd
- file rename -force $temp $file
+ file rename -force $tempname $file
+ forgettempfile $tempname
}
# Update rexdb if it has been modified.
@@ -1187,8 +1202,9 @@ proc editdb {dbname} {
set ed "vi"
}
- set tempfile [maketempfile]
-
+ set temp [maketempfile]
+ set tempfile [lindex $temp 1]
+
mkdbview $dbname $tempfile rexdb
while 1 {
exec $ed $tempfile <@stdin >@stdout 2>@stderr
@@ -1205,8 +1221,6 @@ proc editdb {dbname} {
}
}
}
- file delete $tempfile
- trap SIG_DFL [list SIGINT SIGQUIT SIGHUP SIGTERM]
}
# Return user name for the given host.
@@ -2223,12 +2237,13 @@ proc rex_command args {
set config(command) [join $config(argv)]
if {$config(sudo) != "" && [ispipeline $config(argv)]} {
- set tempscriptname [maketempfile]
- debug 1 "creating temporary script file $tempscriptname"
- set fd [open $tempscriptname w]
+ set temp [maketempfile]
+ set tempname [lindex $temp 1]
+ set fd [lindex $temp 0]
+ debug 1 "creating temporary script file $tempname"
puts $fd $config(command)
close $fd
- set config(command) $tempscriptname
+ set config(command) $tempname
set config(argv) $config(command)
set config(option,copy) 1
}
@@ -2260,8 +2275,6 @@ proc rex_command args {
debug 2 "running epilogue script"
::hostproc::epilogue
-
- cleanup
}
proc rex_list {} {
@@ -2435,14 +2448,12 @@ proc rex_copy_to {} {
debug 2 "sudo=$config(sudo), progname=$config(progname), command=$config(command) ([llength $config(command)] args)"
debug 2 "... on $config(hosts)"
mainloop runcmd "$config(sudo)$config(command)"
- cleanup
return
}
debug 1 "copying $config(argv) to $config(hosts)"
mainloop runcp {*}$config(argv)
- cleanup
}
proc rex_login {} {
@@ -2530,7 +2541,6 @@ proc rex_login {} {
}
}
debug 2 "quitting"
- cleanup
}
proc rex_editdb {} {
@@ -2649,6 +2659,8 @@ switch -- $config(mode) {
}
}
+cleanup
+
exit 0

Return to:

Send suggestions and report system problems to the System administrator.