From 0dcab04668c07092a20330e9b54654d870968189 Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi Date: Fri, 19 Jun 2020 14:23:37 -0700 Subject: [PATCH 1/3] [systemd-generator]: Fix the code to make sure that dependencies of host services are generated correctly for multi-asic platforms. Add code to make sure that systemd timer files are also modified to add the correct service dependency for multi-asic platforms. Signed-off-by: SuvarnaMeenakshi --- .../build_templates/sonic_debian_extension.j2 | 4 +-- .../systemd-sonic-generator.c | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index f1d6cc924a8c..dbac64bc94b6 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -516,10 +516,10 @@ sudo LANG=C cp $SCRIPTS_DIR/sonic-netns-exec $FILESYSTEM_ROOT/usr/bin/sonic-netn # Copy systemd timer configuration # It implements delayed start of services sudo cp $BUILD_TEMPLATES/snmp.timer $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable snmp.timer +echo "snmp.timer" | sudo tee -a $GENERATED_SERVICE_FILE {% if enable_system_telemetry == 'y' %} sudo cp $BUILD_TEMPLATES/telemetry.timer $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable telemetry.timer +echo "telemetry.timer" | sudo tee -a $GENERATED_SERVICE_FILE {% endif %} sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get purge -y python-dev diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 9a5fabd33484..896f7c954a8e 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -146,13 +146,19 @@ static void replace_multi_inst_dep(char *src) { char *word; char *line_copy; char *service_name; + char *dot; char *type; + char *units_list; + char *save_ptr1 = NULL; + char *save_ptr2 = NULL; ssize_t nread; bool section_done = false; char tmp_file_path[PATH_MAX]; - /* assumes that the service files has 3 sections, + /* Assumes that the service files has 3 sections, * in the order: Unit, Service and Install. + * Assumes that the timer file has 3 sectiosn, + * in the order: Unit, Timer and Install. * Read service dependency from Unit and Install * sections, replace if dependent on multi instance * service. @@ -162,30 +168,31 @@ static void replace_multi_inst_dep(char *src) { fp_tmp = fopen(tmp_file_path, "w"); while ((nread = getline(&line, &len, fp_src)) != -1 ) { - if (strstr(line, "[Service]") != NULL) { + if ((strstr(line, "[Service]") != NULL) || + (strstr(line, "[Timer]") != NULL)) { section_done = true; fputs(line,fp_tmp); - } else if (strstr(line, "[Install]") != NULL) { + } else if (strstr(line, "[Install]") != NULL) { section_done = false; fputs(line,fp_tmp); } else if ((strstr(line, "[Unit]") != NULL) || (strstr(line, "Description") != NULL) || - (section_done == true)){ + (section_done == true)) { fputs(line,fp_tmp); } else { line_copy = strdup(line); - token = strtok(line_copy, "="); - while ((word = strtok(NULL, " "))){ + token = strtok_r(line_copy, "=", &save_ptr1); + while ((word = strtok_r(NULL, " ", &save_ptr1))) { if((strchr(word, '.') == NULL) || (strchr(word, '@') != NULL)) { snprintf(buf, MAX_BUF_SIZE,"%s=%s\n",token, word); fputs(buf,fp_tmp); } else { - service_name = strdup(word); - service_name = strtok(service_name, "."); - type = strtok(NULL, " "); + service_name = strdup(word); + service_name = strtok_r(service_name, ".", &save_ptr2); + type = strtok_r(NULL, " ", &save_ptr2); if (is_multi_instance_service(word)) { - for(i = 0; i < num_asics; i++){ + for(i = 0; i < num_asics; i++) { snprintf(buf, MAX_BUF_SIZE, "%s=%s@%d.%s\n", token, service_name, i, type); fputs(buf,fp_tmp); @@ -571,7 +578,8 @@ int main(int argc, char **argv) { return 1; } - num_asics = get_num_of_asic(); + //num_asics = get_num_of_asic(); + num_asics = 6; strcpy(install_dir, argv[1]); strcat(install_dir, "/"); From f30b8177c743eee0c06b974db85ff5378e71a829 Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi Date: Fri, 19 Jun 2020 14:32:49 -0700 Subject: [PATCH 2/3] [systemd-generator]: Minor fix, remove debug code and remove unused variable. Signed-off-by: SuvarnaMeenakshi --- src/systemd-sonic-generator/systemd-sonic-generator.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 896f7c954a8e..ce0b7bc15a33 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -146,9 +146,7 @@ static void replace_multi_inst_dep(char *src) { char *word; char *line_copy; char *service_name; - char *dot; char *type; - char *units_list; char *save_ptr1 = NULL; char *save_ptr2 = NULL; ssize_t nread; @@ -578,8 +576,7 @@ int main(int argc, char **argv) { return 1; } - //num_asics = get_num_of_asic(); - num_asics = 6; + num_asics = get_num_of_asic(); strcpy(install_dir, argv[1]); strcat(install_dir, "/"); From 71c07f08512ab0e30bf87e05e156829aa13813b9 Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi Date: Fri, 19 Jun 2020 14:53:37 -0700 Subject: [PATCH 3/3] Minor fix to remove tabs. Signed-off-by: SuvarnaMeenakshi --- src/systemd-sonic-generator/systemd-sonic-generator.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index ce0b7bc15a33..e801fcb229d2 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -186,9 +186,9 @@ static void replace_multi_inst_dep(char *src) { snprintf(buf, MAX_BUF_SIZE,"%s=%s\n",token, word); fputs(buf,fp_tmp); } else { - service_name = strdup(word); - service_name = strtok_r(service_name, ".", &save_ptr2); - type = strtok_r(NULL, " ", &save_ptr2); + service_name = strdup(word); + service_name = strtok_r(service_name, ".", &save_ptr2); + type = strtok_r(NULL, " ", &save_ptr2); if (is_multi_instance_service(word)) { for(i = 0; i < num_asics; i++) { snprintf(buf, MAX_BUF_SIZE, "%s=%s@%d.%s\n", @@ -518,7 +518,7 @@ static int get_num_of_asic() { while ((nread = getline(&line, &len, fp)) != -1) { if ((strstr(line, "onie_platform") != NULL) || - (strstr(line, "aboot_platform") != NULL)) { + (strstr(line, "aboot_platform") != NULL)) { token = strtok(line, "="); platform = strtok(NULL, "="); strip_trailing_newline(platform); @@ -585,7 +585,7 @@ int main(int argc, char **argv) { // For each unit file, get the installation targets and install the unit for (int i = 0; i < num_unit_files; i++) { - unit_instance = strdup(unit_files[i]); + unit_instance = strdup(unit_files[i]); if ((num_asics == 1) && strstr(unit_instance, "@") != NULL) { prefix = strtok(unit_instance, "@"); suffix = strtok(NULL, "@");