xref: /illumos-gate/usr/src/lib/libcmdutils/common/sysattrs.c (revision 5b080c3aadfabef156c72cedee796d7b3bac5180)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libgen.h>
31 #include <attr.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include "libcmdutils.h"
35 
36 /*
37  * Returns the status of attempting to obtain the extended system
38  * attributes in the specified view.
39  *
40  * Note: If obtaining status for an extended attribute file, the caller must
41  * chdir into the hidden directory prior to calling sysattr_status().
42  *
43  * Returns 1 if the extended system attributes were obtained, otherwise
44  * returns 0.
45  */
46 int
47 sysattr_status(const char *file, xattr_view_t view)
48 {
49 	nvlist_t	*response = NULL;
50 	int		saveerrno;
51 	int		status;
52 
53 	status = getattrat(AT_FDCWD, view, file, &response);
54 
55 	saveerrno = errno;
56 	if (response)
57 		(void) nvlist_free(response);
58 	errno = saveerrno;
59 
60 	return (status == 0);
61 }
62 
63 /*
64  * Returns the type of the specified in file.  If the file name matches
65  * the name of either a read-only or read-write extended system attribute
66  * file then sysattr_type() returns the type of file:
67  *	return value	file type
68  *	------------	---------
69  *	_RO_SATTR	read-only extended system attribute file
70  *	_RW_SATTR	read-write extended system attribute file
71  *	_NOT_SATTR	neither a read-only or read-write extended system
72  *			attribute file.
73  */
74 int
75 sysattr_type(char *file)
76 {
77 	if (file == NULL) {
78 		errno = ENOENT;
79 		return (_NOT_SATTR);
80 	}
81 
82 	if (strcmp(basename(file), file) != 0) {
83 		errno = EINVAL;
84 		return (_NOT_SATTR);
85 	}
86 
87 	errno = 0;
88 	if (strcmp(file, VIEW_READONLY) == 0) {
89 		return (_RO_SATTR);
90 	} else if (strcmp(file, VIEW_READWRITE) == 0) {
91 		return (_RW_SATTR);
92 	} else {
93 		return (_NOT_SATTR);
94 	}
95 }
96 
97 /*
98  * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
99  * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
100  * can still try to figure out if extended system attributes are supported by
101  * testing for a valid extended system attribute file.
102  *
103  * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
104  *
105  * Returns 1 if the underlying file system supports extended system attributes,
106  * otherwise, returns -1.
107  */
108 int
109 sysattr_support(const char *file, int name)
110 {
111 	int	rc;
112 
113 	errno = 0;
114 	if ((name != _PC_SATTR_ENABLED) &&
115 	    (name != _PC_SATTR_EXISTS)) {
116 		errno = EINVAL;
117 		return (-1);
118 	}
119 	if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
120 		return (rc);
121 	}
122 	return (sysattr_status(file, XATTR_VIEW_READONLY));
123 }
124