summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/common-utils.c
diff options
context:
space:
mode:
authorSunny Kumar <sunkumar@redhat.com>2018-08-13 12:18:51 +0530
committerAmar Tumballi <amarts@redhat.com>2018-08-31 01:29:06 +0000
commit4996a229f3d09cbb6ed3b9dcdf1d527f36803919 (patch)
tree0d8416d86e039858f6d9e81a1e3785a4442f622f /libglusterfs/src/common-utils.c
parent25c405ac688787ea3f03ca4d427f2b00544dbbd0 (diff)
libglusterfs : fix coverity issues in common-utils.c
Fixes CID 1351691, 1351678, 1274192, 1274117, 1124845. Change-Id: I65805524de85fb2186260288641390458a719499 updates: bz#789278 Signed-off-by: Sunny Kumar <sunkumar@redhat.com>
Diffstat (limited to 'libglusterfs/src/common-utils.c')
-rw-r--r--libglusterfs/src/common-utils.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
index e547ad929af..4c98e705e5e 100644
--- a/libglusterfs/src/common-utils.c
+++ b/libglusterfs/src/common-utils.c
@@ -385,8 +385,11 @@ gf_dnscache_init (time_t ttl)
{
struct dnscache *cache = GF_MALLOC (sizeof (*cache),
gf_common_mt_dnscache);
- cache->cache_dict = NULL;
- cache->ttl = ttl;
+ if (cache) {
+ cache->cache_dict = NULL;
+ cache->ttl = ttl;
+ }
+
return cache;
}
@@ -489,11 +492,12 @@ out:
goto out;
}
entry->fqdn = fqdn;
- entry->ip = gf_strdup (ip);
if (!ip) {
gf_dnscache_entry_deinit (entry);
goto out;
}
+
+ entry->ip = gf_strdup (ip);
entry->timestamp = time (NULL);
entrydata = bin_to_data (entry, sizeof (*entry));
@@ -3043,6 +3047,8 @@ gf_get_reserved_ports ()
" getting reserved ports info", proc_file);
goto out;
}
+
+ buffer[ret] = '\0';
ports_info = gf_strdup (buffer);
out:
@@ -3418,8 +3424,11 @@ gf_is_local_addr (char *hostname)
gf_msg_debug (this->name, 0, "%s ",
get_ip_from_addrinfo (res, &ip));
- found = gf_is_loopback_localhost (res->ai_addr, hostname)
- || gf_interface_search (ip);
+ if (ip) {
+ found = gf_is_loopback_localhost
+ (res->ai_addr, hostname) ||
+ gf_interface_search (ip);
+ }
if (found) {
GF_FREE (ip);
goto out;
@@ -3895,8 +3904,10 @@ gf_is_service_running (char *pidfile, int *pid)
fno = fileno (file);
ret = lockf (fno, F_TEST, 0);
- if (ret == -1)
+ if (ret == -1) {
running = _gf_true;
+ goto out;
+ }
ret = fscanf (file, "%d", pid);
if (ret <= 0) {
@@ -4429,6 +4440,7 @@ recursive_rmdir (const char *delete_path)
if (ret == -1) {
gf_msg_debug (this->name, 0, "Failed to stat entry %s :"
" %s", path, strerror (errno));
+ (void) sys_closedir (dir);
goto out;
}
' href='#n266'>266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
/*
   Copyright (c) 2010-2016 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/file.h>
#include <netdb.h>
#include <signal.h>
#include <libgen.h>

#include <sys/utsname.h>

#include <stdint.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <semaphore.h>
#include <errno.h>

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#ifdef HAVE_MALLOC_STATS
#ifdef DEBUG
#include <mcheck.h>
#endif
#endif

#include "cli.h"
#include "cli-quotad-client.h"
#include "cli-cmd.h"
#include "cli-mem-types.h"

#include "xlator.h"
#include "glusterfs.h"
#include "compat.h"
#include "logging.h"
#include "dict.h"
#include "list.h"
#include "timer.h"
#include "stack.h"
#include "revision.h"
#include "common-utils.h"
#include "event.h"
#include "globals.h"
#include "syscall.h"
#include "call-stub.h"
#include <fnmatch.h>

#include "xdr-generic.h"

extern int connected;
/* using argp for command line parsing */

const char *argp_program_version = ""                                         \
        PACKAGE_NAME" "PACKAGE_VERSION                                        \
        "\nRepository revision: " GLUSTERFS_REPOSITORY_REVISION "\n"          \
        "Copyright (c) 2006-2016 Red Hat, Inc. "                              \
        "<https://www.gluster.org/>\n"                                        \
        "GlusterFS comes with ABSOLUTELY NO WARRANTY.\n"                      \
        "It is licensed to you under your choice of the GNU Lesser\n"         \
        "General Public License, version 3 or any later version (LGPLv3\n"    \
        "or later), or the GNU General Public License, version 2 (GPLv2),\n"  \
        "in all cases as published by the Free Software Foundation.";
const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";

struct rpc_clnt *global_quotad_rpc;
struct rpc_clnt *global_rpc;

rpc_clnt_prog_t *cli_rpc_prog;


extern struct rpc_clnt_program cli_prog;

static int
glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx)
{
        cmd_args_t    *cmd_args = NULL;
        struct rlimit  lim = {0, };
        call_pool_t   *pool = NULL;
        int            ret         = -1;

        ret = xlator_mem_acct_init (THIS, cli_mt_end);
        if (ret != 0) {
                return ret;
        }

        ctx->process_uuid = generate_glusterfs_ctx_id ();
        if (!ctx->process_uuid)
                return -1;

        ctx->page_size  = 128 * GF_UNIT_KB;

        ctx->iobuf_pool = iobuf_pool_new ();
        if (!ctx->iobuf_pool)
                return -1;

        ctx->event_pool = event_pool_new (DEFAULT_EVENT_POOL_SIZE,
                                          STARTING_EVENT_THREADS);
        if (!ctx->event_pool)
                return -1;

        pool = GF_CALLOC (1, sizeof (call_pool_t),
                          cli_mt_call_pool_t);
        if (!pool)
                return -1;

        /* frame_mem_pool size 112 * 64 */
        pool->frame_mem_pool = mem_pool_new (call_frame_t, 32);
        if (!pool->frame_mem_pool)
                return -1;

        /* stack_mem_pool size 256 * 128 */
        pool->stack_mem_pool = mem_pool_new (call_stack_t, 16);

        if (!pool->stack_mem_pool)
                return -1;

        ctx->stub_mem_pool = mem_pool_new (call_stub_t, 16);
        if (!ctx->stub_mem_pool)
                return -1;

        ctx->dict_pool = mem_pool_new (dict_t, 32);
        if (!ctx->dict_pool)
                return -1;

        ctx->dict_pair_pool = mem_pool_new (data_pair_t, 512);
        if (!ctx->dict_pair_pool)
                return -1;

        ctx->dict_data_pool = mem_pool_new (data_t, 512);
        if (!ctx->dict_data_pool)
                return -1;

        ctx->logbuf_pool = mem_pool_new (log_buf_t, 256);
        if (!ctx->logbuf_pool)
                return -1;

        INIT_LIST_HEAD (&pool->all_frames);
        LOCK_INIT (&pool->lock);
        ctx->pool = pool;

        cmd_args = &ctx->cmd_args;

        INIT_LIST_HEAD (&cmd_args->xlator_options);

        lim.rlim_cur = RLIM_INFINITY;
        lim.rlim_max = RLIM_INFINITY;
        setrlimit (RLIMIT_CORE, &lim);

        return 0;
}


static int
logging_init (glusterfs_ctx_t *ctx, struct cli_state *state)
{
        char *log_file = state->log_file ? state->log_file :
                         DEFAULT_CLI_LOG_FILE_DIRECTORY "/cli.log";

        /* passing ident as NULL means to use default ident for syslog */
        if (gf_log_init (ctx, log_file, NULL) == -1) {
                fprintf (stderr, "ERROR: failed to open logfile %s\n",
                         log_file);
                return -1;
        }

        /* CLI should not have something to DEBUG after the release,
           hence defaulting to INFO loglevel */
        gf_log_set_loglevel ((state->log_level == GF_LOG_NONE) ? GF_LOG_INFO :
                             state->log_level);

        return 0;
}

int
cli_submit_request (struct rpc_clnt *rpc, void *req, call_frame_t *frame,
                    rpc_clnt_prog_t *prog,
                    int procnum, struct iobref *iobref,
                    xlator_t *this, fop_cbk_fn_t cbkfn, xdrproc_t xdrproc)
{
        int                     ret         = -1;
        int                     count      = 0;
        struct iovec            iov         = {0, };
        struct iobuf            *iobuf = NULL;
        char                    new_iobref = 0;
        ssize_t                 xdr_size   = 0;

        GF_ASSERT (this);

        if (req) {
                xdr_size = xdr_sizeof (xdrproc, req);
                iobuf = iobuf_get2 (this->ctx->iobuf_pool, xdr_size);
                if (!iobuf) {
                        goto out;
                };

                if (!iobref) {
                        iobref = iobref_new ();
                        if (!iobref) {
                                goto out;
                        }

                        new_iobref = 1;
                }

                iobref_add (iobref, iobuf);

                iov.iov_base = iobuf->ptr;
                iov.iov_len  = iobuf_size (iobuf);


                /* Create the xdr payload */
                ret = xdr_serialize_generic (iov, req, xdrproc);
                if (ret == -1) {
                        goto out;
                }
                iov.iov_len = ret;
                count = 1;
        }

        if (!rpc)
                rpc = global_rpc;
        /* Send the msg */
        ret = rpc_clnt_submit (rpc, prog, procnum, cbkfn,
                               &iov, count,
                               NULL, 0, iobref, frame, NULL, 0, NULL, 0, NULL);
        ret = 0;

out:
        if (new_iobref)
                iobref_unref (iobref);
        if (iobuf)
                iobuf_unref (iobuf);
        return ret;
}

int
cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,
                void *data)
{
        xlator_t                *this = NULL;
        int                     ret = 0;

        this = mydata;

        switch (event) {
        case RPC_CLNT_CONNECT:
        {

                cli_cmd_broadcast_connected ();
                gf_log (this->name, GF_LOG_TRACE, "got RPC_CLNT_CONNECT");
               break;
        }

        case RPC_CLNT_DISCONNECT:
        {
                gf_log (this->name, GF_LOG_TRACE, "got RPC_CLNT_DISCONNECT");
                connected = 0;
                if (!global_state->prompt && global_state->await_connected) {
                        ret = 1;
                        cli_out ("Connection failed. Please check if gluster "
                                  "daemon is operational.");
                        exit (ret);
                }
                break;
        }

        default:
                gf_log (this->name, GF_LOG_TRACE,
                        "got some other RPC event %d", event);
                ret = 0;
                break;
        }

        return ret;
}

