/[baselayout]/trunk/src/rc.h
Gentoo

Contents of /trunk/src/rc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2996 - (show annotations) (download) (as text)
Thu Oct 4 17:26:57 2007 UTC (6 years, 10 months ago) by uberlord
File MIME type: text/x-chdr
File size: 16214 byte(s)
inline rc_xmalloc, rc_xrealloc and rc_xstrdup so that the library doesn't expose them.
1 /*!
2 * @file rc.h
3 * @brief Describes how to interface with the RC library
4 * @internal
5 *
6 * Copyright 2007 Gentoo Foundation
7 * Released under the GPLv2
8 */
9
10 #ifndef __RC_H__
11 #define __RC_H__
12
13 #ifdef __GNUC__
14 # define GCC_VERSION (__GNUC__ * 1000 + __GNUC__MINOR)
15 # if (GCC_VERSION >= 3005)
16 # define SENTINEL __attribute__ ((__sentinel__))
17 # endif
18 #endif
19 #ifndef SENTINEL
20 # define SENTINEL
21 #endif
22
23 #include <sys/types.h>
24 #include <stdbool.h>
25 //#include <stdio.h>
26
27 /*! @name Reserved runlevel names */
28 #define RC_LEVEL_SYSINIT "sysinit"
29 #define RC_LEVEL_SINGLE "single"
30 #define RC_LEVEL_SHUTDOWN "shutdown"
31 #define RC_LEVEL_REBOOT "reboot"
32
33 /*! Return the current runlevel.
34 * @return the current runlevel */
35 char *rc_runlevel_get (void);
36
37 /*! Checks if the runlevel exists or not
38 * @param runlevel to check
39 * @return true if the runlevel exists, otherwise false */
40 bool rc_runlevel_exists (const char *runlevel);
41
42 /*! Return a NULL terminated list of runlevels
43 * @return a NULL terminated list of runlevels */
44 char **rc_runlevel_list (void);
45
46 /*! Set the runlevel.
47 * This just changes the stored runlevel and does not start or stop any services.
48 * @param runlevel to store */
49 bool rc_runlevel_set (const char *runlevel);
50
51 /*! Is the runlevel starting?
52 * @return true if yes, otherwise false */
53 bool rc_runlevel_starting (void);
54
55 /*! Is the runlevel stopping?
56 * @return true if yes, otherwise false */
57 bool rc_runlevel_stopping (void);
58
59 /*! @name RC
60 * A service can be given as a full path or just its name.
61 * If its just a name then we try to resolve the service to a full path.
62 * This should allow the use if local init.d directories in the future. */
63
64 /*! @brief States a service can be in */
65 typedef enum
66 {
67 /* These are actual states
68 * The service has to be in one only at all times */
69 RC_SERVICE_STOPPED = 0x0001,
70 RC_SERVICE_STARTED = 0x0002,
71 RC_SERVICE_STOPPING = 0x0004,
72 RC_SERVICE_STARTING = 0x0008,
73 RC_SERVICE_INACTIVE = 0x0010,
74
75 /* Service may or may not have been coldplugged */
76 RC_SERVICE_COLDPLUGGED = 0x0100,
77
78 /* Optional states service could also be in */
79 RC_SERVICE_FAILED = 0x0200,
80 RC_SERVICE_SCHEDULED = 0x0400,
81 RC_SERVICE_WASINACTIVE = 0x0800,
82 } rc_service_state_t;
83
84 /*! Add the service to the runlevel
85 * @param runlevel to add to
86 * @param service to add
87 * @return true if successful, otherwise false */
88 bool rc_service_add (const char *runlevel, const char *service);
89
90 /*! Remove the service from the runlevel
91 * @param runlevel to remove from
92 * @param service to remove
93 * @return true if sucessful, otherwise false */
94 bool rc_service_delete (const char *runlevel, const char *service);
95
96 /*! Save the arguments to find a running daemon
97 * @param service to save arguments for
98 * @param exec that we started
99 * @param name of the process (optional)
100 * @param pidfile of the process (optional)
101 * @param started if true, add the arguments otherwise remove existing matching arguments */
102 void rc_service_daemon_set (const char *service, const char *exec,
103 const char *name, const char *pidfile,
104 bool started);
105
106 /*! Returns a description of what the service and/or option does.
107 * @param service to check
108 * @param option to check (if NULL, service description)
109 * @return a newly allocated pointer to the description */
110 char *rc_service_description (const char *service, const char *option);
111
112 /*! Checks if a service exists or not.
113 * @param service to check
114 * @return true if service exists, otherwise false */
115 bool rc_service_exists (const char *service);
116
117 /*! Checks if a service is in a runlevel
118 * @param service to check
119 * @param runlevel it should be in
120 * @return true if service is in the runlevel, otherwise false */
121 bool rc_service_in_runlevel (const char *service, const char *runlevel);
122
123 /*! Marks the service state
124 * @param service to mark
125 * @param state service should be in
126 * @return true if service state change was successful, otherwise false */
127 bool rc_service_mark (const char *service, rc_service_state_t state);
128
129 /*! Lists the extra options a service has
130 * @param service to load the options from
131 * @return NULL terminated string list of options */
132 char **rc_service_options (const char *service);
133
134 /*! Check if the service is allowed to be hot/cold plugged
135 * @param service to check
136 * @return true if allowed, otherwise false */
137 bool rc_service_plugable (char *service);
138
139 /*! Resolves a service name to its full path.
140 * @param service to check
141 * @return pointer to full path of service */
142 char *rc_service_resolve (const char *service);
143
144 /*! Schedule a service to be started when another service starts
145 * @param service that starts the scheduled service when started
146 * @param service_to_start service that will be started */
147 bool rc_service_schedule_start (const char *service,
148 const char *service_to_start);
149 /*! Return a NULL terminated list of services that are scheduled to start
150 * when the given service has started
151 * @param service to check
152 * @return NULL terminated list of services scheduled to start */
153 char **rc_services_scheduled_by (const char *service);
154
155 /*! Clear the list of services scheduled to be started by this service
156 * @param service to clear */
157 void rc_service_schedule_clear (const char *service);
158
159 /*! Checks if a service in in a state
160 * @param service to check
161 * @return state of the service */
162 rc_service_state_t rc_service_state (const char *service);
163
164 /*! Start a service
165 * @param service to start
166 * @return pid of the service starting process */
167 pid_t rc_service_start (const char *service);
168
169 /*! Stop a service
170 * @param service to stop
171 * @return pid of service stopping process */
172 pid_t rc_service_stop (const char *service);
173
174 /*! Check if the service started the daemon
175 * @param service to check
176 * @param exec to check
177 * @param indx of the daemon (optional - 1st daemon, 2nd daemon, etc)
178 * @return true if started by this service, otherwise false */
179 bool rc_service_started_daemon (const char *service, const char *exec,
180 int indx);
181
182 /*! Return a saved value for a service
183 * @param service to check
184 * @param option to load
185 * @return saved value */
186 char *rc_service_value_get (const char *service, const char *option);
187
188 /*! Save a persistent value for a service
189 * @param service to save for
190 * @param option to save
191 * @param value of the option
192 * @return true if saved, otherwise false */
193 bool rc_service_value_set (const char *service, const char *option,
194 const char *value);
195
196 /*! Wait for a service to finish
197 * @param service to wait for
198 * @return true if service finished before timeout, otherwise false */
199 bool rc_service_wait (const char *service);
200
201 /*! List the services in a runlevel
202 * @param runlevel to list
203 * @return NULL terminated list of services */
204 char **rc_services_in_runlevel (const char *runlevel);
205
206 /*! List the services in a state
207 * @param state to list
208 * @return NULL terminated list of services */
209 char **rc_services_in_state (rc_service_state_t state);
210
211 /*! List the services shceduled to start when this one does
212 * @param service to check
213 * @return NULL terminated list of services */
214 char **rc_services_scheduled (const char *service);
215
216 /*! Checks that all daemons started with start-stop-daemon by the service
217 * are still running.
218 * @param service to check
219 * @return true if all daemons started are still running, otherwise false */
220 bool rc_service_daemons_crashed (const char *service);
221
222 /*! Wait for a process to finish
223 * @param pid to wait for
224 * @return exit status of the process */
225 int rc_waitpid (pid_t pid);
226
227 /*! Find processes based on criteria.
228 * All of these are optional.
229 * pid overrides anything else.
230 * If both exec and cmd are given then we ignore exec.
231 * @param exec to check for
232 * @param cmd to check for
233 * @param uid to check for
234 * @param pid to check for
235 * @return NULL terminated list of pids */
236 pid_t *rc_find_pids (const char *exec, const char *cmd,
237 uid_t uid, pid_t pid);
238
239 /*! @name Dependency options
240 * These options can change the services found by the rc_get_depinfo and
241 * rc_get_depends functions. */
242 /*! Trace provided services */
243 #define RC_DEP_TRACE 0x01
244 /*! Only use services added to runlevels */
245 #define RC_DEP_STRICT 0x02
246 /*! Runlevel is starting */
247 #define RC_DEP_START 0x04
248 /*! Runlevel is stopping */
249 #define RC_DEP_STOP 0x08
250
251 /*! @name Dependencies
252 * We analyse each init script and cache the resultant dependency tree.
253 * This tree can be accessed using the below functions. */
254
255 #ifndef _IN_LIBRC
256 /* Handles to internal structures */
257 typedef void *rc_depinfo_t;
258 #endif
259
260 /*! Update the cached dependency tree if it's older than any init script,
261 * its configuration file or an external configuration file the init script
262 * has specified.
263 * @return true if successful, otherwise false */
264 bool rc_deptree_update (void);
265
266 /*! Check if the cached dependency tree is older than any init script,
267 * its configuration file or an external configuration file the init script
268 * has specified.
269 * @return true if it needs updating, otherwise false */
270 bool rc_deptree_update_needed (void);
271
272 /*! Load the cached dependency tree and return a pointer to it.
273 * This pointer should be freed with rc_deptree_free when done.
274 * @return pointer to the dependency tree */
275 rc_depinfo_t *rc_deptree_load (void);
276
277 /*! List all the services in order that the given services have
278 * for the given types and options.
279 * @param deptree to search
280 * @param types to use (ineed, iuse, etc)
281 * @param services to check
282 * @param options to pass
283 * @return NULL terminated list of services in order */
284 char **rc_deptree_depends (rc_depinfo_t *deptree, char **types,
285 char **services, const char *runlevel, int options);
286
287 /*! List all the services that should be stoppned and then started, in order,
288 * for the given runlevel, including sysinit and boot services where
289 * approriate.
290 * @param deptree to search
291 * @param runlevel to change into
292 * @param options to pass
293 * @return NULL terminated list of services in order */
294 char **rc_deptree_order_services (rc_depinfo_t *deptree, const char *runlevel,
295 int options);
296
297 /*! Free a deptree and its information
298 * @param deptree to free */
299 void rc_deptree_free (rc_depinfo_t *deptree);
300
301 /*! @name Plugins
302 * For each plugin loaded we will call rc_plugin_hook with the below
303 * enum and either the runlevel name or service name.
304 *
305 * Plugins are called when rc does something. This does not indicate an
306 * end result and the plugin should use the above functions to query things
307 * like service status.
308 *
309 * The service hooks have extra ones - now and done. This is because after
310 * start_in we may start other services before we start the service in
311 * question. now shows we really will start the service now and done shows
312 * when we have done it as may start scheduled services at this point. */
313 /*! Points at which a plugin can hook into RC */
314 typedef enum
315 {
316 RC_HOOK_RUNLEVEL_STOP_IN = 1,
317 RC_HOOK_RUNLEVEL_STOP_OUT = 4,
318 RC_HOOK_RUNLEVEL_START_IN = 5,
319 RC_HOOK_RUNLEVEL_START_OUT = 8,
320 /*! We send the abort if an init script requests we abort and drop
321 * into single user mode if system not fully booted */
322 RC_HOOK_ABORT = 99,
323 RC_HOOK_SERVICE_STOP_IN = 101,
324 RC_HOOK_SERVICE_STOP_NOW = 102,
325 RC_HOOK_SERVICE_STOP_DONE = 103,
326 RC_HOOK_SERVICE_STOP_OUT = 104,
327 RC_HOOK_SERVICE_START_IN = 105,
328 RC_HOOK_SERVICE_START_NOW = 106,
329 RC_HOOK_SERVICE_START_DONE = 107,
330 RC_HOOK_SERVICE_START_OUT = 108
331 } rc_hook_t;
332
333 /*! Plugin entry point
334 * @param hook point
335 * @param name of runlevel or service
336 * @return 0 for success otherwise -1 */
337 int rc_plugin_hook (rc_hook_t hook, const char *name);
338
339 /*! Plugins should write FOO=BAR to this fd to set any environment
340 * variables they wish. Variables should be separated by NULLs. */
341 extern FILE *rc_environ_fd;
342
343 /*! @name Configuration */
344 /*! Return a NULL terminated list of non comment lines from a file. */
345 char **rc_config_list (const char *file);
346
347 /*! Return a NULL terminated list of key=value lines from a file. */
348 char **rc_config_load (const char *file);
349
350 /*! Return the value of the entry from a key=value list. */
351 char *rc_config_value (char **list, const char *entry);
352
353 /*! Return a NULL terminated string list of variables allowed through
354 * from the current environemnt. */
355 char **rc_env_filter (void);
356
357 /*! Return a NULL terminated string list of enviroment variables made from
358 * our configuration files. */
359 char **rc_env_config (void);
360
361 /*! @name String List functions
362 * Handy functions for dealing with string arrays of char **.
363 * It's safe to assume that any function here that uses char ** is a string
364 * list that can be manipulated with the below functions. Every string list
365 * should be released with a call to rc_strlist_free.*/
366
367 /*! Duplicate the item, add it to end of the list and return a pointer to it.
368 * @param list to add the item too
369 * @param item to add.
370 * @return pointer to newly added item */
371 char *rc_strlist_add (char ***list, const char *item);
372
373 /*! If the item does not exist in the list, duplicate it, add it to the
374 * list and then return a pointer to it.
375 * @param list to add the item too
376 * @param item to add.
377 * @return pointer to newly added item */
378 char *rc_strlist_addu (char ***list, const char *item);
379
380 /*! Duplicate the item, add it to the list at the point based on locale and
381 * then return a pointer to it.
382 * @param list to add the item too
383 * @param item to add.
384 * @return pointer to newly added item */
385 char *rc_strlist_addsort (char ***list, const char *item);
386
387 /*! Duplicate the item, add it to the list at the point based on C locale and
388 * then return a pointer to it.
389 * @param list to add the item too
390 * @param item to add.
391 * @return pointer to newly added item */
392 char *rc_strlist_addsortc (char ***list, const char *item);
393
394 /*! If the item does not exist in the list, duplicate it, add it to the
395 * list based on locale and then return a pointer to it.
396 * @param list to add the item too
397 * @param item to add.
398 * @return pointer to newly added item */
399 char *rc_strlist_addsortu (char ***list, const char *item);
400
401 /*! Free the item and remove it from the list. Return 0 on success otherwise -1.
402 * @param list to add the item too
403 * @param item to add.
404 * @return true on success, otherwise false */
405 bool rc_strlist_delete (char ***list, const char *item);
406
407 /*! Moves the contents of list2 onto list1, so list2 is effectively emptied.
408 * Returns a pointer to the last item on the new list.
409 * @param list1 to append to
410 * @param list2 to move from
411 * @return pointer to the last item on the list */
412 char *rc_strlist_join (char ***list1, char **list2);
413
414 /*! Reverses the contents of the list.
415 * @param list to reverse */
416 void rc_strlist_reverse (char **list);
417
418 /*! Frees each item on the list and the list itself.
419 * @param list to free */
420 void rc_strlist_free (char **list);
421
422 /*! @name Utility
423 * Although not RC specific functions, they are used by the supporting
424 * applications */
425
426 /*! Concatenate paths adding '/' if needed. The resultant pointer should be
427 * freed when finished with.
428 * @param path1 starting path
429 * @param paths NULL terminated list of paths to add
430 * @return pointer to the new path */
431 char *rc_strcatpaths (const char *path1, const char *paths, ...) SENTINEL;
432
433 /*! Check if an environment variable is a boolean and return it's value.
434 * If variable is not a boolean then we set errno to be ENOENT when it does
435 * not exist or EINVAL if it's not a boolean.
436 * @param variable to check
437 * @return true if it matches true, yes or 1, false if otherwise. */
438 bool rc_env_bool (const char *variable);
439
440 /*! @name rc_ls_dir options */
441 /*! Ensure that an init.d service exists for each file returned */
442 #define RC_LS_INITD 0x01
443 #define RC_LS_DIR 0x02
444
445 /*! Return a NULL terminted sorted list of the contents of the directory
446 * @param dir to list
447 * @param options any options to apply
448 * @return NULL terminated list */
449 char **rc_ls_dir (const char *dir, int options);
450
451 /*! Remove a directory
452 * @param pathname to remove
453 * @param top remove the top level directory too
454 * @return true if successful, otherwise false */
455 bool rc_rm_dir (const char *pathname, bool top);
456
457 #endif

  ViewVC Help
Powered by ViewVC 1.1.20