summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/autofs/ns_fnutils.c
blob: 0eebca1f4c9ebebe56bc3e03611ffa47391db9c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * ns_fnutils.c
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <synch.h>
#include <rpc/rpc.h>
#include <xfn/xfn.h>
#include "automount.h"
#include "ns_fnutils.h"


/*
 * FNS file system reference and address types.  Each array is indexed
 * using the corresponding enumeration (reftype_t or addrtype_t).
 */
const char *reftypes[] = {
	"onc_fn_fs",
};

const char *addrtypes[] = {
	"onc_fn_fs_mount",
	"onc_fn_fs_host",
	"onc_fn_fs_user",
};


FN_string_t		*empty_string = NULL;
FN_composite_name_t	*empty_cname = NULL;
FN_composite_name_t	*slash_cname = NULL;


int
init_fn(void)
{
	static mutex_t	init_lock = DEFAULTMUTEX;

	if (slash_cname != NULL) {
		return (0);
	}

	mutex_lock(&init_lock);

	if (empty_string == NULL) {
		if ((empty_string = fn_string_create()) == NULL) {
			log_mem_failure();
			goto unlock;
		}
	}
	if (empty_cname == NULL) {
		if ((empty_cname = new_cname("")) == NULL) {
			goto unlock;
		}
	}
	if (slash_cname == NULL) {
		if ((slash_cname = new_cname("/")) == NULL) {
			goto unlock;
		}
	}
unlock:
	mutex_unlock(&init_lock);
	return ((slash_cname != NULL) ? 0 : -1);
}


FN_composite_name_t *
new_cname(const char *str)
{
	FN_string_t		*string;
	FN_composite_name_t	*cname;

	string = fn_string_from_str((unsigned char *)str);
	if (string == NULL) {
		if (verbose) {
			syslog(LOG_ERR, "Could not create FNS string object");
		}
		return (NULL);
	}
	cname = fn_composite_name_from_string(string);
	fn_string_destroy(string);
	if ((cname == NULL) && verbose) {
		syslog(LOG_ERR, "Could not create FNS composite name object");
	}
	return (cname);
}


reftype_t
reftype(const FN_ref_t *ref)
{
	reftype_t	rtype;

	for (rtype = 0; rtype < NUM_REFTYPES; rtype++) {
		if (ident_str_equal(fn_ref_type(ref), reftypes[rtype])) {
			break;
		}
	}
	return (rtype);
}


addrtype_t
addrtype(const FN_ref_addr_t *addr)
{
	addrtype_t		atype;
	const FN_identifier_t	*ident = fn_ref_addr_type(addr);

	for (atype = 0; atype < NUM_ADDRTYPES; atype++) {
		if (ident_str_equal(ident, addrtypes[atype])) {
			break;
		}
	}
	return (atype);
}


bool_t
ident_equal(const FN_identifier_t *id1, const FN_identifier_t *id2)
{
	return ((id1->format == id2->format) &&
	    (id1->length == id2->length) &&
	    (memcmp(id1->contents, id2->contents, id1->length) == 0));
}


bool_t
ident_str_equal(const FN_identifier_t *id, const char *str)
{
	return ((id->format == FN_ID_STRING) &&
	    (id->length == strlen(str)) &&
	    (strncmp(str, id->contents, id->length) == 0));
}


void
logstat(const FN_status_t *status, const char *msg1, const char *msg2)
{
	FN_string_t	*desc_string;
	const char	*desc = NULL;

	if (verbose) {
		desc_string = fn_status_description(status, DETAIL, NULL);
		if (desc_string != NULL) {
			desc = (const char *)fn_string_str(desc_string, NULL);
		}
		if (desc == NULL) {
			desc = "(no status description)";
		}
		syslog(LOG_ERR, "FNS %s %s: %s (%u)",
		    msg1, msg2, desc, fn_status_code(status));
		fn_string_destroy(desc_string);
	}
}


bool_t
transient(const FN_status_t *status)
{
	unsigned int statcode;

	statcode = fn_status_code(status);
	if (statcode == FN_E_LINK_ERROR) {
		statcode = fn_status_link_code(status);
	}
	switch (statcode) {
	case FN_E_COMMUNICATION_FAILURE:
	case FN_E_CTX_UNAVAILABLE:
	case FN_E_INSUFFICIENT_RESOURCES:
	case FN_E_INVALID_ENUM_HANDLE:
	case FN_E_PARTIAL_RESULT:
	case FN_E_UNSPECIFIED_ERROR:
		return (TRUE);
	default:
		return (FALSE);
	}
}


void
log_mem_failure(void)
{
	if (verbose) {
		syslog(LOG_ERR, "Memory allocation failed");
	}
}