26
27 /*
28 * Internal utility routines for the ZFS library.
29 */
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <libintl.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <ctype.h>
40 #include <math.h>
41 #include <sys/mnttab.h>
42 #include <sys/mntent.h>
43 #include <sys/types.h>
44
45 #include <libzfs.h>
46
47 #include "libzfs_impl.h"
48 #include "zfs_prop.h"
49 #include "zfeature_common.h"
50
51 int
52 libzfs_errno(libzfs_handle_t *hdl)
53 {
54 return (hdl->libzfs_error);
55 }
56
57 const char *
58 libzfs_error_action(libzfs_handle_t *hdl)
59 {
60 return (hdl->libzfs_action);
61 }
62
63 const char *
64 libzfs_error_description(libzfs_handle_t *hdl)
65 {
613 {
614 libzfs_handle_t *hdl;
615
616 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
617 return (NULL);
618 }
619
620 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
621 free(hdl);
622 return (NULL);
623 }
624
625 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
626 (void) close(hdl->libzfs_fd);
627 free(hdl);
628 return (NULL);
629 }
630
631 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
632
633 zfs_prop_init();
634 zpool_prop_init();
635 zpool_feature_init();
636 libzfs_mnttab_init(hdl);
637
638 return (hdl);
639 }
640
641 void
642 libzfs_fini(libzfs_handle_t *hdl)
643 {
644 (void) close(hdl->libzfs_fd);
645 if (hdl->libzfs_mnttab)
646 (void) fclose(hdl->libzfs_mnttab);
647 if (hdl->libzfs_sharetab)
648 (void) fclose(hdl->libzfs_sharetab);
649 zfs_uninit_libshare(hdl);
650 if (hdl->libzfs_log_str)
651 (void) free(hdl->libzfs_log_str);
652 zpool_free_handles(hdl);
653 libzfs_fru_clear(hdl, B_TRUE);
654 namespace_clear(hdl);
655 libzfs_mnttab_fini(hdl);
656 free(hdl);
657 }
658
659 libzfs_handle_t *
660 zpool_get_handle(zpool_handle_t *zhp)
661 {
662 return (zhp->zpool_hdl);
663 }
664
665 libzfs_handle_t *
666 zfs_get_handle(zfs_handle_t *zhp)
667 {
668 return (zhp->zfs_hdl);
669 }
670
671 zpool_handle_t *
672 zfs_get_pool_handle(const zfs_handle_t *zhp)
673 {
674 return (zhp->zpool_hdl);
675 }
797 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
798 &zc->zc_nvlist_src_size, nvl));
799 }
800
801 /*
802 * Unpacks an nvlist from the ZFS ioctl command structure.
803 */
804 int
805 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
806 {
807 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
808 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
809 return (no_memory(hdl));
810
811 return (0);
812 }
813
814 int
815 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
816 {
817 int error;
818
819 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
820 error = ioctl(hdl->libzfs_fd, request, zc);
821 if (hdl->libzfs_log_str) {
822 free(hdl->libzfs_log_str);
823 hdl->libzfs_log_str = NULL;
824 }
825 zc->zc_history = 0;
826
827 return (error);
828 }
829
830 /*
831 * ================================================================
832 * API shared by zfs and zpool property management
833 * ================================================================
834 */
835
836 static void
837 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
838 {
839 zprop_list_t *pl = cbp->cb_proplist;
840 int i;
841 char *title;
842 size_t len;
843
844 cbp->cb_first = B_FALSE;
845 if (cbp->cb_scripted)
846 return;
847
|
26
27 /*
28 * Internal utility routines for the ZFS library.
29 */
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <libintl.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <ctype.h>
40 #include <math.h>
41 #include <sys/mnttab.h>
42 #include <sys/mntent.h>
43 #include <sys/types.h>
44
45 #include <libzfs.h>
46 #include <libzfs_core.h>
47
48 #include "libzfs_impl.h"
49 #include "zfs_prop.h"
50 #include "zfeature_common.h"
51
52 int
53 libzfs_errno(libzfs_handle_t *hdl)
54 {
55 return (hdl->libzfs_error);
56 }
57
58 const char *
59 libzfs_error_action(libzfs_handle_t *hdl)
60 {
61 return (hdl->libzfs_action);
62 }
63
64 const char *
65 libzfs_error_description(libzfs_handle_t *hdl)
66 {
614 {
615 libzfs_handle_t *hdl;
616
617 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
618 return (NULL);
619 }
620
621 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
622 free(hdl);
623 return (NULL);
624 }
625
626 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
627 (void) close(hdl->libzfs_fd);
628 free(hdl);
629 return (NULL);
630 }
631
632 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
633
634 if (libzfs_core_init() != 0) {
635 (void) close(hdl->libzfs_fd);
636 (void) fclose(hdl->libzfs_mnttab);
637 (void) fclose(hdl->libzfs_sharetab);
638 free(hdl);
639 return (NULL);
640 }
641
642 zfs_prop_init();
643 zpool_prop_init();
644 zpool_feature_init();
645 libzfs_mnttab_init(hdl);
646
647 return (hdl);
648 }
649
650 void
651 libzfs_fini(libzfs_handle_t *hdl)
652 {
653 (void) close(hdl->libzfs_fd);
654 if (hdl->libzfs_mnttab)
655 (void) fclose(hdl->libzfs_mnttab);
656 if (hdl->libzfs_sharetab)
657 (void) fclose(hdl->libzfs_sharetab);
658 zfs_uninit_libshare(hdl);
659 zpool_free_handles(hdl);
660 libzfs_fru_clear(hdl, B_TRUE);
661 namespace_clear(hdl);
662 libzfs_mnttab_fini(hdl);
663 libzfs_core_fini();
664 free(hdl);
665 }
666
667 libzfs_handle_t *
668 zpool_get_handle(zpool_handle_t *zhp)
669 {
670 return (zhp->zpool_hdl);
671 }
672
673 libzfs_handle_t *
674 zfs_get_handle(zfs_handle_t *zhp)
675 {
676 return (zhp->zfs_hdl);
677 }
678
679 zpool_handle_t *
680 zfs_get_pool_handle(const zfs_handle_t *zhp)
681 {
682 return (zhp->zpool_hdl);
683 }
805 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
806 &zc->zc_nvlist_src_size, nvl));
807 }
808
809 /*
810 * Unpacks an nvlist from the ZFS ioctl command structure.
811 */
812 int
813 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
814 {
815 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
816 zc->zc_nvlist_dst_size, nvlp, 0) != 0)
817 return (no_memory(hdl));
818
819 return (0);
820 }
821
822 int
823 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
824 {
825 return (ioctl(hdl->libzfs_fd, request, zc));
826 }
827
828 /*
829 * ================================================================
830 * API shared by zfs and zpool property management
831 * ================================================================
832 */
833
834 static void
835 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
836 {
837 zprop_list_t *pl = cbp->cb_proplist;
838 int i;
839 char *title;
840 size_t len;
841
842 cbp->cb_first = B_FALSE;
843 if (cbp->cb_scripted)
844 return;
845
|