Esempio n. 1
0
/* 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;
}
Esempio n. 2
0
/* convenience function to avoid code duplication */
static fsa_node_addr *get_nodes_from_cmd_line(void)
{
    fsa_node_addr *nodes = NULL;

    if (args_index < 0) {
        /* no arguments, so get all nodes */
        nodes = get_storage_nodes();
    }
    else {
        /* optional host name or node number is given, use single node  */
        char *host_or_nodenum = argv[args_index];
        args_index += 1;
        if (args_index != argc) {
            /* shouldn't be any args after this one */
            print_invalid_arg();
            return NULL;
        }

        /* check whether we have a hostname or a node number */
        if (fsa_is_int(host_or_nodenum)) {
            /* get host by number, starting from 0 */
            int node_num = atoi(host_or_nodenum);
            nodes = node_num_to_node_addr(node_num);
            if (nodes == NULL) {
                fsa_error(LOG_ERR, "Node number '%d' not found in config\n",
                          node_num);
                return NULL;
            }
        }
        else {
            /* get host by name */
            nodes = node_name_to_node_addr(host_or_nodenum);
            if (nodes == NULL) {
                fsa_error(LOG_ERR,
                          "Node with name '%s' not found in config\n",
                          host_or_nodenum);
                return NULL;
            }
        }
    }

    return nodes;
}
Esempio n. 3
0
/* Get a port number to run or query admind on */
int fsa_get_admind_port(GKeyFile *config_file)
{
    /* if no config file, use default port */
    if (config_file == NULL) {
        return FS_ADMIND_PORT;
    }

    char *cport;
    int port;
    GError *err = NULL;

    cport = g_key_file_get_value(config_file, "4s-boss", "port", &err);

    /* if field not set in config, use default port */
    if (cport == NULL) {
        g_error_free(err);
        return FS_ADMIND_PORT;
    }

    /* if non-int value given in config file */
    if (!fsa_is_int(cport)) {
        free(cport);
        errno = ADM_ERR_BAD_CONFIG;
        return -1;
    }

    port = atoi(cport);
    free(cport);

    /* if value in config is out of range */
    if (port < 0 || port > 65535) {
        errno = ERANGE;
        return -1;
    }

    return port;
}