chiark / gitweb /
service,strv: introduce strv_find_startswith() and make use of it
authorLennart Poettering <lennart@poettering.net>
Thu, 21 Aug 2014 14:22:34 +0000 (16:22 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 21 Aug 2014 15:24:21 +0000 (17:24 +0200)
Unlike strv_find_prefix() the new call will return a pointer to the
suffix of the item we found, instead of the whole item. This is more
closer inline with what startswith() does, and allows us to simplify a
couple of invocations.

src/core/service.c
src/shared/strv.c
src/shared/strv.h

index fc952e848f08ee9d783c9f3c97a2f9e7e0f788a0..262a40cc8b0e4f00a89429485ad2c8248fed98dc 100644 (file)
@@ -2526,12 +2526,13 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
         }
 
         /* Interpret MAINPID= */
         }
 
         /* Interpret MAINPID= */
-        e = strv_find_prefix(tags, "MAINPID=");
+        e = strv_find_startswith(tags, "MAINPID=");
         if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
         if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
-                if (parse_pid(e + 8, &pid) < 0)
+                if (parse_pid(e, &pid) < 0)
                         log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
                 else {
                         log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
                 else {
-                        log_debug_unit(u->id, "%s: got %s", u->id, e);
+                        log_debug_unit(u->id, "%s: got MAINPID=%s", u->id, e);
+
                         service_set_main_pid(s, pid);
                         unit_watch_pid(UNIT(s), pid);
                         notify_dbus = true;
                         service_set_main_pid(s, pid);
                         unit_watch_pid(UNIT(s), pid);
                         notify_dbus = true;
@@ -2546,44 +2547,41 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
         }
 
         /* Interpret STATUS= */
         }
 
         /* Interpret STATUS= */
-        e = strv_find_prefix(tags, "STATUS=");
+        e = strv_find_startswith(tags, "STATUS=");
         if (e) {
         if (e) {
-                char *t;
+                _cleanup_free_ char *t = NULL;
 
 
-                if (e[7]) {
-                        if (!utf8_is_valid(e+7)) {
+                if (!isempty(e)) {
+                        if (!utf8_is_valid(e))
                                 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
                                 log_warning_unit(u->id, "Status message in notification is not UTF-8 clean.");
-                                return;
-                        }
+                        else {
+                                log_debug_unit(u->id, "%s: got STATUS=%s", u->id, e);
 
 
-                        log_debug_unit(u->id, "%s: got %s", u->id, e);
-
-                        t = strdup(e+7);
-                        if (!t) {
-                                log_oom();
-                                return;
+                                t = strdup(e);
+                                if (!t)
+                                        log_oom();
                         }
                         }
-
-                } else
-                        t = NULL;
+                }
 
                 if (!streq_ptr(s->status_text, t)) {
 
                 if (!streq_ptr(s->status_text, t)) {
+
                         free(s->status_text);
                         s->status_text = t;
                         free(s->status_text);
                         s->status_text = t;
+                        t = NULL;
+
                         notify_dbus = true;
                         notify_dbus = true;
-                } else
-                        free(t);
+                }
         }
 
         /* Interpret ERRNO= */
         }
 
         /* Interpret ERRNO= */
-        e = strv_find_prefix(tags, "ERRNO=");
+        e = strv_find_startswith(tags, "ERRNO=");
         if (e) {
                 int status_errno;
 
         if (e) {
                 int status_errno;
 
-                if (safe_atoi(e + 6, &status_errno) < 0 || status_errno < 0)
+                if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
                         log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
                 else {
                         log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
                 else {
-                        log_debug_unit(u->id, "%s: got %s", u->id, e);
+                        log_debug_unit(u->id, "%s: got ERRNO=%s", u->id, e);
 
                         if (s->status_errno != status_errno) {
                                 s->status_errno = status_errno;
 
                         if (s->status_errno != status_errno) {
                                 s->status_errno = status_errno;
index 6448f3170fc837ac6ef7035fb8506bc9c430ab52..0df978d23b911f53be7e7c597e01c8b26c28a2a6 100644 (file)
@@ -52,6 +52,23 @@ char *strv_find_prefix(char **l, const char *name) {
         return NULL;
 }
 
         return NULL;
 }
 
+char *strv_find_startswith(char **l, const char *name) {
+        char **i, *e;
+
+        assert(name);
+
+        /* Like strv_find_prefix, but actually returns only the
+         * suffix, not the whole item */
+
+        STRV_FOREACH(i, l) {
+                e = startswith(*i, name);
+                if (e)
+                        return e;
+        }
+
+        return NULL;
+}
+
 void strv_free(char **l) {
         char **k;
 
 void strv_free(char **l) {
         char **k;
 
index ee55c148aa2ab7086e753867bd7148f0bc1b8335..9c9633c515f04db60488461c810c89af7cffe927 100644 (file)
@@ -28,6 +28,7 @@
 
 char *strv_find(char **l, const char *name) _pure_;
 char *strv_find_prefix(char **l, const char *name) _pure_;
 
 char *strv_find(char **l, const char *name) _pure_;
 char *strv_find_prefix(char **l, const char *name) _pure_;
+char *strv_find_startswith(char **l, const char *name) _pure_;
 
 void strv_free(char **l);
 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
 
 void strv_free(char **l);
 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);