diff --git a/clients/gnmi-ctl/gnmi_ctl.cc b/clients/gnmi-ctl/gnmi_ctl.cc index 1276431e..a8110206 100644 --- a/clients/gnmi-ctl/gnmi_ctl.cc +++ b/clients/gnmi-ctl/gnmi_ctl.cc @@ -11,8 +11,8 @@ #include "absl/cleanup/cleanup.h" #include "gflags/gflags.h" -#include "gnmi_ctl_utils.h" #include "gnmi/gnmi.grpc.pb.h" +#include "gnmi_ctl_utils.h" #include "grpcpp/grpcpp.h" #include "grpcpp/security/credentials.h" #include "grpcpp/security/tls_credentials_options.h" @@ -26,7 +26,7 @@ #include "stratum/lib/utils.h" DEFINE_bool(grpc_use_insecure_mode, false, - "grpc communication channel in insecure mode"); + "grpc communication channel in insecure mode"); DECLARE_bool(grpc_use_insecure_mode); #define DEFAULT_CERTS_DIR "/usr/share/stratum/certs/" @@ -171,7 +171,7 @@ ::gnmi::GetRequest build_gnmi_get_req(std::string path) { ::gnmi::SetRequest build_gnmi_set_req(std::string path, std::string val) { ::gnmi::SetRequest req; ::gnmi::Update* update; - char *check; + char* check; if (FLAGS_replace) { update = req.add_replace(); @@ -220,8 +220,8 @@ ::gnmi::SubscribeRequest build_gnmi_sub_sample_req( } bool extract_interface_node(char** path, char* node_path, bool* vhost_dev) { - char* key = NULL; - char* value = NULL; + char* key = NULL; + char* value = NULL; int found_node = 0; while (client_parse_key_value(path, &key, &value)) { @@ -242,36 +242,35 @@ bool extract_interface_node(char** path, char* node_path, bool* vhost_dev) { } if (strcmp(key, FLAGS_name_key.c_str()) == 0) { // Hardcoded length of "[name=]/" - snprintf(node_path + strlen(node_path), strlen(value) + 9, - "[name=%s]/", value); + snprintf(node_path + strlen(node_path), strlen(value) + 9, "[name=%s]/", + value); found_node += 1; } - if (found_node == 2) - return 0; + if (found_node == 2) return 0; } return -1; } void traverse_params(char** path, char* node_path, char* config_value, bool& flag) { - char* key = NULL; - char* value = NULL; + char* key = NULL; + char* value = NULL; if (client_parse_key_value(path, &key, &value)) { if ((value != NULL) && value[0] != '\0') { // This should be executed for a pair, specifically for // SET operation. snprintf(node_path + strlen(node_path), - strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, - "%s/%s", FLAGS_subtree_config.c_str(), key); + strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, "%s/%s", + FLAGS_subtree_config.c_str(), key); strcpy(config_value, value); return; } else if (key != NULL && key[0] != '\0') { // This should be executed for a , specifically for // GET operation. snprintf(node_path + strlen(node_path), - strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, - "%s/%s", FLAGS_subtree_config.c_str(), key); + strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, "%s/%s", + FLAGS_subtree_config.c_str(), key); return; } } @@ -283,7 +282,6 @@ ::grpc::ClientReaderWriterInterface< ::gnmi::SubscribeRequest, ::gnmi::SubscribeResponse>* stream_reader_writer; ::util::Status Main(int argc, char** argv) { - // Default certificate file location for TLS-mode FLAGS_ca_cert_file = DEFAULT_CERTS_DIR "ca.crt"; FLAGS_server_key_file = DEFAULT_CERTS_DIR "stratum.key"; @@ -297,7 +295,7 @@ ::util::Status Main(int argc, char** argv) { ::gflags::SetUsageMessage(kUsage); InitGoogle(argv[0], &argc, &argv, true); stratum::InitStratumLogging(); - + bool vhost_device = false; if (argc < 2) { std::cout << kUsage << std::endl; @@ -311,9 +309,9 @@ ::util::Status Main(int argc, char** argv) { RETURN_IF_ERROR( CreatePipeForSignalHandling(&pipe_read_fd_, &pipe_write_fd_)); } - CHECK_RETURN_IF_FALSE(std::signal(SIGINT, HandleSignal) != SIG_ERR); + RET_CHECK(std::signal(SIGINT, HandleSignal) != SIG_ERR); pthread_t context_cancel_tid; - CHECK_RETURN_IF_FALSE(pthread_create(&context_cancel_tid, nullptr, + RET_CHECK(pthread_create(&context_cancel_tid, nullptr, ContextCancelThreadFunc, nullptr) == 0); auto cleaner = absl::MakeCleanup([&context_cancel_tid, &ctx] { int signal = SIGINT; @@ -330,11 +328,11 @@ ::util::Status Main(int argc, char** argv) { std::shared_ptr<::grpc::Channel> channel; if (FLAGS_grpc_use_insecure_mode) { std::shared_ptr<::grpc::ChannelCredentials> channel_credentials = - ::grpc::InsecureChannelCredentials(); + ::grpc::InsecureChannelCredentials(); channel = ::grpc::CreateChannel(FLAGS_grpc_addr, channel_credentials); } else { ASSIGN_OR_RETURN(auto credentials_manager, - CredentialsManager::CreateInstance(true)); + CredentialsManager::CreateInstance(true)); channel = ::grpc::CreateChannel( FLAGS_grpc_addr, credentials_manager->GenerateExternalFacingClientCredentials()); @@ -357,7 +355,7 @@ ::util::Status Main(int argc, char** argv) { return ::util::OkStatus(); } - char *path = argv[2]; + char* path = argv[2]; char buffer[MAX_STR_LENGTH]; bool params = true; @@ -380,7 +378,7 @@ ::util::Status Main(int argc, char** argv) { RETURN_IF_GRPC_ERROR(stub->Get(&ctx, req, &resp)); PRINT_MSG(resp, "Get Response from Server"); } else if (cmd == "set") { - while(params) { + while (params) { char path1[MAX_STR_LENGTH] = {0}; char config_value[MAX_STR_LENGTH] = {0}; @@ -389,8 +387,9 @@ ::util::Status Main(int argc, char** argv) { // If device is 'virtual-device' and port type is 'link', consider it a // 'vhost' type. if (((strcmp(config_value, "link") == 0) || - (strcmp(config_value, "LINK") == 0)) && vhost_device) { - strcpy(config_value, "vhost"); + (strcmp(config_value, "LINK") == 0)) && + vhost_device) { + strcpy(config_value, "vhost"); } if (params) { auto stub = ::gnmi::gNMI::NewStub(channel); @@ -414,7 +413,7 @@ ::util::Status Main(int argc, char** argv) { auto stream_reader_writer_ptr = stub->Subscribe(&ctx); stream_reader_writer = stream_reader_writer_ptr.get(); ::gnmi::SubscribeRequest req = build_gnmi_sub_onchange_req(path); - // CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req)) + // RET_CHECK(stream_reader_writer->Write(req)) // << "Cannot write request."; stream_reader_writer->Write(req); ::gnmi::SubscribeResponse resp; @@ -429,7 +428,7 @@ ::util::Status Main(int argc, char** argv) { stream_reader_writer = stream_reader_writer_ptr.get(); ::gnmi::SubscribeRequest req = build_gnmi_sub_sample_req(path, FLAGS_interval); - // CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req)) + // RET_CHECK(stream_reader_writer->Write(req)) // << "Cannot write request."; stream_reader_writer->Write(req); ::gnmi::SubscribeResponse resp; @@ -446,6 +445,4 @@ ::util::Status Main(int argc, char** argv) { } // namespace gnmi -int main(int argc, char** argv) { - return gnmi::Main(argc, argv).error_code(); -} +int main(int argc, char** argv) { return gnmi::Main(argc, argv).error_code(); } diff --git a/clients/gnmi-ctl/gnmi_ctl_utils.c b/clients/gnmi-ctl/gnmi_ctl_utils.c index 60f58bd2..688f90cd 100644 --- a/clients/gnmi-ctl/gnmi_ctl_utils.c +++ b/clients/gnmi-ctl/gnmi_ctl_utils.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. - * Copyright (c) 2022 Intel Corporation. + * Copyright (c) 2022-2023 Intel Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,35 +22,34 @@ #include #include -static size_t parse_value(const char *s, const char *delimiters) -{ - size_t n = 0; +static size_t parse_value(const char* s, const char* delimiters) { + size_t n = 0; - /* Iterate until we reach a delimiter. - * - * strchr(s, '\0') returns s+strlen(s), so this test handles the null - * terminator at the end of 's'. */ - while (!strchr(delimiters, s[n])) { - if (s[n] == '(') { - int level = 0; - do { - switch (s[n]) { - case '\0': - return n; - case '(': - level++; - break; - case ')': - level--; - break; - } - n++; - } while (level > 0); - } else { - n++; + /* Iterate until we reach a delimiter. + * + * strchr(s, '\0') returns s+strlen(s), so this test handles the null + * terminator at the end of 's'. */ + while (!strchr(delimiters, s[n])) { + if (s[n] == '(') { + int level = 0; + do { + switch (s[n]) { + case '\0': + return n; + case '(': + level++; + break; + case ')': + level--; + break; } + n++; + } while (level > 0); + } else { + n++; } - return n; + } + return n; } /* Parses a key or a key-value pair from '*stringp'. @@ -63,64 +62,63 @@ static size_t parse_value(const char *s, const char *delimiters) * * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to * NULL and returns false. */ -bool client_parse_key_value(char **stringp, char **keyp, char **valuep) -{ - /* Skip white space and delimiters. If that brings us to the end of the - * input string, we are done and there are no more key-value pairs. */ - *stringp += strspn(*stringp, ", \t\r\n"); - if (**stringp == '\0') { - *keyp = *valuep = NULL; - return false; - } +bool client_parse_key_value(char** stringp, char** keyp, char** valuep) { + /* Skip white space and delimiters. If that brings us to the end of the + * input string, we are done and there are no more key-value pairs. */ + *stringp += strspn(*stringp, ", \t\r\n"); + if (**stringp == '\0') { + *keyp = *valuep = NULL; + return false; + } - /* Extract the key and the delimiter that ends the key-value pair or begins - * the value. Advance the input position past the key and delimiter. */ - char *key = *stringp; - size_t key_len = strcspn(key, ":=(, \t\r\n"); - char key_delim = key[key_len]; - key[key_len] = '\0'; - *stringp += key_len + (key_delim != '\0'); + /* Extract the key and the delimiter that ends the key-value pair or begins + * the value. Advance the input position past the key and delimiter. */ + char* key = *stringp; + size_t key_len = strcspn(key, ":=(, \t\r\n"); + char key_delim = key[key_len]; + key[key_len] = '\0'; + *stringp += key_len + (key_delim != '\0'); - /* Figure out what delimiter ends the value: - * - * - If key_delim is ":" or "=", the value extends until white space - * or a comma. - * - * - If key_delim is "(", the value extends until ")". - * - * If there is no value, we are done. */ - const char *value_delims; - if (key_delim == ':' || key_delim == '=') { - value_delims = ", \t\r\n"; - } else if (key_delim == '(') { - value_delims = ")"; - } else { - *keyp = key; - *valuep = key + key_len; /* Empty string. */ - return true; - } + /* Figure out what delimiter ends the value: + * + * - If key_delim is ":" or "=", the value extends until white space + * or a comma. + * + * - If key_delim is "(", the value extends until ")". + * + * If there is no value, we are done. */ + const char* value_delims; + if (key_delim == ':' || key_delim == '=') { + value_delims = ", \t\r\n"; + } else if (key_delim == '(') { + value_delims = ")"; + } else { + *keyp = key; + *valuep = key + key_len; /* Empty string. */ + return true; + } - /* Extract the value. Advance the input position past the value and - * delimiter. */ - char *value = *stringp; - size_t value_len = parse_value(value, value_delims); - char value_delim = value[value_len]; + /* Extract the value. Advance the input position past the value and + * delimiter. */ + char* value = *stringp; + size_t value_len = parse_value(value, value_delims); + char value_delim = value[value_len]; - /* Handle the special case if the value is of the form "(x)->y". - * After parsing, 'valuep' will be pointing to - "x)->y". - * */ - if (key_delim == '(' && value[value_len] == ')' && - value[value_len + 1] == '-' && value[value_len + 2] == '>') { - value_delims = ", \t\r\n"; - value_len += parse_value(&value[value_len], value_delims); - value_delim = value[value_len]; - } - value[value_len] = '\0'; - *stringp += value_len + (value_delim != '\0'); + /* Handle the special case if the value is of the form "(x)->y". + * After parsing, 'valuep' will be pointing to - "x)->y". + * */ + if (key_delim == '(' && value[value_len] == ')' && + value[value_len + 1] == '-' && value[value_len + 2] == '>') { + value_delims = ", \t\r\n"; + value_len += parse_value(&value[value_len], value_delims); + value_delim = value[value_len]; + } + value[value_len] = '\0'; + *stringp += value_len + (value_delim != '\0'); - *keyp = key; - *valuep = value; - return true; + *keyp = key; + *valuep = value; + return true; } /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'. @@ -133,13 +131,13 @@ bool client_parse_key_value(char **stringp, char **keyp, char **valuep) * memset(dst, '\0', size); * client_strlcpy(dst, src, size); * - * (Thus, client_strzcpy() is similar to strncpy() without some of the pitfalls.) + * (Thus, client_strzcpy() is similar to strncpy() without some of the + * pitfalls.) */ -void client_strzcpy(char *dst, const char *src, size_t size) -{ - if (size > 0) { - size_t len = strnlen(src, size - 1); - memcpy(dst, src, len); - memset(dst + len, '\0', size - len); - } +void client_strzcpy(char* dst, const char* src, size_t size) { + if (size > 0) { + size_t len = strnlen(src, size - 1); + memcpy(dst, src, len); + memset(dst + len, '\0', size - len); + } } diff --git a/clients/gnmi-ctl/gnmi_ctl_utils.h b/clients/gnmi-ctl/gnmi_ctl_utils.h index fab40d59..ee820550 100644 --- a/clients/gnmi-ctl/gnmi_ctl_utils.h +++ b/clients/gnmi-ctl/gnmi_ctl_utils.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. - * Copyright (c) 2022 Intel Corporation. + * Copyright (c) 2022-2023 Intel Corporation. + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,9 +28,9 @@ extern "C" { #endif -bool client_parse_key_value(char **stringp, char **keyp, char **valuep); +bool client_parse_key_value(char** stringp, char** keyp, char** valuep); -void client_strzcpy(char *dst, const char *src, size_t size); +void client_strzcpy(char* dst, const char* src, size_t size); #ifdef __cplusplus } diff --git a/clients/sgnmi_cli/sgnmi_cli.cc b/clients/sgnmi_cli/sgnmi_cli.cc index 82fbdcfe..8268987a 100644 --- a/clients/sgnmi_cli/sgnmi_cli.cc +++ b/clients/sgnmi_cli/sgnmi_cli.cc @@ -269,9 +269,9 @@ ::util::Status Main(int argc, char** argv) { RETURN_IF_ERROR( CreatePipeForSignalHandling(&pipe_read_fd_, &pipe_write_fd_)); } - CHECK_RETURN_IF_FALSE(std::signal(SIGINT, HandleSignal) != SIG_ERR); + RET_CHECK(std::signal(SIGINT, HandleSignal) != SIG_ERR); pthread_t context_cancel_tid; - CHECK_RETURN_IF_FALSE(pthread_create(&context_cancel_tid, nullptr, + RET_CHECK(pthread_create(&context_cancel_tid, nullptr, ContextCancelThreadFunc, nullptr) == 0); auto cleaner = absl::MakeCleanup([&context_cancel_tid, &ctx] { int signal = SIGINT; @@ -338,7 +338,7 @@ ::util::Status Main(int argc, char** argv) { auto stream_reader_writer = stub->Subscribe(&ctx); ::gnmi::SubscribeRequest req = BuildGnmiSubOnchangeRequest(path); PRINT_MSG(req, "REQUEST"); - CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req)) << "Can not write request."; + RET_CHECK(stream_reader_writer->Write(req)) << "Cannot write request."; ::gnmi::SubscribeResponse resp; while (stream_reader_writer->Read(&resp)) { PRINT_MSG(resp, "RESPONSE"); @@ -349,7 +349,7 @@ ::util::Status Main(int argc, char** argv) { ::gnmi::SubscribeRequest req = BuildGnmiSubSampleRequest(path, FLAGS_interval); PRINT_MSG(req, "REQUEST"); - CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req)) << "Can not write request."; + RET_CHECK(stream_reader_writer->Write(req)) << "Cannot write request."; ::gnmi::SubscribeResponse resp; while (stream_reader_writer->Read(&resp)) { PRINT_MSG(resp, "RESPONSE");