xref: /onnv-gate/usr/src/lib/libcmdutils/common/sysattrs.c (revision 6812:febeba71273d)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
21*6812Sraf 
225331Samw /*
235902Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
245331Samw  * Use is subject to license terms.
255331Samw  */
265331Samw 
275331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
285331Samw 
295331Samw #include <unistd.h>
305331Samw #include <stdlib.h>
315331Samw #include <string.h>
325331Samw #include <libgen.h>
335331Samw #include <attr.h>
345331Samw #include <fcntl.h>
355331Samw #include <errno.h>
365331Samw #include "libcmdutils.h"
375331Samw 
385331Samw /*
395331Samw  * Returns the status of attempting to obtain the extended system
405331Samw  * attributes in the specified view.
415331Samw  *
425331Samw  * Note: If obtaining status for an extended attribute file, the caller must
435331Samw  * chdir into the hidden directory prior to calling sysattr_status().
445331Samw  *
455331Samw  * Returns 1 if the extended system attributes were obtained, otherwise
465331Samw  * returns 0.
475331Samw  */
485331Samw int
sysattr_status(char * file,xattr_view_t view)495331Samw sysattr_status(char *file, xattr_view_t view)
505331Samw {
515902Smarks 	nvlist_t	*response = NULL;
525331Samw 	int		saveerrno;
535331Samw 	int		status;
545331Samw 
555331Samw 	status = getattrat(AT_FDCWD, view, file, &response);
565331Samw 
575331Samw 	saveerrno = errno;
585902Smarks 	if (response)
595902Smarks 		(void) nvlist_free(response);
605331Samw 	errno = saveerrno;
615331Samw 
625331Samw 	return (status == 0);
635331Samw }
645331Samw 
655331Samw /*
665331Samw  * Returns the type of the specified in file.  If the file name matches
675331Samw  * the name of either a read-only or read-write extended system attribute
685331Samw  * file then sysattr_type() returns the type of file:
695331Samw  * 	return value	file type
705331Samw  *	------------	---------
715331Samw  *	_RO_SATTR	read-only extended system attribute file
725331Samw  *	_RW_SATTR	read-write extended system attribute file
735331Samw  *	_NOT_SATTR	neither a read-only or read-write extended system
745331Samw  *			attribute file.
755331Samw  */
765331Samw int
sysattr_type(char * file)775331Samw sysattr_type(char *file)
785331Samw {
795331Samw 	if (file == NULL) {
805331Samw 		errno = ENOENT;
815331Samw 		return (_NOT_SATTR);
825331Samw 	}
835331Samw 
845331Samw 	if (strcmp(basename(file), file) != 0) {
855331Samw 		errno = EINVAL;
865331Samw 		return (_NOT_SATTR);
875331Samw 	}
885331Samw 
895331Samw 	errno = 0;
905331Samw 	if (strcmp(file, VIEW_READONLY) == 0) {
915331Samw 		return (_RO_SATTR);
925331Samw 	} else if (strcmp(file, VIEW_READWRITE) == 0) {
935331Samw 		return (_RW_SATTR);
945331Samw 	} else {
955331Samw 		return (_NOT_SATTR);
965331Samw 	}
975331Samw }
985331Samw 
995331Samw /*
1005331Samw  * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
1015331Samw  * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
1025331Samw  * can still try to figure out if extended system attributes are supported by
1035331Samw  * testing for a valid extended system attribute file.
1045331Samw  *
1055331Samw  * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
1065331Samw  *
1075331Samw  * Returns 1 if the underlying file system supports extended system attributes,
1085331Samw  * otherwise, returns -1.
1095331Samw  */
1105331Samw int
sysattr_support(char * file,int name)1115331Samw sysattr_support(char *file, int name)
1125331Samw {
1135331Samw 	int	rc;
1145331Samw 
1155331Samw 	errno = 0;
1165331Samw 	if ((name != _PC_SATTR_ENABLED) &&
1175331Samw 	    (name != _PC_SATTR_EXISTS)) {
1185331Samw 		errno = EINVAL;
1195331Samw 		return (-1);
1205331Samw 	}
1215331Samw 	if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
1225331Samw 		return (rc);
1235331Samw 	}
1245331Samw 	return (sysattr_status(file, XATTR_VIEW_READONLY));
1255331Samw }
126