Class: OsCtld::Daemon

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Assets::Definition
Defined in:
lib/osctld/daemon.rb

Defined Under Namespace

Modules: Hooks Classes: ClientHandler

Constant Summary collapse

SOCKET =
File.join(RunState::RUNDIR, 'osctld.sock')
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assets::Definition

#define_assets

Instance Attribute Details

#config ⇒ Config (readonly)

Returns:



75
76
77
# File 'lib/osctld/daemon.rb', line 75

def config
  @config
end

#initialized ⇒ Boolean (readonly)

Returns:

  • (Boolean)


81
82
83
# File 'lib/osctld/daemon.rb', line 81

def initialized
  @initialized
end

#started_at ⇒ Time (readonly)

Returns:

  • (Time)


78
79
80
# File 'lib/osctld/daemon.rb', line 78

def started_at
  @started_at
end

Class Method Details

.create(config) ⇒ Object

Parameters:

  • config (String) —

    path to config file



63
64
65
66
67
# File 'lib/osctld/daemon.rb', line 63

def create(config)
  raise 'Daemon already instantiated' if @@instance

  @@instance = new(config)
end

.get ⇒ Object



69
70
71
# File 'lib/osctld/daemon.rb', line 69

def get
  @@instance
end

Instance Method Details

#abort_shutdown ⇒ Object



325
326
327
328
329
330
331
# File 'lib/osctld/daemon.rb', line 325

def abort_shutdown
  @shutdown_mutex.synchronize do
    @abort_shutdown = true

    remove_shutdown_marker
  end
end

#abort_shutdown? ⇒ Boolean

Returns:

  • (Boolean)


345
346
347
# File 'lib/osctld/daemon.rb', line 345

def abort_shutdown?
  @shutdown_mutex.synchronize { @abort_shutdown }
end

#assets ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/osctld/daemon.rb', line 186

def assets
  define_assets do |add|
    RunState.assets(add)

    add.socket(
      SOCKET,
      desc: 'Management socket',
      user: 0,
      group: 0,
      mode: 0o600
    )

    Devices::V2::BpfProgramCache.assets(add)
  end
end

#begin_shutdown ⇒ Object



321
322
323
# File 'lib/osctld/daemon.rb', line 321

def begin_shutdown
  @shutdown_mutex.synchronize { begin_shutdown_locked }
end

#begin_shutdown_locked ⇒ Object (protected)



355
356
357
358
# File 'lib/osctld/daemon.rb', line 355

def begin_shutdown_locked
  @abort_shutdown = false
  File.new(RunState::SHUTDOWN_MARKER, 'w', 0o000).close
end

#confirm_shutdown ⇒ Object



333
334
335
336
337
338
339
# File 'lib/osctld/daemon.rb', line 333

def confirm_shutdown
  @shutdown_mutex.synchronize do
    raise CommandFailed, 'shutdown aborted' if @abort_shutdown

    confirm_shutdown_locked
  end
end

#confirm_shutdown_locked ⇒ Object (protected)



366
367
368
369
370
371
372
# File 'lib/osctld/daemon.rb', line 366

def confirm_shutdown_locked
  unless File.exist?(RunState::SHUTDOWN_MARKER)
    File.new(RunState::SHUTDOWN_MARKER, 'w', 0o100).close
  end

  File.chmod(0o100, RunState::SHUTDOWN_MARKER)
end

#join_server ⇒ Object



218
219
220
# File 'lib/osctld/daemon.rb', line 218

def join_server
  @server_thread.join
end

#log_type ⇒ Object



349
350
351
# File 'lib/osctld/daemon.rb', line 349

def log_type
  'daemon'
end

#remove_shutdown_marker ⇒ Object (protected)



360
361
362
363
364
# File 'lib/osctld/daemon.rb', line 360

def remove_shutdown_marker
  File.unlink(RunState::SHUTDOWN_MARKER)
rescue Errno::ENOENT
  # ignore
end

#run_pre_stop_hooks ⇒ Object (protected)



374
375
376
377
378
# File 'lib/osctld/daemon.rb', line 374

def run_pre_stop_hooks
  Hook.run(self, :pre_stop)
rescue HookFailed => e
  log(:warn, "Daemon pre-stop hook failed: #{e.message}")
end

#run_shutdown(&block) ⇒ Object

Internal command entry point. Joined callers retain this operation's outcome even if a later request starts before they wake up.



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
# File 'lib/osctld/daemon.rb', line 256

