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

Contents of /trunk/src/rc.h

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20