xref: /titanic_52/usr/src/cmd/fs.d/autofs/ns_fnutils.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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*36e852a1SRaja Andra  * Common Development and Distribution License (the "License").
6*36e852a1SRaja Andra  * 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 /*
227c478bd9Sstevel@tonic-gate  * ns_fnutils.c
237c478bd9Sstevel@tonic-gate  *
24*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*36e852a1SRaja Andra  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
337c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
347c478bd9Sstevel@tonic-gate #include <xfn/xfn.h>
357c478bd9Sstevel@tonic-gate #include "automount.h"
367c478bd9Sstevel@tonic-gate #include "ns_fnutils.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * FNS file system reference and address types.  Each array is indexed
417c478bd9Sstevel@tonic-gate  * using the corresponding enumeration (reftype_t or addrtype_t).
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate const char *reftypes[] = {
447c478bd9Sstevel@tonic-gate 	"onc_fn_fs",
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate const char *addrtypes[] = {
487c478bd9Sstevel@tonic-gate 	"onc_fn_fs_mount",
497c478bd9Sstevel@tonic-gate 	"onc_fn_fs_host",
507c478bd9Sstevel@tonic-gate 	"onc_fn_fs_user",
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate FN_string_t		*empty_string = NULL;
557c478bd9Sstevel@tonic-gate FN_composite_name_t	*empty_cname = NULL;
567c478bd9Sstevel@tonic-gate FN_composite_name_t	*slash_cname = NULL;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int
607c478bd9Sstevel@tonic-gate init_fn(void)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	static mutex_t	init_lock = DEFAULTMUTEX;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (slash_cname != NULL) {
657c478bd9Sstevel@tonic-gate 		return (0);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	mutex_lock(&init_lock);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (empty_string == NULL) {
717c478bd9Sstevel@tonic-gate 		if ((empty_string = fn_string_create()) == NULL) {
727c478bd9Sstevel@tonic-gate 			log_mem_failure();
737c478bd9Sstevel@tonic-gate 			goto unlock;
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	if (empty_cname == NULL) {
777c478bd9Sstevel@tonic-gate 		if ((empty_cname = new_cname("")) == NULL) {
787c478bd9Sstevel@tonic-gate 			goto unlock;
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	if (slash_cname == NULL) {
827c478bd9Sstevel@tonic-gate 		if ((slash_cname = new_cname("/")) == NULL) {
837c478bd9Sstevel@tonic-gate 			goto unlock;
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate unlock:
877c478bd9Sstevel@tonic-gate 	mutex_unlock(&init_lock);
887c478bd9Sstevel@tonic-gate 	return ((slash_cname != NULL) ? 0 : -1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate FN_composite_name_t *
937c478bd9Sstevel@tonic-gate new_cname(const char *str)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	FN_string_t		*string;
967c478bd9Sstevel@tonic-gate 	FN_composite_name_t	*cname;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	string = fn_string_from_str((unsigned char *)str);
997c478bd9Sstevel@tonic-gate 	if (string == NULL) {
1007c478bd9Sstevel@tonic-gate 		if (verbose) {
1017c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not create FNS string object");
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 		return (NULL);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	cname = fn_composite_name_from_string(string);
1067c478bd9Sstevel@tonic-gate 	fn_string_destroy(string);
1077c478bd9Sstevel@tonic-gate 	if ((cname == NULL) && verbose) {
1087c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Could not create FNS composite name object");
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	return (cname);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate reftype_t
1157c478bd9Sstevel@tonic-gate reftype(const FN_ref_t *ref)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	reftype_t	rtype;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	for (rtype = 0; rtype < NUM_REFTYPES; rtype++) {
1207c478bd9Sstevel@tonic-gate 		if (ident_str_equal(fn_ref_type(ref), reftypes[rtype])) {
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	return (rtype);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate addrtype_t
1297c478bd9Sstevel@tonic-gate addrtype(const FN_ref_addr_t *addr)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	addrtype_t		atype;
1327c478bd9Sstevel@tonic-gate 	const FN_identifier_t	*ident = fn_ref_addr_type(addr);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	for (atype = 0; atype < NUM_ADDRTYPES; atype++) {
1357c478bd9Sstevel@tonic-gate 		if (ident_str_equal(ident, addrtypes[atype])) {
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	return (atype);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate bool_t
1447c478bd9Sstevel@tonic-gate ident_equal(const FN_identifier_t *id1, const FN_identifier_t *id2)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	return ((id1->format == id2->format) &&
1477c478bd9Sstevel@tonic-gate 	    (id1->length == id2->length) &&
1487c478bd9Sstevel@tonic-gate 	    (memcmp(id1->contents, id2->contents, id1->length) == 0));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate bool_t
1537c478bd9Sstevel@tonic-gate ident_str_equal(const FN_identifier_t *id, const char *str)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	return ((id->format == FN_ID_STRING) &&
1567c478bd9Sstevel@tonic-gate 	    (id->length == strlen(str)) &&
1577c478bd9Sstevel@tonic-gate 	    (strncmp(str, id->contents, id->length) == 0));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
1627c478bd9Sstevel@tonic-gate logstat(const FN_status_t *status, const char *msg1, const char *msg2)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	FN_string_t	*desc_string;
1657c478bd9Sstevel@tonic-gate 	const char	*desc = NULL;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (verbose) {
1687c478bd9Sstevel@tonic-gate 		desc_string = fn_status_description(status, DETAIL, NULL);
1697c478bd9Sstevel@tonic-gate 		if (desc_string != NULL) {
1707c478bd9Sstevel@tonic-gate 			desc = (const char *)fn_string_str(desc_string, NULL);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		if (desc == NULL) {
1737c478bd9Sstevel@tonic-gate 			desc = "(no status description)";
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "FNS %s %s: %s (%u)",
1767c478bd9Sstevel@tonic-gate 		    msg1, msg2, desc, fn_status_code(status));
1777c478bd9Sstevel@tonic-gate 		fn_string_destroy(desc_string);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate bool_t
1837c478bd9Sstevel@tonic-gate transient(const FN_status_t *status)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	unsigned int statcode;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	statcode = fn_status_code(status);
1887c478bd9Sstevel@tonic-gate 	if (statcode == FN_E_LINK_ERROR) {
1897c478bd9Sstevel@tonic-gate 		statcode = fn_status_link_code(status);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	switch (statcode) {
1927c478bd9Sstevel@tonic-gate 	case FN_E_COMMUNICATION_FAILURE:
1937c478bd9Sstevel@tonic-gate 	case FN_E_CTX_UNAVAILABLE:
1947c478bd9Sstevel@tonic-gate 	case FN_E_INSUFFICIENT_RESOURCES:
1957c478bd9Sstevel@tonic-gate 	case FN_E_INVALID_ENUM_HANDLE:
1967c478bd9Sstevel@tonic-gate 	case FN_E_PARTIAL_RESULT:
1977c478bd9Sstevel@tonic-gate 	case FN_E_UNSPECIFIED_ERROR:
1987c478bd9Sstevel@tonic-gate 		return (TRUE);
1997c478bd9Sstevel@tonic-gate 	default:
2007c478bd9Sstevel@tonic-gate 		return (FALSE);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate void
2067c478bd9Sstevel@tonic-gate log_mem_failure(void)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	if (verbose) {
2097c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed");
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate }
212