Punt rc_is_dir

This commit is contained in:
Roy Marples 2007-10-04 16:54:29 +00:00
parent e2e40afdde
commit 87ea3e9e3b
10 changed files with 68 additions and 123 deletions

View File

@ -132,9 +132,10 @@ int env_update (int argc, char **argv)
STRLIST_FOREACH (files, file, i) { STRLIST_FOREACH (files, file, i) {
char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL); char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
char **entries = NULL; char **entries = NULL;
struct stat buf;
j = strlen (file); j = strlen (file);
if (! rc_is_dir (path) && if (stat (file, &buf) == 0 && S_ISDIR (buf.st_mode) == 0 &&
j > 2 && j > 2 &&
*file >= '0' && *file >= '0' &&
*file <= '9' && *file <= '9' &&

View File

@ -298,6 +298,7 @@ void rc_service_daemon_set (const char *service, const char *exec,
char *mname; char *mname;
char *mpidfile; char *mpidfile;
int nfiles = 0; int nfiles = 0;
char *oldfile = NULL;
free (svc); free (svc);
if (! exec && ! name && ! pidfile) if (! exec && ! name && ! pidfile)
@ -325,28 +326,25 @@ void rc_service_daemon_set (const char *service, const char *exec,
mpidfile = rc_xstrdup ("pidfile="); mpidfile = rc_xstrdup ("pidfile=");
/* Regardless, erase any existing daemon info */ /* Regardless, erase any existing daemon info */
if (rc_is_dir (dirpath)) { files = rc_ls_dir (dirpath, 0);
char *oldfile = NULL; STRLIST_FOREACH (files, file, i) {
files = rc_ls_dir (dirpath, 0); ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
STRLIST_FOREACH (files, file, i) { nfiles++;
ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
nfiles++;
if (! oldfile) { if (! oldfile) {
if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) { if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
unlink (ffile); unlink (ffile);
oldfile = ffile;
nfiles--;
}
} else {
rename (ffile, oldfile);
free (oldfile);
oldfile = 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 */ /* Now store our daemon info */
if (started) { 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), dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
(char *) NULL); (char *) NULL);
free (svc); free (svc);
if (! rc_is_dir (dirpath)) {
free (dirpath);
return (false);
}
i = strlen (exec) + 6; i = strlen (exec) + 6;
mexec = rc_xmalloc (sizeof (char *) * i); mexec = rc_xmalloc (sizeof (char *) * i);
snprintf (mexec, i, "exec=%s", exec); 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); rc_strlist_free (files);
} }
free (dirpath);
free (mexec); free (mexec);
return (retval); return (retval);
} }
@ -445,11 +439,6 @@ bool rc_service_daemons_crashed (const char *service)
(char *) NULL); (char *) NULL);
free (svc); free (svc);
if (! rc_is_dir (dirpath)) {
free (dirpath);
return (false);
}
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
files = rc_ls_dir (dirpath, 0); files = rc_ls_dir (dirpath, 0);
STRLIST_FOREACH (files, file, i) { STRLIST_FOREACH (files, file, i) {

View File

@ -547,6 +547,10 @@ static bool is_newer_than (const char *file, const char *target)
{ {
struct stat buf; struct stat buf;
time_t mtime; time_t mtime;
char **targets;
char *t;
int i;
bool newer = true;
if (stat (file, &buf) != 0 || buf.st_size == 0) if (stat (file, &buf) != 0 || buf.st_size == 0)
return (false); return (false);
@ -560,25 +564,17 @@ static bool is_newer_than (const char *file, const char *target)
if (mtime < buf.st_mtime) if (mtime < buf.st_mtime)
return (false); 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 *path = rc_strcatpaths (target, t, (char *) NULL);
char *t; newer = is_newer_than (file, path);
int i; free (path);
bool newer = true; if (! newer)
STRLIST_FOREACH (targets, t, i) break;
{
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);
} }
rc_strlist_free (targets);
return (true); return (newer);
} }
typedef struct deppair typedef struct deppair

View File

