diff options
author | Anand Avati <avati@redhat.com> | 2012-08-20 10:48:16 -0700 |
---|---|---|
committer | Anand Avati <avati@redhat.com> | 2012-09-21 20:43:05 -0700 |
commit | 373b25827f0250d11461fbe76dd6a0e295069171 (patch) | |
tree | 5f175e9cafdba5346d7a19515d67e659339a3cee | |
parent | a6234eeb2a0fb106b801a3241ce7538fd5562ff6 (diff) |
core: enable process to return the appropriate error code
Setup a pipe() in glusterfs_ctx_t to communicate with the fork'ed
child the exit status and return it to the shell.
Change-Id: Ibbaa581d45acb24d5141e43ae848cae29312d95f
BUG: 762935
Signed-off-by: Anand Avati <avati@redhat.com>
Reviewed-on: http://review.gluster.org/3836
Tested-by: Gluster Build System <jenkins@build.gluster.com>
Reviewed-by: Amar Tumballi <amarts@redhat.com>
-rw-r--r-- | glusterfsd/src/glusterfsd-mgmt.c | 36 | ||||
-rw-r--r-- | glusterfsd/src/glusterfsd.c | 43 | ||||
-rw-r--r-- | libglusterfs/src/ctx.c | 3 | ||||
-rw-r--r-- | libglusterfs/src/glusterfs.h | 1 |
4 files changed, 70 insertions, 13 deletions
diff --git a/glusterfsd/src/glusterfsd-mgmt.c b/glusterfsd/src/glusterfsd-mgmt.c index 8089e5804d6..cd72d32ca7a 100644 --- a/glusterfsd/src/glusterfsd-mgmt.c +++ b/glusterfsd/src/glusterfsd-mgmt.c @@ -45,6 +45,7 @@ int glusterfs_mgmt_pmap_signin (glusterfs_ctx_t *ctx); int glusterfs_volfile_fetch (glusterfs_ctx_t *ctx); int glusterfs_process_volfp (glusterfs_ctx_t *ctx, FILE *fp); int glusterfs_graph_unknown_options (glusterfs_graph_t *graph); +int emancipate(glusterfs_ctx_t *ctx, int ret); int mgmt_cbk_spec (struct rpc_clnt *rpc, void *mydata, void *data) @@ -1531,7 +1532,7 @@ mgmt_getspec_cbk (struct rpc_req *req, struct iovec *iov, int count, if (-1 == rsp.op_ret) { gf_log (frame->this->name, GF_LOG_ERROR, "failed to get the 'volume file' from server"); - ret = -1; + ret = rsp.op_errno; goto out; } @@ -1597,6 +1598,8 @@ out: free (rsp.spec); + emancipate (ctx, ret); + if (ret && ctx && !ctx->active) { /* Do it only for the first time */ /* Failed to get the volume file, something wrong, @@ -1754,6 +1757,8 @@ mgmt_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, cmd_args_t *cmd_args = NULL; glusterfs_ctx_t *ctx = NULL; int ret = 0; + int need_term = 0; + int emval = 0; this = mydata; ctx = this->ctx; @@ -1768,30 +1773,41 @@ mgmt_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, gf_log ("glusterfsd-mgmt", GF_LOG_INFO, "%d connect attempts left", cmd_args->max_connect_attempts); - if (0 >= cmd_args->max_connect_attempts) - cleanup_and_exit (1); + if (0 >= cmd_args->max_connect_attempts) { + need_term = 1; + emval = ENOTCONN; + } } break; case RPC_CLNT_CONNECT: rpc_clnt_set_connected (&((struct rpc_clnt*)ctx->mgmt)->conn); ret = glusterfs_volfile_fetch (ctx); - if (ret && ctx && (ctx->active == NULL)) { - /* Do it only for the first time */ - /* Exit the process.. there is some wrong options */ - gf_log ("mgmt", GF_LOG_ERROR, - "failed to fetch volume file (key:%s)", - ctx->cmd_args.volfile_id); - cleanup_and_exit (0); + if (ret) { + emval = ret; + if (!ctx->active) { + need_term = 1; + gf_log ("glusterfsd-mgmt", GF_LOG_ERROR, + "failed to fetch volume file (key:%s)", + ctx->cmd_args.volfile_id); + break; + + } } if (is_mgmt_rpc_reconnect) glusterfs_mgmt_pmap_signin (ctx); + break; default: break; } + if (need_term) { + emancipate (ctx, emval); + cleanup_and_exit (1); + } + return 0; } diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c index 919c048f9b0..1015a130505 100644 --- a/glusterfsd/src/glusterfsd.c +++ b/glusterfsd/src/glusterfsd.c @@ -516,7 +516,7 @@ err: xlator_destroy (master); } - return -1; + return 1; } @@ -1041,6 +1041,16 @@ reincarnate (int signum) return; } +void +emancipate (glusterfs_ctx_t *ctx, int ret) +{ + /* break free from the parent */ + if (ctx->daemon_pipe[1] != -1) { + write (ctx->daemon_pipe[1], (void *) &ret, sizeof (ret)); + close (ctx->daemon_pipe[1]); + ctx->daemon_pipe[1] = -1; + } +} static uint8_t gf_get_process_mode (char *exec_name) @@ -1642,6 +1652,7 @@ daemonize (glusterfs_ctx_t *ctx) int ret = -1; cmd_args_t *cmd_args = NULL; int cstatus = 0; + int err = 0; cmd_args = &ctx->cmd_args; @@ -1655,15 +1666,36 @@ daemonize (glusterfs_ctx_t *ctx) if (cmd_args->debug_mode) goto postfork; + ret = pipe (ctx->daemon_pipe); + if (ret) { + /* If pipe() fails, retain daemon_pipe[] = {-1, -1} + and parent will just not wait for child status + */ + ctx->daemon_pipe[0] = -1; + ctx->daemon_pipe[1] = -1; + } + ret = os_daemon_return (0, 0); switch (ret) { case -1: + if (ctx->daemon_pipe[0] != -1) { + close (ctx->daemon_pipe[0]); + close (ctx->daemon_pipe[1]); + } + gf_log ("daemonize", GF_LOG_ERROR, "Daemonization failed: %s", strerror(errno)); goto out; case 0: + /* child */ + /* close read */ + close (ctx->daemon_pipe[0]); break; default: + /* parent */ + /* close write */ + close (ctx->daemon_pipe[1]); + if (ctx->mnt_pid > 0) { ret = waitpid (ctx->mnt_pid, &cstatus, 0); if (!(ret == ctx->mnt_pid && cstatus == 0)) { @@ -1672,7 +1704,10 @@ daemonize (glusterfs_ctx_t *ctx) exit (1); } } - _exit (0); + + err = 1; + read (ctx->daemon_pipe[0], (void *)&err, sizeof (err)); + _exit (err); } postfork: @@ -1754,7 +1789,8 @@ glusterfs_volumes_init (glusterfs_ctx_t *ctx) if (cmd_args->volfile_server) { ret = glusterfs_mgmt_init (ctx); - goto out; + /* return, do not emancipate() yet */ + return ret; } fp = get_volfp (ctx); @@ -1771,6 +1807,7 @@ glusterfs_volumes_init (glusterfs_ctx_t *ctx) goto out; out: + emancipate (ctx, ret); return ret; } diff --git a/libglusterfs/src/ctx.c b/libglusterfs/src/ctx.c index 01489fbe0e4..0082601d466 100644 --- a/libglusterfs/src/ctx.c +++ b/libglusterfs/src/ctx.c @@ -34,6 +34,9 @@ glusterfs_ctx_new () INIT_LIST_HEAD (&ctx->graphs); INIT_LIST_HEAD (&ctx->mempool_list); + ctx->daemon_pipe[0] = -1; + ctx->daemon_pipe[1] = -1; + ret = pthread_mutex_init (&ctx->lock, NULL); if (ret) { free (ctx); diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h index 57bc7998ff2..b9c2b603e70 100644 --- a/libglusterfs/src/glusterfs.h +++ b/libglusterfs/src/glusterfs.h @@ -393,6 +393,7 @@ struct _glusterfs_ctx { int mem_acct_enable; + int daemon_pipe[2]; }; typedef struct _glusterfs_ctx glusterfs_ctx_t; |