xref: /freebsd/lib/libbe/be_error.c (revision b6e7c421b75c07ddb0449e1692198dbb3c36547c)
1b179da01SKyle Evans /*-
2b179da01SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
328f16a0fSKyle Evans  *
428f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
528f16a0fSKyle Evans  * All rights reserved.
628f16a0fSKyle Evans  *
728f16a0fSKyle Evans  * Redistribution and use in source and binary forms, with or without
828f16a0fSKyle Evans  * modification, are permitted provided that the following conditions
928f16a0fSKyle Evans  * are met:
1028f16a0fSKyle Evans  * 1. Redistributions of source code must retain the above copyright
1128f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer.
1228f16a0fSKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
1328f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer in the
1428f16a0fSKyle Evans  *    documentation and/or other materials provided with the distribution.
1528f16a0fSKyle Evans  *
1628f16a0fSKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1728f16a0fSKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1828f16a0fSKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1928f16a0fSKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2028f16a0fSKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2128f16a0fSKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2228f16a0fSKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2328f16a0fSKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2428f16a0fSKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2528f16a0fSKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2628f16a0fSKyle Evans  * SUCH DAMAGE.
2728f16a0fSKyle Evans  */
2828f16a0fSKyle Evans 
29*b6e7c421SKyle Evans #include <sys/cdefs.h>
30*b6e7c421SKyle Evans __FBSDID("$FreeBSD$");
31*b6e7c421SKyle Evans 
3228f16a0fSKyle Evans #include "be.h"
3328f16a0fSKyle Evans #include "be_impl.h"
3428f16a0fSKyle Evans 
3528f16a0fSKyle Evans /*
3628f16a0fSKyle Evans  * Usage
3728f16a0fSKyle Evans  */
3828f16a0fSKyle Evans int
3928f16a0fSKyle Evans libbe_errno(libbe_handle_t *lbh)
4028f16a0fSKyle Evans {
41bfe0869cSKyle Evans 
4228f16a0fSKyle Evans 	return (lbh->error);
4328f16a0fSKyle Evans }
4428f16a0fSKyle Evans 
4528f16a0fSKyle Evans 
4628f16a0fSKyle Evans const char *
4728f16a0fSKyle Evans libbe_error_description(libbe_handle_t *lbh)
4828f16a0fSKyle Evans {
49bfe0869cSKyle Evans 
5028f16a0fSKyle Evans 	switch (lbh->error) {
5128f16a0fSKyle Evans 	case BE_ERR_INVALIDNAME:
5228f16a0fSKyle Evans 		return ("invalid boot environment name");
5328f16a0fSKyle Evans 
5428f16a0fSKyle Evans 	case BE_ERR_EXISTS:
5528f16a0fSKyle Evans 		return ("boot environment name already taken");
5628f16a0fSKyle Evans 
5728f16a0fSKyle Evans 	case BE_ERR_NOENT:
5828f16a0fSKyle Evans 		return ("specified boot environment does not exist");
5928f16a0fSKyle Evans 
6028f16a0fSKyle Evans 	case BE_ERR_PERMS:
6128f16a0fSKyle Evans 		return ("insufficient permissions");
6228f16a0fSKyle Evans 
6328f16a0fSKyle Evans 	case BE_ERR_DESTROYACT:
6428f16a0fSKyle Evans 		return ("cannot destroy active boot environment");
6528f16a0fSKyle Evans 
6628f16a0fSKyle Evans 	case BE_ERR_DESTROYMNT:
6728f16a0fSKyle Evans 		return ("cannot destroy mounted boot env unless forced");
6828f16a0fSKyle Evans 
6928f16a0fSKyle Evans 	case BE_ERR_PATHLEN:
7028f16a0fSKyle Evans 		return ("provided path name exceeds maximum length limit");
7128f16a0fSKyle Evans 
7228f16a0fSKyle Evans 	case BE_ERR_INVORIGIN:
7328f16a0fSKyle Evans 		return ("snapshot origin's mountpoint is not \"/\"");
7428f16a0fSKyle Evans 
7528f16a0fSKyle Evans 	case BE_ERR_NOORIGIN:
7628f16a0fSKyle Evans 		return ("could not open snapshot's origin");
7728f16a0fSKyle Evans 
7828f16a0fSKyle Evans 	case BE_ERR_MOUNTED:
7928f16a0fSKyle Evans 		return ("boot environment is already mounted");
8028f16a0fSKyle Evans 
8128f16a0fSKyle Evans 	case BE_ERR_NOMOUNT:
8228f16a0fSKyle Evans 		return ("boot environment is not mounted");
8328f16a0fSKyle Evans 
8428f16a0fSKyle Evans 	case BE_ERR_ZFSOPEN:
8528f16a0fSKyle Evans 		return ("calling zfs_open() failed");
8628f16a0fSKyle Evans 
8728f16a0fSKyle Evans 	case BE_ERR_ZFSCLONE:
8828f16a0fSKyle Evans 		return ("error when calling zfs_clone() to create boot env");
8928f16a0fSKyle Evans 
9028f16a0fSKyle Evans 	case BE_ERR_UNKNOWN:
9128f16a0fSKyle Evans 		return ("unknown error");
9228f16a0fSKyle Evans 
9328f16a0fSKyle Evans 	default:
9428f16a0fSKyle Evans 		assert(lbh->error == BE_ERR_SUCCESS);
9528f16a0fSKyle Evans 		return ("no error");
9628f16a0fSKyle Evans 	}
9728f16a0fSKyle Evans }
9828f16a0fSKyle Evans 
9928f16a0fSKyle Evans 
10028f16a0fSKyle Evans void
10128f16a0fSKyle Evans libbe_print_on_error(libbe_handle_t *lbh, bool val)
10228f16a0fSKyle Evans {
103bfe0869cSKyle Evans 
10428f16a0fSKyle Evans 	lbh->print_on_err = val;
10528f16a0fSKyle Evans 	libzfs_print_on_error(lbh->lzh, val);
10628f16a0fSKyle Evans }
10728f16a0fSKyle Evans 
10828f16a0fSKyle Evans 
10928f16a0fSKyle Evans int
11028f16a0fSKyle Evans set_error(libbe_handle_t *lbh, be_error_t err)
11128f16a0fSKyle Evans {
11228f16a0fSKyle Evans 
11328f16a0fSKyle Evans 	lbh->error = err;
114bfe0869cSKyle Evans 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
11528f16a0fSKyle Evans 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
11628f16a0fSKyle Evans 
11728f16a0fSKyle Evans 	return (err);
11828f16a0fSKyle Evans }
119