src/shared/time-dst.h \
src/shared/calendarspec.c \
src/shared/calendarspec.h \
+ src/shared/fileio.c \
+ src/shared/fileio.h \
src/shared/output-mode.h
#-------------------------------------------------------------------------------
src/shared/mkdir.h \
src/shared/ask-password-api.c \
src/shared/ask-password-api.h \
+ src/shared/fileio-label.c \
+ src/shared/fileio-label.h \
src/shared/dev-setup.c \
src/shared/dev-setup.h
$(DBUS_CFLAGS)
systemd_hostnamed_LDADD = \
+ libsystemd-label.la \
libsystemd-shared.la \
libsystemd-daemon.la \
libsystemd-dbus.la
$(DBUS_CFLAGS)
systemd_timedated_LDADD = \
+ libsystemd-label.la \
libsystemd-shared.la \
libsystemd-daemon.la \
libsystemd-dbus.la
#include "build.h"
#include "util.h"
#include "strxcpyx.h"
+#include "fileio.h"
#define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
#define svg(...) printf(__VA_ARGS__)
#include "strv.h"
#include "util.h"
#include "conf-files.h"
+#include "fileio.h"
static const char conf_file_dirs[] =
"/etc/binfmt.d\0"
#include "bootchart.h"
#include "util.h"
+#include "fileio.h"
double graph_start;
double log_start;
#include "hashmap.h"
#include "cgroup-util.h"
#include "build.h"
+#include "fileio.h"
typedef struct Group {
char *path;
#include "cgroup-attr.h"
#include "cgroup-util.h"
#include "list.h"
+#include "fileio.h"
int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) {
int r;
#include "condition.h"
#include "virt.h"
#include "path-util.h"
+#include "fileio.h"
Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
Condition *c;
#include "strv.h"
#include "dbus-common.h"
#include "syscall-list.h"
+#include "fileio.h"
DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput);
DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput);
#include "cgroup-util.h"
#include "strv.h"
#include "path-util.h"
+#include "fileio.h"
const char bus_unit_interface[] _introspect_("Unit") = BUS_UNIT_INTERFACE;
#include "path-util.h"
#include "syscall-list.h"
#include "env-util.h"
+#include "fileio.h"
#define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
#include "macro.h"
#include "util.h"
#include "log.h"
+#include "fileio.h"
static int read_and_strip_hostname(const char *path, char **hn) {
char *s;
#include "util.h"
#include "macro.h"
#include "virt.h"
+#include "fileio.h"
enum {
/* We don't list LC_ALL here on purpose. People should be
#include "mkdir.h"
#include "log.h"
#include "virt.h"
+#include "fileio.h"
static int shorten_uuid(char destination[36], const char *source) {
unsigned i, j;
#include "locale-setup.h"
#include "selinux-setup.h"
#include "ima-setup.h"
+#include "fileio.h"
static enum {
ACTION_RUN,
#include "util.h"
#include "utf8.h"
#include "env-util.h"
+#include "fileio.h"
#ifdef HAVE_SYSV_COMPAT
#include "missing.h"
#include "cgroup-attr.h"
#include "mkdir.h"
+#include "label.h"
+#include "fileio-label.h"
const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
[UNIT_SERVICE] = &service_vtable,
return -ENOMEM;
mkdir_p(p, 0755);
- return write_one_line_file_atomic(q, data);
+ return write_one_line_file_atomic_label(q, data);
}
int unit_remove_drop_in(Unit *u, bool runtime, const char *name) {
#include "special.h"
#include "bus-errors.h"
#include "virt.h"
+#include "fileio.h"
static bool arg_skip = false;
static bool arg_force = false;
#include "special.h"
#include "mkdir.h"
#include "virt.h"
+#include "fileio.h"
static const char *arg_dest = "/tmp";
static bool arg_enabled = true;
#include "mkdir.h"
#include "unit-name.h"
#include "virt.h"
+#include "fileio.h"
static const char *arg_dest = "/tmp";
#include "strv.h"
#include "sd-id128.h"
#include "virt.h"
+#include "fileio.h"
static enum transport {
TRANSPORT_NORMAL,
#include "def.h"
#include "virt.h"
#include "env-util.h"
+#include "fileio-label.h"
+#include "label.h"
#define INTERFACE \
" <interface name=\"org.freedesktop.hostname1\">\n" \
return 0;
}
-
- return write_one_line_file_atomic("/etc/hostname", data[PROP_STATIC_HOSTNAME]);
+ return write_one_line_file_atomic_label("/etc/hostname", data[PROP_STATIC_HOSTNAME]);
}
static int write_data_other(void) {
return 0;
}
- r = write_env_file("/etc/machine-info", l);
+ r = write_env_file_label("/etc/machine-info", l);
strv_free(l);
return r;
log_open();
umask(0022);
+ label_init("/etc");
if (argc == 2 && streq(argv[1], "--introspect")) {
fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
#include <systemd/sd-login.h>
#endif
+#include "fileio.h"
#include "mkdir.h"
#include "hashmap.h"
#include "journal-file.h"
#include "polkit.h"
#include "def.h"
#include "env-util.h"
+#include "fileio.h"
+#include "fileio-label.h"
+#include "label.h"
#define INTERFACE \
" <interface name=\"org.freedesktop.locale1\">\n" \
return 0;
}
- r = write_env_file("/etc/locale.conf", l);
+ r = write_env_file_label("/etc/locale.conf", l);
strv_free(l);
return r;
return 0;
}
- r = write_env_file("/etc/vconsole.conf", l);
+ r = write_env_file_label("/etc/vconsole.conf", l);
strv_free(l);
return r;
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
-
+ label_init("/etc");
umask(0022);
if (argc == 2 && streq(argv[1], "--introspect")) {
#include "special.h"
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
+#include "fileio-label.h"
+#include "label.h"
#define BUS_MANAGER_INTERFACE \
" <interface name=\"org.freedesktop.login1.Manager\">\n" \
}
mkdir_p_label("/etc/udev/rules.d", 0755);
- r = write_one_line_file_atomic(file, rule);
+ label_init("/etc");
+ r = write_one_line_file_atomic_label(file, rule);
if (r < 0)
goto finish;
#include "mkdir.h"
#include "path-util.h"
#include "logind-inhibit.h"
+#include "fileio.h"
Inhibitor* inhibitor_new(Manager *m, const char* id) {
Inhibitor *i;
#include "path-util.h"
#include "cgroup-util.h"
#include "logind-session.h"
+#include "fileio.h"
Session* session_new(Manager *m, User *u, const char *id) {
Session *s;
#include "cgroup-util.h"
#include "hashmap.h"
#include "strv.h"
+#include "fileio.h"
User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
User *u;
#include "dbus-common.h"
#include "def.h"
#include "socket-util.h"
+#include "fileio.h"
static int parse_argv(pam_handle_t *handle,
int argc, const char **argv,
#include "macro.h"
#include "sd-login.h"
#include "strv.h"
+#include "fileio.h"
_public_ int sd_pid_get_session(pid_t pid, char **session) {
int r;
#include "log.h"
#include "util.h"
#include "cgroup-util.h"
+#include "fileio.h"
int main(int argc, char*argv[]) {
int ret = EXIT_FAILURE;
#include "strv.h"
#include "conf-files.h"
#include "virt.h"
+#include "fileio.h"
static char **arg_proc_cmdline_modules = NULL;
#include "dev-setup.h"
#include "fdset.h"
#include "build.h"
+#include "fileio.h"
typedef enum LinkJournal {
LINK_NO,
#include "util.h"
#include "virt.h"
+#include "fileio.h"
static bool arg_skip = false;
static bool arg_force = false;
#include "readahead-common.h"
#include "util.h"
#include "missing.h"
+#include "fileio.h"
int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
assert(fd >= 0);
#include "audit.h"
#include "util.h"
#include "log.h"
+#include "fileio.h"
int audit_session_from_pid(pid_t pid, uint32_t *id) {
char *s;
#include "capability.h"
#include "util.h"
#include "log.h"
+#include "fileio.h"
int have_effective_cap(int value) {
cap_t cap;
#include "path-util.h"
#include "strv.h"
#include "unit-name.h"
+#include "fileio.h"
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
char *fs;
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+ Copyright 2010 Harald Hoyer
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "fileio-label.h"
+#include "label.h"
+
+int write_one_line_file_atomic_label(const char *fn, const char *line) {
+ int r;
+
+ r = label_context_set(fn, S_IFREG);
+ if (r < 0)
+ return r;
+
+ write_one_line_file_atomic(fn, line);
+
+ label_context_clear();
+
+ return r;
+}
+
+int write_env_file_label(const char *fname, char **l) {
+ int r;
+
+ r = label_context_set(fname, S_IFREG);
+ if (r < 0)
+ return r;
+
+ write_env_file(fname, l);
+
+ label_context_clear();
+
+ return r;
+}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+ Copyright 2010 Harald Hoyer
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdio.h>
+#include "fileio.h"
+
+int write_one_line_file_atomic_label(const char *fn, const char *line);
+int write_env_file_label(const char *fname, char **l);
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <unistd.h>
+#include "fileio.h"
+#include "util.h"
+#include "strv.h"
+
+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 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;
+}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+#include <stddef.h>
+#include "macro.h"
+
+int write_one_line_file(const char *fn, const char *line);
+int write_one_line_file_atomic(const char *fn, const char *line);
+int read_one_line_file(const char *fn, char **line);
+int read_full_file(const char *fn, char **contents, size_t *size);
+
+int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
+int load_env_file(const char *fname, char ***l);
+int write_env_file(const char *fname, char **l);
#include "log.h"
#include "strv.h"
#include "hwclock.h"
+#include "fileio.h"
static int rtc_open(int flags) {
int fd;
#include <malloc.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include "label.h"
+#include "strv.h"
#include "util.h"
#include "path-util.h"
void label_retest_selinux(void);
int label_bind(int fd, const struct sockaddr *addr, socklen_t addrlen);
+
+int label_write_one_line_file_atomic(const char *fn, const char *line);
+int label_write_env_file(const char *fname, char **l);
+int label_fopen_temporary(const char *path, FILE **_f, char **_temp_path);
#include "path-util.h"
#include "socket-util.h"
#include "missing.h"
+#include "fileio.h"
int socket_address_parse(SocketAddress *a, const char *s) {
int r;
#include "exit-status.h"
#include "hashmap.h"
#include "env-util.h"
+#include "fileio.h"
int saved_argc = 0;
char **saved_argv = NULL;
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;
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);
pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
int get_starttime_of_pid(pid_t pid, unsigned long long *st);
-int write_one_line_file(const char *fn, const char *line);
-int write_one_line_file_atomic(const char *fn, const char *line);
-int read_one_line_file(const char *fn, char **line);
-int read_full_file(const char *fn, char **contents, size_t *size);
-
-int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
-int load_env_file(const char *fname, char ***l);
-int write_env_file(const char *fname, char **l);
-
char *strappend(const char *s, const char *suffix);
char *strnappend(const char *s, const char *suffix, size_t length);
#include "util.h"
#include "virt.h"
+#include "fileio.h"
/* Returns a short identifier for the various VM implementations */
int detect_vm(const char **id) {
#include "util.h"
#include "utmp-wtmp.h"
#include "mkdir.h"
+#include "fileio.h"
union shutdown_buffer {
struct sd_shutdown_command command;
#include "util.h"
#include "systemd/sd-id128.h"
#include "systemd/sd-messages.h"
+#include "fileio.h"
int main(int argc, char *argv[]) {
const char *verb;
#include "hashmap.h"
#include "path-util.h"
#include "conf-files.h"
+#include "fileio.h"
static char **arg_prefixes = NULL;
#include "logs-show.h"
#include "path-util.h"
#include "socket-util.h"
+#include "fileio.h"
static const char *arg_type = NULL;
static const char *arg_load_state = NULL;
#include "hashmap.h"
#include "load-fragment.h"
#include "strv.h"
+#include "fileio.h"
static void test_unit_file_get_set(void) {
int r;
#include "hwclock.h"
#include "conf-files.h"
#include "path-util.h"
+#include "fileio-label.h"
+#include "label.h"
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
return 0;
}
}
-
- r = write_one_line_file_atomic("/etc/adjtime", w);
+ label_init("/etc");
+ r = write_one_line_file_atomic_label("/etc/adjtime", w);
free(w);
return r;
#include <linux/pci_regs.h>
#include "udev.h"
+#include "fileio.h"
enum netname_type{
NET_UNDEF,
#include "sd-daemon.h"
#include "cgroup-util.h"
#include "dev-setup.h"
+#include "fileio.h"
static bool debug;
#include "log.h"
#include "macro.h"
#include "virt.h"
+#include "fileio.h"
static bool is_vconsole(int fd) {
unsigned char data[1];