8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / autofs / ns_fnutils.c
blob0eebca1f4c9ebebe56bc3e03611ffa47391db9c4
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * ns_fnutils.c
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <synch.h>
33 #include <rpc/rpc.h>
34 #include <xfn/xfn.h>
35 #include "automount.h"
36 #include "ns_fnutils.h"
40 * FNS file system reference and address types. Each array is indexed
41 * using the corresponding enumeration (reftype_t or addrtype_t).
43 const char *reftypes[] = {
44 "onc_fn_fs",
47 const char *addrtypes[] = {
48 "onc_fn_fs_mount",
49 "onc_fn_fs_host",
50 "onc_fn_fs_user",
54 FN_string_t *empty_string = NULL;
55 FN_composite_name_t *empty_cname = NULL;
56 FN_composite_name_t *slash_cname = NULL;
59 int
60 init_fn(void)
62 static mutex_t init_lock = DEFAULTMUTEX;
64 if (slash_cname != NULL) {
65 return (0);
68 mutex_lock(&init_lock);
70 if (empty_string == NULL) {
71 if ((empty_string = fn_string_create()) == NULL) {
72 log_mem_failure();
73 goto unlock;
76 if (empty_cname == NULL) {
77 if ((empty_cname = new_cname("")) == NULL) {
78 goto unlock;
81 if (slash_cname == NULL) {
82 if ((slash_cname = new_cname("/")) == NULL) {
83 goto unlock;
86 unlock:
87 mutex_unlock(&init_lock);
88 return ((slash_cname != NULL) ? 0 : -1);
92 FN_composite_name_t *
93 new_cname(const char *str)
95 FN_string_t *string;
96 FN_composite_name_t *cname;
98 string = fn_string_from_str((unsigned char *)str);
99 if (string == NULL) {
100 if (verbose) {
101 syslog(LOG_ERR, "Could not create FNS string object");
103 return (NULL);
105 cname = fn_composite_name_from_string(string);
106 fn_string_destroy(string);
107 if ((cname == NULL) && verbose) {
108 syslog(LOG_ERR, "Could not create FNS composite name object");
110 return (cname);
114 reftype_t
115 reftype(const FN_ref_t *ref)
117 reftype_t rtype;
119 for (rtype = 0; rtype < NUM_REFTYPES; rtype++) {
120 if (ident_str_equal(fn_ref_type(ref), reftypes[rtype])) {
121 break;
124 return (rtype);
128 addrtype_t
129 addrtype(const FN_ref_addr_t *addr)
131 addrtype_t atype;
132 const FN_identifier_t *ident = fn_ref_addr_type(addr);
134 for (atype = 0; atype < NUM_ADDRTYPES; atype++) {
135 if (ident_str_equal(ident, addrtypes[atype])) {
136 break;
139 return (atype);
143 bool_t
144 ident_equal(const FN_identifier_t *id1, const FN_identifier_t *id2)
146 return ((id1->format == id2->format) &&
147 (id1->length == id2->length) &&
148 (memcmp(id1->contents, id2->contents, id1->length) == 0));
152 bool_t
153 ident_str_equal(const FN_identifier_t *id, const char *str)
155 return ((id->format == FN_ID_STRING) &&
156 (id->length == strlen(str)) &&
157 (strncmp(str, id->contents, id->length) == 0));
161 void
162 logstat(const FN_status_t *status, const char *msg1, const char *msg2)
164 FN_string_t *desc_string;
165 const char *desc = NULL;
167 if (verbose) {
168 desc_string = fn_status_description(status, DETAIL, NULL);
169 if (desc_string != NULL) {
170 desc = (const char *)fn_string_str(desc_string, NULL);
172 if (desc == NULL) {
173 desc = "(no status description)";
175 syslog(LOG_ERR, "FNS %s %s: %s (%u)",
176 msg1, msg2, desc, fn_status_code(status));
177 fn_string_destroy(desc_string);
182 bool_t
183 transient(const FN_status_t *status)
185 unsigned int statcode;
187 statcode = fn_status_code(status);
188 if (statcode == FN_E_LINK_ERROR) {
189 statcode = fn_status_link_code(status);
191 switch (statcode) {
192 case FN_E_COMMUNICATION_FAILURE:
193 case FN_E_CTX_UNAVAILABLE:
194 case FN_E_INSUFFICIENT_RESOURCES:
195 case FN_E_INVALID_ENUM_HANDLE:
196 case FN_E_PARTIAL_RESULT:
197 case FN_E_UNSPECIFIED_ERROR:
198 return (TRUE);
199 default:
200 return (FALSE);
205 void
206 log_mem_failure(void)
208 if (verbose) {
209 syslog(LOG_ERR, "Memory allocation failed");