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

Contents of /trunk/src/rc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2965 - (show annotations) (download) (as text)
Tue Oct 2 09:02:08 2007 UTC (11 years ago) by uberlord
File MIME type: text/x-chdr
File size: 17791 byte(s)
rc_set_service_option -> rc_service_option_set, rc_get_service_option -> rc_service_option_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 /*! Resolves a service name to its full path.
63 * @param service to check
64 * @return pointer to full path of service */
65 char *rc_service_resolve (const char *service);
66 /*! Checks if a service exists or not.
67 * @param service to check
68 * @return true if service exists, otherwise false */
69 bool rc_service_exists (const char *service);
70
71 /*! Lists the extra options a service has
72 * @param service to load the options from
73 * @return NULL terminated string list of options */
74 char **rc_service_options (const char *service);
75
76 /*! Returns a description of what the service and/or option does.
77 * @param service to check
78 * @param option to check (if NULL, service description)
79 * @return a newly allocated pointer to the description */
80 char *rc_service_description (const char *service, const char *option);
81
82 /*! Checks if a service is in a runlevel
83 * @param service to check
84 * @param runlevel it should be in
85 * @return true if service is in the runlevel, otherwise false */
86 bool rc_service_in_runlevel (const char *service, const char *runlevel);
87
88 /*! Checks if a service in in a state
89 * @param service to check
90 * @return state of the service */
91 rc_service_state_t rc_service_state (const char *service);
92
93 /*! Marks the service state
94 * @param service to mark
95 * @param state service should be in
96 * @return true if service state change was successful, otherwise false */
97 bool rc_service_mark (const char *service, rc_service_state_t state);
98
99 /*! Stop a service
100 * @param service to stop
101 * @return pid of service stopping process */
102 pid_t rc_service_stop (const char *service);
103
104 /*! Start a service
105 * @param service to start
106 * @return pid of the service starting process */
107 pid_t rc_service_start (const char *service);
108
109 /*! Wait for a process to finish
110 * @param pid to wait for
111 * @return exit status of the process */
112 int rc_waitpid (pid_t pid);
113
114 /*! Schedule a service to be started when another service starts
115 * @param service that starts the scheduled service when started
116 * @param service_to_start service that will be started */
117 bool rc_schedule_start_service (const char *service,
118 const char *service_to_start);
119 /*! Return a NULL terminated list of services that are scheduled to start
120 * when the given service has started
121 * @param service to check
122 * @return NULL terminated list of services scheduled to start */
123 char **rc_services_scheduled_by (const char *service);
124
125 /*! Clear the list of services scheduled to be started by this service
126 * @param service to clear */
127 void rc_schedule_clear (const char *service);
128
129 /*! Wait for a service to finish
130 * @param service to wait for
131 * @return true if service finished before timeout, otherwise false */
132 bool rc_service_wait (const char *service);
133
134 /*! Return a saved value for a service
135 * @param service to check
136 * @param option to load
137 * @return saved value */
138 char *rc_service_option_get (const char *service, const char *option);
139 /*! Save a persistent value for a service
140 * @param service to save for
141 * @param option to save
142 * @param value of the option
143 * @return true if saved, otherwise false */
144 bool rc_service_option_set (const char *service, const char *option,
145 const char *value);
146 /*! Save the arguments to find a running daemon
147 * @param service to save arguments for
148 * @param exec that we started
149 * @param name of the process (optional)
150 * @param pidfile of the process (optional)
151 * @param started if true, add the arguments otherwise remove existing matching arguments */
152 void rc_set_service_daemon (const char *service, const char *exec,
153 const char *name, const char *pidfile,
154 bool started);
155 /*! Check if the service started the daemon
156 * @param service to check
157 * @param exec to check
158 * @param indx of the daemon (optional - 1st daemon, 2nd daemon, etc)
159 * @return true if started by this service, otherwise false */
160 bool rc_service_started_daemon (const char *service, const char *exec,
161 int indx);
162
163 /*! Check if the service is allowed to be hot/cold plugged
164 * @param service to check
165 * @return true if allowed, otherwise false */
166 bool rc_allow_plug (char *service);
167
168 /*! Return the current runlevel.
169 * @return the current runlevel */
170 char *rc_get_runlevel (void);
171 /*! Set the runlevel.
172 * This just changes the stored runlevel and does not start or stop any services.
173 * @param runlevel to store */
174 bool rc_set_runlevel (const char *runlevel);
175
176 /*! Checks if the runlevel exists or not
177 * @param runlevel to check
178 * @return true if the runlevel exists, otherwise false */
179 bool rc_runlevel_exists (const char *runlevel);
180
181 /*! Return a NULL terminated list of runlevels
182 * @return a NULL terminated list of runlevels */
183 char **rc_get_runlevels (void);
184
185 /*! Is the runlevel starting?
186 * @return true if yes, otherwise false */
187 bool rc_runlevel_starting (void);
188 /*! Is the runlevel stopping?
189 * @return true if yes, otherwise false */
190 bool rc_runlevel_stopping (void);
191
192 /*! Add the service to the runlevel
193 * @param runlevel to add to
194 * @param service to add
195 * @return true if successful, otherwise false */
196 bool rc_service_add (const char *runlevel, const char *service);
197 /*! Remove the service from the runlevel
198 * @param runlevel to remove from
199 * @param service to remove
200 * @return true if sucessful, otherwise false */
201 bool rc_service_delete (const char *runlevel, const char *service);
202 /*! List the services in a runlevel
203 * @param runlevel to list
204 * @return NULL terminated list of services */
205 char **rc_services_in_runlevel (const char *runlevel);
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 /*! List the services shceduled to start when this one does
211 * @param service to check
212 * @return NULL terminated list of services */
213 char **rc_services_scheduled (const char *service);
214
215 /*! Find processes based on criteria.
216 * All of these are optional.
217 * pid overrides anything else.
218 * If both exec and cmd are given then we ignore exec.
219 * @param exec to check for
220 * @param cmd to check for
221 * @param uid to check for
222 * @param pid to check for
223 * @return NULL terminated list of pids */
224 pid_t *rc_find_pids (const char *exec, const char *cmd,
225 uid_t uid, pid_t pid);
226 /*! Checks that all daemons started with start-stop-daemon by the service
227 * are still running.
228 * @param service to check
229 * @return true if all daemons started are still running, otherwise false */
230 bool rc_service_daemons_crashed (const char *service);
231
232 /*! @name Dependency options
233 * These options can change the services found by the rc_get_depinfo and
234 * rc_get_depends functions. */
235 /*! Trace provided services */
236 #define RC_DEP_TRACE 0x01
237 /*! Only use services added to runlevels */
238 #define RC_DEP_STRICT 0x02
239 /*! Runlevel is starting */
240 #define RC_DEP_START 0x04
241 /*! Runlevel is stopping */
242 #define RC_DEP_STOP 0x08
243
244 /*! @name Dependencies
245 * We analyse each init script and cache the resultant dependency tree.
246 * This tree can be accessed using the below functions. */
247
248 #ifndef _IN_LIBRC
249 /* Handles to internal structures */
250 typedef void *rc_deptype_t;
251 typedef void *rc_depinfo_t;
252 #endif
253
254 /*! Update the cached dependency tree if it's older than any init script,
255 * its configuration file or an external configuration file the init script
256 * has specified.
257 * @return 0 if successful, otherwise -1 */
258 int rc_deptree_update (void);
259 /*! Check if the cached dependency tree is older than any init script,
260 * its configuration file or an external configuration file the init script
261 * has specified.
262 * @return true if it needs updating, otherwise false */
263 bool rc_deptree_update_needed (void);
264 /*! Load the cached dependency tree and return a pointer to it.
265 * This pointer should be freed with rc_deptree_free when done.
266 * @return pointer to the dependency tree */
267 rc_depinfo_t *rc_deptree_load (void);
268 /*! Get a services depedency information from a loaded tree
269 * @param deptree to search
270 * @param service to find
271 * @return service dependency information */
272 rc_depinfo_t *rc_deptree_depinfo (rc_depinfo_t *deptree, const char *service);
273 /*! Get a depenency type from the service dependency information
274 * @param depinfo service dependency to search
275 * @param type to find
276 * @return service dependency type information */
277 rc_deptype_t *rc_deptree_deptype (rc_depinfo_t *depinfo, const char *type);
278 char **rc_deptree_depends (rc_depinfo_t *deptree, char **types,
279 char **services, const char *runlevel, int options);
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 Memory Allocation
336 * Ensure that if we cannot allocate the memory then we exit */
337 /*@{*/
338 /*! Allocate a block of memory
339 * @param size of memory to allocate
340 * @return pointer to memory */
341 void *rc_xmalloc (size_t size);
342 /*! Re-size a block of memory
343 * @param ptr to the block of memory to re-size
344 * @param size memory should be
345 * @return pointer to memory block */
346 void *rc_xrealloc (void *ptr, size_t size);
347 /*! Duplicate a NULL terminated string
348 * @param str to duplicate
349 * @return pointer to the new string */
350 char *rc_xstrdup (const char *str);
351 /*@}*/
352
353 /*! @name Utility
354 * Although not RC specific functions, they are used by the supporting
355 * applications */
356 /*! Concatenate paths adding '/' if needed. The resultant pointer should be
357 * freed when finished with.
358 * @param path1 starting path
359 * @param paths NULL terminated list of paths to add
360 * @return pointer to the new path */
361 char *rc_strcatpaths (const char *path1, const char *paths, ...) SENTINEL;
362 /*! Check if an environment variable is a boolean and return it's value.
363 * If variable is not a boolean then we set errno to be ENOENT when it does
364 * not exist or EINVAL if it's not a boolean.
365 * @param variable to check
366 * @return true if it matches true, yes or 1, false if otherwise. */
367 bool rc_env_bool (const char *variable);
368 /*! Check if the file exists or not
369 * @param pathname to check
370 * @return true if it exists, otherwise false */
371 bool rc_exists (const char *pathname);
372 /*! Check if the file is a real file
373 * @param pathname to check
374 * @return true if it's a real file, otherwise false */
375 bool rc_is_file (const char *pathname);
376 /*! Check if the file is a symbolic link or not
377 * @param pathname to check
378 * @return true if it's a symbolic link, otherwise false */
379 bool rc_is_link (const char *pathname);
380 /*! Check if the file is a directory or not
381 * @param pathname to check
382 * @return true if it's a directory, otherwise false */
383 bool rc_is_dir (const char *pathname);
384 /*! Check if the file is marked executable or not
385 * @param pathname to check
386 * @return true if it's marked executable, otherwise false */
387 bool rc_is_exec (const char *pathname);
388
389 /*! Return a NULL terminted sorted list of the contents of the directory
390 * @param dir to list
391 * @param options any options to apply
392 * @return NULL terminated list */
393 char **rc_ls_dir (const char *dir, int options);
394
395 /*! Remove a directory
396 * @param pathname to remove
397 * @param top remove the top level directory too
398 * @return true if successful, otherwise false */
399 bool rc_rm_dir (const char *pathname, bool top);
400
401 /*! @name Configuration */
402 /*! Return a NULL terminated list of non comment lines from a file. */
403 char **rc_get_list (const char *file);
404 /*! Return a NULL terminated list of key=value lines from a file. */
405 char **rc_get_config (const char *file);
406 /*! Return the value of the entry from a key=value list. */
407 char *rc_get_config_entry (char **list, const char *entry);
408
409 /*! Return a NULL terminated string list of variables allowed through
410 * from the current environemnt. */
411 char **rc_filter_env (void);
412 /*! Return a NULL terminated string list of enviroment variables made from
413 * our configuration files. */
414 char **rc_make_env (void);
415
416 /*! @name String List functions
417 * Handy functions for dealing with string arrays of char **.
418 * It's safe to assume that any function here that uses char ** is a string
419 * list that can be manipulated with the below functions. Every string list
420 * should be released with a call to rc_strlist_free.*/
421 /*! Duplicate the item, add it to end of the list and return a pointer to it.
422 * @param list to add the item too
423 * @param item to add.
424 * @return pointer to newly added item */
425 char *rc_strlist_add (char ***list, const char *item);
426 /*! If the item does not exist in the list, duplicate it, add it to the
427 * list and then 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_addu (char ***list, const char *item);
432 /*! Duplicate the item, add it to the list at the point based on locale and
433 * 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_addsort (char ***list, const char *item);
438 /*! Duplicate the item, add it to the list at the point based on C 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_addsortc (char ***list, const char *item);
444 /*! If the item does not exist in the list, duplicate it, add it to the
445 * list based on locale and 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_addsortu (char ***list, const char *item);
450 /*! Free the item and remove it from the list. Return 0 on success otherwise -1.
451 * @param list to add the item too
452 * @param item to add.
453 * @return true on success, otherwise false */
454 bool rc_strlist_delete (char ***list, const char *item);
455 /*! Moves the contents of list2 onto list1, so list2 is effectively emptied.
456 * Returns a pointer to the last item on the new list.
457 * @param list1 to append to
458 * @param list2 to move from
459 * @return pointer to the last item on the list */
460 char *rc_strlist_join (char ***list1, char **list2);
461 /*! Reverses the contents of the list.
462 * @param list to reverse */
463 void rc_strlist_reverse (char **list);
464 /*! Frees each item on the list and the list itself.
465 * @param list to free */
466 void rc_strlist_free (char **list);
467
468 #endif

  ViewVC Help
Powered by ViewVC 1.1.20