libzfs: return (allocated) strings instead of filling buffers

This also expands the zfs version output from 127 characters to However
Many Are Actually Set

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #13330
This commit is contained in:
наб
2022-04-15 00:00:02 +02:00
committed by Brian Behlendorf
parent 38f4d99f76
commit 2b4f2fc93c
7 changed files with 1427 additions and 1442 deletions
+18 -22
View File
@@ -183,31 +183,27 @@ zfs_destroy_snaps_nvl_os(libzfs_handle_t *hdl, nvlist_t *snaps)
}
/*
* Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
* Returns 0 on success, and -1 on error (with errno set)
* Return allocated loaded module version, or NULL on error (with errno set)
*/
int
zfs_version_kernel(char *version, int len)
char *
zfs_version_kernel(void)
{
int _errno;
int fd;
int rlen;
FILE *f = fopen(ZFS_SYSFS_DIR "/version", "re");
if (f == NULL)
return (NULL);
if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY | O_CLOEXEC)) == -1)
return (-1);
if ((rlen = read(fd, version, len)) == -1) {
version[0] = '\0';
_errno = errno;
(void) close(fd);
errno = _errno;
return (-1);
char *ret = NULL;
size_t l;
ssize_t read;
if ((read = getline(&ret, &l, f)) == -1) {
int err = errno;
fclose(f);
errno = err;
return (NULL);
}
version[rlen-1] = '\0'; /* discard '\n' */
if (close(fd) == -1)
return (-1);
return (0);
fclose(f);
if (ret[read - 1] == '\n')
ret[read - 1] = '\0';
return (ret);
}