aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libzfs/libzfs_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libzfs/libzfs_util.c')
-rw-r--r--lib/libzfs/libzfs_util.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/libzfs/libzfs_util.c b/lib/libzfs/libzfs_util.c
index 23dcb11bd..b988d8f31 100644
--- a/lib/libzfs/libzfs_util.c
+++ b/lib/libzfs/libzfs_util.c
@@ -54,6 +54,7 @@
#include "zfeature_common.h"
#include <zfs_fletcher.h>
#include <libzutil.h>
+#include <sys/zfs_sysfs.h>
int
libzfs_errno(libzfs_handle_t *hdl)
@@ -1940,3 +1941,66 @@ zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
{
return (zprop_iter_common(func, cb, show_all, ordered, type));
}
+
+/*
+ * Fill given version buffer with zfs userland version
+ */
+void
+zfs_version_userland(char *version, int len)
+{
+ (void) strlcpy(version, ZFS_META_ALIAS, len);
+}
+
+/*
+ * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
+ * Returns 0 on success, and -1 on error (with errno set)
+ */
+int
+zfs_version_kernel(char *version, int len)
+{
+ int _errno;
+ int fd;
+ int rlen;
+
+ if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
+ return (-1);
+
+ if ((rlen = read(fd, version, len)) == -1) {
+ version[0] = '\0';
+ _errno = errno;
+ (void) close(fd);
+ errno = _errno;
+ return (-1);
+ }
+
+ version[rlen-1] = '\0'; /* discard '\n' */
+
+ if (close(fd) == -1)
+ return (-1);
+
+ return (0);
+}
+
+/*
+ * Prints both zfs userland and kernel versions
+ * Returns 0 on success, and -1 on error (with errno set)
+ */
+int
+zfs_version_print(void)
+{
+ char zver_userland[128];
+ char zver_kernel[128];
+
+ if (zfs_version_kernel(zver_kernel, sizeof (zver_kernel)) == -1) {
+ fprintf(stderr, "zfs_version_kernel() failed: %s\n",
+ strerror(errno));
+ return (-1);
+ }
+
+ zfs_version_userland(zver_userland, sizeof (zver_userland));
+
+ (void) printf("%s\n", zver_userland);
+ (void) printf("zfs-kmod-%s\n", zver_kernel);
+
+ return (0);
+}