From 1e652740f74298637577e9861cb5092091404d29 Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Mon, 31 Oct 2011 15:54:52 +0530 Subject: glusterd: Extended glusterd_is_service_running to get svcs's pid. Also, volume status cmd would print "N/A" if pid couldn't be retrieved from pidfile. Change-Id: Ie83d228b1cf86397d181885b325e337a403e6ed2 BUG: 3043 Reviewed-on: http://review.gluster.com/650 Tested-by: Gluster Build System Reviewed-by: Amar Tumballi Reviewed-by: Vijay Bellur --- cli/src/cli-cmd-volume.c | 4 +- cli/src/cli-rpc-ops.c | 16 +++++++- cli/src/cli.h | 2 +- xlators/mgmt/glusterd/src/glusterd-utils.c | 61 ++++++++---------------------- xlators/mgmt/glusterd/src/glusterd-utils.h | 3 ++ 5 files changed, 35 insertions(+), 51 deletions(-) diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index 18d17b454..b5a177a86 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -1376,7 +1376,7 @@ cli_cmd_volume_status_cbk (struct cli_state *state, int -cli_print_brick_status (char *brick, int port, int online, int pid) +cli_print_brick_status (char *brick, int port, int online, char *pid) { int fieldlen = CLI_VOL_STATUS_BRICK_LEN; char buf[80] = {0,}; @@ -1400,7 +1400,7 @@ cli_print_brick_status (char *brick, int port, int online, int pid) printf ("%s", p); while (num_tabs-- != 0) printf ("\t"); - cli_out ("%d\t%c\t%d", port, online?'Y':'N', pid); + cli_out ("%d\t%c\t%s", port, online?'Y':'N', pid); bricklen = 0; } } diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c index 2841a32d5..8a88e57d5 100644 --- a/cli/src/cli-rpc-ops.c +++ b/cli/src/cli-rpc-ops.c @@ -3807,7 +3807,8 @@ gf_cli3_1_status_cbk (struct rpc_req *req, struct iovec *iov, int port = 0; int online = 0; char key[1024] = {0,}; - int pid = 0; + int pid = -1; + char *pid_str = NULL; char brick[8192] = {0,}; char *volname = NULL; @@ -3875,11 +3876,22 @@ gf_cli3_1_status_cbk (struct rpc_req *req, struct iovec *iov, memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "brick%d.pid", i); ret = dict_get_int32 (dict, key, &pid); + if (ret) + goto out; + if (pid == -1) + ret = gf_asprintf (&pid_str, "%s", "N/A"); + else + ret = gf_asprintf (&pid_str, "%d", pid); + + if (ret == -1) + goto out; snprintf (brick, sizeof (brick) -1, "%s:%s", hostname, path); cli_print_line (CLI_BRICK_STATUS_LINE_LEN); - cli_print_brick_status (brick, port, online, pid); + cli_print_brick_status (brick, port, online, pid_str); + if (pid_str) + GF_FREE (pid_str); } ret = rsp.op_ret; diff --git a/cli/src/cli.h b/cli/src/cli.h index 70310f103..1d2e06973 100644 --- a/cli/src/cli.h +++ b/cli/src/cli.h @@ -236,7 +236,7 @@ cli_cmd_volume_status_parse (const char **words, int wordcount, dict_t **options); int -cli_print_brick_status (char *brick, int port, int online, int pid); +cli_print_brick_status (char *brick, int port, int online, char *pid); void cli_print_line (int len); diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c index 1e797a882..2eaf4f4a2 100644 --- a/xlators/mgmt/glusterd/src/glusterd-utils.c +++ b/xlators/mgmt/glusterd/src/glusterd-utils.c @@ -2308,12 +2308,13 @@ out: return ret; } +/* Valid only in if service is 'local' to glusterd. + * pid can be -1, if reading pidfile failed */ gf_boolean_t -glusterd_is_service_running (char *pidfile) +glusterd_is_service_running (char *pidfile, int *pid) { FILE *file = NULL; gf_boolean_t running = _gf_false; - gf_boolean_t locked = _gf_false; int ret = 0; int fno = 0; @@ -2322,20 +2323,20 @@ glusterd_is_service_running (char *pidfile) goto out; fno = fileno (file); - ret = lockf (fno, F_TLOCK, 0); - if (!ret) { - locked = _gf_true; + ret = lockf (fno, F_TEST, 0); + if (ret == -1) + running = _gf_true; + if (!pid) goto out; + + ret = fscanf (file, "%d", pid); + if (ret <= 0) { + gf_log ("", GF_LOG_ERROR, "Unable to read pidfile: %s, %s", + pidfile, strerror (errno)); + *pid = -1; } - running = _gf_true; out: - if (locked) { - GF_ASSERT (file); - if (lockf (fno, F_ULOCK, 0) < 0) - gf_log ("", GF_LOG_WARNING, "Cannot unlock pidfile: %s" - " reason: %s", pidfile, strerror(errno)); - } if (file) fclose (file); return running; @@ -2586,7 +2587,7 @@ glusterd_is_nodesvc_running (char *server) glusterd_get_nodesvc_pidfile (server, priv->workdir, pidfile, sizeof (pidfile)); - return glusterd_is_service_running (pidfile); + return glusterd_is_service_running (pidfile, NULL); } int32_t @@ -3094,8 +3095,6 @@ glusterd_add_brick_to_dict (glusterd_volinfo_t *volinfo, char base_key[8192] = {0}; char pidfile[PATH_MAX] = {0}; char path[PATH_MAX] = {0}; - FILE *file = NULL; - int fd = -1; int32_t pid = -1; int32_t brick_online = -1; xlator_t *this = NULL; @@ -3128,39 +3127,12 @@ glusterd_add_brick_to_dict (glusterd_volinfo_t *volinfo, if (ret) goto out; - GLUSTERD_GET_VOLUME_DIR (path, volinfo, priv); GLUSTERD_GET_BRICK_PIDFILE (pidfile, path, brickinfo->hostname, brickinfo->path); - file = fopen (pidfile, "r"); - if (!file) { - gf_log ("", GF_LOG_ERROR, "Unable to open pidfile: %s", - pidfile); - /* pidfile doesn't exist means brick is down*/ - pid = 0; - brick_online = 0; - goto cont; - } else { - ret = fscanf (file, "%d", &pid); - if (ret <= 0) { - gf_log ("", GF_LOG_ERROR, "Unable to read pidfile: %s", - pidfile); - ret = -1; - goto out; - } - - /* check if process is crashed*/ - fd = fileno (file); - if ((fd != -1) && (lockf (fd, F_TEST, 0))) - brick_online = 1; - else { - pid = 0; - brick_online = 0; - } - } + brick_online = glusterd_is_service_running (pidfile, &pid); -cont: memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "%s.pid", base_key); ret = dict_set_int32 (dict, key, pid); @@ -3174,9 +3146,6 @@ cont: goto out; out: - if (file) - fclose (file); - if (ret) gf_log ("", GF_LOG_DEBUG, "Returning %d", ret); diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.h b/xlators/mgmt/glusterd/src/glusterd-utils.h index 8401b61bc..a34956b0c 100644 --- a/xlators/mgmt/glusterd/src/glusterd-utils.h +++ b/xlators/mgmt/glusterd/src/glusterd-utils.h @@ -153,6 +153,9 @@ void glusterd_get_nodesvc_volfile (char *server, char *workdir, char *volfile, size_t len); +gf_boolean_t +glusterd_is_service_running (char *pidfile, int *pid); + gf_boolean_t glusterd_is_nodesvc_running (); -- cgit