Merge lp:~vicamo/upstart/xenial-escape-systemd-strings into lp:~ubuntu-core-dev/upstart/xenial

Proposed by You-Sheng Yang
Status: Merged
Merged at revision: 1632
Proposed branch: lp:~vicamo/upstart/xenial-escape-systemd-strings
Merge into: lp:~ubuntu-core-dev/upstart/xenial
Diff against target: 83 lines (+51/-4)
1 file modified
extra/upstart-local-bridge.c (+51/-4)
To merge this branch: bzr merge lp:~vicamo/upstart/xenial-escape-systemd-strings
Reviewer Review Type Date Requested Status
Łukasz Zemczak not an upstart maintainer Approve
Review via email: mp+307140@code.launchpad.net

Description of the change

upstart-local-bridge: escape strings for systemd

Android property names and values may contain invalid characters for systemd and breaks the mechanism here.

To post a comment you must log in.
Revision history for this message
Łukasz Zemczak (sil2100) wrote :

In overall the proposed change looks fine an seems to work reasonably well. The same could have been realized without the new hexchar() function, but having it makes things potentially slightly faster and simplified.
That being said, I do not know the upstart code-base that well, so maybe it would be nice if some other maintainer took a quick look as well.

One stylistic recommendation: in the inline comments, the if, else if and else parts could use { } even in the single-line conditional commands. It's much cleaner this way and follows the upstart code style better.

review: Approve (not an upstart maintainer)
Revision history for this message
Łukasz Zemczak (sil2100) :
1633. By You-Sheng Yang

Address stylistic review comments.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'extra/upstart-local-bridge.c'
--- extra/upstart-local-bridge.c 2015-01-16 23:55:17 +0000
+++ extra/upstart-local-bridge.c 2016-10-05 09:27:10 +0000
@@ -912,6 +912,47 @@
912912
913}913}
914914
915static char
916hexchar (char c)
917{
918 static const char table[16] = "0123456789abcdef";
919
920 return table[c & 0xf];
921}
922
923static char*
924escape_string (const char *f)
925{
926 char *r = NULL;
927 char *t = NULL;
928
929 nih_assert (f);
930
931 r = t = NIH_MUST (nih_alloc (NULL, strlen(f) * 4 + 1));
932
933#define VALID_CHARS \
934 "0123456789" \
935 "abcdefghijklmnopqrstuvwxyz" \
936 "ABCDEFGHIJKLMNOPQRSTUVWXYX" \
937 ":_."
938 for (; *f; f++) {
939 if (*f == '/') {
940 *(t++) = '-';
941 } else if (!strchr(VALID_CHARS, *f)) {
942 *(t++) = '\\';
943 *(t++) = 'x';
944 *(t++) = hexchar(*f >> 4);
945 *(t++) = hexchar(*f);
946 } else {
947 *(t++) = *f;
948 }
949 }
950
951 *t = '\0';
952
953 return r;
954}
955
915static void956static void
916systemd_launch_instance (ClientConnection *client,957systemd_launch_instance (ClientConnection *client,
917 const char *pair,958 const char *pair,
@@ -919,6 +960,8 @@
919{960{
920 nih_local char *safe_pair = NULL;961 nih_local char *safe_pair = NULL;
921 nih_local char **key_value = NULL;962 nih_local char **key_value = NULL;
963 nih_local char *escaped_key = NULL;
964 nih_local char *escaped_value = NULL;
922 nih_local char *group_name = NULL;965 nih_local char *group_name = NULL;
923 nih_local char *unit_name = NULL;966 nih_local char *unit_name = NULL;
924 nih_local char *job_name = NULL;967 nih_local char *job_name = NULL;
@@ -933,14 +976,18 @@
933 /* Get key val from the key=val pair */976 /* Get key val from the key=val pair */
934 key_value = NIH_MUST (nih_str_split (NULL, safe_pair, "=", TRUE));977 key_value = NIH_MUST (nih_str_split (NULL, safe_pair, "=", TRUE));
935978
979 /* Escape key/value for systemd */
980 escaped_key = NIH_MUST (escape_string (key_value[0]));
981 escaped_value = NIH_MUST (escape_string (key_value[1] ? key_value[1] : "(null)"));
982
936 /* Construct systemd event@key=*.target group name */983 /* Construct systemd event@key=*.target group name */
937 group_name = NIH_MUST (nih_sprintf (NULL, "%s@%s=*.target",984 group_name = NIH_MUST (nih_sprintf (NULL, "%s@%s\\x3d*.target",
938 event_name, key_value[0]));985 event_name, escaped_key));
939986
940 /* Construct systemd event@key=value.target unit name */987 /* Construct systemd event@key=value.target unit name */
941 unit_name = NIH_MUST (nih_sprintf (NULL, "%s@%s\\x3d%s.target",988 unit_name = NIH_MUST (nih_sprintf (NULL, "%s@%s\\x3d%s.target",
942 event_name, key_value[0],989 event_name, escaped_key,
943 key_value[1]));990 escaped_value));
944991
945 /* Stop group */992 /* Stop group */
946 int pid = -1;993 int pid = -1;

Subscribers

People subscribed via source and target branches