diff options
author | Csaba Henk <csaba@redhat.com> | 2017-05-23 08:27:06 +0200 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2017-09-07 10:09:50 +0000 |
commit | b2902477414e5801f00a94b72cf0d0395297aa47 (patch) | |
tree | 5bbd32c18f6b81aec15f58123ceb631d53d4614f /libglusterfs/src | |
parent | 84f8fb81d73b87463092eb082a5cc6a4055103f4 (diff) |
libglusterfs: fix run.c demo mode
run.c can be compiled into a standalone object file
or a demo program with the appropriate defines for
development purposes. This functionality was broken.
We fix it and also clean it up:
- call it "demo mode" not "test mode" as tests should
come with verification of the results of the invocation
which is not being done
- add comments to the source explaining the feature
- provide more comprehensive output in the demo program
This change does not affect standard compilation and
usage of run.c functionality.
BUG: 1454590
Change-Id: I1ea618e3262bf6a4d9f79f6b59209438d5163244
Signed-off-by: Csaba Henk <csaba@redhat.com>
Reviewed-on: https://review.gluster.org/17363
Smoke: Gluster Build System <jenkins@build.gluster.org>
Tested-by: Amar Tumballi <amarts@redhat.com>
NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
Reviewed-by: Amar Tumballi <amarts@redhat.com>
Diffstat (limited to 'libglusterfs/src')
-rw-r--r-- | libglusterfs/src/run.c | 97 |
1 files changed, 82 insertions, 15 deletions
diff --git a/libglusterfs/src/run.c b/libglusterfs/src/run.c index a7d98af502f..06a0a0a734f 100644 --- a/libglusterfs/src/run.c +++ b/libglusterfs/src/run.c @@ -25,7 +25,28 @@ #include <sys/wait.h> #include "syscall.h" -#ifdef RUN_STANDALONE +/* + * Following defines are available for helping development: + * RUN_STANDALONE and RUN_DO_DEMO. + * + * Compiling a standalone object file with no dependencies + * on glusterfs: + * $ cc -DRUN_STANDALONE -c run.c + * + * Compiling a demo progam that exercises bits of run.c + * functionality (linking to glusterfs): + * $ cc -DRUN_DO_DEMO -orun run.c `pkg-config --libs --cflags glusterfs-api` + * + * Compiling a demo progam that exercises bits of run.c + * functionality (with no dependence on glusterfs): + * + * $ cc -DRUN_DO_DEMO -DRUN_STANDALONE -orun run.c + */ +#if defined(RUN_STANDALONE ) || defined(RUN_DO_DEMO) +int close_fds_except (int *fdv, size_t count); +#define sys_read(f, b, c) read(f, b, c) +#define sys_write(f, b, c) write(f, b, c) +#define sys_close(f) close(f) #define GF_CALLOC(n, s, t) calloc(n, s) #define GF_ASSERT(cond) assert(cond) #define GF_REALLOC(p, s) realloc(p, s) @@ -33,17 +54,47 @@ #define gf_strdup(s) strdup(s) #define gf_vasprintf(p, f, va) vasprintf(p, f, va) #define gf_loglevel_t int -#define gf_log(dom, levl, fmt, args...) printf("LOG: " fmt "\n", ##args) +#define gf_msg_callingfn(dom, levl, errnum, msgid, fmt, args...) printf("LOG: " fmt "\n", ##args) #define LOG_DEBUG 0 +#ifdef RUN_STANDALONE +#include <stdbool.h> +#include <sys/resource.h> +int +close_fds_except (int *fdv, size_t count) +{ + int i = 0; + size_t j = 0; + bool should_close = true; + struct rlimit rl; + int ret = -1; + + ret = getrlimit (RLIMIT_NOFILE, &rl); + if (ret) + return ret; + + for (i = 0; i < rl.rlim_cur; i++) { + should_close = true; + for (j = 0; j < count; j++) { + if (i == fdv[j]) { + should_close = false; + break; + } + } + if (should_close) + sys_close (i); + } + return 0; +} +#endif #ifdef __linux__ #define GF_LINUX_HOST_OS #endif -#else /* ! RUN_STANDALONE */ +#else /* ! RUN_STANDALONE || RUN_DO_DEMO */ #include "glusterfs.h" #include "common-utils.h" +#include "libglusterfs-messages.h" #endif -#include "libglusterfs-messages.h" #include "run.h" void runinit (runner_t *runner) @@ -420,11 +471,11 @@ runcmd (const char *arg, ...) return runner_run (&runner); } -#ifdef RUN_DO_TESTS +#ifdef RUN_DO_DEMO static void TBANNER (const char *txt) { - printf("######\n### testing %s\n", txt); + printf("######\n### demoing %s\n", txt); } int @@ -438,15 +489,16 @@ main (int argc, char **argv) long pathmax = pathconf ("/", _PC_PATH_MAX); struct timeval tv = {0,}; struct timeval *tvp = NULL; + char *tfile; wdbuf = malloc (pathmax); assert (wdbuf); getcwd (wdbuf, pathmax); - TBANNER ("basic functionality"); + TBANNER ("basic functionality: running \"echo a b\""); runcmd ("echo", "a", "b", NULL); - TBANNER ("argv extension"); + TBANNER ("argv extension: running \"echo 1 2 ... 100\""); runcmd ("echo", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", @@ -458,7 +510,10 @@ main (int argc, char **argv) "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", NULL); - TBANNER ("add_args, argprintf, log, and popen-style functionality"); + TBANNER ("add_args, argprintf, log, and popen-style functionality:\n" + " running a multiline echo command, emit a log about it,\n" + " redirect it to a pipe, read output lines\n" + " and print them prefixed with \"got: \""); runinit (&runner); runner_add_args (&runner, "echo", "pid:", NULL); runner_argprintf (&runner, "%d\n", getpid()); @@ -471,26 +526,38 @@ main (int argc, char **argv) printf ("got: %s", buf); runner_end (&runner); - TBANNER ("execve error reporting"); + TBANNER ("execve error reporting: running a non-existent command"); ret = runcmd ("bafflavvitty", NULL); printf ("%d %d [%s]\n", ret, errno, strerror (errno)); - TBANNER ("output redirection"); - fd = mkstemp ("/tmp/foof"); + TBANNER ("output redirection: running \"echo foo\" redirected " + "to a temp file"); + tfile = strdup ("/tmp/foofXXXXXX"); + assert (tfile); + fd = mkstemp (tfile); assert (fd != -1); + printf ("redirecting to %s\n", tfile); runinit (&runner); runner_add_args (&runner, "echo", "foo", NULL); runner_redir (&runner, 1, fd); ret = runner_run (&runner); - printf ("%d", ret); + printf ("runner_run returned: %d", ret); if (ret != 0) - printf (" %d [%s]", errno, strerror (errno)); + printf (", with errno %d [%s]", errno, strerror (errno)); putchar ('\n'); + /* sleep for seconds given as argument (0 means forever) + * to allow investigation of post-execution state to + * cbeck for resource leaks (eg. zombies). + */ if (argc > 1) { tv.tv_sec = strtoul (argv[1], NULL, 10); - if (tv.tv_sec > 0) + printf ("### %s", "sleeping for"); + if (tv.tv_sec > 0) { + printf (" %d seconds\n", tv.tv_sec); tvp = &tv; + } else + printf ("%s\n", "ever"); select (0, 0, 0, 0, tvp); } |