snprintf(c, sizeof(c), PID_FMT"\n", pid);
- return write_string_file(fs, c);
+ return write_string_file_no_create(fs, c);
}
int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
sc = strstrip(contents);
if (sc[0] == 0) {
- r = write_string_file(fs, agent);
+ r = write_string_file_no_create(fs, agent);
if (r < 0)
return r;
} else if (!streq(sc, agent))
sc = strstrip(contents);
if (streq(sc, "0")) {
- r = write_string_file(fs, "1");
+ r = write_string_file_no_create(fs, "1");
if (r < 0)
return r;
if (r < 0)
return r;
- r = write_string_file(fs, "0");
+ r = write_string_file_no_create(fs, "0");
if (r < 0)
return r;
if (r < 0)
return r;
- r = write_string_file(fs, "");
+ r = write_string_file_no_create(fs, "");
if (r < 0)
return r;
if (r < 0)
return r;
- return write_string_file(p, value);
+ return write_string_file_no_create(p, value);
}
static const char mask_names[] =
return write_string_stream(f, line);
}
+int write_string_file_no_create(const char *fn, const char *line) {
+ _cleanup_fclose_ FILE *f = NULL;
+ int fd;
+
+ assert(fn);
+ assert(line);
+
+ /* We manually build our own version of fopen(..., "we") that
+ * without O_CREAT */
+ fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0)
+ return -errno;
+
+ f = fdopen(fd, "we");
+ if (!f) {
+ safe_close(fd);
+ return -errno;
+ }
+
+ return write_string_stream(f, line);
+}
+
int write_string_file_atomic(const char *fn, const char *line) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int write_string_stream(FILE *f, const char *line);
int write_string_file(const char *fn, const char *line);
+int write_string_file_no_create(const char *fn, const char *line);
int write_string_file_atomic(const char *fn, const char *line);
int read_one_line_file(const char *fn, char **line);