@ -147,21 +147,6 @@ bool rc_exists (const char *pathname)
} }
librc_hidden_def(rc_exists) 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) char **rc_ls_dir (const char *dir, int options)
{ {
DIR *dp; DIR *dp;
@ -189,6 +174,12 @@ char **rc_ls_dir (const char *dir, int options)
d->d_name[l - 1] == 'h') d->d_name[l - 1] == 'h')
continue; 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); 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 */ We store this special system in RC_SYS so our scripts run fast */
memset (sys, 0, sizeof (sys)); memset (sys, 0, sizeof (sys));
if (rc_is_dir ("/proc/xen")) { if (rc_exists ("/proc/xen")) {
if ((fp = fopen ("/proc/xen/capabilities", "r"))) { if ((fp = fopen ("/proc/xen/capabilities", "r"))) {
fclose (fp); fclose (fp);
if (file_regex ("/proc/xen/capabilities", "control_d")) if (file_regex ("/proc/xen/capabilities", "control_d"))

View File

@ -56,32 +56,19 @@ static const char *rc_parse_service_state (rc_service_state_t state)
bool rc_runlevel_starting (void) bool rc_runlevel_starting (void)
{ {
return (rc_is_dir (RC_STARTING)); return (rc_exists (RC_STARTING));
} }
librc_hidden_def(rc_runlevel_starting) librc_hidden_def(rc_runlevel_starting)
bool rc_runlevel_stopping (void) bool rc_runlevel_stopping (void)
{ {
return (rc_is_dir (RC_STOPPING)); return (rc_exists (RC_STOPPING));
} }
librc_hidden_def(rc_runlevel_stopping) librc_hidden_def(rc_runlevel_stopping)
char **rc_runlevel_list (void) char **rc_runlevel_list (void)
{ {
char **dirs = rc_ls_dir (RC_RUNLEVELDIR, 0); return (rc_ls_dir (RC_RUNLEVELDIR, RC_LS_DIRS));
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);
} }
librc_hidden_def(rc_runlevel_list) librc_hidden_def(rc_runlevel_list)
@ -123,13 +110,15 @@ librc_hidden_def(rc_runlevel_set)
bool rc_runlevel_exists (const char *runlevel) bool rc_runlevel_exists (const char *runlevel)
{ {
char *path; char *path;
bool retval; struct stat buf;
bool retval = false;
if (! runlevel) if (! runlevel)
return (false); return (false);
path = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL); 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); free (path);
return (retval); 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 */ /* Remove any options and daemons the service may have stored */
if (state == RC_SERVICE_STOPPED) { if (state == RC_SERVICE_STOPPED) {
char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL); char *dir = rc_strcatpaths (RC_SVCDIR, "options", base, (char *) NULL);
rc_rm_dir (dir, true);
if (rc_is_dir (dir))
rc_rm_dir (dir, true);
free (dir); free (dir);
dir = rc_strcatpaths (RC_SVCDIR, "daemons", base, (char *) NULL); 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); free (dir);
rc_service_schedule_clear (service); 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); char *file = rc_strcatpaths (path, option, (char *) NULL);
bool retval = false; bool retval = false;
if (! rc_is_dir (path)) { if (mkdir (path, 0755) != 0 && errno != EEXIST) {
if (mkdir (path, 0755) != 0) { free (path);
free (path); free (file);
free (file); return (false);
return (false);
}
} }
if ((fp = fopen (file, "w"))) { 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), dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
(char *) NULL); (char *) NULL);
free (svc); free (svc);
if (! rc_is_dir (dir)) if (mkdir (dir, 0755) != 0 && errno != EEXIST) {
if (mkdir (dir, 0755) != 0) { free (dir);
free (dir); return (false);
return (false); }
}
init = rc_service_resolve (service_to_start); init = rc_service_resolve (service_to_start);
svc = rc_xstrdup (service_to_start); svc = rc_xstrdup (service_to_start);
@ -615,8 +598,7 @@ void rc_service_schedule_clear (const char *service)
(char *) NULL); (char *) NULL);
free (svc); free (svc);
if (rc_is_dir (dir)) rc_rm_dir (dir, true);
rc_rm_dir (dir, true);
free (dir); free (dir);
} }
librc_hidden_def(rc_service_schedule_clear) librc_hidden_def(rc_service_schedule_clear)
@ -713,8 +695,7 @@ char **rc_services_in_state (rc_service_state_t state)
if (dirs) if (dirs)
free (dirs); free (dirs);
} else { } else {
if (rc_is_dir (dir)) list = rc_ls_dir (dir, RC_LS_INITD);
list = rc_ls_dir (dir, RC_LS_INITD);
} }
free (dir); free (dir);
@ -799,10 +780,8 @@ char **rc_services_scheduled (const char *service)
(char *) NULL); (char *) NULL);
char **list = NULL; char **list = NULL;
if (rc_is_dir (dir))
list = rc_ls_dir (dir, RC_LS_INITD);
free (svc); free (svc);
list = rc_ls_dir (dir, RC_LS_INITD);
free (dir); free (dir);
return (list); return (list);
} }

