1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  14  */
  15 
  16 #include <unistd.h>
  17 #include <umem.h>
  18 #include <fcntl.h>
  19 
  20 #include "krrp_error.h"
  21 #include <sys/krrp.h>
  22 #include "libkrrp_impl.h"
  23 #include "libkrrp.h"
  24 
  25 boolean_t
  26 is_krrp_supported(void)
  27 {
  28         return (access(KRRP_DEVICE, 0) == 0);
  29 }
  30 
  31 libkrrp_handle_t *
  32 libkrrp_init(void)
  33 {
  34         libkrrp_handle_t *hdl;
  35 
  36         hdl = umem_zalloc(sizeof (libkrrp_handle_t), UMEM_DEFAULT);
  37         if (hdl == NULL)
  38                 return (NULL);
  39 
  40         if ((hdl->libkrrp_fd = open(KRRP_DEVICE, O_RDONLY)) < 0) {
  41                 umem_free(hdl, sizeof (libkrrp_handle_t));
  42                 return (NULL);
  43         }
  44 
  45         return (hdl);
  46 }
  47 
  48 void
  49 libkrrp_fini(libkrrp_handle_t *hdl)
  50 {
  51         (void) close(hdl->libkrrp_fd);
  52         umem_free(hdl, sizeof (libkrrp_handle_t));
  53 }
  54 
  55 void
  56 libkrrp_reset(libkrrp_handle_t *hdl)
  57 {
  58         hdl->libkrrp_error.libkrrp_errno = LIBKRRP_ERRNO_OK;
  59         hdl->libkrrp_error_descr[0] = '\0';
  60         hdl->libkrrp_error.flags = 0;
  61 }