50dc83
From 462e3988936761317975fd811dd355b81328b60a Mon Sep 17 00:00:00 2001
50dc83
From: Amar Tumballi <amarts@redhat.com>
50dc83
Date: Thu, 14 Mar 2019 10:04:28 +0530
50dc83
Subject: [PATCH 185/192] gfapi: provide an api for setting statedump path
50dc83
50dc83
Currently for an application using glfsapi to use glusterfs, when a
50dc83
statedump is taken, it uses /var/run/gluster dir to dump info.
50dc83
50dc83
There can be concerns as this directory may be owned by some other
50dc83
user, and hence it may fail taking statedump. Such applications
50dc83
should have an option to use different path.
50dc83
50dc83
This patch provides an API to do so.
50dc83
50dc83
Upstream details:
50dc83
> Updates: bz#1689097
50dc83
> Change-Id: I8918e002bc823d83614c972b6c738baa04681b23
50dc83
> URL: https://review.gluster.org/22364
50dc83
50dc83
BUG: 1720461
50dc83
Change-Id: I6079c8d799f35eaf76e62d259b51573bf561ba5b
50dc83
Signed-off-by: Amar Tumballi <amarts@redhat.com>
50dc83
Reviewed-on: https://code.engineering.redhat.com/gerrit/173451
50dc83
Tested-by: RHGS Build Bot <nigelb@redhat.com>
50dc83
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
50dc83
---
50dc83
 api/src/gfapi.aliases |  2 ++
50dc83
 api/src/gfapi.map     |  5 ++++
50dc83
 api/src/glfs.c        | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
50dc83
 api/src/glfs.h        | 28 +++++++++++++++++++++++
50dc83
 4 files changed, 98 insertions(+)
50dc83
50dc83
diff --git a/api/src/gfapi.aliases b/api/src/gfapi.aliases
50dc83
index 09c0fd8..8fdf734 100644
50dc83
--- a/api/src/gfapi.aliases
50dc83
+++ b/api/src/gfapi.aliases
50dc83
@@ -195,3 +195,5 @@ _pub_glfs_zerofill_async _glfs_zerofill_async$GFAPI_6.0
50dc83
 _pub_glfs_copy_file_range _glfs_copy_file_range$GFAPI_6.0
50dc83
 _pub_glfs_fsetattr _glfs_fsetattr$GFAPI_6.0
50dc83
 _pub_glfs_setattr _glfs_setattr$GFAPI_6.0
50dc83
+
50dc83
+_pub_glfs_set_statedump_path _glfs_set_statedump_path@GFAPI_future
50dc83
diff --git a/api/src/gfapi.map b/api/src/gfapi.map
50dc83
index b97a614..cf118e8 100644
50dc83
--- a/api/src/gfapi.map
50dc83
+++ b/api/src/gfapi.map
50dc83
@@ -271,3 +271,8 @@ GFAPI_PRIVATE_6.1 {
50dc83
 	global:
50dc83
 		glfs_setfspid;
50dc83
 } GFAPI_6.0;
50dc83
+
50dc83
+GFAPI_future {
50dc83
+	global:
50dc83
+		glfs_set_statedump_path;
50dc83
+} GFAPI_PRIVATE_6.1;
50dc83
diff --git a/api/src/glfs.c b/api/src/glfs.c
50dc83
index f4a8e08..ba513e6 100644
50dc83
--- a/api/src/glfs.c
50dc83
+++ b/api/src/glfs.c
50dc83
@@ -1212,6 +1212,7 @@ glusterfs_ctx_destroy(glusterfs_ctx_t *ctx)
50dc83
         glusterfs_graph_destroy_residual(trav_graph);
50dc83
     }
50dc83
 
50dc83
+    GF_FREE(ctx->statedump_path);
50dc83
     FREE(ctx);
50dc83
 
50dc83
     return ret;
50dc83
@@ -1738,3 +1739,65 @@ invalid_fs:
50dc83
 }
50dc83
 
50dc83
 GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_upcall_unregister, 3.13.0);
