summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcmdutils/common/sysattrs.c
blob: 5868e765cc4447b06f65aa40e9426246e6779841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <c_synonyms.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <attr.h>
#include <fcntl.h>
#include <errno.h>
#include "libcmdutils.h"

/*
 * Returns the status of attempting to obtain the extended system
 * attributes in the specified view.
 *
 * Note: If obtaining status for an extended attribute file, the caller must
 * chdir into the hidden directory prior to calling sysattr_status().
 *
 * Returns 1 if the extended system attributes were obtained, otherwise
 * returns 0.
 */
int
sysattr_status(char *file, xattr_view_t view)
{
	nvlist_t	*response = NULL;
	int		saveerrno;
	int		status;

	status = getattrat(AT_FDCWD, view, file, &response);

	saveerrno = errno;
	if (response)
		(void) nvlist_free(response);
	errno = saveerrno;

	return (status == 0);
}

/*
 * Returns the type of the specified in file.  If the file name matches
 * the name of either a read-only or read-write extended system attribute
 * file then sysattr_type() returns the type of file:
 * 	return value	file type
 *	------------	---------
 *	_RO_SATTR	read-only extended system attribute file
 *	_RW_SATTR	read-write extended system attribute file
 *	_NOT_SATTR	neither a read-only or read-write extended system
 *			attribute file.
 */
int
sysattr_type(char *file)
{
	if (file == NULL) {
		errno = ENOENT;
		return (_NOT_SATTR);
	}

	if (strcmp(basename(file), file) != 0) {
		errno = EINVAL;
		return (_NOT_SATTR);
	}

	errno = 0;
	if (strcmp(file, VIEW_READONLY) == 0) {
		return (_RO_SATTR);
	} else if (strcmp(file, VIEW_READWRITE) == 0) {
		return (_RW_SATTR);
	} else {
		return (_NOT_SATTR);
	}
}

/*
 * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
 * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
 * can still try to figure out if extended system attributes are supported by
 * testing for a valid extended system attribute file.
 *
 * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
 *
 * Returns 1 if the underlying file system supports extended system attributes,
 * otherwise, returns -1.
 */
int
sysattr_support(char *file, int name)
{
	int	rc;

	errno = 0;
	if ((name != _PC_SATTR_ENABLED) &&
	    (name != _PC_SATTR_EXISTS)) {
		errno = EINVAL;
		return (-1);
	}
	if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
		return (rc);
	}
	return (sysattr_status(file, XATTR_VIEW_READONLY));
}