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