X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Flibcollectdclient%2Fclient.c;h=d47032455325e6e36210d62cc3b6236dd72fd02f;hp=64e8add27b5549a79833dd037444ab7bbf30636a;hb=7111bb6df7628edce3a8e538b386fbe27633a191;hpb=0aa5b32fd9246ad82686616eada68ee50f26bff7 diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index 64e8add2..d4703245 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -90,7 +90,6 @@ #define LCC_SET_ERRSTR(c, ...) \ do { \ snprintf((c)->errbuf, sizeof((c)->errbuf), __VA_ARGS__); \ - (c)->errbuf[sizeof((c)->errbuf) - 1] = 0; \ } while (0) /* @@ -98,7 +97,7 @@ */ struct lcc_connection_s { FILE *fh; - char errbuf[1024]; + char errbuf[2048]; }; struct lcc_response_s { @@ -163,18 +162,18 @@ static char *sstrerror(int errnum, char *buf, size_t buflen) { buf[buflen - 1] = 0; - return (buf); + return buf; } /* char *sstrerror */ static int lcc_set_errno(lcc_connection_t *c, int err) /* {{{ */ { if (c == NULL) - return (-1); + return -1; sstrerror(err, c->errbuf, sizeof(c->errbuf)); c->errbuf[sizeof(c->errbuf) - 1] = 0; - return (0); + return 0; } /* }}} int lcc_set_errno */ static char *lcc_strescape(char *dest, const char *src, @@ -184,7 +183,7 @@ static char *lcc_strescape(char *dest, const char *src, size_t src_pos; if ((dest == NULL) || (src == NULL)) - return (NULL); + return NULL; dest_pos = 0; src_pos = 0; @@ -221,7 +220,7 @@ static char *lcc_strescape(char *dest, const char *src, dest_pos++; src_pos++; - return (dest); + return dest; } /* }}} char *lcc_strescape */ /* lcc_chomp: Removes all control-characters at the end of a string. */ @@ -258,11 +257,11 @@ static int lcc_send(lcc_connection_t *c, const char *command) /* {{{ */ status = fprintf(c->fh, "%s\r\n", command); if (status < 0) { lcc_set_errno(c, errno); - return (-1); + return -1; } fflush(c->fh); - return (0); + return 0; } /* }}} int lcc_send */ static int lcc_receive(lcc_connection_t *c, /* {{{ */ @@ -276,7 +275,7 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ ptr = fgets(buffer, sizeof(buffer), c->fh); if (ptr == NULL) { lcc_set_errno(c, errno); - return (-1); + return -1; } lcc_chomp(buffer); lcc_tracef("receive: <-- %s\n", buffer); @@ -288,7 +287,7 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ res.status = (int)strtol(buffer, &ptr, 0); if ((errno != 0) || (ptr == &buffer[0])) { lcc_set_errno(c, errno); - return (-1); + return -1; } /* Skip white spaces after the status number */ @@ -302,7 +301,7 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ /* Error or no lines follow: We're done. */ if (res.status <= 0) { memcpy(ret_res, &res, sizeof(res)); - return (0); + return 0; } /* Allocate space for the char-pointers */ @@ -311,7 +310,7 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ res.lines = malloc(res.lines_num * sizeof(*res.lines)); if (res.lines == NULL) { lcc_set_errno(c, ENOMEM); - return (-1); + return -1; } /* Now receive all the lines */ @@ -338,11 +337,11 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ free(res.lines[i]); } free(res.lines); - return (-1); + return -1; } memcpy(ret_res, &res, sizeof(res)); - return (0); + return 0; } /* }}} int lcc_receive */ static int lcc_sendreceive(lcc_connection_t *c, /* {{{ */ @@ -352,18 +351,18 @@ static int lcc_sendreceive(lcc_connection_t *c, /* {{{ */ if (c->fh == NULL) { lcc_set_errno(c, EBADF); - return (-1); + return -1; } status = lcc_send(c, command); if (status != 0) - return (status); + return status; status = lcc_receive(c, &res); if (status == 0) memcpy(ret_res, &res, sizeof(*ret_res)); - return (status); + return status; } /* }}} int lcc_sendreceive */ static int lcc_open_unixsocket(lcc_connection_t *c, const char *path) /* {{{ */ @@ -381,7 +380,7 @@ static int lcc_open_unixsocket(lcc_connection_t *c, const char *path) /* {{{ */ fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0); if (fd < 0) { lcc_set_errno(c, errno); - return (-1); + return -1; } sa.sun_family = AF_UNIX; @@ -391,17 +390,17 @@ static int lcc_open_unixsocket(lcc_connection_t *c, const char *path) /* {{{ */ if (status != 0) { lcc_set_errno(c, errno); close(fd); - return (-1); + return -1; } c->fh = fdopen(fd, "r+"); if (c->fh == NULL) { lcc_set_errno(c, errno); close(fd); - return (-1); + return -1; } - return (0); + return 0; } /* }}} int lcc_open_unixsocket */ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */ @@ -430,7 +429,7 @@ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */ port = strchr(addr, ']'); if (port == NULL) { LCC_SET_ERRSTR(c, "malformed address: %s", addr_orig); - return (-1); + return -1; } *port = 0; port++; @@ -441,7 +440,7 @@ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */ port = NULL; else { LCC_SET_ERRSTR(c, "garbage after address: %s", port); - return (-1); + return -1; } } /* if (*addr = ']') */ else if (strchr(addr, '.') != NULL) /* Hostname or IPv4 */ @@ -461,7 +460,7 @@ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */ &ai_res); if (status != 0) { LCC_SET_ERRSTR(c, "getaddrinfo: %s", gai_strerror(status)); - return (-1); + return -1; } for (struct addrinfo *ai_ptr = ai_res; ai_ptr != NULL; @@ -493,11 +492,11 @@ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */ if (status != 0) { lcc_set_errno(c, status); freeaddrinfo(ai_res); - return (-1); + return -1; } freeaddrinfo(ai_res); - return (0); + return 0; } /* }}} int lcc_open_netsocket */ static int lcc_open_socket(lcc_connection_t *c, const char *addr) /* {{{ */ @@ -505,7 +504,7 @@ static int lcc_open_socket(lcc_connection_t *c, const char *addr) /* {{{ */ int status = 0; if (addr == NULL) - return (-1); + return -1; assert(c != NULL); assert(c->fh == NULL); @@ -518,7 +517,7 @@ static int lcc_open_socket(lcc_connection_t *c, const char *addr) /* {{{ */ else status = lcc_open_netsocket(c, addr); - return (status); + return status; } /* }}} int lcc_open_socket */ /* @@ -526,17 +525,17 @@ static int lcc_open_socket(lcc_connection_t *c, const char *addr) /* {{{ */ */ unsigned int lcc_version(void) /* {{{ */ { - return (LCC_VERSION); + return LCC_VERSION; } /* }}} unsigned int lcc_version */ const char *lcc_version_string(void) /* {{{ */ { - return (LCC_VERSION_STRING); + return LCC_VERSION_STRING; } /* }}} const char *lcc_version_string */ const char *lcc_version_extra(void) /* {{{ */ { - return (LCC_VERSION_EXTRA); + return LCC_VERSION_EXTRA; } /* }}} const char *lcc_version_extra */ int lcc_connect(const char *address, lcc_connection_t **ret_con) /* {{{ */ @@ -545,29 +544,29 @@ int lcc_connect(const char *address, lcc_connection_t **ret_con) /* {{{ */ int status; if (address == NULL) - return (-1); + return -1; if (ret_con == NULL) - return (-1); + return -1; c = calloc(1, sizeof(*c)); if (c == NULL) - return (-1); + return -1; status = lcc_open_socket(c, address); if (status != 0) { lcc_disconnect(c); - return (status); + return status; } *ret_con = c; - return (0); + return 0; } /* }}} int lcc_connect */ int lcc_disconnect(lcc_connection_t *c) /* {{{ */ { if (c == NULL) - return (-1); + return -1; if (c->fh != NULL) { fclose(c->fh); @@ -575,7 +574,7 @@ int lcc_disconnect(lcc_connection_t *c) /* {{{ */ } free(c); - return (0); + return 0; } /* }}} int lcc_disconnect */ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ @@ -594,17 +593,17 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ int status; if (c == NULL) - return (-1); + return -1; if (ident == NULL) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } /* Build a commend with an escaped version of the identifier string. */ status = lcc_identifier_to_string(c, ident_str, sizeof(ident_str), ident); if (status != 0) - return (status); + return status; snprintf(command, sizeof(command), "GETVAL %s", lcc_strescape(ident_esc, ident_str, sizeof(ident_esc))); @@ -613,12 +612,12 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ /* Send talk to the daemon.. */ status = lcc_sendreceive(c, command, &res); if (status != 0) - return (status); + return status; if (res.status != 0) { LCC_SET_ERRSTR(c, "Server error: %s", res.message); lcc_response_free(&res); - return (-1); + return -1; } values_num = res.lines_num; @@ -634,7 +633,7 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ } \ free(values_names); \ lcc_response_free(&res); \ - return (-1); \ + return -1; \ } while (0) /* If neither the values nor the names are requested, return here.. */ @@ -642,7 +641,7 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ if (ret_values_num != NULL) *ret_values_num = values_num; lcc_response_free(&res); - return (0); + return 0; } /* Allocate space for the values */ @@ -696,7 +695,7 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ lcc_response_free(&res); - return (0); + return 0; } /* }}} int lcc_getval */ int lcc_putval(lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */ @@ -710,13 +709,13 @@ int lcc_putval(lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */ if ((c == NULL) || (vl == NULL) || (vl->values_len < 1) || (vl->values == NULL) || (vl->values_types == NULL)) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } status = lcc_identifier_to_string(c, ident_str, sizeof(ident_str), &vl->identifier); if (status != 0) - return (status); + return status; SSTRCATF(command, "PUTVAL %s", lcc_strescape(ident_esc, ident_str, sizeof(ident_esc))); @@ -746,16 +745,16 @@ int lcc_putval(lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */ status = lcc_sendreceive(c, command, &res); if (status != 0) - return (status); + return status; if (res.status != 0) { LCC_SET_ERRSTR(c, "Server error: %s", res.message); lcc_response_free(&res); - return (-1); + return -1; } lcc_response_free(&res); - return (0); + return 0; } /* }}} int lcc_putval */ int lcc_flush(lcc_connection_t *c, const char *plugin, /* {{{ */ @@ -766,7 +765,7 @@ int lcc_flush(lcc_connection_t *c, const char *plugin, /* {{{ */ if (c == NULL) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } SSTRCPY(command, "FLUSH"); @@ -786,7 +785,7 @@ int lcc_flush(lcc_connection_t *c, const char *plugin, /* {{{ */ status = lcc_identifier_to_string(c, ident_str, sizeof(ident_str), ident); if (status != 0) - return (status); + return status; SSTRCATF(command, " identifier=%s", lcc_strescape(ident_esc, ident_str, sizeof(ident_esc))); @@ -794,16 +793,16 @@ int lcc_flush(lcc_connection_t *c, const char *plugin, /* {{{ */ status = lcc_sendreceive(c, command, &res); if (status != 0) - return (status); + return status; if (res.status != 0) { LCC_SET_ERRSTR(c, "Server error: %s", res.message); lcc_response_free(&res); - return (-1); + return -1; } lcc_response_free(&res); - return (0); + return 0; } /* }}} int lcc_flush */ /* TODO: Implement lcc_putnotif */ @@ -817,21 +816,21 @@ int lcc_listval(lcc_connection_t *c, /* {{{ */ size_t ident_num; if (c == NULL) - return (-1); + return -1; if ((ret_ident == NULL) || (ret_ident_num == NULL)) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } status = lcc_sendreceive(c, "LISTVAL", &res); if (status != 0) - return (status); + return status; if (res.status != 0) { LCC_SET_ERRSTR(c, "Server error: %s", res.message); lcc_response_free(&res); - return (-1); + return -1; } ident_num = res.lines_num; @@ -839,7 +838,7 @@ int lcc_listval(lcc_connection_t *c, /* {{{ */ if (ident == NULL) { lcc_response_free(&res); lcc_set_errno(c, ENOMEM); - return (-1); + return -1; } for (size_t i = 0; i < res.lines_num; i++) { @@ -873,20 +872,20 @@ int lcc_listval(lcc_connection_t *c, /* {{{ */ if (status != 0) { free(ident); - return (-1); + return -1; } *ret_ident = ident; *ret_ident_num = ident_num; - return (0); + return 0; } /* }}} int lcc_listval */ const char *lcc_strerror(lcc_connection_t *c) /* {{{ */ { if (c == NULL) - return ("Invalid object"); - return (c->errbuf); + return "Invalid object"; + return c->errbuf; } /* }}} const char *lcc_strerror */ int lcc_identifier_to_string(lcc_connection_t *c, /* {{{ */ @@ -894,7 +893,7 @@ int lcc_identifier_to_string(lcc_connection_t *c, /* {{{ */ const lcc_identifier_t *ident) { if ((string == NULL) || (string_size < 6) || (ident == NULL)) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } if (ident->plugin_instance[0] == 0) { @@ -915,7 +914,7 @@ int lcc_identifier_to_string(lcc_connection_t *c, /* {{{ */ } string[string_size - 1] = 0; - return (0); + return 0; } /* }}} int lcc_identifier_to_string */ int lcc_string_to_identifier(lcc_connection_t *c, /* {{{ */ @@ -930,7 +929,7 @@ int lcc_string_to_identifier(lcc_connection_t *c, /* {{{ */ string_copy = strdup(string); if (string_copy == NULL) { lcc_set_errno(c, ENOMEM); - return (-1); + return -1; } host = string_copy; @@ -938,7 +937,7 @@ int lcc_string_to_identifier(lcc_connection_t *c, /* {{{ */ if (plugin == NULL) { LCC_SET_ERRSTR(c, "Malformed identifier string: %s", string); free(string_copy); - return (-1); + return -1; } *plugin = 0; plugin++; @@ -947,7 +946,7 @@ int lcc_string_to_identifier(lcc_connection_t *c, /* {{{ */ if (type == NULL) { LCC_SET_ERRSTR(c, "Malformed identifier string: %s", string); free(string_copy); - return (-1); + return -1; } *type = 0; type++; @@ -975,7 +974,7 @@ int lcc_string_to_identifier(lcc_connection_t *c, /* {{{ */ SSTRCPY(ident->type_instance, type_instance); free(string_copy); - return (0); + return 0; } /* }}} int lcc_string_to_identifier */ int lcc_identifier_compare(const void *a, /* {{{ */ @@ -985,17 +984,17 @@ int lcc_identifier_compare(const void *a, /* {{{ */ int status; if ((i0 == NULL) && (i1 == NULL)) - return (0); + return 0; else if (i0 == NULL) - return (-1); + return -1; else if (i1 == NULL) - return (1); + return 1; #define CMP_FIELD(f) \ do { \ status = strcmp(i0->f, i1->f); \ if (status != 0) \ - return (status); \ + return status; \ } while (0); CMP_FIELD(host); @@ -1006,16 +1005,16 @@ int lcc_identifier_compare(const void *a, /* {{{ */ #undef CMP_FIELD - return (0); + return 0; } /* }}} int lcc_identifier_compare */ int lcc_sort_identifiers(lcc_connection_t *c, /* {{{ */ lcc_identifier_t *idents, size_t idents_num) { if (idents == NULL) { lcc_set_errno(c, EINVAL); - return (-1); + return -1; } qsort(idents, idents_num, sizeof(*idents), lcc_identifier_compare); - return (0); + return 0; } /* }}} int lcc_sort_identifiers */