xref: /freebsd/lib/libbe/be_error.c (revision 50a1972ed878e14a0bb59c43f452058330865f74)
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 
29b6e7c421SKyle Evans #include <sys/cdefs.h>
30b6e7c421SKyle Evans __FBSDID("$FreeBSD$");
31b6e7c421SKyle 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 
69*50a1972eSKyle Evans 	case BE_ERR_BADPATH:
70*50a1972eSKyle Evans 		return ("path not suitable for operation");
71*50a1972eSKyle Evans 
72*50a1972eSKyle Evans 	case BE_ERR_PATHBUSY:
73*50a1972eSKyle Evans 		return ("specified path is busy");
74*50a1972eSKyle Evans 
7528f16a0fSKyle Evans 	case BE_ERR_PATHLEN:
7628f16a0fSKyle Evans 		return ("provided path name exceeds maximum length limit");
7728f16a0fSKyle Evans 
7828f16a0fSKyle Evans 	case BE_ERR_INVORIGIN:
7928f16a0fSKyle Evans 		return ("snapshot origin's mountpoint is not \"/\"");
8028f16a0fSKyle Evans 
8128f16a0fSKyle Evans 	case BE_ERR_NOORIGIN:
8228f16a0fSKyle Evans 		return ("could not open snapshot's origin");
8328f16a0fSKyle Evans 
8428f16a0fSKyle Evans 	case BE_ERR_MOUNTED:
8528f16a0fSKyle Evans 		return ("boot environment is already mounted");
8628f16a0fSKyle Evans 
8728f16a0fSKyle Evans 	case BE_ERR_NOMOUNT:
8828f16a0fSKyle Evans 		return ("boot environment is not mounted");
8928f16a0fSKyle Evans 
9028f16a0fSKyle Evans 	case BE_ERR_ZFSOPEN:
9128f16a0fSKyle Evans 		return ("calling zfs_open() failed");
9228f16a0fSKyle Evans 
9328f16a0fSKyle Evans 	case BE_ERR_ZFSCLONE:
9428f16a0fSKyle Evans 		return ("error when calling zfs_clone() to create boot env");
9528f16a0fSKyle Evans 
96*50a1972eSKyle Evans 	case BE_ERR_IO:
97*50a1972eSKyle Evans 		return ("input/output error");
98*50a1972eSKyle Evans 
9928f16a0fSKyle Evans 	case BE_ERR_UNKNOWN:
10028f16a0fSKyle Evans 		return ("unknown error");
10128f16a0fSKyle Evans 
10228f16a0fSKyle Evans 	default:
10328f16a0fSKyle Evans 		assert(lbh->error == BE_ERR_SUCCESS);
10428f16a0fSKyle Evans 		return ("no error");
10528f16a0fSKyle Evans 	}
10628f16a0fSKyle Evans }
10728f16a0fSKyle Evans 
10828f16a0fSKyle Evans 
10928f16a0fSKyle Evans void
11028f16a0fSKyle Evans libbe_print_on_error(libbe_handle_t *lbh, bool val)
11128f16a0fSKyle Evans {
112bfe0869cSKyle Evans 
11328f16a0fSKyle Evans 	lbh->print_on_err = val;
11428f16a0fSKyle Evans 	libzfs_print_on_error(lbh->lzh, val);
11528f16a0fSKyle Evans }
11628f16a0fSKyle Evans 
11728f16a0fSKyle Evans 
11828f16a0fSKyle Evans int
11928f16a0fSKyle Evans set_error(libbe_handle_t *lbh, be_error_t err)
12028f16a0fSKyle Evans {
12128f16a0fSKyle Evans 
12228f16a0fSKyle Evans 	lbh->error = err;
123bfe0869cSKyle Evans 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
12428f16a0fSKyle Evans 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
12528f16a0fSKyle Evans 
12628f16a0fSKyle Evans 	return (err);
12728f16a0fSKyle Evans }
128