#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
Go to the source code of this file.
Classes | |
struct | rc_deptype |
struct | rc_depinfo |
RC | |
A service can be given as a full path or just its name. If its just a name then we try to resolve the service to a full path. This should allow the use if local init.d directories in the future. | |
enum | rc_service_state_t { rc_service_started, rc_service_stopped, rc_service_starting, rc_service_stopping, rc_service_inactive, rc_service_wasinactive, rc_service_coldplugged, rc_service_failed, rc_service_scheduled, rc_service_crashed } |
States a service can be in. | |
char * | rc_resolve_service (const char *service) |
bool | rc_service_exists (const char *service) |
char ** | rc_service_options (const char *service) |
char * | rc_service_description (const char *service, const char *option) |
bool | rc_service_in_runlevel (const char *service, const char *runlevel) |
bool | rc_service_state (const char *service, rc_service_state_t state) |
bool | rc_mark_service (const char *service, rc_service_state_t state) |
pid_t | rc_stop_service (const char *service) |
pid_t | rc_start_service (const char *service) |
int | rc_waitpid (pid_t pid) |
void | rc_schedule_start_service (const char *service, const char *service_to_start) |
char ** | rc_services_scheduled_by (const char *service) |
void | rc_schedule_clear (const char *service) |
bool | rc_wait_service (const char *service) |
char * | rc_get_service_option (const char *service, const char *option) |
bool | rc_set_service_option (const char *service, const char *option, const char *value) |
void | rc_set_service_daemon (const char *service, const char *exec, const char *name, const char *pidfile, bool started) |
bool | rc_service_started_daemon (const char *service, const char *exec, int indx) |
bool | rc_allow_plug (char *service) |
char * | rc_get_runlevel (void) |
void | rc_set_runlevel (const char *runlevel) |
bool | rc_runlevel_exists (const char *runlevel) |
char ** | rc_get_runlevels (void) |
bool | rc_runlevel_starting (void) |
bool | rc_runlevel_stopping (void) |
bool | rc_service_add (const char *runlevel, const char *service) |
bool | rc_service_delete (const char *runlevel, const char *service) |
char ** | rc_services_in_runlevel (const char *runlevel) |
char ** | rc_services_in_state (rc_service_state_t state) |
char ** | rc_services_scheduled (const char *service) |
pid_t * | rc_find_pids (const char *exec, const char *cmd, uid_t uid, pid_t pid) |
bool | rc_service_daemons_crashed (const char *service) |
Plugins | |
For each plugin loaded we will call rc_plugin_hook with the below enum and either the runlevel name or service name. Plugins are called when rc does something. This does not indicate an end result and the plugin should use the above functions to query things like service status.
The service hooks have extra ones - now and done. This is because after start_in we may start other services before we start the service in question. now shows we really will start the service now and done shows when we have done it as may start scheduled services at this point. | |
enum | rc_hook_t { rc_hook_runlevel_stop_in = 1, rc_hook_runlevel_stop_out = 4, rc_hook_runlevel_start_in = 5, rc_hook_runlevel_start_out = 8, rc_hook_abort = 99, rc_hook_service_stop_in = 101, rc_hook_service_stop_now, rc_hook_service_stop_done, rc_hook_service_stop_out, rc_hook_service_start_in, rc_hook_service_start_now, rc_hook_service_start_done, rc_hook_service_start_out } |
FILE * | rc_environ_fd |
int | rc_plugin_hook (rc_hook_t hook, const char *name) |
Dependencies | |
We analyse each init script and cache the resultant dependency tree. This tree can be accessed using the below structures and functions. | |
typedef struct rc_deptype | rc_deptype_t |
typedef struct rc_depinfo | rc_depinfo_t |
int | rc_update_deptree (bool force) |
rc_depinfo_t * | rc_load_deptree (void) |
rc_depinfo_t * | rc_get_depinfo (rc_depinfo_t *deptree, const char *service) |
rc_deptype_t * | rc_get_deptype (rc_depinfo_t *depinfo, const char *type) |
char ** | rc_get_depends (rc_depinfo_t *deptree, char **types, char **services, const char *runlevel, int options) |
char ** | rc_order_services (rc_depinfo_t *deptree, const char *runlevel, int options) |
void | rc_free_deptree (rc_depinfo_t *deptree) |
Defines | |
Reserved runlevel names | |
#define | RC_LEVEL_SYSINIT "sysinit" |
#define | RC_LEVEL_SINGLE "single" |
#define | RC_LEVEL_SHUTDOWN "shutdown" |
#define | RC_LEVEL_REBOOT "reboot" |
rc_ls_dir options | |
#define | RC_LS_INITD 0x01 |
Ensure that an init.d service exists for each file returned. | |
Dependency options | |
These options can change the services found by the rc_get_depinfo and rc_get_depends functions. | |
#define | RC_DEP_TRACE 0x01 |
#define | RC_DEP_STRICT 0x02 |
#define | RC_DEP_START 0x04 |
#define | RC_DEP_STOP 0x08 |
Functions | |
Memory Allocation | |
Ensure that if we cannot allocate the memory then we exit | |
void * | rc_xmalloc (size_t size) |
void * | rc_xrealloc (void *ptr, size_t size) |
char * | rc_xstrdup (const char *str) |
Utility | |
Although not RC specific functions, they are used by the supporting applications | |
char * | rc_strcatpaths (const char *path1, const char *paths,...) |
bool | rc_is_env (const char *variable, const char *value) |
bool | rc_exists (const char *pathname) |
bool | rc_is_file (const char *pathname) |
bool | rc_is_link (const char *pathname) |
bool | rc_is_dir (const char *pathname) |
bool | rc_is_exec (const char *pathname) |
char ** | rc_ls_dir (const char *dir, int options) |
bool | rc_rm_dir (const char *pathname, bool top) |
Configuration | |
char ** | rc_get_list (const char *file) |
char ** | rc_get_config (const char *file) |
char * | rc_get_config_entry (char **list, const char *entry) |
char ** | rc_filter_env (void) |
char ** | rc_make_env (void) |
String List functions | |
Handy functions for dealing with string arrays of char **. It's safe to assume that any function here that uses char ** is a string list that can be manipulated with the below functions. Every string list should be released with a call to rc_strlist_free. | |
char * | rc_strlist_add (char ***list, const char *item) |
char * | rc_strlist_addu (char ***list, const char *item) |
char * | rc_strlist_addsort (char ***list, const char *item) |
char * | rc_strlist_addsortc (char ***list, const char *item) |
char * | rc_strlist_addsortu (char ***list, const char *item) |
int | rc_strlist_delete (char ***list, const char *item) |
char * | rc_strlist_join (char ***list1, char **list2) |
void | rc_strlist_reverse (char **list) |
void | rc_strlist_free (char **list) |
#define RC_DEP_START 0x04 |
Runlevel is starting
#define RC_DEP_STOP 0x08 |
Runlevel is stopping
#define RC_DEP_STRICT 0x02 |
Only use services added to runlevels
#define RC_DEP_TRACE 0x01 |
Trace provided services
typedef struct rc_depinfo rc_depinfo_t |
Singly linked list of services and their dependencies
typedef struct rc_deptype rc_deptype_t |
Singly linked list of dependency types that list the services the type is for
enum rc_hook_t |
bool rc_allow_plug | ( | char * | service | ) |
Check if the service is allowed to be hot/cold plugged
service | to check |
bool rc_exists | ( | const char * | pathname | ) |
Check if the file exists or not
pathname | to check |
char** rc_filter_env | ( | void | ) |
Return a NULL terminated string list of variables allowed through from the current environemnt.
pid_t* rc_find_pids | ( | const char * | exec, | |
const char * | cmd, | |||
uid_t | uid, | |||
pid_t | pid | |||
) |
Find processes based on criteria. All of these are optional. pid overrides anything else. If both exec and cmd are given then we ignore exec.
exec | to check for | |
cmd | to check for | |
uid | to check for | |
pid | to check for |
void rc_free_deptree | ( | rc_depinfo_t * | deptree | ) |
Free a deptree and its information
deptree | to free |
char** rc_get_config | ( | const char * | file | ) |
Return a NULL terminated list of key=value lines from a file.
char* rc_get_config_entry | ( | char ** | list, | |
const char * | entry | |||
) |
Return the value of the entry from a key=value list.
rc_depinfo_t* rc_get_depinfo | ( | rc_depinfo_t * | deptree, | |
const char * | service | |||
) |
Get a services depedency information from a loaded tree
deptree | to search | |
service | to find |
rc_deptype_t* rc_get_deptype | ( | rc_depinfo_t * | depinfo, | |
const char * | type | |||
) |
Get a depenency type from the service dependency information
depinfo | service dependency to search | |
type | to find |
char** rc_get_list | ( | const char * | file | ) |
Return a NULL terminated list of non comment lines from a file.
char* rc_get_runlevel | ( | void | ) |
Return the current runlevel.
char** rc_get_runlevels | ( | void | ) |
Return a NULL terminated list of runlevels
char* rc_get_service_option | ( | const char * | service, | |
const char * | option | |||
) |
Return a saved value for a service
service | to check | |
option | to load |
bool rc_is_dir | ( | const char * | pathname | ) |
Check if the file is a directory or not
pathname | to check |
bool rc_is_env | ( | const char * | variable, | |
const char * | value | |||
) |
Check if an environment variable matches the given value
variable | to check | |
value | it should be |
bool rc_is_exec | ( | const char * | pathname | ) |
Check if the file is marked executable or not
pathname | to check |
bool rc_is_file | ( | const char * | pathname | ) |
Check if the file is a real file
pathname | to check |
bool rc_is_link | ( | const char * | pathname | ) |
Check if the file is a symbolic link or not
pathname | to check |
rc_depinfo_t* rc_load_deptree | ( | void | ) |
Load the cached dependency tree and return a pointer to it. This pointer should be freed with rc_free_deptree when done.
char** rc_ls_dir | ( | const char * | dir, | |
int | options | |||
) |
Return a NULL terminted sorted list of the contents of the directory
dir | to list | |
options | any options to apply |
char** rc_make_env | ( | void | ) |
Return a NULL terminated string list of enviroment variables made from our configuration files.
bool rc_mark_service | ( | const char * | service, | |
rc_service_state_t | state | |||
) |
Marks the service state
service | to mark | |
state | service should be in |
char** rc_order_services | ( | rc_depinfo_t * | deptree, | |
const char * | runlevel, | |||
int | options | |||
) |
List all the services that should be stoppned and then started, in order, for the given runlevel, including sysinit and boot services where approriate.
deptree | to search | |
runlevel | to change into | |
options | to pass |
int rc_plugin_hook | ( | rc_hook_t | hook, | |
const char * | name | |||
) |
Plugin entry point
hook | point | |
name | of runlevel or service |
char* rc_resolve_service | ( | const char * | service | ) |
Resolves a service name to its full path.
service | to check |
bool rc_rm_dir | ( | const char * | pathname, | |
bool | top | |||
) |
Remove a directory
pathname | to remove | |
top | remove the top level directory too |
bool rc_runlevel_exists | ( | const char * | runlevel | ) |
Checks if the runlevel exists or not
runlevel | to check |
bool rc_runlevel_starting | ( | void | ) |
Is the runlevel starting?
bool rc_runlevel_stopping | ( | void | ) |
Is the runlevel stopping?
void rc_schedule_clear | ( | const char * | service | ) |
Clear the list of services scheduled to be started by this service
service | to clear |
void rc_schedule_start_service | ( | const char * | service, | |
const char * | service_to_start | |||
) |
Schedule a service to be started when another service starts
service | that starts the scheduled service when started | |
service_to_start | service that will be started |
bool rc_service_add | ( | const char * | runlevel, | |
const char * | service | |||
) |
Add the service to the runlevel
runlevel | to add to | |
service | to add |
bool rc_service_daemons_crashed | ( | const char * | service | ) |
Checks that all daemons started with start-stop-daemon by the service are still running.
service | to check |
bool rc_service_delete | ( | const char * | runlevel, | |
const char * | service | |||
) |
Remove the service from the runlevel
runlevel | to remove from | |
service | to remove |
char* rc_service_description | ( | const char * | service, | |
const char * | option | |||
) |
Returns a description of what the service and/or option does.
service | to check | |
option | to check (if NULL, service description) |
bool rc_service_exists | ( | const char * | service | ) |
Checks if a service exists or not.
service | to check |
bool rc_service_in_runlevel | ( | const char * | service, | |
const char * | runlevel | |||
) |
Checks if a service is in a runlevel
service | to check | |
runlevel | it should be in |
char** rc_service_options | ( | const char * | service | ) |
Lists the extra options a service has
service | to load the options from |
bool rc_service_started_daemon | ( | const char * | service, | |
const char * | exec, | |||
int | indx | |||
) |
Check if the service started the daemon
service | to check | |
exec | to check | |
indx | of the daemon (optional - 1st daemon, 2nd daemon, etc) |
bool rc_service_state | ( | const char * | service, | |
rc_service_state_t | state | |||
) |
Checks if a service in in a state
service | to check | |
state | service should be in |
char** rc_services_in_runlevel | ( | const char * | runlevel | ) |
List the services in a runlevel
runlevel | to list |
char** rc_services_in_state | ( | rc_service_state_t | state | ) |
List the services in a state
state | to list |
char** rc_services_scheduled | ( | const char * | service | ) |
List the services shceduled to start when this one does
service | to check |
char** rc_services_scheduled_by | ( | const char * | service | ) |
Return a NULL terminated list of services that are scheduled to start when the given service has started
service | to check |
void rc_set_runlevel | ( | const char * | runlevel | ) |
Set the runlevel. This just changes the stored runlevel and does not start or stop any services.
runlevel | to store |
void rc_set_service_daemon | ( | const char * | service, | |
const char * | exec, | |||
const char * | name, | |||
const char * | pidfile, | |||
bool | started | |||
) |
Save the arguments to find a running daemon
service | to save arguments for | |
exec | that we started | |
name | of the process (optional) | |
pidfile | of the process (optional) | |
started | if true, add the arguments otherwise remove existing matching arguments |
bool rc_set_service_option | ( | const char * | service, | |
const char * | option, | |||
const char * | value | |||
) |
Save a persistent value for a service
service | to save for | |
option | to save | |
value | of the option |
pid_t rc_start_service | ( | const char * | service | ) |
Start a service
service | to start |
pid_t rc_stop_service | ( | const char * | service | ) |
Stop a service
service | to stop |
char* rc_strcatpaths | ( | const char * | path1, | |
const char * | paths, | |||
... | ||||
) |
Concatenate paths adding '/' if needed. The resultant pointer should be freed when finished with.
path1 | starting path | |
paths | NULL terminated list of paths to add |
char* rc_strlist_add | ( | char *** | list, | |
const char * | item | |||
) |
Duplicate the item, add it to end of the list and return a pointer to it.
list | to add the item too | |
item | to add. |
char* rc_strlist_addsort | ( | char *** | list, | |
const char * | item | |||
) |
Duplicate the item, add it to the list at the point based on locale and then return a pointer to it.
list | to add the item too | |
item | to add. |
char* rc_strlist_addsortc | ( | char *** | list, | |
const char * | item | |||
) |
Duplicate the item, add it to the list at the point based on C locale and then return a pointer to it.
list | to add the item too | |
item | to add. |
char* rc_strlist_addsortu | ( | char *** | list, | |
const char * | item | |||
) |
If the item does not exist in the list, duplicate it, add it to the list based on locale and then return a pointer to it.
list | to add the item too | |
item | to add. |
char* rc_strlist_addu | ( | char *** | list, | |
const char * | item | |||
) |
If the item does not exist in the list, duplicate it, add it to the list and then return a pointer to it.
list | to add the item too | |
item | to add. |
int rc_strlist_delete | ( | char *** | list, | |
const char * | item | |||
) |
Free the item and remove it from the list. Return 0 on success otherwise -1.
list | to add the item too | |
item | to add. |
void rc_strlist_free | ( | char ** | list | ) |
Frees each item on the list and the list itself.
list | to free |
char* rc_strlist_join | ( | char *** | list1, | |
char ** | list2 | |||
) |
Moves the contents of list2 onto list1, so list2 is effectively emptied. Returns a pointer to the last item on the new list.
list1 | to append to | |
list2 | to move from |
void rc_strlist_reverse | ( | char ** | list | ) |
Reverses the contents of the list.
list | to reverse |
int rc_update_deptree | ( | bool | force | ) |
Update the cached dependency tree if it's older than any init script, its configuration file or an external configuration file the init script has specified.
force | an update |
bool rc_wait_service | ( | const char * | service | ) |
Wait for a service to finish
service | to wait for |
int rc_waitpid | ( | pid_t | pid | ) |
Wait for a process to finish
pid | to wait for |
void* rc_xmalloc | ( | size_t | size | ) |
Allocate a block of memory
size | of memory to allocate |
void* rc_xrealloc | ( | void * | ptr, | |
size_t | size | |||
) |
Re-size a block of memory
ptr | to the block of memory to re-size | |
size | memory should be |
char* rc_xstrdup | ( | const char * | str | ) |
Duplicate a NULL terminated string
str | to duplicate |
FILE* rc_environ_fd |
Plugins should write FOO=BAR to this fd to set any environment variables they wish. Variables should be separated by NULLs.