def run_shutdown(&block)
  result = error = operation = nil
  owner = completed = false

  # Admission and publication must survive interruption; resource work and
  # its lock cleanup run outside the guard.
  Thread.handle_interrupt(Exception => :never) do
    @shutdown_mutex.synchronize do
      if @shutdown_operation && !@shutdown_operation[:done]
        operation = @shutdown_operation
      else
        owner = true
        operation = { done: false, result: nil, error: nil }
        @shutdown_operation = operation
        begin_shutdown_locked
      end
    end

    if owner
      result = Thread.handle_interrupt(Exception => :immediate, &block)
      completed = true
    else
      Thread.handle_interrupt(Exception => :on_blocking) do
        @shutdown_mutex.synchronize do
          @shutdown_cv.wait(@shutdown_mutex) until operation[:done]
        end
      end
      result = operation[:result]
      error = operation[:error]
    end
  rescue Exception => e # rubocop:disable Lint/RescueException -- Publish interruptions to joined callers too.
    error = e
  ensure
    if owner
      error ||= CommandFailed.new('shutdown aborted') unless completed

      @shutdown_mutex.synchronize do
        if @abort_shutdown
          # A joined CLI may have recreated its pre-request marker
          # after abort. It must not restore pending restart intent.
          remove_shutdown_marker
          error ||= CommandFailed.new('shutdown aborted')
        end
        if error.nil?
          confirm_shutdown_locked
        end
      rescue Exception => e # rubocop:disable Lint/RescueException -- Preserve interruption and cleanup outcomes.
        error ||= e
      ensure
        operation[:result] = result
        operation[:error] = error
        operation[:done] = true
        @shutdown_cv.broadcast
      end

      # A nonlocal block exit is not successful completion either.
      raise error if !completed && $!.nil?
    end
  end

  raise error if error

  result
end

#serve ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/osctld/daemon.rb', line 202

def serve
  @server_thread = Thread.new do
    log(:info, "Listening on control socket at #{SOCKET}")

    socket = UNIXServer.new(SOCKET)
    File.chmod(0o600, SOCKET)

    @server = Generic::Server.new(
      socket,
      Daemon::ClientHandler,
      thread_group: :management
    )
    @server.start
  end
end

#setup ⇒ Object



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
# File 'lib/osctld/daemon.rb', line 120

def setup
  # Setup /run/osctl
  RunState.create

  # Open start config
  start_cfg = RunState.open_start_config

  SystemLimits.instance

  CpuScheduler.setup

  # Increase allowed number of open files
  PrLimits.set(Process.pid, PrLimits::NOFILE, 131_072, 131_072)

  # Setup shared AppArmor files
  if AppArmor.enabled?
    log(:info, 'AppArmor enabled')
    AppArmor.setup
  else
    log(:info, 'AppArmor disabled')
  end

  # Setup BPF FS
  BpfFs.setup

  # User-control supervisor
  UserControl::Supervisor.instance

  # Send/Receive hooks and server
  SendReceive.setup

  # Setup network interfaces
  NetInterface.setup

  # Start accepting client commands
  serve

  # Load data pools
  if KernelParams.import_pools?
    autostart = KernelParams.autostart_cts?

    Commands::Pool::Import.run(all: true, autostart:)

    unless autostart
      log(:info, 'Container autostart disabled by kernel parameter')
    end
  else
    log(:info, 'Pool autoimport disabled by kernel parameter')
  end

  # Resume shutdown
  if shutdown?
    log(:info, 'Resuming shutdown')
    Commands::Self::Shutdown.run
  end

  # Close start config
  start_cfg.close

  # All components are up
  @initialized = true

  # Wait for the server to finish
  join_server
end

#shutdown? ⇒ Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/osctld/daemon.rb', line 341

def shutdown?
  @shutdown_mutex.synchronize { File.exist?(RunState::SHUTDOWN_MARKER) }
end

#stop ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/osctld/daemon.rb', line 222

def stop
  @stopping = true
  log(:info, 'Stopping daemon')
  run_pre_stop_hooks
  @server.stop if @server
  join_server if @server_thread && @server_thread != Thread.current
  FileUtils.rm_f(SOCKET)
  ThreadReaper.drain(group: :management)
  UserControl.stop
  ThreadReaper.stop
  Eventd.shutdown
  SendReceive.stop
  DB::Repositories.each(&:stop)
  DB::Pools.get.each(&:stop)
  CpuScheduler.shutdown
  Monitor::Master.stop
  LockRegistry.stop
  log(:info, 'Daemon stopped successfully')
  # Stop can run from a signal helper thread while other background threads
  # are still reacting to shutdown. The graceful cleanup above is complete,
  # so terminate the process without waiting for unmanaged threads.
  exit!
end

#stopping? ⇒ Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/osctld/daemon.rb', line 246

def stopping?
  @stopping
end

#user_hook_script_dir ⇒ Object



250
251
252
# File 'lib/osctld/daemon.rb', line 250

def user_hook_script_dir
  RunState::DAEMON_HOOK_DIR
end