From 42a2e7cff98f2d9b5569ad4f3b6671cfca0ba592 Mon Sep 17 00:00:00 2001 From: Bob Glossman Date: Thu, 8 Aug 2013 10:59:59 -0700 Subject: [PATCH] LU-3683 libcfs: improve validity test for valid file descriptors File descriptor members of struct lustre_kernelcomm aren't set to anything special at start time. This leads to incorrect tests and skipping valid fd closes at stop time. Fixed by defining, setting, and testing against a specific LK_NOFD value. Signed-off-by: Bob Glossman Change-Id: I9306ce380c5e049ef442b99ac20e51271deafbc5 Reviewed-on: http://review.whamcloud.com/7275 Tested-by: Hudson Reviewed-by: John L. Hammond Tested-by: Maloo Reviewed-by: Keith Mannthey Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/libcfs_kernelcomm.h | 1 + libcfs/libcfs/kernel_user_comm.c | 30 ++++++++++++++++++------------ lustre/utils/liblustreapi.c | 2 +- lustre/utils/liblustreapi_hsm.c | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/libcfs/include/libcfs/libcfs_kernelcomm.h b/libcfs/include/libcfs/libcfs_kernelcomm.h index f7be4b7..acf4779 100644 --- a/libcfs/include/libcfs/libcfs_kernelcomm.h +++ b/libcfs/include/libcfs/libcfs_kernelcomm.h @@ -97,6 +97,7 @@ extern int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func, void *cb_arg); #define LK_FLG_STOP 0x01 +#define LK_NOFD -1U /* kernelcomm control structure, passed from userspace to kernel */ typedef struct lustre_kernelcomm { diff --git a/libcfs/libcfs/kernel_user_comm.c b/libcfs/libcfs/kernel_user_comm.c index 4cac2e5..e819be0 100644 --- a/libcfs/libcfs/kernel_user_comm.c +++ b/libcfs/libcfs/kernel_user_comm.c @@ -54,24 +54,30 @@ */ int libcfs_ukuc_start(lustre_kernelcomm *link, int group) { - int pfd[2]; + int pfd[2]; - if (pipe(pfd) < 0) - return -errno; + link->lk_rfd = link->lk_wfd = LK_NOFD; - memset(link, 0, sizeof(*link)); - link->lk_rfd = pfd[0]; - link->lk_wfd = pfd[1]; - link->lk_group = group; - link->lk_uid = getpid(); - return 0; + if (pipe(pfd) < 0) + return -errno; + + memset(link, 0, sizeof(*link)); + link->lk_rfd = pfd[0]; + link->lk_wfd = pfd[1]; + link->lk_group = group; + link->lk_uid = getpid(); + return 0; } int libcfs_ukuc_stop(lustre_kernelcomm *link) { - if (link->lk_wfd > 0) - close(link->lk_wfd); - return close(link->lk_rfd); + int rc; + + if (link->lk_wfd != LK_NOFD) + close(link->lk_wfd); + rc = close(link->lk_rfd); + link->lk_rfd = link->lk_wfd = LK_NOFD; + return rc; } #define lhsz sizeof(*kuch) diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 05e9929..a02f344 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -3889,7 +3889,7 @@ int llapi_changelog_start(void **priv, int flags, const char *device, startrec, flags); /* Only the kernel reference keeps the write side open */ close(cp->kuc.lk_wfd); - cp->kuc.lk_wfd = 0; + cp->kuc.lk_wfd = LK_NOFD; if (rc < 0) { /* frees and clears priv */ llapi_changelog_fini(priv); diff --git a/lustre/utils/liblustreapi_hsm.c b/lustre/utils/liblustreapi_hsm.c index 72b64cc..751680d 100644 --- a/lustre/utils/liblustreapi_hsm.c +++ b/lustre/utils/liblustreapi_hsm.c @@ -155,7 +155,7 @@ int llapi_hsm_copytool_register(struct hsm_copytool_private **priv, /* Only the kernel reference keeps the write side open */ close(ct->kuc.lk_wfd); - ct->kuc.lk_wfd = 0; + ct->kuc.lk_wfd = LK_NOFD; if (rc < 0) goto out_kuc; -- 1.8.3.1