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