xref: /illumos-gate/usr/src/lib/libipmp/common/ipmp.c (revision e11c3f44)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*e11c3f44Smeem  * Common Development and Distribution License (the "License").
6*e11c3f44Smeem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * IPMP general interfaces (PSARC/2002/615).
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <assert.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
34*e11c3f44Smeem #include <string.h>
35*e11c3f44Smeem #include <errno.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "ipmp_impl.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Allocate a handle and store it in `*handlep' upon success.  Returns an IPMP
417c478bd9Sstevel@tonic-gate  * error code.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate int
ipmp_open(ipmp_handle_t * handlep)447c478bd9Sstevel@tonic-gate ipmp_open(ipmp_handle_t *handlep)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	ipmp_state_t	*statep;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	statep = malloc(sizeof (ipmp_state_t));
497c478bd9Sstevel@tonic-gate 	if (statep == NULL)
507c478bd9Sstevel@tonic-gate 		return (IPMP_ENOMEM);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	statep->st_fd = -1;
537c478bd9Sstevel@tonic-gate 	statep->st_snap = NULL;
547c478bd9Sstevel@tonic-gate 	statep->st_magic = IPMP_MAGIC;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	*handlep = statep;
577c478bd9Sstevel@tonic-gate 	return (IPMP_SUCCESS);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Destroy the IPMP handle named by `handle'.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate void
ipmp_close(ipmp_handle_t handle)647c478bd9Sstevel@tonic-gate ipmp_close(ipmp_handle_t handle)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	ipmp_state_t	*statep = handle;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * If this assertion triggers, someone called ipmp_close() twice in
707c478bd9Sstevel@tonic-gate 	 * a row or stomped on us.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	assert(statep->st_magic == IPMP_MAGIC);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * If this assertion triggers, something's gone wrong internally.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	assert(statep->st_fd == -1);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (statep->st_snap != NULL)
807c478bd9Sstevel@tonic-gate 		ipmp_snap_free(statep->st_snap);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	statep->st_magic = 0;
837c478bd9Sstevel@tonic-gate 	free(statep);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Error messages; must be in the same order as the codes in <ipmp.h>
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate static char *errmsgs[IPMP_NERR] = {
907c478bd9Sstevel@tonic-gate 	"operation succeeded", 			/*  0 IPMP_SUCCESS	*/
917c478bd9Sstevel@tonic-gate 	"operation failed",			/*  1 IPMP_FAILURE	*/
927c478bd9Sstevel@tonic-gate 	"minimum failover redundancy not met",	/*  2 IPMP_EMINRED	*/
937c478bd9Sstevel@tonic-gate 	"failback disabled",			/*  3 IPMP_EFBDISABLED  */
94*e11c3f44Smeem 	"unknown IPMP data address", 		/*  4 IPMP_EUNKADDR	*/
957c478bd9Sstevel@tonic-gate 	"invalid argument",			/*  5 IPMP_EINVAL	*/
967c478bd9Sstevel@tonic-gate 	"out of memory",			/*  6 IPMP_ENOMEM	*/
977c478bd9Sstevel@tonic-gate 	"cannot contact in.mpathd",		/*  7 IPMP_ENOMPATHD	*/
987c478bd9Sstevel@tonic-gate 	"unknown IPMP group", 			/*  8 IPMP_EUNKGROUP	*/
997c478bd9Sstevel@tonic-gate 	"interface is not using IPMP",		/*  9 IPMP_EUNKIF	*/
100*e11c3f44Smeem 	"unable to communicate with in.mpathd",	/* 10 IPMP_EPROTO	*/
101*e11c3f44Smeem 	"interface has duplicate hardware address"
102*e11c3f44Smeem 						/* 11 IPMP_EHWADDRDUP	*/
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Return a string describing the IPMP error code named by `error'.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate const char *
ipmp_errmsg(int error)1097c478bd9Sstevel@tonic-gate ipmp_errmsg(int error)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	if (error >= IPMP_NERR || error < 0)
1127c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "<unknown error>"));
1137c478bd9Sstevel@tonic-gate 
114*e11c3f44Smeem 	if (error == IPMP_FAILURE)
115*e11c3f44Smeem 		return (strerror(errno));
116*e11c3f44Smeem 
1177c478bd9Sstevel@tonic-gate 	return (dgettext(TEXT_DOMAIN, errmsgs[error]));
1187c478bd9Sstevel@tonic-gate }
119