diff options
Diffstat (limited to 'src/librc.c')
-rw-r--r-- | src/librc.c | 151 |
1 files changed, 78 insertions, 73 deletions
diff --git a/src/librc.c b/src/librc.c index 632f35fc..dd2e0b6b 100644 --- a/src/librc.c +++ b/src/librc.c @@ -31,13 +31,13 @@ static const char *rc_service_state_names[] = { NULL }; -int rc_runlevel_starting (void) +bool rc_runlevel_starting (void) { return (rc_is_dir (RC_STARTING)); } librc_hidden_def(rc_runlevel_starting) -int rc_runlevel_stopping (void) +bool rc_runlevel_stopping (void) { return (rc_is_dir (RC_STOPPING)); } @@ -52,7 +52,7 @@ char **rc_get_runlevels (void) STRLIST_FOREACH (dirs, dir, i) { char *path = rc_strcatpaths (RC_RUNLEVELDIR, dir, (char *) NULL); - if (rc_is_dir (path) == 0) + if (rc_is_dir (path)) rc_strlist_addsort (&runlevels, dir); free (path); } @@ -95,13 +95,13 @@ void rc_set_runlevel (const char *runlevel) } librc_hidden_def(rc_set_runlevel) -int rc_runlevel_exists (const char *runlevel) +bool rc_runlevel_exists (const char *runlevel) { char *path; - int retval; + bool retval; if (! runlevel) - return (-1); + return (false); path = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL); retval = rc_is_dir (path); @@ -124,10 +124,10 @@ char *rc_resolve_service (const char *service) return (rc_xstrdup (service)); file = rc_strcatpaths (RC_SVCDIR, "started", service, (char *) NULL); - if (rc_is_link (file) != 0) { + if (! rc_is_link (file)) { free (file); file = rc_strcatpaths (RC_SVCDIR, "inactive", service, (char *) NULL); - if (rc_is_link (file) != 0) { + if (! rc_is_link (file)) { free (file); file = NULL; } @@ -146,14 +146,14 @@ char *rc_resolve_service (const char *service) } librc_hidden_def(rc_resolve_service) -int rc_service_exists (const char *service) +bool rc_service_exists (const char *service) { char *file; - int retval = -1; + bool retval = false; int len; if (! service) - return (-1); + return (false); len = strlen (service); @@ -161,10 +161,11 @@ int rc_service_exists (const char *service) if (len > 2 && service[len - 3] == '.' && service[len - 2] == 's' && service[len - 1] == 'h') - return (-1); + return (false); file = rc_resolve_service (service); - retval = rc_is_exec (file); + if (rc_exists (file)) + retval = rc_is_exec (file); free (file); return (retval); } @@ -246,14 +247,14 @@ char *rc_service_description (const char *service, const char *option) } librc_hidden_def(rc_service_description) -int rc_service_in_runlevel (const char *service, const char *runlevel) +bool rc_service_in_runlevel (const char *service, const char *runlevel) { char *file; - int retval; + bool retval; char *svc; if (! runlevel || ! service) - return (-1); + return (false); svc = rc_xstrdup (service); file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), @@ -266,7 +267,7 @@ int rc_service_in_runlevel (const char *service, const char *runlevel) } librc_hidden_def(rc_service_in_runlevel) -int rc_mark_service (const char *service, const rc_service_state_t state) +bool rc_mark_service (const char *service, const rc_service_state_t state) { char *file; int i = 0; @@ -277,21 +278,21 @@ int rc_mark_service (const char *service, const rc_service_state_t state) bool skip_wasinactive = false; if (! service) - return (-1); + return (false); svc = rc_xstrdup (service); base = basename (svc); if (state != rc_service_stopped) { - if (rc_is_file(init) != 0) { + if (! rc_is_file(init)) { free (init); free (svc); - return (-1); + return (false); } file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], base, (char *) NULL); - if (rc_exists (file) == 0) + if (rc_exists (file)) unlink (file); i = symlink (init, file); if (i != 0) { @@ -299,7 +300,7 @@ int rc_mark_service (const char *service, const rc_service_state_t state) free (file); free (init); free (svc); - return (-1); + return (false); } free (file); @@ -309,7 +310,7 @@ int rc_mark_service (const char *service, const rc_service_state_t state) if (state == rc_service_coldplugged) { free (init); free (svc); - return (0); + return (true); } /* Remove any old states now */ @@ -324,7 +325,7 @@ int rc_mark_service (const char *service, const rc_service_state_t state) { file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[i], base, (char *) NULL); - if (rc_exists (file) == 0) { + if (rc_exists (file)) { if ((state == rc_service_starting || state == rc_service_stopping) && i == rc_service_inactive) @@ -357,7 +358,7 @@ int rc_mark_service (const char *service, const rc_service_state_t state) state == rc_service_inactive) { file = rc_strcatpaths (RC_SVCDIR, "exclusive", base, (char *) NULL); - if (rc_exists (file) == 0) + if (rc_exists (file)) if (unlink (file) != 0) eerror ("unlink `%s': %s", file, strerror (errno)); free (file); @@ -367,12 +368,12 @@ int rc_mark_service (const char *service, const rc_service_state_t state) if (state == rc_service_stopped) { char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL); - if (rc_is_dir (dir) == 0) + if (rc_is_dir (dir)) rc_rm_dir (dir, true); free (dir); dir = rc_strcatpaths (RC_SVCDIR, "daemons", base, (char *) NULL); - if (rc_is_dir (dir) == 0) + if (rc_is_dir (dir)) rc_rm_dir (dir, true); free (dir); @@ -389,7 +390,7 @@ int rc_mark_service (const char *service, const rc_service_state_t state) STRLIST_FOREACH (dirs, dir, i) { char *bdir = rc_strcatpaths (sdir, dir, (char *) NULL); file = rc_strcatpaths (bdir, base, (char *) NULL); - if (rc_exists (file) == 0) + if (rc_exists (file)) if (unlink (file) != 0) eerror ("unlink `%s': %s", file, strerror (errno)); free (file); @@ -406,11 +407,11 @@ int rc_mark_service (const char *service, const rc_service_state_t state) free (svc); free (init); - return (0); + return (true); } librc_hidden_def(rc_mark_service) -int rc_service_state (const char *service, const rc_service_state_t state) +bool rc_service_state (const char *service, const rc_service_state_t state) { char *file; bool retval; @@ -418,22 +419,24 @@ int rc_service_state (const char *service, const rc_service_state_t state) /* If the init script does not exist then we are stopped */ if (! rc_service_exists (service)) - return (state == rc_service_stopped ? 0 : -1); + return (state == rc_service_stopped ? true : false); /* We check stopped state by not being in any of the others */ if (state == rc_service_stopped) - return (rc_service_state (service, rc_service_started) | - rc_service_state (service, rc_service_starting) | - rc_service_state (service, rc_service_stopping) | - rc_service_state (service, rc_service_inactive)); + return ( ! (rc_service_state (service, rc_service_started) || + rc_service_state (service, rc_service_starting) || + rc_service_state (service, rc_service_stopping) || + rc_service_state (service, rc_service_inactive))); /* The crashed state and scheduled states are virtual */ if (state == rc_service_crashed) return (rc_service_daemons_crashed (service)); else if (state == rc_service_scheduled) { char **services = rc_services_scheduled_by (service); - rc_strlist_free (services); - return (services ? 0 : -1); + retval = (services); + if (services) + free (services); + return (retval); } /* Now we just check if a file by the service name rc_exists @@ -456,7 +459,7 @@ char *rc_get_service_option (const char *service, const char *option) (char *) NULL); char *value = NULL; - if (rc_exists (file) == 0) { + if (rc_exists (file)) { if ((fp = fopen (file, "r")) == NULL) eerror ("fopen `%s': %s", file, strerror (errno)); else { @@ -472,20 +475,20 @@ char *rc_get_service_option (const char *service, const char *option) } librc_hidden_def(rc_get_service_option) -int rc_set_service_option (const char *service, const char *option, +bool rc_set_service_option (const char *service, const char *option, const char *value) { FILE *fp; char *path = rc_strcatpaths (RC_SVCDIR, "options", service, (char *) NULL); char *file = rc_strcatpaths (path, option, (char *) NULL); - int retval = -1; + bool retval = false; - if (rc_is_dir (path) != 0) { + if (! rc_is_dir (path)) { if (mkdir (path, 0755) != 0) { eerror ("mkdir `%s': %s", path, strerror (errno)); free (path); free (file); - return (-1); + return (false); } } @@ -495,7 +498,7 @@ int rc_set_service_option (const char *service, const char *option, if (value) fprintf (fp, "%s", value); fclose (fp); - retval = 0; + retval = true; } free (path); @@ -512,7 +515,7 @@ static pid_t _exec_service (const char *service, const char *arg) char *svc; file = rc_resolve_service (service); - if (rc_is_file (file) != 0) { + if (! rc_is_file (file)) { rc_mark_service (service, rc_service_stopped); free (file); return (0); @@ -563,7 +566,7 @@ int rc_waitpid (pid_t pid) { pid_t rc_stop_service (const char *service) { - if (rc_service_state (service, rc_service_stopped) == 0) + if (rc_service_state (service, rc_service_stopped)) return (0); return (_exec_service (service, "stop")); @@ -572,7 +575,7 @@ librc_hidden_def(rc_stop_service) pid_t rc_start_service (const char *service) { - if (! rc_service_state (service, rc_service_stopped) == 0) + if (! rc_service_state (service, rc_service_stopped)) return (0); return (_exec_service (service, "start")); @@ -595,7 +598,7 @@ void rc_schedule_start_service (const char *service, dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), (char *) NULL); free (svc); - if (rc_is_dir (dir) != 0) + if (! rc_is_dir (dir)) if (mkdir (dir, 0755) != 0) { eerror ("mkdir `%s': %s", dir, strerror (errno)); free (dir); @@ -606,7 +609,7 @@ void rc_schedule_start_service (const char *service, svc = rc_xstrdup (service_to_start); file = rc_strcatpaths (dir, basename (svc), (char *) NULL); free (svc); - if (rc_exists (file) != 0 && symlink (init, file) != 0) + if (! rc_exists (file) && symlink (init, file) != 0) eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno)); free (init); @@ -622,24 +625,24 @@ void rc_schedule_clear (const char *service) (char *) NULL); free (svc); - if (rc_is_dir (dir) == 0) + if (rc_is_dir (dir)) rc_rm_dir (dir, true); free (dir); } librc_hidden_def(rc_schedule_clear) -int rc_wait_service (const char *service) +bool rc_wait_service (const char *service) { char *svc; char *base; char *fifo; struct timespec ts; int nloops = WAIT_MAX / WAIT_INTERVAL; - int retval = -1; + bool retval = false; bool forever = false; if (! service) - return (-1); + return (false); svc = rc_xstrdup (service); base = basename (svc); @@ -654,8 +657,8 @@ int rc_wait_service (const char *service) ts.tv_nsec = WAIT_INTERVAL; while (nloops) { - if (rc_exists (fifo) != 0) { - retval = 0; + if (! rc_exists (fifo)) { + retval = true; break; } @@ -689,7 +692,7 @@ char **rc_services_in_runlevel (const char *runlevel) return (NULL); dir = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL); - if (rc_is_dir (dir) != 0) + if (! rc_is_dir (dir)) eerror ("runlevel `%s' does not exist", runlevel); else list = rc_ls_dir (dir, RC_LS_INITD); @@ -726,7 +729,7 @@ char **rc_services_in_state (rc_service_state_t state) if (dirs) free (dirs); } else { - if (rc_is_dir (dir) == 0) + if (rc_is_dir (dir)) list = rc_ls_dir (dir, RC_LS_INITD); } @@ -735,21 +738,21 @@ char **rc_services_in_state (rc_service_state_t state) } librc_hidden_def(rc_services_in_state) -int rc_service_add (const char *runlevel, const char *service) +bool rc_service_add (const char *runlevel, const char *service) { - int retval; + bool retval; char *init; char *file; char *svc; if (! rc_runlevel_exists (runlevel)) { errno = ENOENT; - return (-1); + return (false); } - if (rc_service_in_runlevel (service, runlevel) == 0) { + if (rc_service_in_runlevel (service, runlevel)) { errno = EEXIST; - return (-1); + return (false); } init = rc_resolve_service (service); @@ -757,27 +760,29 @@ int rc_service_add (const char *runlevel, const char *service) file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), (char *) NULL); free (svc); - retval = symlink (init, file); + retval = (symlink (init, file) == 0); free (init); free (file); return (retval); } librc_hidden_def(rc_service_add) -int rc_service_delete (const char *runlevel, const char *service) +bool rc_service_delete (const char *runlevel, const char *service) { char *file; char *svc; - int retval; + bool retval = false; if (! runlevel || ! service) - return (-1); + return (false); svc = rc_xstrdup (service); file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), (char *) NULL); free (svc); - retval = unlink (file); + if (unlink (file) == 0) + retval = true; + free (file); return (retval); } @@ -793,7 +798,7 @@ char **rc_services_scheduled_by (const char *service) STRLIST_FOREACH (dirs, dir, i) { char *file = rc_strcatpaths (RC_SVCDIR, "scheduled", dir, service, (char *) NULL); - if (rc_exists (file) == 0) + if (rc_exists (file)) rc_strlist_add (&list, file); free (file); } @@ -810,7 +815,7 @@ char **rc_services_scheduled (const char *service) (char *) NULL); char **list = NULL; - if (rc_is_dir (dir) == 0) + if (rc_is_dir (dir)) list = rc_ls_dir (dir, RC_LS_INITD); free (svc); @@ -819,23 +824,23 @@ char **rc_services_scheduled (const char *service) } librc_hidden_def(rc_services_scheduled) -int rc_allow_plug (char *service) +bool rc_allow_plug (char *service) { char *list; char *p; char *star; char *token; - int allow = 0; + bool allow = true; char *match = getenv ("RC_PLUG_SERVICES"); if (! match) - return (0); + return true; list = rc_xstrdup (match); p = list; while ((token = strsep (&p, " "))) { - int truefalse = 0; + bool truefalse = true; if (token[0] == '!') { - truefalse = -1; + truefalse = false; token++; } |