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