da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or http://www.opensolaris.org/os/licensing.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
7257d1b4d25bfac0c802847390e98a464fd787acraf
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
575bd8a230f2c20b1878ebb473c30a12f0eb1b2emarks * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#pragma ident "%Z%%M% %I% %E% SMI"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <unistd.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <stdlib.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <string.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <libgen.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <attr.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <fcntl.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <errno.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include "libcmdutils.h"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the status of attempting to obtain the extended system
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * attributes in the specified view.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Note: If obtaining status for an extended attribute file, the caller must
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * chdir into the hidden directory prior to calling sysattr_status().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns 1 if the extended system attributes were obtained, otherwise
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * returns 0.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsysattr_status(char *file, xattr_view_t view)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
575bd8a230f2c20b1878ebb473c30a12f0eb1b2emarks nvlist_t *response = NULL;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int saveerrno;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int status;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw status = getattrat(AT_FDCWD, view, file, &response);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw saveerrno = errno;
575bd8a230f2c20b1878ebb473c30a12f0eb1b2emarks if (response)
575bd8a230f2c20b1878ebb473c30a12f0eb1b2emarks (void) nvlist_free(response);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = saveerrno;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (status == 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the type of the specified in file. If the file name matches
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the name of either a read-only or read-write extended system attribute
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file then sysattr_type() returns the type of file:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * return value file type
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ------------ ---------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * _RO_SATTR read-only extended system attribute file
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * _RW_SATTR read-write extended system attribute file
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * _NOT_SATTR neither a read-only or read-write extended system
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * attribute file.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsysattr_type(char *file)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (file == NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = ENOENT;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (_NOT_SATTR);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (strcmp(basename(file), file) != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = EINVAL;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (_NOT_SATTR);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (strcmp(file, VIEW_READONLY) == 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (_RO_SATTR);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else if (strcmp(file, VIEW_READWRITE) == 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (_RW_SATTR);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (_NOT_SATTR);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * can still try to figure out if extended system attributes are supported by
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * testing for a valid extended system attribute file.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns 1 if the underlying file system supports extended system attributes,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * otherwise, returns -1.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsysattr_support(char *file, int name)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((name != _PC_SATTR_ENABLED) &&
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (name != _PC_SATTR_EXISTS)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw errno = EINVAL;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (sysattr_status(file, XATTR_VIEW_READONLY));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}