Whamcloud - gitweb
LU-17888 osd-ldiskfs: osd_scrub_refresh_mapping deadlock
[fs/lustre-release.git] / lnet / utils / lnetconfig / cyaml.c
index 7c82645..a71f72c 100644 (file)
@@ -1,30 +1,12 @@
+// SPDX-License-Identifier: LGPL-2.0
+
 /*
- * LGPL HEADER START
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of the
- * License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- * LGPL HEADER END
- *
  * Copyright (c) 2014, 2017, Intel Corporation.
- *
- * Author:
- *   Amir Shehata <amir.shehata@intel.com>
  */
 
 /*
+ * This file is part of Lustre, http://www.lustre.org/
+ *
  *  The cYAML tree is constructed as an n-tree.
  *  root -> cmd 1
  *          ||
@@ -33,6 +15,8 @@
  *                             ||
  *                             \/
  *                           attr2.1 -> attr2.1.1 -> attr2.1.2
+ *
+ * Author: Amir Shehata <amir.shehata@intel.com>
  */
 
 #include <yaml.h>
@@ -242,7 +226,7 @@ static struct cYAML *cYAML_ll_pop(struct list_head *list,
        struct cYAML *obj = NULL;
 
        if (!list_empty(list)) {
-               pop = list_entry(list->next, struct cYAML_ll, list);
+               pop = list_first_entry(list, struct cYAML_ll, list);
 
                obj = pop->obj;
                if (print_info != NULL)
@@ -348,6 +332,23 @@ static bool parse_number(struct cYAML *item, const char *input)
        int subscale = 0, signsubscale = 1;
        const char *num = input;
 
+       if (!strncmp(input, "0x", 2)) {
+               int64_t hex; /* hex input is always an integer */
+               char *invalid = NULL;
+
+               errno = 0;
+               hex = strtoll(input, &invalid, 16);
+               if (errno)
+                       return false;
+               if (*invalid)
+                       return false;
+
+               item->cy_valuedouble = (double) hex;
+               item->cy_valueint = hex;
+               item->cy_type = CYAML_TYPE_NUMBER;
+               return true;
+       }
+
        if (*num == '-') {
                sign = -1;
                num++;
@@ -750,7 +751,7 @@ bool cYAML_is_sequence(struct cYAML *node)
        return (node != NULL ? node->cy_type == CYAML_TYPE_ARRAY : 0);
 }
 
-void cYAML_tree_recursive_walk(struct cYAML *node, cYAML_walk_cb cb,
+static void cYAML_tree_recursive_walk(struct cYAML *node, cYAML_walk_cb cb,
                                      bool cb_first,
                                      void *usr_data,
                                      void **out)
@@ -1257,7 +1258,7 @@ void cYAML_build_error(int rc, int seq_no, char *cmd,
                       char *entity, char *err_str,
                       struct cYAML **root)
 {
-       struct cYAML *r = NULL, *err, *s, *itm, *cmd_obj;
+       struct cYAML *r = NULL, *err, *s, *itm = NULL, *cmd_obj;
        if (root == NULL)
                return;
 
@@ -1277,8 +1278,9 @@ void cYAML_build_error(int rc, int seq_no, char *cmd,
        else if (cmd_obj == NULL) {
                s = cYAML_create_seq(r, cmd);
                itm = cYAML_create_seq_item(s);
-       } else if (cmd_obj != NULL && cmd_obj->cy_type != CYAML_TYPE_ARRAY)
+       } else if (cmd_obj != NULL && cmd_obj->cy_type != CYAML_TYPE_ARRAY) {
                goto failed;
+       }
 
        err = cYAML_create_object(itm, entity);
        if (err == NULL)
@@ -1382,12 +1384,13 @@ struct cYAML *cYAML_build_tree(char *path,
                               bool debug)
 {
        yaml_parser_t parser;
-       struct cYAML *yaml;
+       struct cYAML *yaml = NULL;
        char err_str[256];
        FILE *input = NULL;
 
        /* Create the Parser object. */
-       yaml_parser_initialize(&parser);
+       if (yaml_parser_initialize(&parser) == 0)
+               goto out_init;
 
        /* file always takes precedence */
        if (path != NULL) {
@@ -1419,6 +1422,6 @@ struct cYAML *cYAML_build_tree(char *path,
 
        if (input != NULL)
                fclose(input);
-
+out_init:
        return yaml;
 }