diff options
Diffstat (limited to 'src/policy.c')
| -rw-r--r-- | src/policy.c | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/policy.c b/src/policy.c index 47a64f1..657e0c9 100644 --- a/src/policy.c +++ b/src/policy.c @@ -10,6 +10,7 @@ #include "authz.h" #include <ctype.h> +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -290,25 +291,30 @@ void policy_free(policy_t *p) int policy_load(policy_t *p) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int lineno = 0; + int rc; - f = fopen(p->path, "r"); - if (f == NULL) { + // A read that stopped because the file would not give an answer is not an + // empty policy: a directory, an I/O error halfway, a mount pulled out. The + // lines read so far are not the file, so this is the broken-host answer (3), + // not the empty-policy answer (0). + + rc = authz_read_lines(p->path, &lines); + if (rc != 0) { p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); + fprintf(stderr, "%s: cannot read the policy: %s\n", p->path, + strerror(errno)); + lines_free(&lines); return -2; } - while ((len = getline(&line, &cap, f)) != -1) { + for (i = 0; i < lines.n; i++) { char *hash; char *text; lineno++; - text = authz_trim(line); + text = authz_trim(lines.v[i]); hash = strchr(text, '#'); if (hash != NULL) { *hash = '\0'; @@ -322,19 +328,7 @@ int policy_load(policy_t *p) else parse_rule(p, text, lineno); } - free(line); - // A read that stopped because the file would not give an answer is not an - // empty policy: a directory, an I/O error halfway, a mount pulled out. The - // lines read so far are not the file, so this is the broken-host answer (3), - // not the empty-policy answer (0). - if (ferror(f)) { - fclose(f); - p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); - return -2; - } - fclose(f); + lines_free(&lines); return p->errors > 0 ? -1 : 0; } |
