xref: /titanic_41/usr/src/cmd/fs.d/autofs/ns_fnutils.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ns_fnutils.c
24  *
25  * Copyright (c) 1995, 1996, by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <synch.h>
36 #include <rpc/rpc.h>
37 #include <xfn/xfn.h>
38 #include "automount.h"
39 #include "ns_fnutils.h"
40 
41 
42 /*
43  * FNS file system reference and address types.  Each array is indexed
44  * using the corresponding enumeration (reftype_t or addrtype_t).
45  */
46 const char *reftypes[] = {
47 	"onc_fn_fs",
48 };
49 
50 const char *addrtypes[] = {
51 	"onc_fn_fs_mount",
52 	"onc_fn_fs_host",
53 	"onc_fn_fs_user",
54 	"onc_fn_fs_user_nisplus",
55 };
56 
57 
58 FN_string_t		*empty_string = NULL;
59 FN_composite_name_t	*empty_cname = NULL;
60 FN_composite_name_t	*slash_cname = NULL;
61 
62 
63 int
64 init_fn(void)
65 {
66 	static mutex_t	init_lock = DEFAULTMUTEX;
67 
68 	if (slash_cname != NULL) {
69 		return (0);
70 	}
71 
72 	mutex_lock(&init_lock);
73 
74 	if (empty_string == NULL) {
75 		if ((empty_string = fn_string_create()) == NULL) {
76 			log_mem_failure();
77 			goto unlock;
78 		}
79 	}
80 	if (empty_cname == NULL) {
81 		if ((empty_cname = new_cname("")) == NULL) {
82 			goto unlock;
83 		}
84 	}
85 	if (slash_cname == NULL) {
86 		if ((slash_cname = new_cname("/")) == NULL) {
87 			goto unlock;
88 		}
89 	}
90 unlock:
91 	mutex_unlock(&init_lock);
92 	return ((slash_cname != NULL) ? 0 : -1);
93 }
94 
95 
96 FN_composite_name_t *
97 new_cname(const char *str)
98 {
99 	FN_string_t		*string;
100 	FN_composite_name_t	*cname;
101 
102 	string = fn_string_from_str((unsigned char *)str);
103 	if (string == NULL) {
104 		if (verbose) {
105 			syslog(LOG_ERR, "Could not create FNS string object");
106 		}
107 		return (NULL);
108 	}
109 	cname = fn_composite_name_from_string(string);
110 	fn_string_destroy(string);
111 	if ((cname == NULL) && verbose) {
112 		syslog(LOG_ERR, "Could not create FNS composite name object");
113 	}
114 	return (cname);
115 }
116 
117 
118 reftype_t
119 reftype(const FN_ref_t *ref)
120 {
121 	reftype_t	rtype;
122 
123 	for (rtype = 0; rtype < NUM_REFTYPES; rtype++) {
124 		if (ident_str_equal(fn_ref_type(ref), reftypes[rtype])) {
125 			break;
126 		}
127 	}
128 	return (rtype);
129 }
130 
131 
132 addrtype_t
133 addrtype(const FN_ref_addr_t *addr)
134 {
135 	addrtype_t		atype;
136 	const FN_identifier_t	*ident = fn_ref_addr_type(addr);
137 
138 	for (atype = 0; atype < NUM_ADDRTYPES; atype++) {
139 		if (ident_str_equal(ident, addrtypes[atype])) {
140 			break;
141 		}
142 	}
143 	return (atype);
144 }
145 
146 
147 bool_t
148 ident_equal(const FN_identifier_t *id1, const FN_identifier_t *id2)
149 {
150 	return ((id1->format == id2->format) &&
151 		(id1->length == id2->length) &&
152 		(memcmp(id1->contents, id2->contents, id1->length) == 0));
153 }
154 
155 
156 bool_t
157 ident_str_equal(const FN_identifier_t *id, const char *str)
158 {
159 	return ((id->format == FN_ID_STRING) &&
160 		(id->length == strlen(str)) &&
161 		(strncmp(str, id->contents, id->length) == 0));
162 }
163 
164 
165 void
166 logstat(const FN_status_t *status, const char *msg1, const char *msg2)
167 {
168 	FN_string_t	*desc_string;
169 	const char	*desc = NULL;
170 
171 	if (verbose) {
172 		desc_string = fn_status_description(status, DETAIL, NULL);
173 		if (desc_string != NULL) {
174 			desc = (const char *)fn_string_str(desc_string, NULL);
175 		}
176 		if (desc == NULL) {
177 			desc = "(no status description)";
178 		}
179 		syslog(LOG_ERR, "FNS %s %s: %s (%u)",
180 				msg1, msg2, desc, fn_status_code(status));
181 		fn_string_destroy(desc_string);
182 	}
183 }
184 
185 
186 bool_t
187 transient(const FN_status_t *status)
188 {
189 	unsigned int statcode;
190 
191 	statcode = fn_status_code(status);
192 	if (statcode == FN_E_LINK_ERROR) {
193 		statcode = fn_status_link_code(status);
194 	}
195 	switch (statcode) {
196 	case FN_E_COMMUNICATION_FAILURE:
197 	case FN_E_CTX_UNAVAILABLE:
198 	case FN_E_INSUFFICIENT_RESOURCES:
199 	case FN_E_INVALID_ENUM_HANDLE:
200 	case FN_E_PARTIAL_RESULT:
201 	case FN_E_UNSPECIFIED_ERROR:
202 		return (TRUE);
203 	default:
204 		return (FALSE);
205 	}
206 }
207 
208 
209 void
210 log_mem_failure(void)
211 {
212 	if (verbose) {
213 		syslog(LOG_ERR, "Memory allocation failed");
214 	}
215 }
216