util: add new write_string_filef() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Jun 2018 14:02:32 +0000 (16:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 Jun 2018 12:39:15 +0000 (14:39 +0200)
This new helper combines asprintf() and write_string_file() in one,
which is useful at various places to shorten the code a bit.

src/basic/capability-util.c
src/basic/fileio.c
src/basic/fileio.h
src/core/main.c

index 645cd32..a425580 100644 (file)
@@ -228,10 +228,10 @@ finish:
 }
 
 static int drop_from_file(const char *fn, uint64_t keep) {
-        int r, k;
-        uint32_t hi, lo;
+        _cleanup_free_ char *p = NULL;
         uint64_t current, after;
-        char *p;
+        uint32_t hi, lo;
+        int r, k;
 
         r = read_one_line_file(fn, &p);
         if (r < 0)
@@ -241,8 +241,6 @@ static int drop_from_file(const char *fn, uint64_t keep) {
         assert_cc(sizeof(lo) == sizeof(unsigned));
 
         k = sscanf(p, "%u %u", &lo, &hi);
-        free(p);
-
         if (k != 2)
                 return -EIO;
 
@@ -255,13 +253,7 @@ static int drop_from_file(const char *fn, uint64_t keep) {
         lo = (unsigned) (after & 0xFFFFFFFFULL);
         hi = (unsigned) ((after >> 32ULL) & 0xFFFFFFFFULL);
 
-        if (asprintf(&p, "%u %u", lo, hi) < 0)
-                return -ENOMEM;
-
-        r = write_string_file(fn, p, WRITE_STRING_FILE_CREATE);
-        free(p);
-
-        return r;
+        return write_string_filef(fn, WRITE_STRING_FILE_CREATE, "%u %u", lo, hi);
 }
 
 int capability_bounding_set_drop_usermode(uint64_t keep) {
index c12b4ac..5837163 100644 (file)
@@ -206,6 +206,25 @@ fail:
         return 0;
 }
 
+int write_string_filef(
+                const char *fn,
+                WriteStringFileFlags flags,
+                const char *format, ...) {
+
+        _cleanup_free_ char *p = NULL;
+        va_list ap;
+        int r;
+
+        va_start(ap, format);
+        r = vasprintf(&p, format, ap);
+        va_end(ap);
+
+        if (r < 0)
+                return -ENOMEM;
+
+        return write_string_file(fn, p, flags);
+}
+
 int read_one_line_file(const char *fn, char **line) {
         _cleanup_fclose_ FILE *f = NULL;
         int r;
index addd9ba..744d319 100644 (file)
@@ -39,6 +39,8 @@ static inline int write_string_file(const char *fn, const char *line, WriteStrin
         return write_string_file_ts(fn, line, flags, NULL);
 }
 
+int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
+
 int read_one_line_file(const char *fn, char **line);
 int read_full_file(const char *fn, char **contents, size_t *size);
 int read_full_stream(FILE *f, char **contents, size_t *size);
index 4158424..86a2f43 100644 (file)
@@ -1259,10 +1259,9 @@ static int bump_unix_max_dgram_qlen(void) {
         unsigned long v;
         int r;
 
-        /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel
-         * default of 16 is simply too low. We set the value really
-         * really early during boot, so that it is actually applied to
-         * all our sockets, including the $NOTIFY_SOCKET one. */
+        /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel default of 16 is simply too low. We set the value
+         * really really early during boot, so that it is actually applied to all our sockets, including the
+         * $NOTIFY_SOCKET one. */
 
         r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen);
         if (r < 0)
@@ -1270,16 +1269,12 @@ static int bump_unix_max_dgram_qlen(void) {
 
         r = safe_atolu(qlen, &v);
         if (r < 0)
-                return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length, ignoring: %m");
+                return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length '%s', ignoring: %m", qlen);
 
         if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN)
                 return 0;
 
-        qlen = mfree(qlen);
-        if (asprintf(&qlen, "%lu\n", DEFAULT_UNIX_MAX_DGRAM_QLEN) < 0)
-                return log_oom();
-
-        r = write_string_file("/proc/sys/net/unix/max_dgram_qlen", qlen, 0);
+        r = write_string_filef("/proc/sys/net/unix/max_dgram_qlen", 0, "%lu", DEFAULT_UNIX_MAX_DGRAM_QLEN);
         if (r < 0)
                 return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
                                       "Failed to bump AF_UNIX datagram queue length, ignoring: %m");