static gf_boolean_t
is_valid_int (char *str)
{
        if (*str == '-')
                ++str;

        /* Handle empty string or just "-".*/
        if (!*str)
                return _gf_false;

        /* Check for non-digit chars in the rest of the string */
        while (*str) {
                if (!isdigit(*str))
                        return _gf_false;
        else
                ++str;
        }
        return _gf_true;
}

/*
 * ret: 0: option successfully processed
 *      1: signalling end of option list
 *     -1: unknown option
 *     -2: parsing issue (avoid unknown option error)
 */
int
cli_opt_parse (char *opt, struct cli_state *state)
{
        char            *oarg           = NULL;
        gf_boolean_t    secure_mgmt_tmp = 0;

        if (strcmp (opt, "") == 0)
                return 1;

        if (strcmp (opt, "version") == 0) {
                cli_out ("%s", argp_program_version);
                exit (0);
        }

        if (strcmp (opt, "print-logdir") == 0) {
                cli_out ("%s", DEFAULT_LOG_FILE_DIRECTORY);
                exit (0);
        }

        if (strcmp (opt, "print-statedumpdir") == 0) {
                cli_out ("%s", DEFAULT_VAR_RUN_DIRECTORY);
                exit (0);
        }

        if (strcmp (opt, "xml") == 0) {
#if (HAVE_LIB_XML)
                state->mode |= GLUSTER_MODE_XML;
#else
                cli_err ("XML output not supported. Ignoring '--xml' option");
#endif
                return 0;
        }

        if (strcmp (opt, "wignore") == 0) {
                state->mode |= GLUSTER_MODE_WIGNORE;
                return 0;
        }

        oarg = strtail (opt, "mode=");
        if (oarg) {
                if (strcmp (oarg, "script") == 0) {
                        state->mode |= GLUSTER_MODE_SCRIPT;
                        return 0;
                }

                if (strcmp (oarg, "interactive") == 0)
                        return 0;

                return -1;
        }

        oarg = strtail (opt, "remote-host=");
        if (oarg) {
                state->remote_host = oarg;
                return 0;
        }

        oarg = strtail (opt, "log-file=");
        if (oarg) {
                state->log_file = oarg;
                return 0;
        }
        oarg = strtail (opt, "timeout=");
        if (oarg) {
                if (!is_valid_int (oarg) || atoi(oarg) <= 0) {
                        cli_err ("timeout value should be a postive integer");
                        return -2; /* -2 instead of -1 to avoid unknown option
                                      error */
                }
                cli_default_conn_timeout = atoi(oarg);
                return 0;
        }

        oarg = strtail (opt, "log-level=");
        if (oarg) {
                int log_level = glusterd_check_log_level(oarg);
                if (log_level == -1)
                        return -1;
                state->log_level = (gf_loglevel_t) log_level;
                return 0;
        }

        oarg = strtail (opt, "glusterd-sock=");
        if (oarg) {
                state->glusterd_sock = oarg;
                return 0;
        }

        oarg = strtail (opt, "secure-mgmt=");
        if (oarg) {
                if (gf_string2boolean(oarg,&secure_mgmt_tmp) == 0) {
                        if (secure_mgmt_tmp) {
                                /* See declaration for why this is an int. */
                                state->ctx->secure_mgmt = 1;
                        }
                }
                else {
                        cli_err ("invalide secure-mgmt value (ignored)");
                }
                return 0;
        }

        return -1;
}

