/* convenience function to return all nodes, or default node if none found */ static fsa_node_addr *get_storage_nodes(void) { GKeyFile *config = fsa_get_config(); int default_port = fsa_get_admind_port(config); fsa_node_addr *nodes = NULL; /* use localhost if no config file found */ if (config == NULL) { fsa_error(LOG_WARNING, "Unable to read config file at '%s', assuming localhost\n", fs_get_config_file()); nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } nodes = fsa_get_node_list(config); /* done with config */ fsa_config_free(config); /* if no nodes found in config file, use localhost */ if (nodes == NULL) { fsa_error(LOG_WARNING, "No nodes found in '%s', assuming localhost\n", fs_get_config_file()); nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } return nodes; }
/* Get a port number to run or query admind on */ int fsaf_get_admind_usage(void) { GKeyFile *conf = fsa_get_config(); if (conf == NULL) { fsa_error(LOG_DEBUG, "could not read config file, returning default port"); return ADMIND_USAGE_NONE; } char *usage_str; int usage = -1; GError *err = NULL; usage_str = g_key_file_get_value(conf, "4s-boss", "discovery", &err); /* field not set in config file */ if (usage_str == NULL) { fsa_error(LOG_DEBUG, "no port set in config file, returning default port"); g_error_free(err); fsa_config_free(conf); return ADMIND_USAGE_NONE; } if (strcmp(usage_str, "default") == 0) { usage = ADMIND_USAGE_DEFAULT; } else if (strcmp(usage_str, "fallback") == 0) { usage = ADMIND_USAGE_FALLBACK; } else if (strcmp(usage_str, "sole") == 0) { usage = ADMIND_USAGE_SOLE; } else if (strcmp(usage_str, "none") == 0) { usage = ADMIND_USAGE_NONE; } else { usage = ADM_ERR_BAD_CONFIG; } free(usage_str); fsa_config_free(conf); return usage; }
/* check command line opts and config file to set server_port global */ static int init_server_port(void) { int port; /* no port given on command line */ if (cport == NULL) { /* get default or config file port */ GKeyFile *config = fsa_get_config(); port = fsa_get_admind_port(config); fsa_config_free(config); if (port == -1) { if (errno == ADM_ERR_BAD_CONFIG) { fprintf( stderr, "%s: non-numeric port specified in %s", program_invocation_short_name, fs_get_config_file() ); } else if (errno == ERANGE) { fprintf( stderr, "%s: port number out of range 0-65535 in %s", program_invocation_short_name, fs_get_config_file() ); } else { fprintf( stderr, "%s: unknown error reading port from config file at %s\n", program_invocation_short_name, fs_get_config_file() ); } return -1; } } else { /* cport has been specified on command line */ if (!fsa_is_int(cport)) { fprintf(stderr, "%s: non-numeric port specified on command line\n", program_invocation_short_name); return -1; } port = atoi(cport); if (port < 0 || port > 65535) { fprintf(stderr, "%s: port number %d out of range 0-65535\n", program_invocation_short_name, port); return -1; } } /* have a port 0-65535 if we got here */ sprintf(server_port, "%d", port); return 0; }
/* get single node_addr based on matching host name in config file */ static fsa_node_addr *node_name_to_node_addr(char *name) { GKeyFile *config = fsa_get_config(); int default_port = fsa_get_admind_port(config); fsa_node_addr *nodes = NULL; /* use localhost if no config file found */ if (config == NULL) { if (strcmp(name, "localhost") == 0) { nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } else { return NULL; } } nodes = fsa_get_node_list(config); /* done with config */ fsa_config_free(config); /* if no nodes found in config file, use localhost */ if (nodes == NULL) { if (strcmp(name, "localhost") == 0) { nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } else { return NULL; } } /* count through node list to get node node_num in */ fsa_node_addr *cur = nodes; fsa_node_addr *tmp = NULL; while (cur != NULL) { if (strcmp(name, cur->host) == 0) { /* free rest of node list, then return the current */ fsa_node_addr_free(cur->next); cur->next = NULL; return cur; } /* free current node then move to next */ tmp = cur; cur = cur->next; fsa_node_addr_free_one(tmp); } return NULL; }
/* Get info on a kb from any number of nodes. For all kbs, or all nodes, set the corresponding argument to NULL */ fsa_kb_info *fsaf_fetch_kb_info(const unsigned char *kb_name, fsa_node_addr *nodes) { fsa_node_addr *cur_node; int free_nodes = 0; /* free nodes if they werem't passed to function */ /* if no nodes given, get all nodes from config */ if (nodes == NULL) { fsa_error(LOG_DEBUG, "fetching kb info for all nodes"); GKeyFile *conf = fsa_get_config(); nodes = fsa_get_node_list(conf); fsa_config_free(conf); free_nodes = 1; } unsigned char *buf; unsigned char header_buf[ADM_HEADER_LEN]; /* store single header packet */ int nbytes, sock_fd, rv; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* command to be sent to all stores */ int cmd_len; /* length of command packet */ unsigned char *cmd_pkt; /* command packet itself */ fsa_kb_info *ki_list = NULL; uint8_t cmdval; uint16_t datasize; char ipaddr[INET6_ADDRSTRLEN]; /* large enough to hold a v4 or v6 addr */ if (kb_name == NULL) { /* get info on all kbs */ fsa_error(LOG_DEBUG, "fetching info for all kbs"); cmd_pkt = fsap_encode_cmd_get_kb_info_all(&cmd_len); } else { /* request info on named kb */ fsa_error(LOG_DEBUG, "fetching info for kb '%s'", kb_name); cmd_pkt = fsap_encode_cmd_get_kb_info(kb_name, &cmd_len); } fsa_error(LOG_DEBUG, "admin command length is %d bytes", cmd_len); /* connect to each storage node */ for (cur_node = nodes; cur_node != NULL; cur_node = cur_node->next) { sock_fd = fsaf_connect_to_admind(cur_node->host, cur_node->port, &hints, ipaddr); if (sock_fd == -1) { fsa_error(LOG_ERR, "failed to connect to %s:%d, skipping node", cur_node->host, cur_node->port); continue; } /* send command to node */ nbytes = cmd_len; rv = fsa_sendall(sock_fd, cmd_pkt, &nbytes); if (rv == -1) { fsa_error(LOG_ERR, "failed to send command to %s:%d, skipping node", cur_node->host, cur_node->port); continue; } fsa_error(LOG_DEBUG, "header (%d bytes) sent to %s:%d, waiting for response", nbytes, cur_node->host, cur_node->port); /* get response header from node */ rv = fsa_fetch_header(sock_fd, header_buf); if (rv == -1) { fsa_error(LOG_ERR, "failed to get response from %s:%d, skipping node", cur_node->host, cur_node->port); continue; } fsa_error(LOG_DEBUG, "response received from %s:%d", cur_node->host, cur_node->port); /* server sent us data */ rv = fsap_decode_header(header_buf, &cmdval, &datasize); if (rv == -1) { fsa_error(LOG_ERR, "unable to decode header from %s:%d", cur_node->host, cur_node->port); close(sock_fd); continue; } /* alloc buffer for receiving further data into */ buf = (unsigned char *)malloc(datasize); if (buf == NULL) { errno = ENOMEM; free(cmd_pkt); return NULL; } fsa_error(LOG_DEBUG, "response header from %s:%d decoded", cur_node->host, cur_node->port); /* handle response from client */ if (cmdval == ADM_RSP_GET_KB_INFO_ALL) { fsa_error(LOG_DEBUG, "ADM_RSP_GET_KB_INFO_ALL received"); fsa_error(LOG_DEBUG, "fetching data from client"); nbytes = fsaf_recv_from_admind(sock_fd, buf, datasize); if (nbytes <= 0) { /* error already handled */ free(buf); continue; } /* local list of kb info for a single node */ fsa_kb_info *kid = fsap_decode_rsp_get_kb_info_all(buf); free(buf); fsa_kb_info *tmp_ki = NULL; fsa_kb_info *cur_ki = NULL; /* add kb info to list of info across nodes */ if (kid != NULL) { tmp_ki = kid; /* pointer to start of list */ cur_ki = kid; int done = 0; while (!done) { cur_ki->ipaddr = (unsigned char *)strdup(ipaddr); if (cur_ki->next == NULL) { /* if last item in list */ cur_ki->next = ki_list; /* append existing vals */ ki_list = tmp_ki; /* point to new head */ done = 1; } else { cur_ki = cur_ki->next; } } } } else if (cmdval == ADM_RSP_GET_KB_INFO) { fsa_error(LOG_DEBUG, "ADM_RSP_GET_KB_INFO_ALL received"); fsa_error(LOG_DEBUG, "fetching data from client"); nbytes = fsaf_recv_from_admind(sock_fd, buf, datasize); if (nbytes <= 0) { /* error already handled */ free(buf); continue; } fsa_kb_info *kid = fsap_decode_rsp_get_kb_info(buf); free(buf); if (kid != NULL) { /* copy ip addr to struct */ kid->ipaddr = (unsigned char *)strdup(ipaddr); kid->next = ki_list; ki_list = kid; } } close(sock_fd); } free(cmd_pkt); if (free_nodes) { fsa_node_addr_free(nodes); } return ki_list; }
/* gets host info from config file based on node number (starting at 0) */ static fsa_node_addr *node_num_to_node_addr(int node_num) { /* sanity check node number */ if (node_num < 0 || node_num >= FS_MAX_SEGMENTS) { return NULL; } GKeyFile *config = fsa_get_config(); fsa_node_addr *nodes = NULL; int default_port = fsa_get_admind_port(config); /* assume localhost if no config file found */ if (config == NULL) { if (node_num == 0) { nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } else { return NULL; } } /* if no nodes found in config file, use localhost */ nodes = fsa_get_node_list(config); /* done with config */ fsa_config_free(config); if (nodes == NULL) { if (node_num == 0) { nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; return nodes; } else { return NULL; } } /* count through node list to get node node_num in */ int i = 0; fsa_node_addr *cur = nodes; fsa_node_addr *tmp = NULL; while (cur != NULL) { if (i == node_num) { /* free rest of node list, then return the current */ fsa_node_addr_free(cur->next); cur->next = NULL; return cur; } /* free current node then move to next */ tmp = cur; cur = cur->next; fsa_node_addr_free_one(tmp); i += 1; } return NULL; }
/* Check whether admin daemon on all nodes is reachable */ static int cmd_list_nodes(void) { /* this command has no arguments, exit if any are found */ if (args_index >= 0) { print_invalid_arg(); return 1; } /* network related vars */ struct addrinfo hints; int default_port = FS_ADMIND_PORT; fsa_node_addr *nodes = NULL; fsa_node_addr *p; int sock_fd; char ipaddr[INET6_ADDRSTRLEN]; /* printing/output related vars */ int all_nodes_ok = 0; int hostlen = 13; int len; int node_num = 0; int n_nodes = 0; /* Setup hints information */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* attempt to read /etc/4store.conf */ GKeyFile *config = fsa_get_config(); if (config == NULL) { /* assume localhost if no config file found */ fsa_error(LOG_WARNING, "Unable to read config file at '%s', assuming localhost\n", fs_get_config_file()); } else { nodes = fsa_get_node_list(config); if (nodes == NULL) { fsa_error(LOG_WARNING, "No nodes found in '%s', assuming localhost\n", fs_get_config_file()); default_port = fsa_get_admind_port(config); } } if (nodes == NULL) { /* Use localhost and default port */ nodes = fsa_node_addr_new("localhost"); nodes->port = default_port; } /* loop through once to get lengths of various fields */ for (p = nodes; p != NULL; p = p->next) { len = strlen(p->host) + 1; if (len > hostlen) { hostlen = len; } n_nodes += 1; } int n_nodes_len = int_len(n_nodes); if (n_nodes_len < 11) { n_nodes_len = 11; } /* print column headers */ if (colour_flag) { printf(ANSI_COLOUR_BLUE); } printf("%-*s %-*s port status ip_address\n", n_nodes_len, "node_number", hostlen, "hostname"); if (colour_flag) { printf(ANSI_COLOUR_RESET); } /* loop through all nodes and attempt to connect admin daemon on each */ for (p = nodes; p != NULL; p = p->next) { /* set default output for IP address */ strcpy(ipaddr, "unknown"); /* check if we can open conn to admin daemon */ sock_fd = fsaf_connect_to_admind(p->host, p->port, &hints, ipaddr); /* print result of attempted connection */ printf("%-*d %-*s %-5d ", n_nodes_len, node_num, hostlen, p->host, p->port); if (sock_fd == -1) { print_colour("unreachable", ANSI_COLOUR_RED); all_nodes_ok = 2; } else { print_colour("ok ", ANSI_COLOUR_GREEN); close(sock_fd); } printf(" %s\n", ipaddr); node_num += 1; } fsa_node_addr_free(nodes); fsa_config_free(config); return all_nodes_ok; }