View File

@ -65,7 +65,6 @@ librc_hidden_proto(rc_env_config)
librc_hidden_proto(rc_env_filter) librc_hidden_proto(rc_env_filter)
librc_hidden_proto(rc_exists) librc_hidden_proto(rc_exists)
librc_hidden_proto(rc_find_pids) librc_hidden_proto(rc_find_pids)
librc_hidden_proto(rc_is_dir)
librc_hidden_proto(rc_ls_dir) librc_hidden_proto(rc_ls_dir)
librc_hidden_proto(rc_rm_dir) librc_hidden_proto(rc_rm_dir)
librc_hidden_proto(rc_runlevel_exists) librc_hidden_proto(rc_runlevel_exists)

View File

@ -22,8 +22,8 @@
#define RC_CONFDIR "/etc/conf.d" #define RC_CONFDIR "/etc/conf.d"
#define RC_KSOFTLEVEL RC_SVCDIR "/ksoftlevel" #define RC_KSOFTLEVEL RC_SVCDIR "/ksoftlevel"
#define RC_STARTING RC_SVCDIR "/softscripts.new" #define RC_STARTING RC_SVCDIR "/rc.starting"
#define RC_STOPPING RC_SVCDIR "/softscripts.old" #define RC_STOPPING RC_SVCDIR "/rc.stopping"
#define RC_SVCDIR_STARTING RC_SVCDIR "/starting" #define RC_SVCDIR_STARTING RC_SVCDIR "/starting"
#define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive" #define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive"

View File

@ -110,10 +110,8 @@ static void cleanup (void)
/* Clean runlevel start, stop markers */ /* Clean runlevel start, stop markers */
if (! rc_in_plugin) { if (! rc_in_plugin) {
if (rc_is_dir (RC_STARTING)) rmdir (RC_STARTING);
rc_rm_dir (RC_STARTING, true); rmdir (RC_STOPPING);
if (rc_is_dir (RC_STOPPING))
rc_rm_dir (RC_STOPPING, true);
} }
free (runlevel); free (runlevel);
@ -1016,10 +1014,8 @@ int main (int argc, char **argv)
/* Check if runlevel is valid if we're changing */ /* Check if runlevel is valid if we're changing */
if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) { if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) {
tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL); if (! rc_runlevel_exists (newlevel))
if (! rc_is_dir (tmp))
eerrorx ("%s: is not a valid runlevel", newlevel); eerrorx ("%s: is not a valid runlevel", newlevel);
CHAR_FREE (tmp);
} }
/* Load our deptree now */ /* Load our deptree now */
@ -1027,8 +1023,7 @@ int main (int argc, char **argv)
eerrorx ("failed to load deptree"); eerrorx ("failed to load deptree");
/* Clean the failed services state dir now */ /* 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); 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 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 stores a list of coldplugged services in DEVBOOT for us to pick up
here when we are ready for them */ 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); rc_rm_dir (DEVBOOT, true);
STRLIST_FOREACH (start_services, service, i) STRLIST_FOREACH (start_services, service, i)

View File

@ -463,14 +463,10 @@ bool rc_env_bool (const char *variable);
* @return true if it exists, otherwise false */ * @return true if it exists, otherwise false */
bool rc_exists (const char *pathname); 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 */ /*! @name rc_ls_dir options */
/*! Ensure that an init.d service exists for each file returned */ /*! Ensure that an init.d service exists for each file returned */
#define RC_LS_INITD 0x01 #define RC_LS_INITD 0x01
#define RC_LS_DIRS 0x02
/*! Return a NULL terminted sorted list of the contents of the directory /*! Return a NULL terminted sorted list of the contents of the directory
* @param dir to list * @param dir to list

View File

@ -15,7 +15,6 @@ global:
rc_environ_fd; rc_environ_fd;
rc_exists; rc_exists;
rc_find_pids; rc_find_pids;
rc_is_dir;
rc_ls_dir; rc_ls_dir;
rc_rm_dir; rc_rm_dir;
rc_runlevel_exists; rc_runlevel_exists;