int
parse_cmdline (int argc, char *argv[], struct cli_state *state)
{
        int                 ret            = 0;
        int                 i              = 0;
        int                 j              = 0;
        char               *opt            = NULL;
        gf_boolean_t       geo_rep_config  = _gf_false;

        state->argc=argc-1;
        state->argv=&argv[1];

        /* Do this first so that an option can override. */
        if (sys_access (SECURE_ACCESS_FILE, F_OK) == 0) {
                state->ctx->secure_mgmt = 1;
        }

        if (state->argc > GEO_REP_CMD_CONFIG_INDEX &&
            strtail (state->argv[GEO_REP_CMD_INDEX], "geo") &&
            strtail (state->argv[GEO_REP_CMD_CONFIG_INDEX], "co"))
                geo_rep_config = _gf_true;

        for (i = 0; i < state->argc; i++) {
                opt = strtail (state->argv[i], "--");
                if (opt && !geo_rep_config) {
                        ret = cli_opt_parse (opt, state);
                        if (ret == -1) {
                                cli_out ("unrecognized option --%s", opt);
                                return ret;
                        } else if (ret == -2) {
                                return ret;
                        }
                        for (j = i; j < state->argc - 1; j++)
                                state->argv[j] = state->argv[j + 1];
                        state->argc--;
                        /* argv shifted, next check should be at i again */
                        i--;
                        if (ret == 1) {
                                /* end of cli options */
                                ret = 0;
                                break;
                        }
                }
        }

        state->argv[state->argc] = NULL;

        return ret;
}


