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

Contents of /trunk/src/rc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2986 - (show annotations) (download) (as text)
Thu Oct 4 14:11:45 2007 UTC (6 years, 6 months ago) by uberlord
File MIME type: text/x-chdr
File size: 17322 byte(s)
Revert making function private
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 char **rc_deptree_depends (rc_depinfo_t *deptree, char **types,
278 char **services, const char *runlevel, int options);
279
280 /*! List all the services that should be stoppned and then started, in order,
281 * for the given runlevel, including sysinit and boot services where
282 * approriate.
283 * @param deptree to search
284 * @param runlevel to change into
285 * @param options to pass
286 * @return NULL terminated list of services in order */
287 char **rc_deptree_order_services (rc_depinfo_t *deptree, const char *runlevel,
288 int options);
289 /*! Free a deptree and its information
290 * @param deptree to free */
291 void rc_deptree_free (rc_depinfo_t *deptree);
292
293 /*! @name Plugins
294 * For each plugin loaded we will call rc_plugin_hook with the below
295 * enum and either the runlevel name or service name.
296 *
297 * Plugins are called when rc does something. This does not indicate an
298 * end result and the plugin should use the above functions to query things
299 * like service status.
300 *
301 * The service hooks have extra ones - now and done. This is because after
302 * start_in we may start other services before we start the service in
303 * question. now shows we really will start the service now and done shows
304 * when we have done it as may start scheduled services at this point. */
305 /*! Points at which a plugin can hook into RC */
306 typedef enum
307 {
308 RC_HOOK_RUNLEVEL_STOP_IN = 1,
309 RC_HOOK_RUNLEVEL_STOP_OUT = 4,
310 RC_HOOK_RUNLEVEL_START_IN = 5,
311 RC_HOOK_RUNLEVEL_START_OUT = 8,
312 /*! We send the abort if an init script requests we abort and drop
313 * into single user mode if system not fully booted */
314 RC_HOOK_ABORT = 99,
315 RC_HOOK_SERVICE_STOP_IN = 101,
316 RC_HOOK_SERVICE_STOP_NOW = 102,
317 RC_HOOK_SERVICE_STOP_DONE = 103,
318 RC_HOOK_SERVICE_STOP_OUT = 104,
319 RC_HOOK_SERVICE_START_IN = 105,
320 RC_HOOK_SERVICE_START_NOW = 106,
321 RC_HOOK_SERVICE_START_DONE = 107,
322 RC_HOOK_SERVICE_START_OUT = 108
323 } rc_hook_t;
324
325 /*! Plugin entry point
326 * @param hook point
327 * @param name of runlevel or service
328 * @return 0 for success otherwise -1 */
329 int rc_plugin_hook (rc_hook_t hook, const char *name);
330
331 /*! Plugins should write FOO=BAR to this fd to set any environment
332 * variables they wish. Variables should be separated by NULLs. */
333 extern FILE *rc_environ_fd;
334
335 /*! @name Configuration */
336 /*! Return a NULL terminated list of non comment lines from a file. */
337 char **rc_config_list (const char *file);
338
339 /*! Return a NULL terminated list of key=value lines from a file. */
340 char **rc_config_load (const char *file);
341
342 /*! Return the value of the entry from a key=value list. */
343 char *rc_config_value (char **list, const char *entry);
344
345 /*! Return a NULL terminated string list of variables allowed through
346 * from the current environemnt. */
347 char **rc_env_filter (void);
348
349 /*! Return a NULL terminated string list of enviroment variables made from
350 * our configuration files. */
351 char **rc_env_config (void);
352
353 /*! @name String List functions
354 * Handy functions for dealing with string arrays of char **.
355 * It's safe to assume that any function here that uses char ** is a string
356 * list that can be manipulated with the below functions. Every string list
357 * should be released with a call to rc_strlist_free.*/
358
359 /*! Duplicate the item, add it to end of the list and return a pointer to it.
360 * @param list to add the item too
361 * @param item to add.
362 * @return pointer to newly added item */
363 char *rc_strlist_add (char ***list, const char *item);
364
365 /*! If the item does not exist in the list, duplicate it, add it to the
366 * list and then return a pointer to it.
367 * @param list to add the item too
368 * @param item to add.
369 * @return pointer to newly added item */
370 char *rc_strlist_addu (char ***list, const char *item);
371
372 /*! Duplicate the item, add it to the list at the point based on locale and
373 * then return a pointer to it.
374 * @param list to add the item too
375 * @param item to add.
376 * @return pointer to newly added item */
377 char *rc_strlist_addsort (char ***list, const char *item);
378
379 /*! Duplicate the item, add it to the list at the point based on C locale and
380 * then return a pointer to it.
381 * @param list to add the item too
382 * @param item to add.
383 * @return pointer to newly added item */
384 char *rc_strlist_addsortc (char ***list, const char *item);
385
386 /*! If the item does not exist in the list, duplicate it, add it to the
387 * list based on locale and then return a pointer to it.
388 * @param list to add the item too
389 * @param item to add.
390 * @return pointer to newly added item */
391 char *rc_strlist_addsortu (char ***list, const char *item);
392
393 /*! Free the item and remove it from the list. Return 0 on success otherwise -1.
394 * @param list to add the item too
395 * @param item to add.
396 * @return true on success, otherwise false */
397 bool rc_strlist_delete (char ***list, const char *item);
398
399 /*! Moves the contents of list2 onto list1, so list2 is effectively emptied.
400 * Returns a pointer to the last item on the new list.
401 * @param list1 to append to
402 * @param list2 to move from
403 * @return pointer to the last item on the list */
404 char *rc_strlist_join (char ***list1, char **list2);
405
406 /*! Reverses the contents of the list.
407 * @param list to reverse */
408 void rc_strlist_reverse (char **list);
409
410 /*! Frees each item on the list and the list itself.
411 * @param list to free */
412 void rc_strlist_free (char **list);
413
414 /*! @name Memory Allocation
415 * Ensure that if we cannot allocate the memory then we exit */
416 /*@{*/
417
418 /*! Allocate a block of memory
419 * @param size of memory to allocate
420 * @return pointer to memory */
421 void *rc_xmalloc (size_t size);
422
423 /*! Re-size a block of memory
424 * @param ptr to the block of memory to re-size
425 * @param size memory should be
426 * @return pointer to memory block */
427 void *rc_xrealloc (void *ptr, size_t size);
428
429 /*! Duplicate a NULL terminated string
430 * @param str to duplicate
431 * @return pointer to the new string */
432 char *rc_xstrdup (const char *str);
433 /*@}*/
434
435 /*! @name Utility
436 * Although not RC specific functions, they are used by the supporting
437 * applications */
438
439 /*! Concatenate paths adding '/' if needed. The resultant pointer should be
440 * freed when finished with.
441 * @param path1 starting path
442 * @param paths NULL terminated list of paths to add
443 * @return pointer to the new path */
444 char *rc_strcatpaths (const char *path1, const char *paths, ...) SENTINEL;
445
446 /*! Check if an environment variable is a boolean and return it's value.
447 * If variable is not a boolean then we set errno to be ENOENT when it does
448 * not exist or EINVAL if it's not a boolean.
449 * @param variable to check
450 * @return true if it matches true, yes or 1, false if otherwise. */
451 bool rc_env_bool (const char *variable);
452
453 /*! Check if the file exists or not
454 * @param pathname to check
455 * @return true if it exists, otherwise false */
456 bool rc_exists (const char *pathname);
457
458 /*! Check if the file is a real file
459 * @param pathname to check
460 * @return true if it's a real file, otherwise false */
461 bool rc_is_file (const char *pathname);
462
463 /*! Check if the file is a symbolic link or not
464 * @param pathname to check
465 * @return true if it's a symbolic link, otherwise false */
466 bool rc_is_link (const char *pathname);
467
468 /*! Check if the file is a directory or not
469 * @param pathname to check
470 * @return true if it's a directory, otherwise false */
471 bool rc_is_dir (const char *pathname);
472
473 /*! Check if the file is marked executable or not
474 * @param pathname to check
475 * @return true if it's marked executable, otherwise false */
476 bool rc_is_exec (const char *pathname);
477
478 /*! @name rc_ls_dir options */
479 /*! Ensure that an init.d service exists for each file returned */
480 #define RC_LS_INITD 0x01
481
482 /*! Return a NULL terminted sorted list of the contents of the directory
483 * @param dir to list
484 * @param options any options to apply
485 * @return NULL terminated list */
486 char **rc_ls_dir (const char *dir, int options);
487
488 /*! Remove a directory
489 * @param pathname to remove
490 * @param top remove the top level directory too
491 * @return true if successful, otherwise false */
492 bool rc_rm_dir (const char *pathname, bool top);
493
494 #endif

  ViewVC Help
Powered by ViewVC 1.1.20