Runlevel editor library

documentation for developers

If you need activate or de-activate services from your yast2 package, use module Service. It is a replacement for the old client runlevel_adjust that you should have been using till now. You may also use it to obtain information about services. Following functions are self-containing. You do not need to run Service::Read if you want to use any of them.

Enabling/disabling service

boolean Service::Adjust (string name, string action);
Does some operation with init script. name is the name of the init script. action is one of: "enable", "disable", "default".

"enable" and "disable" take no action when init script links are OK, but "default" runs always insserv.
example: Service::Adjust ("cups", "disable");

Specifying runlevels for service

boolean Service::Finetune (string name, list rl);
Sets service to run in list rl. rl is list of strings.
example: Service::Finetune ("cups", [ "5" ]);
example: Service::Finetune ("cups", [ "S", "3", "5" ]);

Init script actions

integer RunInitScript (string name, string param);
Runs init script name with parameter param. Returns init scripts exit value. I do not think it is worth to import module Service only because of this one function when you may simply call SCR::Execute (.target.bash, ...) with the same result. But if you use Service for something else, this function may increase readability of the code.
example: RunInitScript ("cups", "restart");

Service info

integer Service::Status (string name)
map Service::Info (string name)
map Service::FullInfo (string name)

Service::Status tells whether service runs. It calls init script status and returns exit value.
Service::Info returns information about service. The map contains:

$[ "defstart" : [ ... ], // list of strings
   "defstop"  : [ ... ], // list of default runlevels

   "start"    : [ ... ], // list of strings
   "stop"     : [ ... ], // list of real runlevels

   "reqstart" : [ ... ], // list of strings
   "reqstop"  : [ ... ], // prerequisites

   "description": string,// description
]
Values "def{start|stop}", "req{start|stop}" and "description" are taken from init script comments. "start" and "stop" are taken from links. Service::FullInfo combines info from Service::Info and Service::Status into one map. It adds key "started" with integeral value of script status. Service is enabled if "start" is not empty.

Is service enabled?

boolean Service::Enabled (string name)
Returns true if service is set to run in any runlevel. False otherwise.

Example of usage

import "Service";

boolean Read () ``{
	if (0 != Service::RunInitScript ("foo", "restart"))
	{
		y2error ("Can not start service");
		return false;
	}
	// ... your code ...
}

boolean Write () ``{
	// ... write settings ...

	// set service to correct state
	if (start_it)
	{
		// enable service
		Service::Adjust ("foo", "enable");
		// reload changed settings
		Service::RunInitScript ("foo", "reload");
	}
	else
	{
		// disable service
		Service::Adjust ("foo", "disable");
		// and stop it
		Service::RunInitScript ("foo", "stop");
	}
}

What should you know?

It has sense to run some services only in case that some other service runs. Now the only case known to me is portmap that is needed by NFS, NIS and others. Runlevel UI cares about this case. But if you use functions like Service::Adjust, you must take care about these dependencies yourself. If you know about other dependencies, let me know, I will implement them in UI.
Just for your info: Runleve UI warns user who wants to stop service xdm. If you know about another cases where it is wise idea to warn user, please let me know...

Autogenerated documentation

Can be found here.


Petr Blahos pblahos@suse.cz