1 /* 2 * be_access.c 3 * 4 * Copyright (c) 2017 Kyle J. Kneitinger <[email protected]> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "be.h" 30 #include "be_impl.h" 31 32 /* 33 * usage 34 */ 35 int 36 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags, 37 char *result_loc) 38 { 39 char be[BE_MAXPATHLEN]; 40 char mnt_temp[BE_MAXPATHLEN]; 41 char *path; 42 int mntflags; 43 int err; 44 45 if ((err = be_root_concat(lbh, bootenv, be)) != 0) 46 return (set_error(lbh, err)); 47 48 if (!be_exists(lbh, bootenv)) 49 return (set_error(lbh, BE_ERR_NOENT)); 50 51 if (is_mounted(lbh->lzh, be, &path)) 52 return (set_error(lbh, BE_ERR_MOUNTED)); 53 54 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; 55 56 /* Create mountpoint if it is not specified */ 57 if (mountpoint == NULL) { 58 strcpy(mnt_temp, "/tmp/be_mount.XXXX"); 59 if (mkdtemp(mnt_temp) == NULL) 60 /* XXX TODO: create error for this */ 61 return (set_error(lbh, BE_ERR_UNKNOWN)); 62 } 63 64 char opt = '\0'; 65 if ((err = zmount(be, (mountpoint == NULL) ? mnt_temp : mountpoint, 66 mntflags, __DECONST(char *, MNTTYPE_ZFS), NULL, 0, &opt, 1)) != 0) 67 /* 68 * XXX TODO: zmount returns the nmount error, look into what 69 * kind of errors we can report from that 70 */ 71 return (set_error(lbh, BE_ERR_UNKNOWN)); 72 73 if (result_loc != NULL) 74 strcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint); 75 76 return (BE_ERR_SUCCESS); 77 } 78 79 80 /* 81 * usage 82 */ 83 int 84 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags) 85 { 86 int err, mntflags; 87 char be[BE_MAXPATHLEN]; 88 struct statfs *mntbuf; 89 int mntsize; 90 char *mntpath; 91 92 if ((err = be_root_concat(lbh, bootenv, be)) != 0) 93 return (set_error(lbh, err)); 94 95 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 96 /* XXX TODO correct error */ 97 return (set_error(lbh, BE_ERR_NOMOUNT)); 98 99 mntpath = NULL; 100 for (int i = 0; i < mntsize; ++i) { 101 /* 0x000000de is the type number of zfs */ 102 if (mntbuf[i].f_type != 0x000000de) 103 continue; 104 105 if (strcmp(mntbuf[i].f_mntfromname, be) == 0) { 106 mntpath = mntbuf[i].f_mntonname; 107 break; 108 } 109 } 110 111 if (mntpath == NULL) 112 return (set_error(lbh, BE_ERR_NOMOUNT)); 113 114 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; 115 116 if ((err = unmount(mntpath, mntflags)) != 0) 117 /* XXX TODO correct error */ 118 return (set_error(lbh, BE_ERR_NOMOUNT)); 119 120 return (set_error(lbh, BE_ERR_SUCCESS)); 121 } 122