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 #pragma ident "%Z%%M% %I% %E% SMI"
28
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
sysattr_status(char * file,xattr_view_t view)49 sysattr_status(char *file, xattr_view_t view)
50 {
51 nvlist_t *response = NULL;
52 int saveerrno;
53 int status;
54
55 status = getattrat(AT_FDCWD, view, file, &response);
56
57 saveerrno = errno;
58 if (response)
59 (void) nvlist_free(response);
60 errno = saveerrno;
61
62 return (status == 0);
63 }
64
65 /*
66 * Returns the type of the specified in file. If the file name matches
67 * the name of either a read-only or read-write extended system attribute
68 * file then sysattr_type() returns the type of file:
69 * return value file type
70 * ------------ ---------
71 * _RO_SATTR read-only extended system attribute file
72 * _RW_SATTR read-write extended system attribute file
73 * _NOT_SATTR neither a read-only or read-write extended system
74 * attribute file.
75 */
76 int
sysattr_type(char * file)77 sysattr_type(char *file)
78 {
79 if (file == NULL) {
80 errno = ENOENT;
81 return (_NOT_SATTR);
82 }
83
84 if (strcmp(basename(file), file) != 0) {
85 errno = EINVAL;
86 return (_NOT_SATTR);
87 }
88
89 errno = 0;
90 if (strcmp(file, VIEW_READONLY) == 0) {
91 return (_RO_SATTR);
92 } else if (strcmp(file, VIEW_READWRITE) == 0) {
93 return (_RW_SATTR);
94 } else {
95 return (_NOT_SATTR);
96 }
97 }
98
99 /*
100 * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
101 * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
102 * can still try to figure out if extended system attributes are supported by
103 * testing for a valid extended system attribute file.
104 *
105 * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
106 *
107 * Returns 1 if the underlying file system supports extended system attributes,
108 * otherwise, returns -1.
109 */
110 int
sysattr_support(char * file,int name)111 sysattr_support(char *file, int name)
112 {
113 int rc;
114
115 errno = 0;
116 if ((name != _PC_SATTR_ENABLED) &&
117 (name != _PC_SATTR_EXISTS)) {
118 errno = EINVAL;
119 return (-1);
120 }
121 if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
122 return (rc);
123 }
124 return (sysattr_status(file, XATTR_VIEW_READONLY));
125 }
126