[libvirt] [PATCH 0/4] Add HAP to domain features
by Jim Fehlig
Hardware features such as Extended Page Table and Nested Page
Table augment hypervisor software techniques such as shadow
page table. Adding HAP (hardware assisted paging) to the
virDomainFeature enumeration allows users to select between
hardware and software memory management mechanisms for their
guests. I've seen reports [1] of users asking for this
capability in the past.
Patch 3 is an attempt to advertise the feature in hvm guest
capabilities for the Xen hypervisor. I have not found a way
to discover the presence of HAP support through the xen tools.
Instead, the feature is only advertised on Xen >= 3.3, which
sadly won't accommodate distro backports to older Xen versions.
Thanks for your comments,
Jim
[1] http://www.mail-archive.com/fedora-xen@redhat.com/msg02680.html
Jim Fehlig (4):
Add HAP to virDomainFeature enum
Add support for HAP feature to xen drivers
Add HAP to xen hypervisor capabilities
Document HAP domain feature
docs/formatdomain.html.in | 5 +++++
docs/schemas/domain.rng | 7 ++++++-
src/conf/domain_conf.c | 3 ++-
src/conf/domain_conf.h | 1 +
src/xen/xen_hypervisor.c | 10 ++++++++++
src/xen/xend_internal.c | 4 ++++
src/xen/xm_internal.c | 8 ++++++++
src/xenapi/xenapi_driver.c | 2 ++
src/xenapi/xenapi_utils.c | 2 ++
9 files changed, 40 insertions(+), 2 deletions(-)
--
1.7.3.1
13 years, 9 months
[libvirt] virsh setmem and setmaxmem: which XML do they match?
by Justin Clift
Hi all,
Updating the man pages for the virsh setmem and setmaxmem commands at the moment.
These two commands only run on active guest domains. So, as a helpful pointer on how
to make the change with an inactive domain, I'm going to mention what the equivalent XML
tag is for use with virsh edit. Like this:
Note, this command only works on active guest domains. To change the memory
allocation for an inactive guest domain, use the virsh B<edit> command to
update the XML <memory> element.
I'm just not sure which XML element the setmem and setmaxmem commands map to.
Does "setmaxmem" map to <memory>, and "setmem" map to <currentMemory>?
The description in our XML format domain doc is less than clear. :/
Regards and best wishes,
Justin Clift
13 years, 9 months
[libvirt] [PATCH] util: add missing string->integer conversion functions
by Eric Blake
It was awkward having only int conversion in the virStrToLong family,
but only long conversion in the virXPath family. Make both families
support both types.
* src/util/util.h (virStrToLong_l, virStrToLong_ul): New
prototypes.
* src/util/xml.h (virXPathInt, virXPathUInt): Likewise.
* src/util/util.c (virStrToLong_l, virStrToLong_ul): New
functions.
* src/util/xml.c (virXPathInt, virXPathUInt): Likewise.
* src/libvirt_private.syms (util.h, xml.h): Export them.
---
src/libvirt_private.syms | 4 +++
src/util/util.c | 40 +++++++++++++++++++++++++++++-
src/util/util.h | 8 ++++++
src/util/xml.c | 60 +++++++++++++++++++++++++++++++++++++++++++++-
src/util/xml.h | 12 +++++++--
5 files changed, 119 insertions(+), 5 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a166bd9..d95ef31 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -886,8 +886,10 @@ virSetUIDGID;
virSkipSpaces;
virStrToDouble;
virStrToLong_i;
+virStrToLong_l;
virStrToLong_ll;
virStrToLong_ui;
+virStrToLong_ul;
virStrToLong_ull;
virStrcpy;
virStrncpy;
@@ -926,6 +928,7 @@ virStrerror;
# xml.h
virXMLPropString;
virXPathBoolean;
+virXPathInt;
virXPathLong;
virXPathLongHex;
virXPathLongLong;
@@ -934,6 +937,7 @@ virXPathNodeSet;
virXPathNumber;
virXPathString;
virXPathStringLimit;
+virXPathUInt;
virXPathULong;
virXPathULongHex;
virXPathULongLong;
diff --git a/src/util/util.c b/src/util/util.c
index 18b38f4..f412a83 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -2081,7 +2081,45 @@ virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *result)
return 0;
}
-/* Just like virStrToLong_i, above, but produce an "long long" value. */
+/* Just like virStrToLong_i, above, but produce a "long" value. */
+int
+virStrToLong_l(char const *s, char **end_ptr, int base, long *result)
+{
+ long int val;
+ char *p;
+ int err;
+
+ errno = 0;
+ val = strtol(s, &p, base);
+ err = (errno || (!end_ptr && *p) || p == s);
+ if (end_ptr)
+ *end_ptr = p;
+ if (err)
+ return -1;
+ *result = val;
+ return 0;
+}
+
+/* Just like virStrToLong_i, above, but produce an "unsigned long" value. */
+int
+virStrToLong_ul(char const *s, char **end_ptr, int base, unsigned long *result)
+{
+ unsigned long int val;
+ char *p;
+ int err;
+
+ errno = 0;
+ val = strtoul(s, &p, base);
+ err = (errno || (!end_ptr && *p) || p == s);
+ if (end_ptr)
+ *end_ptr = p;
+ if (err)
+ return -1;
+ *result = val;
+ return 0;
+}
+
+/* Just like virStrToLong_i, above, but produce a "long long" value. */
int
virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result)
{
diff --git a/src/util/util.h b/src/util/util.h
index 932db03..8373038 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -188,6 +188,14 @@ int virStrToLong_ui(char const *s,
char **end_ptr,
int base,
unsigned int *result);
+int virStrToLong_l(char const *s,
+ char **end_ptr,
+ int base,
+ long *result);
+int virStrToLong_ul(char const *s,
+ char **end_ptr,
+ int base,
+ unsigned long *result);
int virStrToLong_ll(char const *s,
char **end_ptr,
int base,
diff --git a/src/util/xml.c b/src/util/xml.c
index e2c2c6c..de5e9de 100644
--- a/src/util/xml.c
+++ b/src/util/xml.c
@@ -1,7 +1,7 @@
/*
* xml.c: XML based interfaces for the libvir library
*
- * Copyright (C) 2005, 2007-2010 Red Hat, Inc.
+ * Copyright (C) 2005, 2007-2011 Red Hat, Inc.
*
* See COPYING.LIB for the License of this software
*
@@ -195,6 +195,35 @@ virXPathLongBase(const char *xpath,
}
/**
+ * virXPathInt:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned int value
+ *
+ * Convenience function to evaluate an XPath number
+ *
+ * Returns 0 in case of success in which case @value is set,
+ * or -1 if the XPath evaluation failed or -2 if the
+ * value doesn't have an int format.
+ */
+int
+virXPathInt(const char *xpath,
+ xmlXPathContextPtr ctxt,
+ int *value)
+{
+ long tmp;
+ int ret;
+
+ ret = virXPathLongBase(xpath, ctxt, 10, &tmp);
+ if (ret < 0)
+ return ret;
+ if ((int) tmp != tmp)
+ return -2;
+ *value = tmp;
+ return 0;
+}
+
+/**
* virXPathLong:
* @xpath: the XPath string to evaluate
* @ctxt: an XPath context
@@ -279,6 +308,35 @@ virXPathULongBase(const char *xpath,
}
/**
+ * virXPathUInt:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned int value
+ *
+ * Convenience function to evaluate an XPath number
+ *
+ * Returns 0 in case of success in which case @value is set,
+ * or -1 if the XPath evaluation failed or -2 if the
+ * value doesn't have an int format.
+ */
+int
+virXPathUInt(const char *xpath,
+ xmlXPathContextPtr ctxt,
+ unsigned int *value)
+{
+ unsigned long tmp;
+ int ret;
+
+ ret = virXPathULongBase(xpath, ctxt, 10, &tmp);
+ if (ret < 0)
+ return ret;
+ if ((unsigned int) tmp != tmp)
+ return -2;
+ *value = tmp;
+ return 0;
+}
+
+/**
* virXPathULong:
* @xpath: the XPath string to evaluate
* @ctxt: an XPath context
diff --git a/src/util/xml.h b/src/util/xml.h
index b1da741..b342e83 100644
--- a/src/util/xml.h
+++ b/src/util/xml.h
@@ -21,19 +21,25 @@ char * virXPathStringLimit(const char *xpath,
int virXPathNumber(const char *xpath,
xmlXPathContextPtr ctxt,
double *value);
+int virXPathInt(const char *xpath,
+ xmlXPathContextPtr ctxt,
+ int *value);
+int virXPathUInt(const char *xpath,
+ xmlXPathContextPtr ctxt,
+ unsigned int *value);
int virXPathLong(const char *xpath,
xmlXPathContextPtr ctxt,
long *value);
-int virXPathULong(const char *xpath,
+int virXPathULong(const char *xpath,
xmlXPathContextPtr ctxt,
unsigned long *value);
int virXPathULongLong(const char *xpath,
xmlXPathContextPtr ctxt,
unsigned long long *value);
-int virXPathLongLong(const char *xpath,
+int virXPathLongLong(const char *xpath,
xmlXPathContextPtr ctxt,
long long *value);
-int virXPathLongHex (const char *xpath,
+int virXPathLongHex(const char *xpath,
xmlXPathContextPtr ctxt,
long *value);
int virXPathULongHex(const char *xpath,
--
1.7.3.4
13 years, 9 months
[libvirt] [PATCH] docs: clarify that virsh setvcpus and setmem only work on active domains
by Justin Clift
Addresses BZ # 622534:
https://bugzilla.redhat.com/show_bug.cgi?id=622534
---
tools/virsh.pod | 28 ++++++++++++++++++----------
1 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 0e03d68..b4c6ff7 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -568,13 +568,16 @@ XEN_CREDIT scheduler and are now I<DEPRECATED>.
=item B<setmem> I<domain-id> B<kilobytes>
-Change the current memory allocation in the guest domain. This should take
-effect immediately. The memory limit is specified in
-kilobytes.
+Change the current memory allocation in an B<active> guest domain. This
+should take effect immediately. The memory limit is specified in kilobytes.
For Xen, you can only adjust the memory of a running domain if the
domain is paravirtualized or running the PV balloon driver.
+B<Note>, this command only works on active guest domains. To change the
+number of virtual CPUs in a inactive guest domain, use the virsh B<edit>
+command to update the XML <memory> element.
+
=item B<setmaxmem> I<domain-id> B<kilobytes>
Change the maximum memory allocation limit in the guest domain. This should
@@ -593,24 +596,29 @@ QEMU/KVM supports I<--hard-limit>, I<--soft-limit>, and I<--swap-hard-limit>.
=item B<setvcpus> I<domain-id> I<count> optional I<--maximum> I<--config>
I<--live>
-Change the number of virtual CPUs active in the guest domain. Note that
-I<count> may be limited by host, hypervisor or limit coming from the
-original description of domain.
+Change the number of virtual CPUs active in an B<active> guest domain.
+
+The I<count> value may be limited by host, hypervisor or limit coming from
+the original description of the guest domain.
For Xen, you can only adjust the virtual CPUs of a running domain if
the domain is paravirtualized.
If I<--config> is specified, the change will only affect the next
boot of a domain. If I<--live> is specified, the domain must be
-running, and the change takes place immediately. Both flags may be
-specified, if supported by the hypervisor. If neither flag is given,
-then I<--live> is implied and it is up to the hypervisor whether
-I<--config> is also implied.
+running, and the change takes place immediately. Both I<--config> and
+I<--live> flags may be specified, if supported by the hypervisor. If
+neither flag is given, then I<--live> is implied and it is up to the
+hypervisor whether I<--config> is also implied.
If I<--maximum> is specified, then you must use I<--config> and
avoid I<--live>; this flag controls the maximum limit of vcpus that
can be hot-plugged the next time the domain is booted.
+B<Note>, this command only works on active guest domains. To change the
+number of virtual CPUs in a inactive guest domain, use the virsh B<edit>
+command to update the XML <vcpu> element.
+
=item B<shutdown> I<domain-id>
Gracefully shuts down a domain. This coordinates with the domain OS
--
1.7.3.2
13 years, 9 months
[libvirt] esx driver: XML format for guest OS type/variant
by Jake Xu
Hi,
I am trying to create a VM using the Python bindings of Libvirt. I can
successfully create VM from a XML template, but I can't find any way to
define the guest OS type/variant like CentOS 5.5 64bit for my VM. The native
format converted from XML is always guestOS="other-64" - which doesn't tell
us much about the guest operating system.
I have looked at the C libvirt source code a bit, and it seems like libvirt
does not support defining guest os type using XML description yet.
Is there any way I can set the guest OS type for my VM?
Thanks,
Jake
13 years, 9 months
[libvirt] [PATCH 0/7] security: Allow disabling security per VM
by Cole Robinson
Enabling a security driver in qemu.conf is currently all or nothing.
The option to disable security on a per VM basis can be a useful debugging
tool or work around for frustrated users.
Patches 1-3 and 5-6 are prep and cleanup work. Patch 4 fixes an
easily triggerable segfault when defining a domain in qemu. Patch 7
is the actual feature.
Cole Robinson (7):
tests: Add qemuxml2xml tests for <seclabel> handling
security: Use virDomainSeclabelDefClear
security: Add virSecurityIsSpecifiedDriver
qemu: Fix segfault if defining a domain without <seclabel>
domain: Handle seclabel model with an enum
domain: Always validate seclabel model
security: Allow disabling security on a per VM basis
cfg.mk | 1 +
docs/schemas/domain.rng | 13 ++-
src/conf/domain_conf.c | 69 ++++++++++-----
src/conf/domain_conf.h | 15 +++-
src/libvirt_private.syms | 2 +-
src/qemu/qemu_driver.c | 4 +-
src/security/security_apparmor.c | 31 ++-----
src/security/security_driver.c | 25 ++++++
src/security/security_driver.h | 3 +
src/security/security_manager.c | 90 +++++++++++++-------
src/security/security_selinux.c | 50 +++---------
tests/domainschematest | 2 +-
.../qemuxml2xml-balloon-device-auto-out.xml | 25 ++++++
.../qemuxml2xml-channel-virtio-auto-out.xml | 54 ++++++++++++
.../qemuxml2xml-console-compat-auto-out.xml | 31 +++++++
.../qemuxml2xml-console-virtio-out.xml | 29 ++++++
.../qemuxml2xml-disk-scsi-device-auto-out.xml | 31 +++++++
.../qemuxml2xml-seclabel-dynamic-in.xml | 24 +++++
.../qemuxml2xml-seclabel-dynamic-out.xml | 21 +++++
.../qemuxml2xml-seclabel-model-none-in.xml | 21 +++++
.../qemuxml2xml-seclabel-model-none-out.xml | 21 +++++
.../qemuxml2xml-seclabel-static-in.xml | 24 +++++
.../qemuxml2xml-seclabel-static-out.xml | 23 +++++
.../qemuxml2xmlout-balloon-device-auto.xml | 25 ------
.../qemuxml2xmlout-channel-virtio-auto.xml | 54 ------------
.../qemuxml2xmlout-console-compat-auto.xml | 31 -------
.../qemuxml2xmlout-console-virtio.xml | 29 ------
.../qemuxml2xmlout-disk-scsi-device-auto.xml | 31 -------
tests/qemuxml2xmltest.c | 26 ++++--
29 files changed, 501 insertions(+), 304 deletions(-)
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-balloon-device-auto-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-channel-virtio-auto-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-console-compat-auto-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-console-virtio-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-disk-scsi-device-auto-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-dynamic-in.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-dynamic-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-model-none-in.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-model-none-out.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-static-in.xml
create mode 100644 tests/qemuxml2xmldata/qemuxml2xml-seclabel-static-out.xml
delete mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-balloon-device-auto.xml
delete mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-auto.xml
delete mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-console-compat-auto.xml
delete mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-console-virtio.xml
delete mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-disk-scsi-device-auto.xml
--
1.7.3.2
13 years, 9 months
[libvirt] [PATCH] Fix crash in SELinuxSecurityVerify
by Laine Stump
When attempting to edit a domain, libvirtd segfaulted in
SELinuxSecurityVerify() on this line:
if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
because secdef->model was NULL. Although I'm too tired to investigate
in depth, I noticed that all the other functions in that file that do
the same STREQ() will first check that def->seclabel.label is
non-NULL, but this function doesn't. I also noticed that label *is*
NULL in my case, so I tried adding that check to
SELinuxSecurityVerify(), and the crash goes away.
I have no idea if this is the correct fix, but it allowed me to
continue my testing of a new (unrelated) feature.
---
src/security/security_selinux.c | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index d06afde..b97ca4c 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -871,6 +871,10 @@ SELinuxSecurityVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
virDomainDefPtr def)
{
const virSecurityLabelDefPtr secdef = &def->seclabel;
+
+ if (def->seclabel.label == NULL)
+ return 0;
+
if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
_("security label driver mismatch: "
--
1.7.3.4
13 years, 9 months
[libvirt] [PATCH 0/6 v2] events: Add helpers for driver dispatching
by Cole Robinson
I noticed that there is quite a bit of code duplication among the
drivers that support domain events. This patch series is an attempt
to consolidate the shared logic.
v2:
2 patches were applied
Addressed Eric's comments:
NONNULL tagging
Use bool for isDispatching
Move libvirt_private.syms earlier
Add NULL check in StateFree
Cole Robinson (6):
domain_event: Add virDomainEventState structure
domain_event: Add common domain event queue/flush helpers
qemu: Use virDomainEventState helpers
lxc: Use virDomainEventState helpers
test: Use virDomainEventState helpers
remote: Use virDomainEventState helpers
cfg.mk | 1 +
src/conf/domain_event.c | 166 ++++++++++++++++++++++++++++++++++++++-----
src/conf/domain_event.h | 69 +++++++++++++++----
src/libvirt_private.syms | 6 ++
src/lxc/lxc_conf.h | 6 +--
src/lxc/lxc_driver.c | 76 ++++++--------------
src/qemu/qemu_conf.h | 6 +--
src/qemu/qemu_driver.c | 80 +++++++--------------
src/remote/remote_driver.c | 162 ++++++++++++++++--------------------------
src/test/test_driver.c | 104 ++++++++-------------------
10 files changed, 353 insertions(+), 323 deletions(-)
--
1.7.3.3
13 years, 9 months
[libvirt] [PATCH] virFindFileInPath: only find executable non-directory
by Eric Blake
Without this patch, at least tests/daemon-conf (which sticks
$builddir/src in the PATH) tries to execute the directory
$builddir/src/qemu rather than the real /usr/bin/qemu binary.
That was fine when qemu_capabilities silently ignored execution
failure, but not so good once it is converted to use virCommand.
* src/util/util.h (virFileExists): Adjust prototype.
(virFileIsExecutable): New prototype.
* src/util/util.c (virFindFileInPath): Reject non-executables and
directories. Avoid huge stack allocation.
(virFileExists): Use lighter-weight syscall.
(virFileIsExecutable): New function.
* src/libvirt_private.syms (util.h): Export new function.
---
Questions:
Should I be requiring S_ISREG, rather than !S_ISDIR (that is,
should we be rejecting devices and sockets as non-exectuable)?
Should I import the gnulib module euidaccess (and/or faccessat)
for the access check? Using access(F_OK) is okay regardless of
uid/gid, but access(X_OK) may have different answers than
euidaccess(X_OK) when the effective uid/gid do not match the
current uid/gid. However, dragging in the gnulib module will
require adding an extra link library for some platforms (for
example, Solaris needs -lgen), which means the change is more
invasive as it will also affect Makefiles.
src/libvirt_private.syms | 1 +
src/util/util.c | 53 ++++++++++++++++++++++++++++++---------------
src/util/util.h | 7 +++--
3 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 65911df..948bbe1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -850,6 +850,7 @@ virFileDeletePid;
virFileExists;
virFileFindMountPoint;
virFileHasSuffix;
+virFileIsExecutable;
virFileLinkPointsTo;
virFileMakePath;
virFileMatchesNameSuffix;
diff --git a/src/util/util.c b/src/util/util.c
index 60feb79..25e6185 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1255,7 +1255,7 @@ int virFileResolveLink(const char *linkpath,
}
/*
- * Finds a requested file in the PATH env. e.g.:
+ * Finds a requested executable file in the PATH env. e.g.:
* "kvm-img" will return "/usr/bin/kvm-img"
*
* You must free the result
@@ -1263,19 +1263,18 @@ int virFileResolveLink(const char *linkpath,
char *virFindFileInPath(const char *file)
{
char *path;
- char pathenv[PATH_MAX];
- char *penv = pathenv;
+ char *pathiter;
char *pathseg;
- char fullpath[PATH_MAX];
+ char *fullpath = NULL;
if (file == NULL)
return NULL;
/* if we are passed an absolute path (starting with /), return a
- * copy of that path
+ * copy of that path, after validating that it is executable
*/
- if (file[0] == '/') {
- if (virFileExists(file))
+ if (IS_ABSOLUTE_FILE_NAME(file)) {
+ if (virFileIsExecutable(file))
return strdup(file);
else
return NULL;
@@ -1284,27 +1283,45 @@ char *virFindFileInPath(const char *file)
/* copy PATH env so we can tweak it */
path = getenv("PATH");
- if (path == NULL || virStrcpyStatic(pathenv, path) == NULL)
+ if (path == NULL || (path = strdup(path)) == NULL)
return NULL;
/* for each path segment, append the file to search for and test for
* it. return it if found.
*/
- while ((pathseg = strsep(&penv, ":")) != NULL) {
- snprintf(fullpath, PATH_MAX, "%s/%s", pathseg, file);
- if (virFileExists(fullpath))
- return strdup(fullpath);
+ pathiter = path;
+ while ((pathseg = strsep(&pathiter, ":")) != NULL) {
+ if (virAsprintf(&fullpath, "%s/%s", pathseg, file) < 0 ||
+ virFileIsExecutable(fullpath))
+ break;
+ VIR_FREE(fullpath);
}
- return NULL;
+ VIR_FREE(path);
+ return fullpath;
}
-int virFileExists(const char *path)
+
+bool virFileExists(const char *path)
{
- struct stat st;
+ return access(path, F_OK) == 0;
+}
- if (stat(path, &st) >= 0)
- return(1);
- return(0);
+/* Check that a file can be executed by the current user, and is not
+ * a directory. */
+bool
+virFileIsExecutable(const char *file)
+{
+ struct stat sb;
+
+ /* The existence of ACLs means that checking (sb.st_mode&0111) for
+ * executable bits may give false results; plus, access is
+ * lighter-weight than stat for a first pass filter.
+ *
+ * XXX: should this use gnulib's euidaccess module?
+ */
+ return (access(file, X_OK) == 0 &&
+ stat(file, &sb) == 0 &&
+ !S_ISDIR(sb.st_mode));
}
#ifndef WIN32
diff --git a/src/util/util.h b/src/util/util.h
index 989962f..54c3058 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -1,8 +1,7 @@
-
/*
* utils.h: common, generic utility functions
*
- * Copyright (C) 2010 Red Hat, Inc.
+ * Copyright (C) 2010-2011 Red Hat, Inc.
* Copyright (C) 2006, 2007 Binary Karma
* Copyright (C) 2006 Shuveb Hussain
*
@@ -32,6 +31,7 @@
# include <sys/select.h>
# include <sys/types.h>
# include <stdarg.h>
+# include <stdbool.h>
# ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -120,7 +120,8 @@ int virFileResolveLink(const char *linkpath,
char *virFindFileInPath(const char *file);
-int virFileExists(const char *path);
+bool virFileExists(const char *file);
+bool virFileIsExecutable(const char *file);
char *virFileSanitizePath(const char *path);
--
1.7.3.4
13 years, 9 months