xref: /onnv-gate/usr/src/cmd/fs.d/autofs/ns_fnutils.c (revision 11262:b7ebfbf2359e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11262SRajagopal.Andra@Sun.COM  * Common Development and Distribution License (the "License").
6*11262SRajagopal.Andra@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * ns_fnutils.c
230Sstevel@tonic-gate  *
24*11262SRajagopal.Andra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*11262SRajagopal.Andra@Sun.COM  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate #include <synch.h>
330Sstevel@tonic-gate #include <rpc/rpc.h>
340Sstevel@tonic-gate #include <xfn/xfn.h>
350Sstevel@tonic-gate #include "automount.h"
360Sstevel@tonic-gate #include "ns_fnutils.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * FNS file system reference and address types.  Each array is indexed
410Sstevel@tonic-gate  * using the corresponding enumeration (reftype_t or addrtype_t).
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate const char *reftypes[] = {
440Sstevel@tonic-gate 	"onc_fn_fs",
450Sstevel@tonic-gate };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate const char *addrtypes[] = {
480Sstevel@tonic-gate 	"onc_fn_fs_mount",
490Sstevel@tonic-gate 	"onc_fn_fs_host",
500Sstevel@tonic-gate 	"onc_fn_fs_user",
510Sstevel@tonic-gate };
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate FN_string_t		*empty_string = NULL;
550Sstevel@tonic-gate FN_composite_name_t	*empty_cname = NULL;
560Sstevel@tonic-gate FN_composite_name_t	*slash_cname = NULL;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate int
init_fn(void)600Sstevel@tonic-gate init_fn(void)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	static mutex_t	init_lock = DEFAULTMUTEX;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (slash_cname != NULL) {
650Sstevel@tonic-gate 		return (0);
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	mutex_lock(&init_lock);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (empty_string == NULL) {
710Sstevel@tonic-gate 		if ((empty_string = fn_string_create()) == NULL) {
720Sstevel@tonic-gate 			log_mem_failure();
730Sstevel@tonic-gate 			goto unlock;
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	if (empty_cname == NULL) {
770Sstevel@tonic-gate 		if ((empty_cname = new_cname("")) == NULL) {
780Sstevel@tonic-gate 			goto unlock;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	if (slash_cname == NULL) {
820Sstevel@tonic-gate 		if ((slash_cname = new_cname("/")) == NULL) {
830Sstevel@tonic-gate 			goto unlock;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate unlock:
870Sstevel@tonic-gate 	mutex_unlock(&init_lock);
880Sstevel@tonic-gate 	return ((slash_cname != NULL) ? 0 : -1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate FN_composite_name_t *
new_cname(const char * str)930Sstevel@tonic-gate new_cname(const char *str)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	FN_string_t		*string;
960Sstevel@tonic-gate 	FN_composite_name_t	*cname;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	string = fn_string_from_str((unsigned char *)str);
990Sstevel@tonic-gate 	if (string == NULL) {
1000Sstevel@tonic-gate 		if (verbose) {
1010Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not create FNS string object");
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 		return (NULL);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	cname = fn_composite_name_from_string(string);
1060Sstevel@tonic-gate 	fn_string_destroy(string);
1070Sstevel@tonic-gate 	if ((cname == NULL) && verbose) {
1080Sstevel@tonic-gate 		syslog(LOG_ERR, "Could not create FNS composite name object");
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 	return (cname);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate reftype_t
reftype(const FN_ref_t * ref)1150Sstevel@tonic-gate reftype(const FN_ref_t *ref)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	reftype_t	rtype;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	for (rtype = 0; rtype < NUM_REFTYPES; rtype++) {
1200Sstevel@tonic-gate 		if (ident_str_equal(fn_ref_type(ref), reftypes[rtype])) {
1210Sstevel@tonic-gate 			break;
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	return (rtype);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate addrtype_t
addrtype(const FN_ref_addr_t * addr)1290Sstevel@tonic-gate addrtype(const FN_ref_addr_t *addr)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	addrtype_t		atype;
1320Sstevel@tonic-gate 	const FN_identifier_t	*ident = fn_ref_addr_type(addr);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	for (atype = 0; atype < NUM_ADDRTYPES; atype++) {
1350Sstevel@tonic-gate 		if (ident_str_equal(ident, addrtypes[atype])) {
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	return (atype);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate bool_t
ident_equal(const FN_identifier_t * id1,const FN_identifier_t * id2)1440Sstevel@tonic-gate ident_equal(const FN_identifier_t *id1, const FN_identifier_t *id2)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	return ((id1->format == id2->format) &&
147*11262SRajagopal.Andra@Sun.COM 	    (id1->length == id2->length) &&
148*11262SRajagopal.Andra@Sun.COM 	    (memcmp(id1->contents, id2->contents, id1->length) == 0));
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate bool_t
ident_str_equal(const FN_identifier_t * id,const char * str)1530Sstevel@tonic-gate ident_str_equal(const FN_identifier_t *id, const char *str)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	return ((id->format == FN_ID_STRING) &&
156*11262SRajagopal.Andra@Sun.COM 	    (id->length == strlen(str)) &&
157*11262SRajagopal.Andra@Sun.COM 	    (strncmp(str, id->contents, id->length) == 0));
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate void
logstat(const FN_status_t * status,const char * msg1,const char * msg2)1620Sstevel@tonic-gate logstat(const FN_status_t *status, const char *msg1, const char *msg2)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	FN_string_t	*desc_string;
1650Sstevel@tonic-gate 	const char	*desc = NULL;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (verbose) {
1680Sstevel@tonic-gate 		desc_string = fn_status_description(status, DETAIL, NULL);
1690Sstevel@tonic-gate 		if (desc_string != NULL) {
1700Sstevel@tonic-gate 			desc = (const char *)fn_string_str(desc_string, NULL);
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		if (desc == NULL) {
1730Sstevel@tonic-gate 			desc = "(no status description)";
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		syslog(LOG_ERR, "FNS %s %s: %s (%u)",
176*11262SRajagopal.Andra@Sun.COM 		    msg1, msg2, desc, fn_status_code(status));
1770Sstevel@tonic-gate 		fn_string_destroy(desc_string);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate bool_t
transient(const FN_status_t * status)1830Sstevel@tonic-gate transient(const FN_status_t *status)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	unsigned int statcode;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	statcode = fn_status_code(status);
1880Sstevel@tonic-gate 	if (statcode == FN_E_LINK_ERROR) {
1890Sstevel@tonic-gate 		statcode = fn_status_link_code(status);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	switch (statcode) {
1920Sstevel@tonic-gate 	case FN_E_COMMUNICATION_FAILURE:
1930Sstevel@tonic-gate 	case FN_E_CTX_UNAVAILABLE:
1940Sstevel@tonic-gate 	case FN_E_INSUFFICIENT_RESOURCES:
1950Sstevel@tonic-gate 	case FN_E_INVALID_ENUM_HANDLE:
1960Sstevel@tonic-gate 	case FN_E_PARTIAL_RESULT:
1970Sstevel@tonic-gate 	case FN_E_UNSPECIFIED_ERROR:
1980Sstevel@tonic-gate 		return (TRUE);
1990Sstevel@tonic-gate 	default:
2000Sstevel@tonic-gate 		return (FALSE);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate void
log_mem_failure(void)2060Sstevel@tonic-gate log_mem_failure(void)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	if (verbose) {
2090Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed");
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate }
212