Beispiel #1
0
static lagopus_result_t
tmp_dir_opt_parse(const char *const *argv[],
                  void *c, void *out_configs,
                  lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *path = NULL;
  (void) out_configs;
  (void) c;

  if (argv != NULL && out_configs != NULL && result != NULL) {
    if (*(*argv + 1) != NULL) {
      (*argv)++;
      if (IS_VALID_STRING(*(*argv)) == true) {
        ret = flow_cmd_dump_tmp_dir_set(*(*argv));
        if (ret != LAGOPUS_RESULT_OK) {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Bad opt value = %s.",
                                                  *(*argv));
        }
      } else {
        ret = datastore_json_result_string_setf(result, ret,
                                                "Bad opt value = %s.",
                                                *(*argv));
      }
    } else {
      if ((ret = flow_cmd_dump_tmp_dir_get(&path)) ==
          LAGOPUS_RESULT_OK) {
        if ((ret = lagopus_dstring_appendf(result, "{")) !=
            LAGOPUS_RESULT_OK) {
          lagopus_perror(ret);
          goto done;
        }
        if ((ret = datastore_json_string_append(
                     result, ATTR_NAME_GET(opt_strs, OPT_TMP_DIR),
                     path, false)) !=
            LAGOPUS_RESULT_OK) {
          lagopus_perror(ret);
          goto done;
        }
        if ((ret = lagopus_dstring_appendf(result, "}")) !=
            LAGOPUS_RESULT_OK) {
          lagopus_perror(ret);
          goto done;
        }
      } else {
        ret = datastore_json_result_string_setf(result, ret,
                                                "Can't get tmp dir.");
      }
    }
  } else {
    ret = datastore_json_result_set(result,
                                    LAGOPUS_RESULT_INVALID_ARGS,
                                    NULL);
  }

