1*5331Samw /*
2*5331Samw  * CDDL HEADER START
3*5331Samw  *
4*5331Samw  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7*5331Samw  *
8*5331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5331Samw  * or http://www.opensolaris.org/os/licensing.
10*5331Samw  * See the License for the specific language governing permissions
11*5331Samw  * and limitations under the License.
12*5331Samw  *
13*5331Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*5331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5331Samw  * If applicable, add the following below this CDDL HEADER, with the
16*5331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*5331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5331Samw  *
19*5331Samw  * CDDL HEADER END
20*5331Samw  */
21*5331Samw /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*5331Samw  * Use is subject to license terms.
24*5331Samw  */
25*5331Samw 
26*5331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*5331Samw 
28*5331Samw #include <c_synonyms.h>
29*5331Samw #include <unistd.h>
30*5331Samw #include <stdlib.h>
31*5331Samw #include <string.h>
32*5331Samw #include <libgen.h>
33*5331Samw #include <attr.h>
34*5331Samw #include <fcntl.h>
35*5331Samw #include <errno.h>
36*5331Samw #include "libcmdutils.h"
37*5331Samw 
38*5331Samw /*
39*5331Samw  * Returns the status of attempting to obtain the extended system
40*5331Samw  * attributes in the specified view.
41*5331Samw  *
42*5331Samw  * Note: If obtaining status for an extended attribute file, the caller must
43*5331Samw  * chdir into the hidden directory prior to calling sysattr_status().
44*5331Samw  *
45*5331Samw  * Returns 1 if the extended system attributes were obtained, otherwise
46*5331Samw  * returns 0.
47*5331Samw  */
48*5331Samw int
49*5331Samw sysattr_status(char *file, xattr_view_t view)
50*5331Samw {
51*5331Samw 	nvlist_t	*response;
52*5331Samw 	int		saveerrno;
53*5331Samw 	int		status;
54*5331Samw 
55*5331Samw 	if (nvlist_alloc(&response, NV_UNIQUE_NAME, 0) != 0) {
56*5331Samw 		return (0);
57*5331Samw 	}
58*5331Samw 
59*5331Samw 	status = getattrat(AT_FDCWD, view, file, &response);
60*5331Samw 
61*5331Samw 	saveerrno = errno;
62*5331Samw 	(void) nvlist_free(response);
63*5331Samw 	errno = saveerrno;
64*5331Samw 
65*5331Samw 	return (status == 0);
66*5331Samw }
67*5331Samw 
68*5331Samw /*
69*5331Samw  * Returns the type of the specified in file.  If the file name matches
70*5331Samw  * the name of either a read-only or read-write extended system attribute
71*5331Samw  * file then sysattr_type() returns the type of file:
72*5331Samw  * 	return value	file type
73*5331Samw  *	------------	---------
74*5331Samw  *	_RO_SATTR	read-only extended system attribute file
75*5331Samw  *	_RW_SATTR	read-write extended system attribute file
76*5331Samw  *	_NOT_SATTR	neither a read-only or read-write extended system
77*5331Samw  *			attribute file.
78*5331Samw  */
79*5331Samw int
80*5331Samw sysattr_type(char *file)
81*5331Samw {
82*5331Samw 	if (file == NULL) {
83*5331Samw 		errno = ENOENT;
84*5331Samw 		return (_NOT_SATTR);
85*5331Samw 	}
86*5331Samw 
87*5331Samw 	if (strcmp(basename(file), file) != 0) {
88*5331Samw 		errno = EINVAL;
89*5331Samw 		return (_NOT_SATTR);
90*5331Samw 	}
91*5331Samw 
92*5331Samw 	errno = 0;
93*5331Samw 	if (strcmp(file, VIEW_READONLY) == 0) {
94*5331Samw 		return (_RO_SATTR);
95*5331Samw 	} else if (strcmp(file, VIEW_READWRITE) == 0) {
96*5331Samw 		return (_RW_SATTR);
97*5331Samw 	} else {
98*5331Samw 		return (_NOT_SATTR);
99*5331Samw 	}
100*5331Samw }
101*5331Samw 
102*5331Samw /*
103*5331Samw  * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
104*5331Samw  * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
105*5331Samw  * can still try to figure out if extended system attributes are supported by
106*5331Samw  * testing for a valid extended system attribute file.
107*5331Samw  *
108*5331Samw  * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
109*5331Samw  *
110*5331Samw  * Returns 1 if the underlying file system supports extended system attributes,
111*5331Samw  * otherwise, returns -1.
112*5331Samw  */
113*5331Samw int
114*5331Samw sysattr_support(char *file, int name)
115*5331Samw {
116*5331Samw 	int	rc;
117*5331Samw 
118*5331Samw 	errno = 0;
119*5331Samw 	if ((name != _PC_SATTR_ENABLED) &&
120*5331Samw 	    (name != _PC_SATTR_EXISTS)) {
121*5331Samw 		errno = EINVAL;
122*5331Samw 		return (-1);
123*5331Samw 	}
124*5331Samw 	if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
125*5331Samw 		return (rc);
126*5331Samw 	}
127*5331Samw 	return (sysattr_status(file, XATTR_VIEW_READONLY));
128*5331Samw }
129