50dc83
+
50dc83
+int
50dc83
+pub_glfs_set_statedump_path(struct glfs *fs, const char *path)
50dc83
+{
50dc83
+    struct stat st;
50dc83
+    int ret;
50dc83
+    DECLARE_OLD_THIS;
50dc83
+    __GLFS_ENTRY_VALIDATE_FS(fs, invalid_fs);
50dc83
+
50dc83
+    if (!path) {
50dc83
+        gf_log("glfs", GF_LOG_ERROR, "path is NULL");
50dc83
+        errno = EINVAL;
50dc83
+        goto err;
50dc83
+    }
50dc83
+
50dc83
+    /* If path is not present OR, if it is directory AND has enough permission
50dc83
+     * to create files, then proceed */
50dc83
+    ret = sys_stat(path, &st);
50dc83
+    if (ret && errno != ENOENT) {
50dc83
+        gf_log("glfs", GF_LOG_ERROR, "%s: not a valid path (%s)", path,
50dc83
+               strerror(errno));
50dc83
+        errno = EINVAL;
50dc83
+        goto err;
50dc83
+    }
50dc83
+
50dc83
+    if (!ret) {
50dc83
+        /* file is present, now check other things */
50dc83
+        if (!S_ISDIR(st.st_mode)) {
50dc83
+            gf_log("glfs", GF_LOG_ERROR, "%s: path is not directory", path);
50dc83
+            errno = EINVAL;
50dc83
+            goto err;
50dc83
+        }
50dc83
+        if (sys_access(path, W_OK | X_OK) < 0) {
50dc83
+            gf_log("glfs", GF_LOG_ERROR,
50dc83
+                   "%s: path doesn't have write permission", path);
50dc83
+            errno = EPERM;
50dc83
+            goto err;
50dc83
+        }
50dc83
+    }
50dc83
+
50dc83
+    /* If set, it needs to be freed, so we don't have leak */
50dc83
+    GF_FREE(fs->ctx->statedump_path);
50dc83
+
50dc83
+    fs->ctx->statedump_path = gf_strdup(path);
50dc83
+    if (!fs->ctx->statedump_path) {
50dc83
+        gf_log("glfs", GF_LOG_ERROR,
50dc83
+               "%s: failed to set statedump path, no memory", path);
50dc83
+        errno = ENOMEM;
50dc83
+        goto err;
50dc83
+    }
50dc83
+
50dc83
+    __GLFS_EXIT_FS;
50dc83
+
50dc83
+    return 0;
50dc83
+err:
50dc83
+    __GLFS_EXIT_FS;
50dc83
+
50dc83
+invalid_fs:
50dc83
+    return -1;
50dc83
+}
50dc83
+
50dc83
+GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_set_statedump_path, future);
50dc83
diff --git a/api/src/glfs.h b/api/src/glfs.h
50dc83
index 6714782..a6c12e1 100644
50dc83
--- a/api/src/glfs.h
50dc83
+++ b/api/src/glfs.h
50dc83
@@ -1453,5 +1453,33 @@ int
50dc83
 glfs_setattr(struct glfs *fs, const char *path, struct glfs_stat *stat,
50dc83
              int follow) __THROW GFAPI_PUBLIC(glfs_setattr, 6.0);
50dc83
 
50dc83
+/*
50dc83
+  SYNOPSIS
50dc83
+
50dc83
+  glfs_set_statedump_path: Function to set statedump path.
50dc83
+
50dc83
+  DESCRIPTION
50dc83
+
50dc83
+  This function is used to set statedump directory
50dc83
+
50dc83
+  PARAMETERS
50dc83
+
50dc83
+  @fs: The 'virtual mount' object to be configured with the volume
50dc83
+       specification file.
50dc83
+
50dc83
+  @path: statedump path. Should be a directory. But the API won't fail if the
50dc83
+  directory doesn't exist yet, as one may create it later.
50dc83
+
50dc83
+  RETURN VALUES
50dc83
+
50dc83
+   0 : Success.
50dc83
+  -1 : Failure. @errno will be set with the type of failure.
50dc83
+
50dc83
+ */
50dc83
+
50dc83
+int
50dc83
+glfs_set_statedump_path(struct glfs *fs, const char *path) __THROW
50dc83
+    GFAPI_PUBLIC(glfs_set_statedump_path, future);
50dc83
+
50dc83
 __END_DECLS
50dc83
 #endif /* !_GLFS_H */
50dc83
-- 
50dc83
1.8.3.1
50dc83