Commit b0a4effc authored by Shawn Webb's avatar Shawn Webb
Browse files

Initial commit


Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parents
SHLIB= lattzfs
SHLIB_MAJOR= 0
.PATH: ${.CURDIR}/include
.PATH: ${.CURDIR}/src
SRCS+= liblattzfs.c
INCS+= liblattzfs.h
CFLAGS+= -I${.CURDIR}/include
CFLAGS+= -DIN_BASE -DHAVE_RPC_TYPES
CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include
CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include/os/freebsd
CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include
CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include/os/freebsd
CFLAGS+= -I${SRCTOP}/sys
CFLAGS+= -I${SRCTOP}/cddl/compat/opensolaris/include
CFLAGS+= -include ${SRCTOP}/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompile.h
CFLAGS+= -DHAVE_ISSETUGID
LDADD+= -lzfs -lnvpair -lspl
.if defined(PREFIX)
INCLUDEDIR= ${PREFIX}/include
LIBDIR= ${PREFIX}/lib
.endif
.include <bsd.lib.mk>
# liblattzfs
This is a tiny little wrapper around libzfs so that applications in
ports don't have to depend on all the weird, funky CFLAGS.
This code is developed out-of-tree, but will eventually be imported as
vendor code into HardenedBSD.
The reason behind this project is to make ZFS integration in hbsdmon
sane.
#ifndef _LIBLATTZFS_H
#define _LIBLATTZFS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#define LATTZFS_VERSION 1
#define LATTZFS_FLAG_LAZY_OPEN 0x1
/*
* Keep this in-sync with OpenZFS' zpool_errata_t
*
* Current version: HardenedBSD 14-CURRENT, 14 Aug 2021
*/
typedef enum _lattzfs_zpool_errata {
LATT_ZPOOL_ERRATA_NONE=1,
LATT_ZPOOL_ERRATA_ZOL_2094_SCRUB=1,
LATT_ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY=2,
LATT_ZPOOL_ERRATA_ZOL_6845_ENCRYPTION=3,
LATT_ZPOOL_ERRATA_ZOL_8308_ENCRYPTION=4,
} lattzfs_zpool_errata_t;
/*
* Keep this in-sync with OpenZFS zpool_status_t
*
* Current version: HardenedBSD 14-CURRENT, 14 Aug 2021
*/
typedef enum _lattzfs_zpool_status {
LATT_ZPOOL_STATUS_CORRUPT_CACHE=0,
LATT_ZPOOL_STATUS_MISSING_DEV_R=1,
LATT_ZPOOL_STATUS_MISSING_DEV_NR=2,
LATT_ZPOOL_STATUS_CORRUPT_LABEL_R=3,
LATT_ZPOOL_STATUS_CORRUPT_LABEL_NR=4,
LATT_ZPOOL_STATUS_BAD_GUID_SUM=5,
LATT_ZPOOL_STATUS_CORRUPT_POOL=6,
LATT_ZPOOL_STATUS_CORRUPT_DATA=7,
LATT_ZPOOL_STATUS_FAILING_DEV=8,
LATT_ZPOOL_STATUS_VERSION_NEWER=9,
LATT_ZPOOL_STATUS_HOSTID_MISMATCH=10,
LATT_ZPOOL_STATUS_HOSTID_ACTIVE=11,
LATT_ZPOOL_STATUS_HOSTID_REQUIRED=12,
LATT_ZPOOL_STATUS_IO_FAILURE_WAIT=13,
LATT_ZPOOL_STATUS_IO_FAILURE_CONTINUE=14,
LATT_ZPOOL_STATUS_IO_FAILURE_MMP=15,
LATT_ZPOOL_STATUS_BAD_LOG=16,
LATT_ZPOOL_STATUS_ERRATA=17,
LATT_ZPOOL_STATUS_UNSUP_FEAT_READ=18,
LATT_ZPOOL_STATUS_UNSUP_FEAT_WRITE=19,
LATT_ZPOOL_STATUS_FAULTED_DEV_R=20,
LATT_ZPOOL_STATUS_FAULTED_DEV_NR=21,
LATT_ZPOOL_STATUS_VERSION_OLDER=22,
LATT_ZPOOL_STATUS_FEAT_DISABLED=23,
LATT_ZPOOL_STATUS_RESILVERING=24,
LATT_ZPOOL_STATUS_OFFLINE_DEV=25,
LATT_ZPOOL_STATUS_REMOVED_DEV=26,
LATT_ZPOOL_STATUS_REBUILDING=27,
LATT_ZPOOL_STATUS_REBUILD_SCRUB=28,
LATT_ZPOOL_STATUS_NON_NATIVE_ASHIFT=29,
LATT_ZPOOL_STATUS_COMPATIBILITY_ERR=30,
LATT_ZPOOL_STATUS_INCOMPATIBLE_FEAT=31,
LATT_ZPOOL_STATUS_OK=32,
} lattzfs_zpool_status_t;
typedef struct _lattzfs_ctx {
uint64_t lc_version;
uint64_t lc_flags;
void *lc_zfs_handle;
void *lc_pool_handle;
char *lc_pool_name;
} lattzfs_ctx_t;
lattzfs_ctx_t *lattzfs_ctx_new(const char *, uint64_t);
void lattzfs_ctx_free(lattzfs_ctx_t **);
bool lattzfs_zpool_get_status(lattzfs_ctx_t *, lattzfs_zpool_status_t *,
lattzfs_zpool_errata_t *);
#ifdef __cplusplus
}
#endif
#endif /* !_LIBLATTZFS_H */
*
!.gitignore
PROG= poc
MAN=
SRCS+= poc.c
CFLAGS+= -I${.CURDIR}/../include
LDFLAGS+= -L${.CURDIR}/../obj
LDADD+= -llattzfs
.include <bsd.prog.mk>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <liblattzfs.h>
int
main(int argc, char *argv[])
{
lattzfs_zpool_errata_t errata;
lattzfs_zpool_status_t status;
lattzfs_ctx_t *ctx;
if (argc != 2) {
printf("USAGE: %s poolname\n", argv[0]);
return (1);
}
ctx = lattzfs_ctx_new(argv[1], 0);
if (ctx == NULL) {
printf("ctx null\n");
return (1);
}
status = 0;
errata = 0;
if (!lattzfs_zpool_get_status(ctx, &status, &errata)) {
printf("get_status error\n");
return (1);
}
printf("Status: %d\n", status);
printf("Errata: 0x%016lx\n", (uint64_t)errata);
return (0);
}
/*-
* Copyright (c) 2021 Shawn Webb <shawn.webb@hardenedbsd.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libzfs.h>
#include <libzfs_core.h>
#include "liblattzfs.h"
static bool _lattzfs_pool_open(lattzfs_ctx_t *);
lattzfs_ctx_t *
lattzfs_ctx_new(const char *pool, uint64_t flags)
{
lattzfs_ctx_t *ctx;
if (pool == NULL) {
/* No flags are supported, yet */
return (NULL);
}
ctx = calloc(1, sizeof(*ctx));
if (ctx == NULL) {
return (NULL);
}
ctx->lc_version = LATTZFS_VERSION;
ctx->lc_pool_name = strdup(pool);
if (ctx->lc_pool_name == NULL) {
free(ctx);
return (NULL);
}
ctx->lc_zfs_handle = libzfs_init();
if (!(flags & LATTZFS_FLAG_LAZY_OPEN)) {
if (!_lattzfs_pool_open(ctx)) {
free(ctx->lc_pool_name);
free(ctx);
return (NULL);
}
}
return (ctx);
}
void
lattzfs_ctx_free(lattzfs_ctx_t **ctx)
{
lattzfs_ctx_t *ctxp;
if (ctx == NULL || *ctx == NULL) {
return;
}
ctxp = *ctx;
if (ctxp->lc_pool_handle != NULL) {
zpool_close(ctxp->lc_pool_handle);
}
if (ctxp->lc_zfs_handle != NULL) {
zfs_close(ctxp->lc_zfs_handle);
}
free(ctxp->lc_pool_name);
memset(ctxp, 0, sizeof(*ctxp));
free(ctxp);
*ctx = NULL;
}
bool
lattzfs_zpool_get_status(lattzfs_ctx_t *ctx, lattzfs_zpool_status_t *status,
lattzfs_zpool_errata_t *errata)
{
zpool_errata_t zerrata;
zpool_status_t zstatus;
char *msgid;
if (ctx == NULL || status == NULL || errata == NULL) {
return (false);
}
if (!_lattzfs_pool_open(ctx)) {
return (false);
}
msgid = NULL;
zstatus = zpool_get_status(ctx->lc_pool_handle, &msgid, &zerrata);
*errata = (lattzfs_zpool_errata_t)zerrata;
*status = (lattzfs_zpool_status_t)zstatus;
return (true);
}
static bool
_lattzfs_pool_open(lattzfs_ctx_t *ctx)
{
if (ctx == NULL) {
return (false);
}
if (ctx->lc_pool_handle != NULL) {
return (true);
}
ctx->lc_pool_handle = zpool_open(ctx->lc_zfs_handle,
ctx->lc_pool_name);
if (ctx->lc_pool_handle == NULL) {
return (false);
}
return (true);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment