summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
authorHarald Hoyer <harald@redhat.com>2013-02-14 12:26:13 +0100
committerHarald Hoyer <harald@redhat.com>2013-02-14 16:19:38 +0100
commita5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7 (patch)
treeb3cc19cede403ac324f56c54ca89db0feef2a72d /src/shared/util.c
parentedc211f314f257921eb3599b5dca8d51984b9328 (diff)
honor SELinux labels, when creating and writing config files
Also split out some fileio functions to fileio.c and provide a SELinux aware pendant in fileio-label.c see https://bugzilla.redhat.com/show_bug.cgi?id=881577
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c359
1 files changed, 1 insertions, 358 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 4f0b652f4f..152724949d 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -71,6 +71,7 @@
#include "exit-status.h"
#include "hashmap.h"
#include "env-util.h"
+#include "fileio.h"
int saved_argc = 0;
char **saved_argv = NULL;
@@ -528,31 +529,6 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
return 0;
}
-int write_one_line_file(const char *fn, const char *line) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(fn);
- assert(line);
-
- f = fopen(fn, "we");
- if (!f)
- return -errno;
-
- errno = 0;
- if (fputs(line, f) < 0)
- return errno ? -errno : -EIO;
-
- if (!endswith(line, "\n"))
- fputc('\n', f);
-
- fflush(f);
-
- if (ferror(f))
- return errno ? -errno : -EIO;
-
- return 0;
-}
-
int fchmod_umask(int fd, mode_t m) {
mode_t u;
int r;
@@ -564,339 +540,6 @@ int fchmod_umask(int fd, mode_t m) {
return r;
}
-int write_one_line_file_atomic(const char *fn, const char *line) {
- _cleanup_fclose_ FILE *f = NULL;
- _cleanup_free_ char *p = NULL;
- int r;
-
- assert(fn);
- assert(line);
-
- r = fopen_temporary(fn, &f, &p);
- if (r < 0)
- return r;
-
- fchmod_umask(fileno(f), 0644);
-
- errno = 0;
- if (fputs(line, f) < 0) {
- r = -errno;
- goto finish;
- }
-
- if (!endswith(line, "\n"))
- fputc('\n', f);
-
- fflush(f);
-
- if (ferror(f))
- r = errno ? -errno : -EIO;
- else {
- if (rename(p, fn) < 0)
- r = -errno;
- else
- r = 0;
- }
-
-finish:
- if (r < 0)
- unlink(p);
-
- return r;
-}
-
-int read_one_line_file(const char *fn, char **line) {
- _cleanup_fclose_ FILE *f = NULL;
- char t[LINE_MAX], *c;
-
- assert(fn);
- assert(line);
-
- f = fopen(fn, "re");
- if (!f)
- return -errno;
-
- if (!fgets(t, sizeof(t), f)) {
-
- if (ferror(f))
- return errno ? -errno : -EIO;
-
- t[0] = 0;
- }
-
- c = strdup(t);
- if (!c)
- return -ENOMEM;
- truncate_nl(c);
-
- *line = c;
- return 0;
-}
-
-int read_full_file(const char *fn, char **contents, size_t *size) {
- _cleanup_fclose_ FILE *f = NULL;
- size_t n, l;
- _cleanup_free_ char *buf = NULL;
- struct stat st;
-
- assert(fn);
- assert(contents);
-
- f = fopen(fn, "re");
- if (!f)
- return -errno;
-
- if (fstat(fileno(f), &st) < 0)
- return -errno;
-
- /* Safety check */
- if (st.st_size > 4*1024*1024)
- return -E2BIG;
-
- n = st.st_size > 0 ? st.st_size : LINE_MAX;
- l = 0;
-
- for (;;) {
- char *t;
- size_t k;
-
- t = realloc(buf, n+1);
- if (!t)
- return -ENOMEM;
-
- buf = t;
- k = fread(buf + l, 1, n - l, f);
-
- if (k <= 0) {
- if (ferror(f))
- return -errno;
-
- break;
- }
-
- l += k;
- n *= 2;
-
- /* Safety check */
- if (n > 4*1024*1024)
- return -E2BIG;
- }
-
- buf[l] = 0;
- *contents = buf;
- buf = NULL;
-
- if (size)
- *size = l;
-
- return 0;
-}
-
-int parse_env_file(
- const char *fname,
- const char *separator, ...) {
-
- int r = 0;
- char *contents = NULL, *p;
-
- assert(fname);
- assert(separator);
-
- if ((r = read_full_file(fname, &contents, NULL)) < 0)
- return r;
-
- p = contents;
- for (;;) {
- const char *key = NULL;
-
- p += strspn(p, separator);
- p += strspn(p, WHITESPACE);
-
- if (!*p)
- break;
-
- if (!strchr(COMMENTS, *p)) {
- va_list ap;
- char **value;
-
- va_start(ap, separator);
- while ((key = va_arg(ap, char *))) {
- size_t n;
- char *v;
-
- value = va_arg(ap, char **);
-
- n = strlen(key);
- if (!strneq(p, key, n) ||
- p[n] != '=')
- continue;
-
- p += n + 1;
- n = strcspn(p, separator);
-
- if (n >= 2 &&
- strchr(QUOTES, p[0]) &&
- p[n-1] == p[0])
- v = strndup(p+1, n-2);
- else
- v = strndup(p, n);
-
- if (!v) {
- r = -ENOMEM;
- va_end(ap);
- goto fail;
- }
-
- if (v[0] == '\0') {
- /* return empty value strings as NULL */
- free(v);
- v = NULL;
- }
-
- free(*value);
- *value = v;
-
- p += n;
-
- r ++;
- break;
- }
- va_end(ap);
- }
-
- if (!key)
- p += strcspn(p, separator);
- }
-
-fail:
- free(contents);
- return r;
-}
-
-int load_env_file(const char *fname, char ***rl) {
-
- _cleanup_fclose_ FILE *f;
- _cleanup_strv_free_ char **m = NULL;
- _cleanup_free_ char *c = NULL;
-
- assert(fname);
- assert(rl);
-
- /* This reads an environment file, but will not complain about
- * any invalid assignments, that needs to be done by the
- * caller */
-
- f = fopen(fname, "re");
- if (!f)
- return -errno;
-
- while (!feof(f)) {
- char l[LINE_MAX], *p, *cs, *b;
-
- if (!fgets(l, sizeof(l), f)) {
- if (ferror(f))
- return -errno;
-
- /* The previous line was a continuation line?
- * Let's process it now, before we leave the
- * loop */
- if (c)
- goto process;
-
- break;
- }
-
- /* Is this a continuation line? If so, just append
- * this to c, and go to next line right-away */
- cs = endswith(l, "\\\n");
- if (cs) {
- *cs = '\0';
- b = strappend(c, l);
- if (!b)
- return -ENOMEM;
-
- free(c);
- c = b;
- continue;
- }
-
- /* If the previous line was a continuation line,
- * append the current line to it */
- if (c) {
- b = strappend(c, l);
- if (!b)
- return -ENOMEM;
-
- free(c);
- c = b;
- }
-
- process:
- p = strstrip(c ? c : l);
-
- if (*p && !strchr(COMMENTS, *p)) {
- _cleanup_free_ char *u;
- int k;
-
- u = normalize_env_assignment(p);
- if (!u)
- return -ENOMEM;
-
- k = strv_extend(&m, u);
- if (k < 0)
- return -ENOMEM;
- }
-
- free(c);
- c = NULL;
- }
-
- *rl = m;
- m = NULL;
-
- return 0;
-}
-
-int write_env_file(const char *fname, char **l) {
- char **i, *p;
- FILE *f;
- int r;
-
- r = fopen_temporary(fname, &f, &p);
- if (r < 0)
- return r;
-
- fchmod_umask(fileno(f), 0644);
-
- errno = 0;
- STRV_FOREACH(i, l) {
- fputs(*i, f);
- fputc('\n', f);
- }
-
- fflush(f);
-
- if (ferror(f)) {
- if (errno != 0)
- r = -errno;
- else
- r = -EIO;
- } else {
- if (rename(p, fname) < 0)
- r = -errno;
- else
- r = 0;
- }
-
- if (r < 0)
- unlink(p);
-
- fclose(f);
- free(p);
-
- return r;
-}
-
char *truncate_nl(char *s) {
assert(s);