X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fmodbus.c;h=df169825706a9bde4d2d06fa22d3ed63ed883d1f;hb=9c3ed6462c15fd1cb664cd0ec2a5efb289aa1af6;hp=f40258d09c4b3f0c472bc3e1ee343a108ed1e859;hpb=06e9e7692aa0d1df7744cca4e70ea404eb25b093;p=collectd.git diff --git a/src/modbus.c b/src/modbus.c index f40258d0..df169825 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -377,8 +377,6 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ uint16_t values[2]; int values_num; const data_set_t *ds; - struct sockaddr sockaddr; - socklen_t saddrlen = sizeof(sockaddr); int status; if ((host == NULL) || (slave == NULL) || (data == NULL)) @@ -416,32 +414,40 @@ static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */ else values_num = 1; - if(host->connection == NULL) - goto try_connect; - - if(getpeername(modbus_get_socket(host->connection), - &sockaddr, &saddrlen) != 0) - switch(errno){ - case EBADF: - case ENOTSOCK: - case ENOTCONN: - try_connect: - status = mb_init_connection (host); - if (status != 0) - { - ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ", - host->host, host->node); - host->is_connected = 0; - host->connection = NULL; - return (-1); - } - break; - default: + if (host->connection == NULL) + { + status = EBADF; + } + else + { + struct sockaddr sockaddr; + socklen_t saddrlen = sizeof (sockaddr); + + status = getpeername (modbus_get_socket (host->connection), + &sockaddr, &saddrlen); + if (status != 0) + status = errno; + } + + if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN)) + { + status = mb_init_connection (host); + if (status != 0) + { + ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ", + host->host, host->node); + host->is_connected = 0; + host->connection = NULL; + return (-1); + } + } + else if (status != 0) + { #if LEGACY_LIBMODBUS - modbus_close (&host->connection); + modbus_close (&host->connection); #else - modbus_close (host->connection); - modbus_free (host->connection); + modbus_close (host->connection); + modbus_free (host->connection); #endif } @@ -670,7 +676,6 @@ static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - status = 0; if (strcasecmp ("Type", child->key) == 0) status = cf_util_get_string_buffer (child, @@ -809,7 +814,6 @@ static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - status = 0; if (strcasecmp ("Instance", child->key) == 0) status = cf_util_get_string_buffer (child,