xref: /titanic_41/usr/src/lib/libipmp/common/ipmp.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * IPMP general interfaces (PSARC/2002/615).
28  */
29 
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <locale.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 
37 #include "ipmp_impl.h"
38 
39 /*
40  * Allocate a handle and store it in `*handlep' upon success.  Returns an IPMP
41  * error code.
42  */
43 int
44 ipmp_open(ipmp_handle_t *handlep)
45 {
46 	ipmp_state_t	*statep;
47 
48 	statep = malloc(sizeof (ipmp_state_t));
49 	if (statep == NULL)
50 		return (IPMP_ENOMEM);
51 
52 	statep->st_fd = -1;
53 	statep->st_snap = NULL;
54 	statep->st_magic = IPMP_MAGIC;
55 
56 	*handlep = statep;
57 	return (IPMP_SUCCESS);
58 }
59 
60 /*
61  * Destroy the IPMP handle named by `handle'.
62  */
63 void
64 ipmp_close(ipmp_handle_t handle)
65 {
66 	ipmp_state_t	*statep = handle;
67 
68 	/*
69 	 * If this assertion triggers, someone called ipmp_close() twice in
70 	 * a row or stomped on us.
71 	 */
72 	assert(statep->st_magic == IPMP_MAGIC);
73 
74 	/*
75 	 * If this assertion triggers, something's gone wrong internally.
76 	 */
77 	assert(statep->st_fd == -1);
78 
79 	if (statep->st_snap != NULL)
80 		ipmp_snap_free(statep->st_snap);
81 
82 	statep->st_magic = 0;
83 	free(statep);
84 }
85 
86 /*
87  * Error messages; must be in the same order as the codes in <ipmp.h>
88  */
89 static char *errmsgs[IPMP_NERR] = {
90 	"operation succeeded", 			/*  0 IPMP_SUCCESS	*/
91 	"operation failed",			/*  1 IPMP_FAILURE	*/
92 	"minimum failover redundancy not met",	/*  2 IPMP_EMINRED	*/
93 	"failback disabled",			/*  3 IPMP_EFBDISABLED  */
94 	"unknown IPMP data address", 		/*  4 IPMP_EUNKADDR	*/
95 	"invalid argument",			/*  5 IPMP_EINVAL	*/
96 	"out of memory",			/*  6 IPMP_ENOMEM	*/
97 	"cannot contact in.mpathd",		/*  7 IPMP_ENOMPATHD	*/
98 	"unknown IPMP group", 			/*  8 IPMP_EUNKGROUP	*/
99 	"interface is not using IPMP",		/*  9 IPMP_EUNKIF	*/
100 	"unable to communicate with in.mpathd",	/* 10 IPMP_EPROTO	*/
101 	"interface has duplicate hardware address"
102 						/* 11 IPMP_EHWADDRDUP	*/
103 };
104 
105 /*
106  * Return a string describing the IPMP error code named by `error'.
107  */
108 const char *
109 ipmp_errmsg(int error)
110 {
111 	if (error >= IPMP_NERR || error < 0)
112 		return (dgettext(TEXT_DOMAIN, "<unknown error>"));
113 
114 	if (error == IPMP_FAILURE)
115 		return (strerror(errno));
116 
117 	return (dgettext(TEXT_DOMAIN, errmsgs[error]));
118 }
119