4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
25 #include "../../libzfs_impl.h"
28 #include <sys/sysctl.h>
30 #include <sys/linker.h>
31 #include <sys/module.h>
33 #include <sys/param.h>
36 #define ZFS_KMOD "zfs"
38 #define ZFS_KMOD "openzfs"
43 execvPe(const char *name, const char *path, char * const *argv,
48 int eacces, save_errno;
50 const char *bp, *np, *op, *p;
55 /* If it's an absolute or relative path name, it's easy. */
56 if (strchr(name, '/')) {
63 /* If it's an empty path name, fail in the usual POSIX way. */
72 np = strchrnul(op, ':');
75 * It's a SHELL path -- double, leading and trailing colons
76 * mean the current directory.
79 /* Empty component. */
83 /* Non-empty component. */
88 /* Advance to the next component or terminate after this. */
95 * If the path is too long complain. This is a possible
96 * security issue; given a way to make the path too long
97 * the user may execute the wrong program.
99 if (lp + ln + 2 > sizeof (buf)) {
100 (void) write(STDERR_FILENO, "execvP: ", 8);
101 (void) write(STDERR_FILENO, p, lp);
102 (void) write(STDERR_FILENO, ": path too long\n",
108 memcpy(buf + lp + 1, name, ln);
109 buf[lp + ln + 1] = '\0';
111 retry: (void) execve(bp, argv, envp);
120 for (cnt = 0; argv[cnt]; ++cnt)
124 * cnt may be 0 above; always allocate at least
125 * 3 entries so that we can at least fit "sh", bp, and
126 * the NULL terminator. We can rely on cnt to take into
127 * account the NULL terminator in all other scenarios,
128 * as we drop argv[0].
130 memp = alloca(MAX(3, cnt + 2) * sizeof (char *));
132 /* errno = ENOMEM; XXX override ENOEXEC? */
138 memcpy(memp + 2, argv + 1,
139 cnt * sizeof (char *));
145 (void) execve(_PATH_BSHELL,
146 __DECONST(char **, memp), envp);
154 * We used to retry here, but sh(1) doesn't.
159 * EACCES may be for an inaccessible directory or
160 * a non-executable file. Call stat() to decide
161 * which. This also handles ambiguities for EFAULT
162 * and EIO, and undocumented errors like ESTALE.
163 * We hope that the race for a stat() is unimportant.
166 if (stat(bp, &sb) != 0)
168 if (save_errno == EACCES) {
185 execvpe(const char *name, char * const argv[], char * const envp[])
189 /* Get the path we're searching. */
190 if ((path = getenv("PATH")) == NULL)
191 path = _PATH_DEFPATH;
193 return (execvPe(name, path, argv, envp));
196 static __thread char errbuf[ERRBUFLEN];
199 libzfs_error_init(int error)
202 size_t msglen = sizeof (errbuf);
204 if (modfind("zfs") < 0) {
205 size_t len = snprintf(msg, msglen, dgettext(TEXT_DOMAIN,
206 "Failed to load %s module: "), ZFS_KMOD);
211 (void) snprintf(msg, msglen, "%s", strerror(error));
217 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
219 return (lzc_ioctl_fd(hdl->libzfs_fd, request, zc));
223 * Verify the required ZFS_DEV device is available and optionally attempt
224 * to load the ZFS modules. Under normal circumstances the modules
225 * should already have been loaded by some external mechanism.
228 libzfs_load_module(void)
231 * XXX: kldfind(ZFS_KMOD) would be nice here, but we retain
232 * modfind("zfs") so out-of-base openzfs userland works with the
235 if (modfind("zfs") < 0) {
236 /* Not present in kernel, try loading it. */
237 if (kldload(ZFS_KMOD) < 0 && errno != EEXIST) {
245 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
247 (void) hdl, (void) path, (void) msg;
252 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
254 (void) hdl, (void) zhp, (void) name;
259 find_shares_object(differ_info_t *di)
266 zfs_destroy_snaps_nvl_os(libzfs_handle_t *hdl, nvlist_t *snaps)
268 (void) hdl, (void) snaps;
273 * Attach/detach the given filesystem to/from the given jail.
276 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
278 libzfs_handle_t *hdl = zhp->zfs_hdl;
279 zfs_cmd_t zc = {"\0"};
284 (void) snprintf(errbuf, sizeof (errbuf),
285 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
287 (void) snprintf(errbuf, sizeof (errbuf),
288 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
291 switch (zhp->zfs_type) {
292 case ZFS_TYPE_VOLUME:
293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
294 "volumes can not be jailed"));
295 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
296 case ZFS_TYPE_SNAPSHOT:
297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
298 "snapshots can not be jailed"));
299 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
300 case ZFS_TYPE_BOOKMARK:
301 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
302 "bookmarks can not be jailed"));
303 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
306 "vdevs can not be jailed"));
307 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
308 case ZFS_TYPE_INVALID:
309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
310 "invalid zfs_type_t: ZFS_TYPE_INVALID"));
311 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
313 case ZFS_TYPE_FILESYSTEM:
317 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
319 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
320 zc.zc_objset_type = DMU_OST_ZFS;
321 zc.zc_zoneid = jailid;
323 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
324 if ((ret = zfs_ioctl(hdl, cmd, &zc)) != 0)
325 zfs_standard_error(hdl, errno, errbuf);
331 * Set loader options for next boot.
334 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
337 zfs_cmd_t zc = {"\0"};
340 args = fnvlist_alloc();
341 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
342 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
343 fnvlist_add_string(args, "command", command);
344 zcmd_write_src_nvlist(hdl, &zc, args);
345 int error = zfs_ioctl(hdl, ZFS_IOC_NEXTBOOT, &zc);
346 zcmd_free_nvlists(&zc);
352 * Return allocated loaded module version, or NULL on error (with errno set)
355 zfs_version_kernel(void)
358 if (sysctlbyname("vfs.zfs.version.module",
359 NULL, &l, NULL, 0) == -1)
361 char *version = malloc(l);
364 if (sysctlbyname("vfs.zfs.version.module",
365 version, &l, NULL, 0) == -1) {