Module: Baal::MatchingOptions

Included in:
Daemon
Defined in:
lib/baal/matching_options.rb

Overview

MatchingOptions is a container for all methods relating to adding Matching Options to your start-stop-daemon script. Matching Options are used to target existing processes or identify new ones to be acted upon using a Command.

At least one Matching Option is required to execute a start-stop-daemon script.

Constant Summary collapse

MATCHING_OPTIONS =

All possible Matching Options

{
  pid: '--pid',
  ppid: '--ppid',
  pid_file: '--pidfile',
  exec: '--exec',
  name: '--name',
  user: '--user'
}.freeze

Instance Method Summary collapse

Instance Method Details

#exec(abs_path_to_exec) ⇒ Object Also known as: instance_of_exec

Used with Commands#start.

Checks for processes that are instances of the executable specified by abs_path_to_exec.

NOTE: might not work if used with interpreted scripts, as the executable

will point to the interpreter.

WARNING: take into account processes running from inside a chroot will

also be identified, so other restrictions might be needed to
avoid this.

Parameters:

  • abs_path_to_exec (String)

    the absolute path to the executable



75
76
77
78
# File 'lib/baal/matching_options.rb', line 75

def exec(abs_path_to_exec)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:exec]}=#{abs_path_to_exec}"
  self
end

#name(process_name) ⇒ Object Also known as: with_name

Checks for processes with the name specified by @process_name

NOTE: the name of the process is often the filename, but be wary that it

could have been changed by process itself

NOTE: for most systems the process name is retrieved from the process

comm name from the kernel. This is typically a shortened version
of the expected process name that is 15 characters long.

Parameters:

  • process_name (String)

    name of process



92
93
94
95
# File 'lib/baal/matching_options.rb', line 92

def name(process_name)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:name]}=#{process_name}"
  self
end

#pid(id) ⇒ Object Also known as: with_pid

Checks for a process with a specified process id.

TODO: Add error to catch for 0 or less.

Parameters:

  • id (String, Integer)

    the process id to be targeted. Must be greater than 0.



28
29
30
31
# File 'lib/baal/matching_options.rb', line 28

def pid(id)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:pid]}=#{id}"
  self
end

#pid_file(path) ⇒ Object Also known as: with_pid_file, pidfile

Checks whether or not a process has created the pid_file.

WARNING: if used alone AND the old process terminated without removing

the pid_file specified by @path, then this might cause
unintended consequences.

Parameters:

  • path (String)

    the path to the pid_file to be checked.



54
55
56
57
# File 'lib/baal/matching_options.rb', line 54

def pid_file(path)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:pid_file]}=#{path}"
  self
end

#ppid(id) ⇒ Object Also known as: with_ppid

Checks for a process with a specified parent process id.

TODO: Add error to catch for 0 or less.

Parameters:

  • id (String, Integer)

    the parent process id to be targeted. Must be greater than 0.



40
41
42
43
# File 'lib/baal/matching_options.rb', line 40

def ppid(id)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:ppid]}=#{id}"
  self
end

#user(username_or_uid) ⇒ Object Also known as: username, uid, owned_by_user, owned_by_username, owned_by_uid

Checks for processes owned by the user specified by a username or uid.

WARNING: Using this matching option ALONE will cause all matching

processes to be acted upon.

Parameters:

  • username_or_uid (String, Symbol, Integer)


105
106
107
108
# File 'lib/baal/matching_options.rb', line 105

def user(username_or_uid)
  @commands_and_opts.push "#{MATCHING_OPTIONS[:user]}=#{username_or_uid}"
  self
end