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