[libvirt] main: fix some compilation issues on non-linux platforms

Stefan Berger stefanb at linux.vnet.ibm.com
Tue Aug 16 19:01:59 UTC 2011


On 08/16/2011 02:34 PM, Stefan Berger wrote:
> On 08/16/2011 02:14 PM, Eric Blake wrote:
>> On 08/16/2011 12:07 PM, Stefan Berger wrote:
>>> I split this off and pushed the rest.
>>> If procfs's are different, then maybe we should use #if
>>> PROCFS_PID_EXE_LINK_AVAIL here.
>>
>> Seems like a reasonable name.
>>
>>> What is needed in this case seems to be that /proc/<pid>/exe is a
>>> symbolic link to the executable. This works in Linux and cygwin. Would
>>> testing via
>>>
>>> stat /proc/<pid>/exe | sed -n 's/.*\(symbolic link\).*/\1/p'
>>>
>>> be a valid test for this that works on all targeted platforms?
>>
>> stat(1) is not universal, and while it is on both Linux and Cygwin, 
>> it might not be present on some other platform that has a procfs that 
>> does what we want.  It may be sufficient to just use 'test -h 
>> /proc/pid/exe' to see if there is a symlink.
>>
>> Also, you'd need to wrap the test in a cache variable (to allow 
>> overrides if we guessed wrong), as well as to skip the test (and 
>> assume the worst or at least make some default guesses based on known 
>> $host_os) when cross-compiling, since the existence of procfs on the 
>> host machine says nothing about it being on the target machine.
>>
> Following the latter couldn't it just be handled during runtime 
> altogether?
>
>     Stefan
>
Along those lines I propose this patch below:

Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>

---
  src/libvirt_private.syms |    1 +
  src/util/util.c          |   17 +++++++++++++++++
  src/util/util.h          |    2 ++
  src/util/virpidfile.c    |    7 ++++---
  4 files changed, 24 insertions(+), 3 deletions(-)

Index: libvirt-acl/src/util/virpidfile.c
===================================================================
--- libvirt-acl.orig/src/util/virpidfile.c
+++ libvirt-acl/src/util/virpidfile.c
@@ -210,10 +210,11 @@ int virPidFileReadPathIfAlive(const char
          *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;
Index: libvirt-acl/src/util/util.c
===================================================================
--- libvirt-acl.orig/src/util/util.c
+++ libvirt-acl/src/util/util.c
@@ -570,6 +570,23 @@ int virFileResolveLink(const char *linkp
      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);
+}
+
+
  /*
   * Finds a requested executable file in the PATH env. e.g.:
   * "kvm-img" will return "/usr/bin/kvm-img"
Index: libvirt-acl/src/util/util.h
===================================================================
--- libvirt-acl.orig/src/util/util.h
+++ libvirt-acl/src/util/util.h
@@ -78,6 +78,8 @@ int virFileLinkPointsTo(const char *chec
  int virFileResolveLink(const char *linkpath,
                         char **resultpath) ATTRIBUTE_RETURN_CHECK;

+int virFileIsLink(const char *linkpath) ATTRIBUTE_RETURN_CHECK;
+
  char *virFindFileInPath(const char *file);

  bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
Index: libvirt-acl/src/libvirt_private.syms
===================================================================
--- libvirt-acl.orig/src/libvirt_private.syms
+++ libvirt-acl/src/libvirt_private.syms
@@ -1047,6 +1047,7 @@ virFileExists;
  virFileFindMountPoint;
  virFileHasSuffix;
  virFileIsExecutable;
+virFileIsLink;
  virFileLinkPointsTo;
  virFileLock;
  virFileMakePath;




More information about the libvir-list mailing list