X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Futils_ovs.c;h=296d6857a6ca75f1f503a2737747624e9cc3944c;hp=ff2b151eee5a28c67c05bf284237e57c3c7f27f6;hb=7b8851b26928b609ce850e78c1eabb50ff319244;hpb=99d0d374adc5c628d937d750caa04c68e87b3eca diff --git a/src/utils_ovs.c b/src/utils_ovs.c index ff2b151e..296d6857 100644 --- a/src/utils_ovs.c +++ b/src/utils_ovs.c @@ -3,14 +3,17 @@ * * Copyright(c) 2016 Intel Corporation. All rights reserved. * - * Permission is hereby granted, free of charge, to any person obtaining a copy of + * Permission is hereby granted, free of charge, to any person obtaining a copy + *of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do + * of the Software, and to permit persons to whom the Software is furnished to + *do * so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all + * The above copyright notice and this permission notice shall be included in + *all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR @@ -210,7 +213,7 @@ static _Bool ovs_db_poll_is_running(ovs_db_t *pdb) { pthread_mutex_lock(&pdb->poll_thread.mutex); state = pdb->poll_thread.state; pthread_mutex_unlock(&pdb->poll_thread.mutex); - return (state == OVS_DB_POLL_STATE_RUNNING); + return state == OVS_DB_POLL_STATE_RUNNING; } /* Generate unique identifier (UID). It is used by OVS DB API @@ -293,11 +296,11 @@ static int ovs_db_data_send(const ovs_db_t *pdb, const char *data, size_t len) { while (rem > 0) { if ((nbytes = send(pdb->sock, data + off, rem, 0)) <= 0) - return (-1); + return -1; rem -= (size_t)nbytes; off += (size_t)nbytes; } - return (0); + return 0; } /* @@ -328,7 +331,10 @@ static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) { yajl_val jobj_value = NULL; const char *obj_key = NULL; size_t obj_len = 0; - yajl_gen_status yajl_gen_ret; + yajl_gen_status yajl_gen_ret = yajl_gen_status_ok; + + if (jval == NULL) + return yajl_gen_generation_complete; if (YAJL_IS_STRING(jval)) OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, YAJL_GET_STRING(jval)); @@ -347,14 +353,14 @@ static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) { array_len = YAJL_GET_ARRAY(jval)->len; jvalues = YAJL_GET_ARRAY(jval)->values; OVS_YAJL_CALL(yajl_gen_array_open, jgen); - for (int i = 0; i < array_len; i++) + for (size_t i = 0; i < array_len; i++) OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jvalues[i]); OVS_YAJL_CALL(yajl_gen_array_close, jgen); } else if (YAJL_IS_OBJECT(jval)) { /* create new object and add all elements into the object */ OVS_YAJL_CALL(yajl_gen_map_open, jgen); obj_len = YAJL_GET_OBJECT(jval)->len; - for (int i = 0; i < obj_len; i++) { + for (size_t i = 0; i < obj_len; i++) { obj_key = YAJL_GET_OBJECT(jval)->keys[i]; jobj_value = YAJL_GET_OBJECT(jval)->values[i]; OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, obj_key); @@ -388,7 +394,7 @@ static int ovs_db_table_echo_cb(const ovs_db_t *pdb, yajl_val jnode) { yajl_gen_status yajl_gen_ret; if ((jgen = yajl_gen_alloc(NULL)) == NULL) - return (-1); + return -1; /* check & get request attributes */ if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL || @@ -421,12 +427,12 @@ static int ovs_db_table_echo_cb(const ovs_db_t *pdb, yajl_val jnode) { } /* clean up and return success */ yajl_gen_clear(jgen); - return (0); + return 0; yajl_gen_failure: /* release memory */ yajl_gen_clear(jgen); - return (-1); + return -1; } /* Get OVS DB registered callback by YAJL val. The YAJL @@ -459,9 +465,6 @@ static int ovs_db_table_update_cb(ovs_db_t *pdb, yajl_val jnode) { yajl_val jvalue; yajl_val jparams; yajl_val jtable_updates; - yajl_val jtable_update; - size_t obj_len = 0; - const char *table_name = NULL; const char *params_path[] = {"params", NULL}; const char *id_path[] = {"id", NULL}; @@ -469,21 +472,21 @@ static int ovs_db_table_update_cb(ovs_db_t *pdb, yajl_val jnode) { if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL || (yajl_tree_get(jnode, id_path, yajl_t_null) == NULL)) { OVS_ERROR("invalid OVS DB request received"); - return (-1); + return -1; } /* check array length: [, ] */ if ((YAJL_GET_ARRAY(jparams) == NULL) || (YAJL_GET_ARRAY(jparams)->len != 2)) { OVS_ERROR("invalid OVS DB request received"); - return (-1); + return -1; } jvalue = YAJL_GET_ARRAY(jparams)->values[0]; jtable_updates = YAJL_GET_ARRAY(jparams)->values[1]; if ((!YAJL_IS_OBJECT(jtable_updates)) || (!YAJL_IS_STRING(jvalue))) { OVS_ERROR("invalid OVS DB request id or table update received"); - return (-1); + return -1; } /* find registered callback based on */ @@ -492,7 +495,7 @@ static int ovs_db_table_update_cb(ovs_db_t *pdb, yajl_val jnode) { if (cb == NULL || cb->table.call == NULL) { OVS_ERROR("No OVS DB table update callback found"); pthread_mutex_unlock(&pdb->mutex); - return (-1); + return -1; } /* call registered callback */ @@ -521,7 +524,7 @@ static int ovs_db_result_cb(ovs_db_t *pdb, yajl_val jnode) { /* check & get result attributes */ if (!jresult || !jerror || !jid) - return (-1); + return -1; /* try to find registered callback */ pthread_mutex_lock(&pdb->mutex); @@ -534,7 +537,7 @@ static int ovs_db_result_cb(ovs_db_t *pdb, yajl_val jnode) { } pthread_mutex_unlock(&pdb->mutex); - return (0); + return 0; } /* Handle JSON data (one request) and call @@ -554,22 +557,26 @@ static int ovs_db_json_data_process(ovs_db_t *pdb, const char *data, /* duplicate the data to make null-terminated string * required for yajl_tree_parse() */ if ((sjson = calloc(1, len + 1)) == NULL) - return (-1); + return -1; sstrncpy(sjson, data, len + 1); - OVS_DEBUG("[len=%zu] %s", len, sjson); + OVS_DEBUG("[len=%" PRIsz "] %s", len, sjson); /* parse json data */ jnode = yajl_tree_parse(sjson, yajl_errbuf, sizeof(yajl_errbuf)); if (jnode == NULL) { OVS_ERROR("yajl_tree_parse() %s", yajl_errbuf); sfree(sjson); - return (-1); + return -1; } /* get method name */ if ((jval = yajl_tree_get(jnode, method_path, yajl_t_string)) != NULL) { - method = YAJL_GET_STRING(jval); + if ((method = YAJL_GET_STRING(jval)) == NULL) { + yajl_tree_free(jnode); + sfree(sjson); + return -1; + } if (strcmp("echo", method) == 0) { /* echo request from the server */ if (ovs_db_table_echo_cb(pdb, jnode) < 0) @@ -589,7 +596,7 @@ static int ovs_db_json_data_process(ovs_db_t *pdb, const char *data, /* release memory */ yajl_tree_free(jnode); sfree(sjson); - return (0); + return 0; } /* @@ -624,7 +631,7 @@ static int ovs_json_reader_push_data(ovs_json_reader_t *jreader, /* allocate new chunk of memory */ new_buff = realloc(jreader->buff_ptr, (jreader->buff_size + data_len)); if (new_buff == NULL) - return (-1); + return -1; /* point to new allocated memory */ jreader->buff_ptr = new_buff; @@ -634,7 +641,7 @@ static int ovs_json_reader_push_data(ovs_json_reader_t *jreader, /* store input data */ memcpy(jreader->buff_ptr + jreader->buff_offset, data, data_len); jreader->buff_offset += data_len; - return (0); + return 0; } /* Pop one fully-fledged JSON if already exists. Returns 0 if @@ -647,7 +654,7 @@ static int ovs_json_reader_pop(ovs_json_reader_t *jreader, char *json = NULL; /* search open/close brace */ - for (int i = jreader->json_offset; i < jreader->buff_offset; i++) { + for (size_t i = jreader->json_offset; i < jreader->buff_offset; i++) { if (jreader->buff_ptr[i] == '{') { nbraces++; } else if (jreader->buff_ptr[i] == '}') @@ -657,7 +664,7 @@ static int ovs_json_reader_pop(ovs_json_reader_t *jreader, *json_ptr = jreader->buff_ptr + jreader->json_offset; *json_len_ptr = json_len + 1; jreader->json_offset = i + 1; - return (0); + return 0; } /* increase JSON data length */ @@ -671,7 +678,7 @@ static int ovs_json_reader_pop(ovs_json_reader_t *jreader, * and zero rest of the buffer data */ json = &jreader->buff_ptr[jreader->json_offset]; json_len = jreader->buff_offset - jreader->json_offset; - for (int i = 0; i < jreader->buff_size; i++) + for (size_t i = 0; i < jreader->buff_size; i++) jreader->buff_ptr[i] = ((i < json_len) ? (json[i]) : (0)); jreader->buff_offset = json_len; } else @@ -682,7 +689,7 @@ static int ovs_json_reader_pop(ovs_json_reader_t *jreader, jreader->json_offset = 0; } - return (-1); + return -1; } /* Reset JSON reader. It is useful when start processing @@ -707,10 +714,8 @@ static void ovs_json_reader_free(ovs_json_reader_t *jreader) { * if connection has been established. */ static void ovs_db_reconnect(ovs_db_t *pdb) { - const char unix_prefix[] = "unix:"; const char *node_info = pdb->node; struct addrinfo *result; - struct sockaddr_un saunix; if (pdb->unix_path[0] != '\0') { /* use UNIX socket instead of INET address */ @@ -786,7 +791,7 @@ static void *ovs_poll_worker(void *arg) { /* create JSON reader instance */ if ((jreader = ovs_json_reader_alloc()) == NULL) { OVS_ERROR("initialize json reader failed"); - return (NULL); + return NULL; } /* poll data */ @@ -846,7 +851,7 @@ static void *ovs_poll_worker(void *arg) { OVS_DEBUG("poll thread has been completed"); ovs_json_reader_free(jreader); - return (NULL); + return NULL; } /* EVENT worker thread. @@ -892,20 +897,20 @@ static void *ovs_event_worker(void *arg) { } OVS_DEBUG("event thread has been completed"); - return (NULL); + return NULL; } /* Initialize EVENT thread */ static int ovs_db_event_thread_init(ovs_db_t *pdb) { - pdb->event_thread.tid = -1; + pdb->event_thread.tid = (pthread_t)-1; /* init event thread condition variable */ if (pthread_cond_init(&pdb->event_thread.cond, NULL)) { - return (-1); + return -1; } /* init event thread mutex */ if (pthread_mutex_init(&pdb->event_thread.mutex, NULL)) { pthread_cond_destroy(&pdb->event_thread.cond); - return (-1); + return -1; } /* Hold the event thread mutex. It ensures that no events * will be lost while thread is still starting. Once event @@ -914,7 +919,7 @@ static int ovs_db_event_thread_init(ovs_db_t *pdb) { if (pthread_mutex_lock(&pdb->event_thread.mutex)) { pthread_mutex_destroy(&pdb->event_thread.mutex); pthread_cond_destroy(&pdb->event_thread.cond); - return (-1); + return -1; } /* start event thread */ pthread_t tid; @@ -923,20 +928,20 @@ static int ovs_db_event_thread_init(ovs_db_t *pdb) { pthread_mutex_unlock(&pdb->event_thread.mutex); pthread_mutex_destroy(&pdb->event_thread.mutex); pthread_cond_destroy(&pdb->event_thread.cond); - return (-1); + return -1; } pdb->event_thread.tid = tid; - return (0); + return 0; } /* Destroy EVENT thread */ static int ovs_db_event_thread_destroy(ovs_db_t *pdb) { - if (pdb->event_thread.tid < 0) + if (pdb->event_thread.tid == (pthread_t)-1) /* already destroyed */ - return (0); + return 0; ovs_db_event_post(pdb, OVS_DB_EVENT_TERMINATE); if (pthread_join(pdb->event_thread.tid, NULL) != 0) - return (-1); + return -1; /* Event thread always holds the thread mutex when * performs some task (handles event) and releases it when * while sleeping. Thus, if event thread exits, the mutex @@ -944,16 +949,16 @@ static int ovs_db_event_thread_destroy(ovs_db_t *pdb) { pthread_mutex_unlock(&pdb->event_thread.mutex); pthread_mutex_destroy(&pdb->event_thread.mutex); pthread_cond_destroy(&pdb->event_thread.cond); - pdb->event_thread.tid = -1; - return (0); + pdb->event_thread.tid = (pthread_t)-1; + return 0; } /* Initialize POLL thread */ static int ovs_db_poll_thread_init(ovs_db_t *pdb) { - pdb->poll_thread.tid = -1; + pdb->poll_thread.tid = (pthread_t)-1; /* init event thread mutex */ if (pthread_mutex_init(&pdb->poll_thread.mutex, NULL)) { - return (-1); + return -1; } /* start poll thread */ pthread_t tid; @@ -961,27 +966,27 @@ static int ovs_db_poll_thread_init(ovs_db_t *pdb) { if (plugin_thread_create(&tid, NULL, ovs_poll_worker, pdb, "utils_ovs:poll") != 0) { pthread_mutex_destroy(&pdb->poll_thread.mutex); - return (-1); + return -1; } pdb->poll_thread.tid = tid; - return (0); + return 0; } /* Destroy POLL thread */ static int ovs_db_poll_thread_destroy(ovs_db_t *pdb) { - if (pdb->poll_thread.tid < 0) + if (pdb->poll_thread.tid == (pthread_t)-1) /* already destroyed */ - return (0); + return 0; /* change thread state */ pthread_mutex_lock(&pdb->poll_thread.mutex); pdb->poll_thread.state = OVS_DB_POLL_STATE_EXITING; pthread_mutex_unlock(&pdb->poll_thread.mutex); /* join the thread */ if (pthread_join(pdb->poll_thread.tid, NULL) != 0) - return (-1); + return -1; pthread_mutex_destroy(&pdb->poll_thread.mutex); - pdb->poll_thread.tid = -1; - return (0); + pdb->poll_thread.tid = (pthread_t)-1; + return 0; } /* @@ -992,12 +997,12 @@ ovs_db_t *ovs_db_init(const char *node, const char *service, const char *unix_path, ovs_db_callback_t *cb) { /* sanity check */ if (node == NULL || service == NULL || unix_path == NULL) - return (NULL); + return NULL; /* allocate db data & fill it */ ovs_db_t *pdb = pdb = calloc(1, sizeof(*pdb)); if (pdb == NULL) - return (NULL); + return NULL; /* store the OVS DB address */ sstrncpy(pdb->node, node, sizeof(pdb->node)); @@ -1013,21 +1018,21 @@ ovs_db_t *ovs_db_init(const char *node, const char *service, if (pthread_mutexattr_init(&mutex_attr)) { OVS_ERROR("OVS DB mutex attribute init failed"); sfree(pdb); - return (NULL); + return NULL; } /* set OVS DB mutex as recursive */ if (pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE)) { OVS_ERROR("Failed to set OVS DB mutex as recursive"); pthread_mutexattr_destroy(&mutex_attr); sfree(pdb); - return (NULL); + return NULL; } /* init OVS DB mutex */ if (pthread_mutex_init(&pdb->mutex, &mutex_attr)) { OVS_ERROR("OVS DB mutex init failed"); pthread_mutexattr_destroy(&mutex_attr); sfree(pdb); - return (NULL); + return NULL; } /* destroy mutex attributes */ pthread_mutexattr_destroy(&mutex_attr); @@ -1035,14 +1040,14 @@ ovs_db_t *ovs_db_init(const char *node, const char *service, /* init event thread */ if (ovs_db_event_thread_init(pdb) < 0) { ovs_db_destroy(pdb); - return (NULL); + return NULL; } /* init polling thread */ pdb->sock = -1; if (ovs_db_poll_thread_init(pdb) < 0) { ovs_db_destroy(pdb); - return (NULL); + return NULL; } return pdb; } @@ -1062,16 +1067,16 @@ int ovs_db_send_request(ovs_db_t *pdb, const char *method, const char *params, /* sanity check */ if (!pdb || !method || !params) - return (-1); + return -1; if ((jgen = yajl_gen_alloc(NULL)) == NULL) - return (-1); + return -1; /* try to parse params */ if ((jparams = yajl_tree_parse(params, NULL, 0)) == NULL) { OVS_ERROR("params is not a JSON string"); yajl_gen_clear(jgen); - return (-1); + return -1; } /* generate method field */ @@ -1088,7 +1093,7 @@ int ovs_db_send_request(ovs_db_t *pdb, const char *method, const char *params, /* generate id field */ OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "id"); uid = ovs_uid_generate(); - ssnprintf(uid_buff, sizeof(uid_buff), "%" PRIX64, uid); + snprintf(uid_buff, sizeof(uid_buff), "%" PRIX64, uid); OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, uid_buff); OVS_YAJL_CALL(yajl_gen_map_close, jgen); @@ -1150,16 +1155,16 @@ int ovs_db_table_cb_register(ovs_db_t *pdb, const char *tb_name, /* sanity check */ if (pdb == NULL || tb_name == NULL || update_cb == NULL) - return (-1); + return -1; /* allocate new update callback */ if ((new_cb = calloc(1, sizeof(ovs_callback_t))) == NULL) - return (-1); + return -1; /* init YAJL generator */ if ((jgen = yajl_gen_alloc(NULL)) == NULL) { sfree(new_cb); - return (-1); + return -1; } /* add new callback to front */ @@ -1174,7 +1179,7 @@ int ovs_db_table_cb_register(ovs_db_t *pdb, const char *tb_name, OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, OVS_DB_DEFAULT_DB_NAME); /* uid string */ - ssnprintf(uid_str, sizeof(uid_str), "%" PRIX64, new_cb->uid); + snprintf(uid_str, sizeof(uid_str), "%" PRIX64, new_cb->uid); OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, uid_str); /* */ @@ -1243,12 +1248,12 @@ int ovs_db_destroy(ovs_db_t *pdb) { /* sanity check */ if (pdb == NULL) - return (-1); + return -1; /* try to lock the structure before releasing */ if ((ret = pthread_mutex_lock(&pdb->mutex))) { OVS_ERROR("pthread_mutex_lock() DB mutex lock failed (%d)", ret); - return (-1); + return -1; } /* stop poll thread */ @@ -1297,7 +1302,7 @@ yajl_val ovs_utils_get_value_by_key(yajl_val jval, const char *key) { return NULL; /* find a value by key */ - for (int i = 0; i < YAJL_GET_OBJECT(jval)->len; i++) { + for (size_t i = 0; i < YAJL_GET_OBJECT(jval)->len; i++) { obj_key = YAJL_GET_OBJECT(jval)->keys[i]; if (strcmp(obj_key, key) == 0) return YAJL_GET_OBJECT(jval)->values[i]; @@ -1355,7 +1360,7 @@ yajl_val ovs_utils_get_map_value(yajl_val jval, const char *key) { /* try to find map value by map key */ map_len = YAJL_GET_ARRAY(array_values[1])->len; map_values = YAJL_GET_ARRAY(array_values[1])->values; - for (int i = 0; i < map_len; i++) { + for (size_t i = 0; i < map_len; i++) { /* check YAJL array */ if (!YAJL_IS_ARRAY(map_values[i])) break;