/*
* 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 (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <nfs/fedfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "fedfs_impl.h"
#ifndef REFERRAL_EA
#define REFERRAL_EA "SUNWreferral"
#endif
char *
get_rp_data(char *path)
{
char *res, buf[SYMLINK_MAX + 1];
struct stat stbuf;
ssize_t sz;
int err;
if (path == NULL)
return (NULL);
err = lstat(path, &stbuf);
if (err == -1)
return (NULL);
if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
sz = readlink(path, buf, SYMLINK_MAX);
if (sz == -1)
return (NULL);
buf[sz] = '\0';
res = strdup(buf);
} else {
int fd1, fd2;
fd1 = open(path, O_RDONLY);
if (fd1 == -1)
return (NULL);
fd2 = openat(fd1, REFERRAL_EA, O_RDONLY | O_XATTR);
(void) close(fd1);
if (fd2 == -1)
return (NULL);
err = fstat(fd2, &stbuf);
if (err == -1) {
(void) close(fd2);
return (NULL);
}
res = malloc(stbuf.st_size);
if (res == NULL) {
(void) close(fd2);
return (NULL);
}
sz = read(fd2, res, stbuf.st_size);
(void) close(fd2);
if (sz != stbuf.st_size) {
free(res);
return (NULL);
}
}
return (res);
}