User:Rabisg/OpenRC

From Gentoo Wiki
Jump to:navigation Jump to:search

Service States in OpenRC

As a part of my project I have been trying to understand openrc's functionality so that I can abstract out the dependency of netifrc on openrc and run it on systemd too.

Storing values in Runscripts

The main functions providing this functionality are service_set_value and service_get_value ( refer to runscript(8) ). The corresponding C functions are rc_service_value_set (L821 src/librc/librc.c) and rc_service_value_get (L806 src/librc/librc.c). The implementation is rather straightforward. It stores the value in file at $SVCDIR/options/$SERVICE/$KEY (for example /run/openrc/options/net.wlan0/SSID stores the current SSID) and retrieves it on get operation. Keys do not persist across reboots

Maintaining state in runscripts

The functionality for marking services is coded in rc_service_mark (L647 src/librc/librc.c). Although the implementation is again by storing symlinks in the corresponding state folder ($SVCDIR/$STATE), the semantics of this operation are not quite clear to me. The implementation is as follows:

  • First find the init file corresponding to this service. Unless the service is being marked as stopped the init file should exist.
  • Create a symlink in the folder $SCVDIR/$STATE to the init file
  • Next delete all previous symlinks in other directories (i.e. delete all old values of state for the current init file). This has some caveats like values for service_was_inactive has to be updated correspondingly and service_hotplugged value shouldnt be changed
  • Finally if the service was stopped then cleanup its options and the daemons started by it.

Doubts

  • What is the exclusive state and what is it used for? (Lines 721-729 src/librc/librc.c)
  • In netifrc various modules mark the service as inactive. Who marks it active again? Does any other process try to restart net.wlan0? Currently my machine has net.wlan0 marked as inactive and netmount is scheduled to start when net.wlan0 starts ( symlink $SVCDIR/schedules/net.wlan0/netmount ) but net.wlan0 is not in the scheduled folder. So how does it know when to restart?