int
cli_cmd_tree_init (struct cli_cmd_tree *tree)
{
        struct cli_cmd_word  *root = NULL;
        int                   ret = 0;

        root = &tree->root;
        root->tree = tree;

        return ret;
}


int
cli_state_init (struct cli_state *state)
{
        struct cli_cmd_tree  *tree = NULL;
        int                   ret = 0;


        state->log_level = GF_LOG_NONE;

        tree = &state->tree;
        tree->state = state;

        ret = cli_cmd_tree_init (tree);

        return ret;
}

int
cli_usage_out (const char *usage)
{
        GF_ASSERT (usage);
        GF_ASSERT (usage[0] != '\0');

        if (!usage || usage[0] == '\0')
                return -1;

        cli_err ("Usage: %s", usage);
        return 0;
}

int
_cli_err (const char *fmt, ...)
{
        va_list           ap;
        int               ret = 0;
#ifdef HAVE_READLINE
        struct cli_state *state = global_state;
#endif

        va_start (ap, fmt);

#ifdef HAVE_READLINE
        if (state->rl_enabled && !state->rl_processing) {
                va_end (ap);
                return cli_rl_err (state, fmt, ap);
        }
#endif

        ret = vfprintf (stderr, fmt, ap);
        fprintf (stderr, "\n");
        va_end (ap);

        return ret;
}


int
_cli_out (const char *fmt, ...)
{
        va_list           ap;
        int               ret = 0;
#ifdef HAVE_READLINE
        struct cli_state *state = global_state;
#endif

        va_start (ap, fmt);
#ifdef HAVE_READLINE
        if (state->rl_enabled && !state->rl_processing) {
                va_end (ap);
                return cli_rl_out (state, fmt, ap);
        }
#endif

        ret = vprintf (fmt, ap);
        printf ("\n");
        va_end (ap);

        return ret;
}

struct rpc_clnt *
cli_quotad_clnt_rpc_init (void)
{
        struct rpc_clnt *rpc = NULL;
        dict_t          *rpc_opts = NULL;
        int             ret = -1;

        rpc_opts = dict_new ();
        if (!rpc_opts) {
                        ret = -1;
                        goto out;
                }

        ret = dict_set_str (rpc_opts, "transport.address-family", "unix");
        if (ret)
                goto out;

        ret = dict_set_str (rpc_opts, "transport-type", "socket");
        if (ret)
                goto out;

        ret = dict_set_str (rpc_opts, "transport.socket.connect-path",