done:
  free(path);

  return ret;
}
Beispiel #2
0
void
test_lagopus_dstring_appendf_empty(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *str = NULL;
  char test_str1[] = "";
  char test_str2[] = "1234 5678\n";

  /* 1st */
  ret = lagopus_dstring_appendf(&ds, "");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str1);

  /* 2nd */
  ret = lagopus_dstring_appendf(&ds, "");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str1);

  /* append int. */
  ret = lagopus_dstring_appendf(&ds, "%d 5678\n", 1234);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str2);

  /* 3nd */
  ret = lagopus_dstring_appendf(&ds, "");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str2);
}
Beispiel #3
0
void
test_lagopus_dstring_concat_clear_srt_get(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *str = NULL;
  char test_str1[] = "hoge foo\n";
  char test_str2[] = "bas\n";
  char test_str3[] = "hoge foo\nbas\n";
  char test_str4[] = "\0";
  char test_str5[] = "1234 5678\n";
  char test_str6[] = "hoge foo\nbas\n1234 5678\n";

  /* append ds. */
  ret = lagopus_dstring_appendf(&ds, "%s foo\n", "hoge");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str1);

  /* append ds2. */
  ret = lagopus_dstring_appendf(&ds2, "%s\n", "bas");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds2, str, test_str2);

  /* concat (ds + ds2). */
  ret = lagopus_dstring_concat(&ds, &ds2);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_concat error.");
  TEST_DSTRING(ret, &ds, str, test_str3);

  /* check const ds2. */
  TEST_DSTRING(ret, &ds2, str, test_str2);

  /* clear ds2. */
  ret = lagopus_dstring_clear(&ds2);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_clear error.");
  TEST_DSTRING(ret, &ds2, str, test_str4);

  /* append ds2. */
  ret = lagopus_dstring_appendf(&ds2, "1234 %d\n", 5678);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds2, str, test_str5);

  /* concat (ds + ds2).*/
  ret = lagopus_dstring_concat(&ds, &ds2);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_concat error.");
  TEST_DSTRING(ret, &ds, str, test_str6);

  /* check const ds2. */
  TEST_DSTRING(ret, &ds2, str, test_str5);
}
Beispiel #4
0
void
test_lagopus_dstring_len_get(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *str = NULL;
  char test_str1[] = "1 2\n";
  char test_str2[] = "1 2\na b\n";
  char test_str3[] = "\0";
  char test_str4[] = "98 76\n";

  /* check len. */
  ret = lagopus_dstring_len_get(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(0, ret,
                            "lagopus_dstring_len_get error.");

  /* append int. */
  ret = lagopus_dstring_appendf(&ds, "%d 2\n", 1);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str1);
  ret = lagopus_dstring_len_get(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(4, ret,
                            "lagopus_dstring_len_get error.");

  /* append str. */
  ret = lagopus_dstring_appendf(&ds, "a %s\n", "b");

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str2);
  ret = lagopus_dstring_len_get(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(4 + 4, ret,
                            "lagopus_dstring_len_get error.");

  /* clear. */
  ret = lagopus_dstring_clear(&ds);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_clear error.");
  TEST_DSTRING(ret, &ds, str, test_str3);
  ret = lagopus_dstring_len_get(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(0, ret,
                            "lagopus_dstring_len_get error.");

  /* append int. */
  ret = lagopus_dstring_appendf(&ds, "%d 76\n", 98);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str4);
  ret = lagopus_dstring_len_get(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(6, ret,
                            "lagopus_dstring_len_get error.");
}
Beispiel #5
0
void
test_lagopus_dstring_appendf_null(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  static lagopus_dstring_t ds_null = NULL;

  ret = lagopus_dstring_appendf(NULL, "hoge\n");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "lagopus_dstring_appendf error.");

  ret = lagopus_dstring_appendf(&ds_null, "hoge\n");
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "lagopus_dstring_appendf error.");

  ret = lagopus_dstring_appendf(&ds, NULL);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "lagopus_dstring_appendf error.");
}
Beispiel #6
0
static inline void
s_attr_dump(lagopus_dstring_t *ds, testobj_attr_t *o) {
  if (o != NULL) {
    (void)lagopus_dstring_appendf(ds,
                                  "{\"intval\": %u, \"strval\": \"%s\"}",
                                  o->m_intval,
                                  (IS_VALID_STRING(o->m_strval) == true) ?
                                  o->m_strval : "");
  }
}
Beispiel #7
0
static inline lagopus_result_t
s_parse_begin_show(lagopus_dstring_t *result) {
    lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
    lagopus_dstring_t show_result = NULL;
    char *str = NULL;

    /* show. */
    if ((ret = lagopus_dstring_create(&show_result)) ==
            LAGOPUS_RESULT_OK) {
        if ((ret = lagopus_dstring_appendf(&show_result, "{")) !=
                LAGOPUS_RESULT_OK) {
            lagopus_perror(ret);
            goto done;
        }
        if ((ret = datastore_json_string_append(
                       &show_result, ATTR_NAME_GET(opt_strs, OPT_TMP_DIR),
                       save_tmp_dir, false)) !=
                LAGOPUS_RESULT_OK) {
            lagopus_perror(ret);
            goto done;
        }
        if ((ret = lagopus_dstring_appendf(&show_result, "}")) !=
                LAGOPUS_RESULT_OK) {
            lagopus_perror(ret);
            goto done;
        }

        ret = lagopus_dstring_str_get(&show_result, &str);
        if (ret == LAGOPUS_RESULT_OK) {
            ret = datastore_json_result_set(result, LAGOPUS_RESULT_OK, str);
        }
    } else {
        lagopus_perror(ret);
    }

done:
    free(str);
    lagopus_dstring_destroy(&show_result);

    return  ret;
}
Beispiel #8
0
lagopus_result_t
datastore_cmd_serialize(lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  /* addr */
  char *addr_str = NULL;
  char *escaped_addr_str = NULL;

  /* protocol */
  const char *protocol_str = NULL;
  char *escaped_protocol_str = NULL;

  /* tls */
  const char *tls_str = NULL;

  bool is_escaped = false;

  if (result != NULL) {
    /* cmmand name. */
    if ((ret = lagopus_dstring_appendf(result, DATASTORE_CMD_NAME)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      goto done;
    }

    /* addr opt. */
    if ((ret = lagopus_ip_address_str_get(s_bindaddr,
                                          &addr_str)) ==
        LAGOPUS_RESULT_OK) {
      if (IS_VALID_STRING(addr_str) == true) {
        if ((ret = lagopus_str_escape(addr_str, "\"",
                                      &is_escaped,
                                      &escaped_addr_str)) ==
            LAGOPUS_RESULT_OK) {
          if ((ret = lagopus_dstring_appendf(result, " -addr")) ==
              LAGOPUS_RESULT_OK) {
            if ((ret = lagopus_dstring_appendf(
                         result,
                         ESCAPE_NAME_FMT(is_escaped, escaped_addr_str),
                         escaped_addr_str)) !=
                LAGOPUS_RESULT_OK) {
              lagopus_perror(ret);
              goto done;
            }
          } else {
            lagopus_perror(ret);
            goto done;
          }
        } else {
          lagopus_perror(ret);
          goto done;
        }
      }
    } else {
      lagopus_perror(ret);
      goto done;
    }

    /* port opt. */
    if ((ret = lagopus_dstring_appendf(result, " -port")) ==
        LAGOPUS_RESULT_OK) {
      if ((ret = lagopus_dstring_appendf(result, " %d",
                                         s_port)) !=
          LAGOPUS_RESULT_OK) {
        lagopus_perror(ret);
        goto done;
      }
    } else {
      lagopus_perror(ret);
      goto done;
    }

    /* protocol opt. */
    if (session_type_is_tcp(s_protocol) == true) {
      protocol_str = "tcp";
    } else if (session_type_is_tcp6(s_protocol) == true) {
      protocol_str = "tcp6";
    } else {
      lagopus_perror(LAGOPUS_RESULT_ANY_FAILURES);
      goto done;
    }

    if (IS_VALID_STRING(protocol_str) == true) {
      if ((ret = lagopus_str_escape(protocol_str, "\"",
                                    &is_escaped,
                                    &escaped_protocol_str)) ==
          LAGOPUS_RESULT_OK) {
        if ((ret = lagopus_dstring_appendf(result, " -protocol")) ==
            LAGOPUS_RESULT_OK) {
          if ((ret = lagopus_dstring_appendf(
                       result,
                       ESCAPE_NAME_FMT(is_escaped, escaped_protocol_str),
                       escaped_protocol_str)) !=
              LAGOPUS_RESULT_OK) {
            lagopus_perror(ret);
            goto done;
          }
        } else {
          lagopus_perror(ret);
          goto done;
        }
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    /* tls opt. */
    if ((ret = lagopus_dstring_appendf(result, " -tls")) ==
        LAGOPUS_RESULT_OK) {
      if (s_tls == true) {
        tls_str = "true";
      } else {
        tls_str = "false";
      }

      if ((ret = lagopus_dstring_appendf(result, " %s", tls_str)) !=
          LAGOPUS_RESULT_OK) {
        lagopus_perror(ret);
        goto done;
      }
    } else {
      lagopus_perror(ret);
      goto done;
    }

    /* Add newline. */
    if ((ret = lagopus_dstring_appendf(result, "\n\n")) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      goto done;
    }

  done:
    free(addr_str);
    free(escaped_addr_str);
    free(escaped_protocol_str);
  } else {
    ret = LAGOPUS_RESULT_INVALID_ARGS;
  }

  return ret;
}
Beispiel #9
0
void
test_lagopus_dstring_appendf_clear_srt_get(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  bool empty;
  char *str = NULL;
  char test_str1[] = "1234 5678\n";
  char test_str2[] = "1234 5678\nhoge foo\n";
  char test_str3[] = "\0";
  char test_str4[] = "9876 5432\n";
  char test_str5[] = "9876 5432\nhoge foo bar\n";
  char test_str6[] = "9876 5432\nhoge foo bar\nbas\n";

  /* check empty. */
  empty = lagopus_dstring_empty(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(true, empty,
                            "empty error.");

  /* append int. */
  ret = lagopus_dstring_appendf(&ds, "%d 5678\n", 1234);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str1);
  empty = lagopus_dstring_empty(&ds);
  TEST_ASSERT_EQUAL_MESSAGE(false, empty,
                            "empty error.");

  /* append str. */
  ret = lagopus_dstring_appendf(&ds, "hoge %s\n", "foo");

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str2);

  /* clear. */
  ret = lagopus_dstring_clear(&ds);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_clear error.");
  TEST_DSTRING(ret, &ds, str, test_str3);

  /* append int. */
  ret = lagopus_dstring_appendf(&ds, "%d 5432\n", 9876);

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str4);

  /* append str. */
  ret = lagopus_dstring_appendf(&ds, "hoge %s %s\n", "foo", "bar");

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str5);

  /* not args. */
  ret = lagopus_dstring_appendf(&ds, "bas\n");

  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret,
                            "lagopus_dstring_appendf error.");
  TEST_DSTRING(ret, &ds, str, test_str6);
}
Beispiel #10
0
static inline lagopus_result_t
s_parse_dryrun(datastore_interp_t *iptr,
               datastore_interp_state_t state,
               size_t argc, const char *const argv[],
               lagopus_hashmap_t *hptr,
               datastore_update_proc_t u_proc,
               datastore_enable_proc_t e_proc,
               datastore_serialize_proc_t s_proc,
               datastore_destroy_proc_t d_proc,
               lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  size_t i;

  (void)iptr;
  (void)hptr;
  (void)u_proc;
  (void)e_proc;
  (void)s_proc;
  (void)d_proc;

  for (i = 0; i < argc; i++) {
    lagopus_msg_debug(1, "argv[" PFSZS(4, u) "]:\t'%s'\n", i, argv[i]);
  }

  if (argc == 1) {
    (void)lagopus_dstring_clear(result);

    ret = lagopus_dstring_appendf(result,
                                  "{\"ret\":\"OK\",\n"
                                  "\"data\":{\"dryrun\": ");

    if (ret == LAGOPUS_RESULT_OK) {
      if (state == DATASTORE_INTERP_STATE_DRYRUN) {
        ret = lagopus_dstring_appendf(result, "true}}");
      } else {
        ret = lagopus_dstring_appendf(result, "false}}");
      }

      if (ret != LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result,
                                                LAGOPUS_RESULT_ANY_FAILURES,
                                                "dstring append failed.");
      }
    } else {
      ret = datastore_json_result_string_setf(result,
                                              LAGOPUS_RESULT_ANY_FAILURES,
                                              "dstring append failed.");
    }
  } else if (argc == 2) {
    argv++;

    if (strcmp(*argv, "begin") == 0) {
      ret = dryrun_begin();

      if (ret != LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result,
                                                ret,
                                                "dryrun begin failed.");
      }
    } else if (strcmp(*argv, "end") == 0) {
      ret = dryrun_end();

      if (ret != LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result,
                                                ret,
                                                "dryrun end failed.");
      }
    } else {
      ret = datastore_json_result_string_setf(result,
                                              LAGOPUS_RESULT_INVALID_ARGS,
                                              "Unknown option '%s'.",
                                              *argv);
    }

    if (ret != LAGOPUS_RESULT_DATASTORE_INTERP_ERROR) {
      ret = datastore_json_result_set(result, ret, NULL);
    }
  } else {
    ret = datastore_json_result_string_setf(result,
                                            LAGOPUS_RESULT_INVALID_ARGS,
                                            "Unknown option.");
  }

  return ret;
}
Beispiel #11
0
lagopus_result_t
snmp_cmd_serialize(lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  /* master-agentx-socket */
  char *master_agentx_socket_str = NULL;
  char *escaped_master_agentx_socket_str = NULL;

  /* ping-interval-second */
  uint16_t ping_interval_second = 0;

  bool is_escaped = false;

  if (result != NULL) {
    /* cmmand name. */
    if ((ret = lagopus_dstring_appendf(result, SNMP_CMD_NAME)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      goto done;
    }

    /* master-agentx-socket opt. */
    if ((ret = lagopus_snmp_get_agentx_sock(&master_agentx_socket_str))
        == LAGOPUS_RESULT_OK) {
      if (IS_VALID_STRING(master_agentx_socket_str) == true) {
        if ((ret = lagopus_str_escape(master_agentx_socket_str, "\"",
                                      &is_escaped,
                                      &escaped_master_agentx_socket_str)) ==
            LAGOPUS_RESULT_OK) {
          if ((ret = lagopus_dstring_appendf(result, " -master-agentx-socket")) ==
              LAGOPUS_RESULT_OK) {
            if ((ret = lagopus_dstring_appendf(
                         result,
                         ESCAPE_NAME_FMT(is_escaped, escaped_master_agentx_socket_str),
                         escaped_master_agentx_socket_str)) !=
                LAGOPUS_RESULT_OK) {
              lagopus_perror(ret);
              goto done;
            }
          } else {
            lagopus_perror(ret);
            goto done;
          }
        } else {
          lagopus_perror(ret);
          goto done;
        }
      }
    } else {
      lagopus_perror(ret);
      goto done;
    }

    /* ping-interval-second opt. */
    if ((ret = lagopus_snmp_get_ping_interval(&ping_interval_second)) ==
        LAGOPUS_RESULT_OK) {
      if ((ret = lagopus_dstring_appendf(result, " -ping-interval-second")) ==
          LAGOPUS_RESULT_OK) {
        if ((ret = lagopus_dstring_appendf(result, " %d",
                                           ping_interval_second)) !=
            LAGOPUS_RESULT_OK) {
          lagopus_perror(ret);
          goto done;
        }
      } else {
        lagopus_perror(ret);
        goto done;
      }
    } else {
      lagopus_perror(ret);
      goto done;
    }

    /* Add newline. */
    if ((ret = lagopus_dstring_appendf(result, "\n\n")) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      goto done;
    }

  done:
    free((void *) master_agentx_socket_str);
    free((void *) escaped_master_agentx_socket_str);
  } else {
    ret = LAGOPUS_RESULT_INVALID_ARGS;
  }

  return ret;
}
Beispiel #12
0
static inline lagopus_result_t
s_parse_snmp_internal(const char *const argv[],
                      lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *agentx_sock = NULL;
  char *esc_agentx = NULL;
  char *ping = NULL;

  while (IS_VALID_STRING(*argv) == true) {
    if (strcmp(*argv, "-master-agentx-socket") == 0) {
      if (IS_VALID_STRING(*(argv + 1)) == true) {
        argv++;
        if ((ret = lagopus_str_unescape(*argv, "\"'", &agentx_sock))
            > 0) {

          if ((ret = lagopus_snmp_set_agentx_sock(agentx_sock))
              != LAGOPUS_RESULT_OK) {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't set %s.",
                                                    agentx_sock);
            goto done;
          }

        } else if (ret == 0) {
          ret = datastore_json_result_string_setf(result,
                                                  LAGOPUS_RESULT_INVALID_ARGS,
                                                  "Bad opt val = %s ",
                                                  *argv);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't convert to an "
                                                  "unescaped strings.");
          goto done;
        }
      } else {

        if ((ret = lagopus_snmp_get_agentx_sock(&agentx_sock))
            == LAGOPUS_RESULT_OK) {
          ret = datastore_json_string_escape(agentx_sock,
                                             (char **)&esc_agentx);
          if (ret == LAGOPUS_RESULT_OK) {
            ret = datastore_json_result_setf(result, ret, "[{"
                                             "\"master-agentx-socket\":\"%s\""
                                             "}]", esc_agentx);
            goto done;
          } else {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't convert to "
                                                    "an escaped strings.");
            goto done;
          }
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't get "
                                                  "master-agentx-socket.");
          goto done;
        }
      }

    } else if (strcmp(*argv, "-ping-interval-second") == 0) {

      if (IS_VALID_STRING(*(argv + 1)) == true) {
        argv++;
        ret = lagopus_str_unescape(*argv, "\"'", &ping);
        if (ret > 0) {
          uint16_t new_ping;
          ret = lagopus_str_parse_uint16(ping, &new_ping);
          if (ret == LAGOPUS_RESULT_OK) {
            if ((ret = lagopus_snmp_set_ping_interval(new_ping))
                != LAGOPUS_RESULT_OK) {
              ret = datastore_json_result_string_setf(result, ret,
                                                      "Can't set "
                                                      "ping-interval.");

              goto done;
            }
          } else {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't parse string "
                                                    "to uint16_t.");
            goto done;
          }
        } else if (ret == 0) {
          ret = datastore_json_result_string_setf(result,
                                                  LAGOPUS_RESULT_INVALID_ARGS,
                                                  "Bad opt val = %s ",
                                                  *argv);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't convert to an "
                                                  "unescaped strings.");
          goto done;
        }
      } else {
        uint16_t ping_interval;
        ret = lagopus_snmp_get_ping_interval(&ping_interval);
        if (ret == LAGOPUS_RESULT_OK) {
          ret = datastore_json_result_setf(result, ret, "[{"
                                           "\"ping-interval-second\":%d"
                                           "}]", ping_interval);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't get "
                                                  "ping-interval-second.");
          goto done;
        }
      }

    } else if (strcmp(*argv, "enable") == 0) {
      if ((ret = lagopus_snmp_set_enable(true))
          != LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result, ret,
                                                "Can't set enable.");
        goto done;
      }
    } else {
      char *esc_str = NULL;
      ret = datastore_json_string_escape(*argv, (char **)&esc_str);
      if (ret == LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_NOT_FOUND,
                                                "option = %s",
                                                esc_str);
      }
      free((void *)esc_str);
    }

 done:
    free((void *)agentx_sock);
    free((void *)esc_agentx);
    free((void *)ping);
    agentx_sock = NULL;
    esc_agentx = NULL;
    ping = NULL;

    if (ret != LAGOPUS_RESULT_OK) {
      break;
    }
    argv++;
  }

  if (ret == LAGOPUS_RESULT_OK &&
      lagopus_dstring_empty(result)) {
    ret = lagopus_dstring_appendf(result, "{\"ret\":\"OK\"}");
    if (ret != LAGOPUS_RESULT_OK) {
      ret = datastore_json_result_string_setf(result, ret,
                                              "Can't add strings "
                                              "to dstring.");
    }
  }

  return ret;
}
Beispiel #13
0
static bool
queue_conf_iterate(void *key, void *val, lagopus_hashentry_t he,
                   void *arg) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  lagopus_dstring_t ds;
  char *prefix = NULL;
  size_t len = 0;
  bool result = false;

  char *fullname = (char *)key;
  queue_conf_iter_ctx_t *ctx =
    (queue_conf_iter_ctx_t *)arg;

  (void)he;
  if (((queue_conf_t *) val)->is_destroying == false) {
    if (ctx->m_namespace == NULL) {
      if (ctx->m_n_configs < ctx->m_max) {
        ctx->m_configs[ctx->m_n_configs++] =
          (queue_conf_t *)val;
        result = true;
      } else {
        result = false;
      }
    } else {
      ret = lagopus_dstring_create(&ds);
      if (ret == LAGOPUS_RESULT_OK) {
        if (ctx->m_namespace[0] == '\0') {
          ret = lagopus_dstring_appendf(&ds,
                                        "%s",
                                        NAMESPACE_DELIMITER);
        } else {
          ret = lagopus_dstring_appendf(&ds,
                                        "%s%s",
                                        ctx->m_namespace,
                                        NAMESPACE_DELIMITER);
        }

        if (ret == LAGOPUS_RESULT_OK) {
          ret = lagopus_dstring_str_get(&ds, &prefix);
          if (ret == LAGOPUS_RESULT_OK) {
            len = strlen(prefix);
            if (ctx->m_n_configs < ctx->m_max &&
                strncmp(fullname, prefix, len) == 0) {
              ctx->m_configs[ctx->m_n_configs++] =
                (queue_conf_t *)val;
            }
            result = true;
          } else {
            lagopus_msg_warning("dstring get failed.\n");
            result = false;
          }
        } else {
          lagopus_msg_warning("dstring append failed.\n");
          result = false;
        }
      } else {
        lagopus_msg_warning("dstring create failed.\n");
        result = false;
      }

      free((void *) prefix);

      (void)lagopus_dstring_clear(&ds);
      (void)lagopus_dstring_destroy(&ds);
    }
  } else {
    /* skip destroying conf.*/
    result = true;
  }

  return result;
}