/usr/bin/rlock is in ruby-lockfile 2.1.0-4.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/env ruby
#
# built-in
#
require 'optparse'
require 'logger'
#
# http://raa.ruby-lang.org/project/lockfile/
#
require 'lockfile'
class Main
#--{{{
VERSION = Lockfile::VERSION
USAGE =
#--{{{
<<-usage
NAME
rlock v#{ VERSION }
SYNOPSIS
rlock [options]+ lockfile [program [args]+ | -- program options+ [args]+]
DESCRIPTTION
rlock creates NFS safe lockfiles. it can optionally run a program while
holding the lock, ensuring lockfile removal on program exit. if a program
is specified to be run rlock will spawn a background thread to kept the
lockfile 'fresh' by touching it at a regular interval. in this way a lease
is maintained on the lockfile and other processes attempting to obtain the
lock can determine that it is in use. see the '--refresh' option for how to
control the touch interval. any other process trying to obtain a lock will
automatically remove a stale lockfile; a stale lockfile is one that is older
than a certain age. this age be controled via the '--max_age' option.
ENVIRONMENT
LOCKFILE_DEBUG=1
causes internal actions of the library to be shown on STDERR
DIAGNOSTICS
rlock attempts to exit with the status of 'program' except where it
cannot due to exceptional conditions. in addition the message
'RLOCK SUBCOMMAND FAILURE'
will be printed on STDERR if 'program' exits with non-zero status.
success => $? == 0
failure => $? != 0
AUTHOR
ara.t.howard@gmail.com
BUGS
1 < bugno && bugno < 42
OPTIONS
usage
#--}}}
EXAMPLES =
#--{{{
<<-examples
EXAMPLES
0) simple usage - create lockfile in an atomic fashion (obtain a lock)
~ > rlock lockfile
1) safe usage - create a lockfile, execute a command, and remove lockfile
~ > rlock lockfile ls lockfile
2) same as above, but logging verbose messages
~ > rlock -v4 lockfile ls lockfile
3) same as above, but logging verbose messages and showing actions internal
to lockfile library
~ > rlock -v4 -d lockfile ls lockfile
4) same as above
~ > LOCKFILE_DEBUG=1 rlock -v4 lockfile ls lockfile
5) same as above
~ > export LOCKFILE_DEBUG=1
~ > rlock -v4 lockfile ls lockfile
6) you need to tell the option parser to stop parsing rlock options if you
intend to pass options to 'program'
~ > rlock -v4 -d lockfile -- ls -ltar lockfile
without the '--' rlock would consume the '-ltar' option as one of
it's own.
7) lock lockfile and exec 'program' - remove the lockfile if it is older
than 4242 seconds
~ > rlock --max_age=4242 lockfile program
8) lock lockfile and exec 'program' - remove the lockfile if it is older
than 4242 seconds, set the refresh rate to be 8 seconds.
~ > rlock --max_age=4242 --refresh=8 lockfile program
9) same as above, but fail if lockfile cannot be obtained within 1 minute
~ > rlock --max_age=4242 --refresh=8 --timeout=60 lockfile program
10) lockfile creation involves making some temporary files. normally these
are cleaned up unless rlock is killed with 'kill -9'. these temp files are
normally 'sweeped' - searched for and removed - unless the '--dont_sweep'
option is given. note that sweeping can remove ONLY old temp files created
by the same host since there is otherwise no way to tell if the offending
process is still running.
lock lockfile and run program - do not do any sweeping
~ > rlock --dont_sweep lockfile program
examples
#--}}}
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
attr :argv
attr :op
attr :logger
attr :config
def initialize argv = ARGV
#--{{{
@argv = mcp argv
parse_opts
if @opt_version
puts Main::VERSION
exit EXIT_SUCCESS
end
if @opt_help
usage
exit EXIT_SUCCESS
end
parse_argv
run
#--}}}
end
def run
#--{{{
init_logging
debug{ "lockpath <#{ @lockpath }>" }
opts = {}
options =
%w(retries max_age sleep_inc min_sleep max_sleep suspend timeout refresh poll_retries poll_max_sleep)
options.each do |opt|
#if((val = eval("opt_#{ opt }")))
if(send("opt_#{ opt }?"))
val = send "opt_#{ opt }"
begin
val = (opts[opt] = String === val ? Integer(val) : val)
logger.debug{ "<#{ opt }> <#{ val.inspect }>" }
rescue
logger.fatal{ "illegal value <#{ val.inspect }> for opt <#{ opt }>" }
exit EXIT_FAILURE
end
end
end
opts['debug'] = true if opt_debug
begin
case @argv.size
when 0
opts['dont_clean'] = true
logger.debug{ "opts <#{ opts.inspect }>" }
logger.debug{ "aquiring lock <#{ @lockpath }>..." }
#
# simple usage - just create the lockfile with opts
#
lockfile = ::Lockfile.new @lockpath, opts
lockfile.lock
logger.debug{ "aquired lock <#{ @lockpath }>" }
else
logger.debug{ "opts <#{ opts.inspect }>" }
logger.debug{ "aquiring lock <#{ @lockpath }>..." }
#
# block usage - create the lockfile with opts, run block, rm lockfile
#
status = 1
lockfile = ::Lockfile.new @lockpath, opts
lockfile.lock do
logger.debug{ "aquired lock <#{ @lockpath }>" }
logger.debug{ "cmd <#{ @argv.join ' ' }>" }
v = nil
begin
v = $VERBOSE
$VERBOSE = nil
STDOUT.flush
STDERR.flush
fork{ exec(*@argv) }
pid, status = Process::wait2
ensure
$VERBOSE = v
end
logger.debug{ "status <#{ $? }>" }
end
status = status.exitstatus
STDERR.puts "RLOCK SUBCOMMAND FAILURE" unless status == 0
exit status
end
rescue => e
logger.fatal{ e }
exit EXIT_FAILURE
end
exit EXIT_SUCCESS
#--}}}
end
def parse_opts
#--{{{
@op = OptionParser::new
@op.banner = ''
define_options
@op.parse! argv
#--}}}
end
def parse_argv
#--{{{
usage and exit EXIT_FAILURE if @argv.empty?
@lockpath = @argv.shift
#--}}}
end
def define_options
#--{{{
options = [
['--retries=n','-r', "default(#{ Lockfile.retries.inspect }) - (nil => forever)"],
['--max_age=n','-a', "default(#{ Lockfile.max_age.inspect })"],
['--sleep_inc=n','-s', "default(#{ Lockfile.sleep_inc.inspect })"],
['--max_sleep=n','-p', "default(#{ Lockfile.max_sleep.inspect })"],
['--min_sleep=n','-P', "default(#{ Lockfile.min_sleep.inspect })"],
['--suspend=n','-u', "default(#{ Lockfile.suspend.inspect })"],
['--timeout=n','-t', "default(#{ Lockfile.timeout.inspect }) - (nil => never)"],
['--refresh=n','-f', "default(#{ Lockfile.refresh.inspect })"],
['--debug','-d', "default(#{ Lockfile.debug.inspect })"],
['--poll_retries=n','-R', "default(#{ Lockfile.poll_retries.inspect })"],
['--poll_max_sleep=n','-S', "default(#{ Lockfile.poll_max_sleep.inspect })"],
['--dont_sweep','-w', "default(#{ Lockfile.dont_sweep.inspect })"],
['--version'],
['--verbosity=0-4|debug|info|warn|error|fatal','-v'],
['--log=path','-l'],
['--log_age=log_age'],
['--log_size=log_size'],
['--help','-h'],
]
options.each do |option|
opt = option.first.gsub(%r/(?:--)|(?:=.*$)/o,'').strip
get, set = opt_attr opt
value4 = lambda do |v|
case v
when NilClass, %r/^t|true$/i
true
when %r/^f|false$/i
false
when %r/^nil|nul|null$/i
nil
else
v
end
end
@op.def_option(*option) do |v|
send set, value4[v]
end
end
#--}}}
end
%w(debug info warn error fatal).each do |m|
eval "def #{ m }(*args,&block);@logger.#{ m }(*args,&block);end"
end
def init_logging
#--{{{
if @opt_log_age
@opt_log_age = @opt_log_age.to_i if @opt_log_age =~ /\d/
end
if @opt_log_size
@opt_log_size = @opt_log_size.to_i if @opt_log_size =~ /\d/
end
$logger = @logger = Logger::new(@opt_log || STDERR, @opt_log_age, @opt_log_size)
level = nil
@opt_verbosity ||= 'info'
@opt_verbosity =
case @opt_verbosity
when /^\s*(?:4|d|debug)\s*$/io
level = 'Logging::DEBUG'
4
when /^\s*(?:3|i|info)\s*$/io
level = 'Logging::INFO'
3
when /^\s*(?:2|w|warn)\s*$/io
level = 'Logging::WARN'
2
when /^\s*(?:1|e|error)\s*$/io
level = 'Logging::ERROR'
1
when /^\s*(?:0|f|fatal)\s*$/io
level = 'Logging::FATAL'
0
else
abort "illegal verbosity setting <#{ @opt_verbosity }>"
end
@logger.level = 2 - ((@opt_verbosity % 5) - 2)
#--}}}
end
def usage io = STDOUT
#--{{{
io << USAGE
io << "\n"
io << @op
io << "\n"
io << EXAMPLES if defined? EXAMPLES
self
#--}}}
end
def opt_attr opt
#--{{{
query = "opt_#{ opt }?"
get = "opt_#{ opt }"
set = "#{ get }="
code = <<-code
class << self
def #{ query }; defined? @#{ get }; end
def #{ get }; defined?(@#{ get }) ? @#{ get } : nil; end
def #{ set } value; @#{ get } = value; end
end
code
instance_eval code
[get, set]
#--}}}
end
def mcp obj
#--{{{
Marshal::load(Marshal::dump(obj))
#--}}}
end
#--}}}
end
Main::new
|