From 87ea3e9e3bfa8e96b31d682f29fc7959bdd1b39e Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 4 Oct 2007 16:54:29 +0000 Subject: [PATCH] Punt rc_is_dir --- src/env-update.c | 3 ++- src/librc-daemon.c | 45 +++++++++++++--------------------- src/librc-depend.c | 30 ++++++++++------------- src/librc-misc.c | 23 ++++++----------- src/librc.c | 61 +++++++++++++++------------------------------- src/librc.h | 1 - src/rc-misc.h | 4 +-- src/rc.c | 17 +++++-------- src/rc.h | 6 +---- src/rc.map | 1 - 10 files changed, 68 insertions(+), 123 deletions(-) diff --git a/src/env-update.c b/src/env-update.c index aef50bc4..c9fbf1a8 100644 --- a/src/env-update.c +++ b/src/env-update.c @@ -132,9 +132,10 @@ int env_update (int argc, char **argv) STRLIST_FOREACH (files, file, i) { char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL); char **entries = NULL; + struct stat buf; j = strlen (file); - if (! rc_is_dir (path) && + if (stat (file, &buf) == 0 && S_ISDIR (buf.st_mode) == 0 && j > 2 && *file >= '0' && *file <= '9' && diff --git a/src/librc-daemon.c b/src/librc-daemon.c index b1734f67..e3da58d7 100644 --- a/src/librc-daemon.c +++ b/src/librc-daemon.c @@ -298,6 +298,7 @@ void rc_service_daemon_set (const char *service, const char *exec, char *mname; char *mpidfile; int nfiles = 0; + char *oldfile = NULL; free (svc); if (! exec && ! name && ! pidfile) @@ -325,28 +326,25 @@ void rc_service_daemon_set (const char *service, const char *exec, mpidfile = rc_xstrdup ("pidfile="); /* Regardless, erase any existing daemon info */ - if (rc_is_dir (dirpath)) { - char *oldfile = NULL; - files = rc_ls_dir (dirpath, 0); - STRLIST_FOREACH (files, file, i) { - ffile = rc_strcatpaths (dirpath, file, (char *) NULL); - nfiles++; + files = rc_ls_dir (dirpath, 0); + STRLIST_FOREACH (files, file, i) { + ffile = rc_strcatpaths (dirpath, file, (char *) NULL); + nfiles++; - if (! oldfile) { - if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) { - unlink (ffile); - oldfile = ffile; - nfiles--; - } - } else { - rename (ffile, oldfile); - free (oldfile); + if (! oldfile) { + if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) { + unlink (ffile); oldfile = ffile; + nfiles--; } + } else { + rename (ffile, oldfile); + free (oldfile); + oldfile = ffile; } - free (ffile); - rc_strlist_free (files); } + free (ffile); + rc_strlist_free (files); /* Now store our daemon info */ if (started) { @@ -387,12 +385,7 @@ bool rc_service_started_daemon (const char *service, const char *exec, dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), (char *) NULL); free (svc); - - if (! rc_is_dir (dirpath)) { - free (dirpath); - return (false); - } - + i = strlen (exec) + 6; mexec = rc_xmalloc (sizeof (char *) * i); snprintf (mexec, i, "exec=%s", exec); @@ -413,6 +406,7 @@ bool rc_service_started_daemon (const char *service, const char *exec, rc_strlist_free (files); } + free (dirpath); free (mexec); return (retval); } @@ -445,11 +439,6 @@ bool rc_service_daemons_crashed (const char *service) (char *) NULL); free (svc); - if (! rc_is_dir (dirpath)) { - free (dirpath); - return (false); - } - memset (buffer, 0, sizeof (buffer)); files = rc_ls_dir (dirpath, 0); STRLIST_FOREACH (files, file, i) { diff --git a/src/librc-depend.c b/src/librc-depend.c index 150edb16..c701c214 100644 --- a/src/librc-depend.c +++ b/src/librc-depend.c @@ -547,6 +547,10 @@ static bool is_newer_than (const char *file, const char *target) { struct stat buf; time_t mtime; + char **targets; + char *t; + int i; + bool newer = true; if (stat (file, &buf) != 0 || buf.st_size == 0) return (false); @@ -560,25 +564,17 @@ static bool is_newer_than (const char *file, const char *target) if (mtime < buf.st_mtime) return (false); - if (rc_is_dir (target)) + targets = rc_ls_dir (target, 0); + STRLIST_FOREACH (targets, t, i) { - char **targets = rc_ls_dir (target, 0); - char *t; - int i; - bool newer = true; - STRLIST_FOREACH (targets, t, i) - { - char *path = rc_strcatpaths (target, t, (char *) NULL); - newer = is_newer_than (file, path); - free (path); - if (! newer) - break; - } - rc_strlist_free (targets); - return (newer); + char *path = rc_strcatpaths (target, t, (char *) NULL); + newer = is_newer_than (file, path); + free (path); + if (! newer) + break; } - - return (true); + rc_strlist_free (targets); + return (newer); } typedef struct deppair diff --git a/src/librc-misc.c b/src/librc-misc.c index 29d2763d..4cc69be0 100644 --- a/src/librc-misc.c +++ b/src/librc-misc.c @@ -147,21 +147,6 @@ bool rc_exists (const char *pathname) } librc_hidden_def(rc_exists) -bool rc_is_dir (const char *pathname) -{ - struct stat buf; - - if (! pathname) - return (false); - - if (stat (pathname, &buf) == 0) - return (S_ISDIR (buf.st_mode)); - - errno = 0; - return (false); -} -librc_hidden_def(rc_is_dir) - char **rc_ls_dir (const char *dir, int options) { DIR *dp; @@ -189,6 +174,12 @@ char **rc_ls_dir (const char *dir, int options) d->d_name[l - 1] == 'h') continue; } + if (options & RC_LS_DIRS) { + struct stat buf; + + if (stat (d->d_name, &buf) == 0 && ! S_ISDIR (buf.st_mode)) + continue; + } rc_strlist_addsort (&list, d->d_name); } } @@ -592,7 +583,7 @@ char **rc_env_config (void) We store this special system in RC_SYS so our scripts run fast */ memset (sys, 0, sizeof (sys)); - if (rc_is_dir ("/proc/xen")) { + if (rc_exists ("/proc/xen")) { if ((fp = fopen ("/proc/xen/capabilities", "r"))) { fclose (fp); if (file_regex ("/proc/xen/capabilities", "control_d")) diff --git a/src/librc.c b/src/librc.c index 58f450da..851e784d 100644 --- a/src/librc.c +++ b/src/librc.c @@ -56,32 +56,19 @@ static const char *rc_parse_service_state (rc_service_state_t state) bool rc_runlevel_starting (void) { - return (rc_is_dir (RC_STARTING)); + return (rc_exists (RC_STARTING)); } librc_hidden_def(rc_runlevel_starting) bool rc_runlevel_stopping (void) { - return (rc_is_dir (RC_STOPPING)); + return (rc_exists (RC_STOPPING)); } librc_hidden_def(rc_runlevel_stopping) char **rc_runlevel_list (void) { - char **dirs = rc_ls_dir (RC_RUNLEVELDIR, 0); - char **runlevels = NULL; - int i; - char *dir; - - STRLIST_FOREACH (dirs, dir, i) { - char *path = rc_strcatpaths (RC_RUNLEVELDIR, dir, (char *) NULL); - if (rc_is_dir (path)) - rc_strlist_addsort (&runlevels, dir); - free (path); - } - rc_strlist_free (dirs); - - return (runlevels); + return (rc_ls_dir (RC_RUNLEVELDIR, RC_LS_DIRS)); } librc_hidden_def(rc_runlevel_list) @@ -123,13 +110,15 @@ librc_hidden_def(rc_runlevel_set) bool rc_runlevel_exists (const char *runlevel) { char *path; - bool retval; + struct stat buf; + bool retval = false; if (! runlevel) return (false); path = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL); - retval = rc_is_dir (path); + if (stat (path, &buf) == 0 && S_ISDIR (buf.st_mode)) + retval = true; free (path); return (retval); } @@ -374,14 +363,11 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) /* Remove any options and daemons the service may have stored */ if (state == RC_SERVICE_STOPPED) { char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL); - - if (rc_is_dir (dir)) - rc_rm_dir (dir, true); + rc_rm_dir (dir, true); free (dir); dir = rc_strcatpaths (RC_SVCDIR, "daemons", base, (char *) NULL); - if (rc_is_dir (dir)) - rc_rm_dir (dir, true); + rc_rm_dir (dir, true); free (dir); rc_service_schedule_clear (service); @@ -475,12 +461,10 @@ bool rc_service_value_set (const char *service, const char *option, char *file = rc_strcatpaths (path, option, (char *) NULL); bool retval = false; - if (! rc_is_dir (path)) { - if (mkdir (path, 0755) != 0) { - free (path); - free (file); - return (false); - } + if (mkdir (path, 0755) != 0 && errno != EEXIST) { + free (path); + free (file); + return (false); } if ((fp = fopen (file, "w"))) { @@ -589,11 +573,10 @@ bool rc_service_schedule_start (const char *service, dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), (char *) NULL); free (svc); - if (! rc_is_dir (dir)) - if (mkdir (dir, 0755) != 0) { - free (dir); - return (false); - } + if (mkdir (dir, 0755) != 0 && errno != EEXIST) { + free (dir); + return (false); + } init = rc_service_resolve (service_to_start); svc = rc_xstrdup (service_to_start); @@ -615,8 +598,7 @@ void rc_service_schedule_clear (const char *service) (char *) NULL); free (svc); - if (rc_is_dir (dir)) - rc_rm_dir (dir, true); + rc_rm_dir (dir, true); free (dir); } librc_hidden_def(rc_service_schedule_clear) @@ -713,8 +695,7 @@ char **rc_services_in_state (rc_service_state_t state) if (dirs) free (dirs); } else { - if (rc_is_dir (dir)) - list = rc_ls_dir (dir, RC_LS_INITD); + list = rc_ls_dir (dir, RC_LS_INITD); } free (dir); @@ -799,10 +780,8 @@ char **rc_services_scheduled (const char *service) (char *) NULL); char **list = NULL; - if (rc_is_dir (dir)) - list = rc_ls_dir (dir, RC_LS_INITD); - free (svc); + list = rc_ls_dir (dir, RC_LS_INITD); free (dir); return (list); } diff --git a/src/librc.h b/src/librc.h index a370754f..1a65c3bc 100644 --- a/src/librc.h +++ b/src/librc.h @@ -65,7 +65,6 @@ librc_hidden_proto(rc_env_config) librc_hidden_proto(rc_env_filter) librc_hidden_proto(rc_exists) librc_hidden_proto(rc_find_pids) -librc_hidden_proto(rc_is_dir) librc_hidden_proto(rc_ls_dir) librc_hidden_proto(rc_rm_dir) librc_hidden_proto(rc_runlevel_exists) diff --git a/src/rc-misc.h b/src/rc-misc.h index 0df58b5c..bfe54d15 100644 --- a/src/rc-misc.h +++ b/src/rc-misc.h @@ -22,8 +22,8 @@ #define RC_CONFDIR "/etc/conf.d" #define RC_KSOFTLEVEL RC_SVCDIR "/ksoftlevel" -#define RC_STARTING RC_SVCDIR "/softscripts.new" -#define RC_STOPPING RC_SVCDIR "/softscripts.old" +#define RC_STARTING RC_SVCDIR "/rc.starting" +#define RC_STOPPING RC_SVCDIR "/rc.stopping" #define RC_SVCDIR_STARTING RC_SVCDIR "/starting" #define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive" diff --git a/src/rc.c b/src/rc.c index 2f1f7bcf..ae814877 100644 --- a/src/rc.c +++ b/src/rc.c @@ -110,10 +110,8 @@ static void cleanup (void) /* Clean runlevel start, stop markers */ if (! rc_in_plugin) { - if (rc_is_dir (RC_STARTING)) - rc_rm_dir (RC_STARTING, true); - if (rc_is_dir (RC_STOPPING)) - rc_rm_dir (RC_STOPPING, true); + rmdir (RC_STARTING); + rmdir (RC_STOPPING); } free (runlevel); @@ -1016,10 +1014,8 @@ int main (int argc, char **argv) /* Check if runlevel is valid if we're changing */ if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) { - tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL); - if (! rc_is_dir (tmp)) + if (! rc_runlevel_exists (newlevel)) eerrorx ("%s: is not a valid runlevel", newlevel); - CHAR_FREE (tmp); } /* Load our deptree now */ @@ -1027,8 +1023,7 @@ int main (int argc, char **argv) eerrorx ("failed to load deptree"); /* Clean the failed services state dir now */ - if (rc_is_dir (RC_SVCDIR "/failed")) - rc_rm_dir (RC_SVCDIR "/failed", false); + rc_rm_dir (RC_SVCDIR "/failed", false); mkdir (RC_STOPPING, 0755); @@ -1037,8 +1032,8 @@ int main (int argc, char **argv) its coldplugging thing. runscript knows when we're not ready so it stores a list of coldplugged services in DEVBOOT for us to pick up here when we are ready for them */ - if (rc_is_dir (DEVBOOT)) { - start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD); + start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD); + if (start_services) { rc_rm_dir (DEVBOOT, true); STRLIST_FOREACH (start_services, service, i) diff --git a/src/rc.h b/src/rc.h index 17aee369..5d5ded67 100644 --- a/src/rc.h +++ b/src/rc.h @@ -463,14 +463,10 @@ bool rc_env_bool (const char *variable); * @return true if it exists, otherwise false */ bool rc_exists (const char *pathname); -/*! Check if the file is a directory or not - * @param pathname to check - * @return true if it's a directory, otherwise false */ -bool rc_is_dir (const char *pathname); - /*! @name rc_ls_dir options */ /*! Ensure that an init.d service exists for each file returned */ #define RC_LS_INITD 0x01 +#define RC_LS_DIRS 0x02 /*! Return a NULL terminted sorted list of the contents of the directory * @param dir to list diff --git a/src/rc.map b/src/rc.map index 020dfa32..b6ef0a54 100644 --- a/src/rc.map +++ b/src/rc.map @@ -15,7 +15,6 @@ global: rc_environ_fd; rc_exists; rc_find_pids; - rc_is_dir; rc_ls_dir; rc_rm_dir; rc_runlevel_exists;