summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2008-11-02 09:34:54 -0800
committerDan Nicholson <dbn.lists@gmail.com>2008-11-02 09:58:11 -0800
commit254d7b96be22ca827385dae4675ca190b61824bb (patch)
treeb13a52add6e3f7f22057dc03c16f642b6cc57dda
parent4cead4059f4971c2be63bde5b0b4ab500e493186 (diff)
Report errors with more useful messages
This still doesn't do a great job of handling errors, but at least the user may see some reasonable information if they occur.
-rw-r--r--lib/active.c20
-rw-r--r--lib/initd-list.c15
-rw-r--r--lib/initd.c4
-rw-r--r--lib/installrm.c17
-rw-r--r--lib/parse.c10
-rw-r--r--lib/str.c5
-rw-r--r--lib/strarg.c8
7 files changed, 41 insertions, 38 deletions
diff --git a/lib/active.c b/lib/active.c
index 2eedfd6..7ab22b4 100644
--- a/lib/active.c
+++ b/lib/active.c
@@ -36,7 +36,7 @@ void initd_list_set_actives(initd_list_t *ilp, const char *dir)
/* Change to the init.d directory */
cwd = get_current_dir_name();
if (chdir(dir) != 0)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "chdir %s", dir);
/* Check the various sysinit directories, stopping after the
* first one. */
@@ -52,7 +52,7 @@ void initd_list_set_actives(initd_list_t *ilp, const char *dir)
/* Return to the original directory */
if (chdir(cwd) != 0)
- error(1, errno, "%s", cwd);
+ error(EXIT_FAILURE, errno, "chdir %s", cwd);
free(cwd);
}
@@ -121,13 +121,13 @@ static bool read_dir_symlinks(initd_list_t *ilp, const struct rcpair *rcp)
errno = 0;
return false;
} else {
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "chdir %s", dir);
}
}
dfd = opendir(".");
if (!dfd)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "opendir %s", dir);
errno = 0;
while ((de = readdir(dfd))) {
@@ -146,10 +146,10 @@ static bool read_dir_symlinks(initd_list_t *ilp, const struct rcpair *rcp)
/* if errno is set, readdir had issues */
if (errno)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "readdir %s", dir);
if (closedir(dfd) != 0)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "closedir %s", dir);
return true;
}
@@ -161,7 +161,7 @@ static char *rel_readlink(const char *path)
ssize_t tlen;
if (lstat(path, &ls) != 0)
- error(1, errno, "%s", path);
+ error(EXIT_FAILURE, errno, "lstat %s", path);
if (!S_ISLNK(ls.st_mode)) {
if (active_verbose) {
@@ -176,7 +176,7 @@ static char *rel_readlink(const char *path)
tlen = readlink(path, target, sizeof(target));
if (tlen < 0)
- error(1, errno, "%s", path);
+ error(EXIT_FAILURE, errno, "readlink %s", path);
/* append a null byte if target was truncated */
if (tlen == PATH_MAX)
@@ -255,9 +255,9 @@ static void add_active_link(const initd_t *ip, const char *link,
pathlen = strlen(rcp->dir) + strlen(link) + 2;
fullpath = malloc(sizeof(char) * pathlen);
if (!fullpath)
- error(2, errno, "malloc");
+ error(EXIT_FAILURE, errno, "malloc");
if (snprintf(fullpath, pathlen, "%s/%s", rcp->dir, link) >= pathlen)
- error(2, errno, "snprintf");
+ error(EXIT_FAILURE, errno, "snprintf");
switch (key) {
case KEY_ASTART:
diff --git a/lib/initd-list.c b/lib/initd-list.c
index 8e1b585..8ef568b 100644
--- a/lib/initd-list.c
+++ b/lib/initd-list.c
@@ -19,7 +19,7 @@ initd_list_t *initd_list_new(void)
{
initd_list_t *ilp = malloc(sizeof(initd_list_t));
if (!ilp)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "malloc initd_list_t");
ilp->first = NULL;
ilp->last = NULL;
@@ -117,7 +117,7 @@ initd_list_t *initd_list_from_dir(const char *dir)
dfd = opendir(dir);
if (!dfd)
- error(2, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "opendir %s", dir);
errno = 0;
while ((de = readdir(dfd))) {
@@ -128,11 +128,12 @@ initd_list_t *initd_list_from_dir(const char *dir)
plen = snprintf(ip_path, PATH_MAX, "%s/%s", dir, de->d_name);
if (plen < 0)
- error(2, errno, "%s", de->d_name);
+ error(EXIT_FAILURE, errno, "snprintf %s/%s",
+ dir, de->d_name);
struct stat ip_buf;
if (stat(ip_path, &ip_buf) < 0)
- error(2, errno, "%s", ip_path);
+ error(EXIT_FAILURE, errno, "stat %s", ip_path);
if (!S_ISREG(ip_buf.st_mode))
continue;
@@ -144,11 +145,11 @@ initd_list_t *initd_list_from_dir(const char *dir)
}
/* if errno is set, readdir had issues */
- if (errno)
- error(2, errno, "%s", dir);
+ if (!de && errno != 0)
+ error(EXIT_FAILURE, errno, "readdir %s", dir);
if (closedir(dfd) != 0)
- error(2, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "closedir %s", dir);
/* Set the active field for all scripts */
initd_list_set_actives(ilp, dir);
diff --git a/lib/initd.c b/lib/initd.c
index 6a1d648..8c832e0 100644
--- a/lib/initd.c
+++ b/lib/initd.c
@@ -14,7 +14,7 @@ static initd_rc_t *initd_get_rc_from_key(const initd_t *ip,
initd_t *initd_new(const char *name) {
initd_t *ip = malloc(sizeof(initd_t));
if (!ip)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "malloc initd_t");
ip->name = d_string_new(name);
@@ -113,7 +113,7 @@ initd_node_t *initd_node_new(initd_t *ip)
{
initd_node_t *inp = malloc(sizeof(initd_node_t));
if (!inp)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "malloc initd_node_t");
inp->initd = ip;
inp->prev = NULL;
diff --git a/lib/installrm.c b/lib/installrm.c
index f9fafbe..067fdf4 100644
--- a/lib/installrm.c
+++ b/lib/installrm.c
@@ -39,13 +39,13 @@ bool initd_installrm_links(const initd_list_t *ilp, const char *dir,
/* move to the init.d directory */
cwd = get_current_dir_name();
if (chdir(dir) != 0)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "chdir %s", dir);
/* See if we can can find an existing sysinit directory */
for (cur = rcsdirs; cur->dir; cur++) {
if (stat(cur->dir, &sbuf) != 0) {
if (errno != ENOENT)
- error(1, errno, "%s", cur->dir);
+ error(EXIT_FAILURE, errno, "stat %s", cur->dir);
} else {
/* found one */
break;
@@ -65,7 +65,7 @@ bool initd_installrm_links(const initd_list_t *ilp, const char *dir,
/* Return to the original directory */
if (chdir(cwd) != 0)
- error(1, errno, "%s", cwd);
+ error(EXIT_FAILURE, errno, "chdir %s", cwd);
free(cwd);
ret = true;
@@ -105,12 +105,12 @@ static void install_level_links(const initd_list_t *ilp,
errno = 0;
mode_t dirmode = (S_IRWXU|S_IRWXG|S_IRWXO);
if (mkdir(dir, dirmode) < 0)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "mkdir %s", dir);
/* Change to the created directory */
if (chdir(dir) < 0)
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "chdir %s", dir);
} else {
- error(1, errno, "%s", dir);
+ error(EXIT_FAILURE, errno, "chdir %s", dir);
}
}
@@ -176,7 +176,8 @@ static void remove_existing_link(const initd_t *ip,
}
if (remove(path) != 0) {
if (errno != ENOENT)
- error(1, errno, "%s", path);
+ error(EXIT_FAILURE, errno,
+ "remove %s", path);
}
/* Clear the active bit for this level */
@@ -235,7 +236,7 @@ static void install_new_link(const initd_t *ip, const struct rcpair *rcp,
path, target, bdir);
}
if (symlink(target, path) < 0)
- error(1, errno, "%s -> %s", path, target);
+ error(EXIT_FAILURE, errno, "symlink %s -> %s", path, target);
/* Set the active bit for this level */
initd_set_rc(ip, akey, rc);
diff --git a/lib/parse.c b/lib/parse.c
index 32c3950..7d0d03e 100644
--- a/lib/parse.c
+++ b/lib/parse.c
@@ -60,7 +60,7 @@ initd_t *initd_parse(const char *path)
}
if (ferror(ifd))
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "fgets %s", path);
initd_close(ifd);
@@ -83,7 +83,7 @@ FILE *initd_open(const char *path)
fd = fopen(path, "r");
if (!fd)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "fopen %s", path);
out:
return fd;
@@ -95,7 +95,7 @@ static void initd_close(FILE *ifd)
return;
if (fclose(ifd) != 0)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "fclose %s", path);
}
static initd_key_t initd_parse_line(initd_t *ip, const char *line,
@@ -175,7 +175,7 @@ static initd_key_t initd_parse_line(initd_t *ip, const char *line,
/* Store the key string */
kstring = strndup(tmp, pos - tmp);
if (!kstring)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "strndup");
/* Strip any trailing spaces from the key string */
for (n = strlen(kstring) - 1; n >= 0; n--) {
@@ -357,7 +357,7 @@ static void initd_parse_line_tokens(initd_t *ip, const char *line,
/* reached end of line or space following token */
tok = strndup(a, b-a);
if (!tok)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "strndup");
switch(key) {
case KEY_DSTART:
diff --git a/lib/str.c b/lib/str.c
index 6d1fbe7..e378175 100644
--- a/lib/str.c
+++ b/lib/str.c
@@ -16,7 +16,8 @@ char *d_string_new(const char *init)
new = strdup(init ? init : "");
if (!new)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "strdup %s",
+ init ? init : "");
return new;
}
@@ -32,7 +33,7 @@ static char *d_string_grow(char *ds, size_t len) {
ds = realloc(ds, strlen(ds) + len + 1);
if (!ds)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "realloc");
out:
return ds;
diff --git a/lib/strarg.c b/lib/strarg.c
index 1fb8968..6cd04ff 100644
--- a/lib/strarg.c
+++ b/lib/strarg.c
@@ -11,11 +11,11 @@ strarg_t *strarg_new(void)
{
strarg_t *sp = malloc(sizeof(strarg_t));
if (!sp)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "malloc");
sp->str = malloc(sizeof(char *));
if (!sp)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "malloc");
*(sp->str) = NULL;
sp->nstr = 0;
@@ -47,7 +47,7 @@ void strarg_add(strarg_t *sp, const char *s)
sp->str = realloc(sp->str, (sp->nstr + 2) * sizeof(char *));
if (!sp)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "realloc");
sp->str[(sp->nstr)++] = d_string_new(s);
sp->str[sp->nstr] = NULL;
@@ -69,7 +69,7 @@ void strarg_pop(strarg_t *sp)
/* resize for one less element */
sp->str = realloc(sp->str, len * sizeof(char *));
if (!sp->str)
- error(2, errno, "%s", __FUNCTION__);
+ error(EXIT_FAILURE, errno, "realloc");
sp->str[--(sp->nstr)] = NULL;
}