diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 39d3dc4c6a3f12bd6d01a07a2bf116ca67739743..acae122bc2668de3513d60caa88e212f557d2741 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1047,6 +1047,7 @@ virFileExists; virFileFindMountPoint; virFileHasSuffix; virFileIsExecutable; +virFileIsLink; virFileLinkPointsTo; virFileLock; virFileMakePath; diff --git a/src/util/util.c b/src/util/util.c index e3b216f8aa3b1717cda45099f8e496e746b8b1b8..b278165a3275cb454190af1eaf1d808845358a62 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -570,6 +570,23 @@ int virFileResolveLink(const char *linkpath, return *resultpath == NULL ? -1 : 0; } + +/* + * Check whether the given file is a link. + * Returns 1 in case of the file being a link, 0 in case it is not + * a link and the negative errno in all other cases. + */ +int virFileIsLink(const char *linkpath) +{ + struct stat st; + + if (lstat(linkpath, &st) < 0) + return -errno; + + return (S_ISLNK(st.st_mode) != 0); +} + + /* * Finds a requested executable file in the PATH env. e.g.: * "kvm-img" will return "/usr/bin/kvm-img" diff --git a/src/util/util.h b/src/util/util.h index e76da9cd183cd7d4ee5a440a261b5079571cd2f0..6e6265f5d9ea0cec7142dee362f88313cb94aa7d 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -78,6 +78,9 @@ int virFileLinkPointsTo(const char *checkLink, int virFileResolveLink(const char *linkpath, char **resultpath) ATTRIBUTE_RETURN_CHECK; +int virFileIsLink(const char *linkpath) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; + char *virFindFileInPath(const char *file); bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1); diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c index 7dd3c51ad96c83413c2f18b3ba7c4598a2f1aae6..e64b0b3139b13969fa4c3654f93f949c14ea88d4 100644 --- a/src/util/virpidfile.c +++ b/src/util/virpidfile.c @@ -210,10 +210,11 @@ int virPidFileReadPathIfAlive(const char *path, *pid = -1; return 0; } -#ifdef __linux__ - if (virFileLinkPointsTo(procpath, binpath) == 0) + + if (virFileIsLink(procpath) && + virFileLinkPointsTo(procpath, binpath) == 0) *pid = -1; -#endif + VIR_FREE(procpath); return 0;