xref: /onnv-gate/usr/src/lib/libbc/libc/sys/common/_stat.c (revision 11798:1e7f1f154004)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53224Sraf  * Common Development and Distribution License (the "License").
63224Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213224Sraf 
220Sstevel@tonic-gate /*
23*11798SRoger.Faulkner@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
243224Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/errno.h>
280Sstevel@tonic-gate #include <sys/syscall.h>
290Sstevel@tonic-gate #include <sys/fcntl.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include "compat.h"
330Sstevel@tonic-gate #include "s5sysmacros.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define ST_FSTYPSZ	16	/* array size for file system type name */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate struct ts {
380Sstevel@tonic-gate 	long	tv_sec;		/* seconds */
390Sstevel@tonic-gate 	long	tv_nsec;	/* nanoseconds */
400Sstevel@tonic-gate };
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct n_stat {
430Sstevel@tonic-gate         unsigned long   st_dev;
440Sstevel@tonic-gate         long            st_pad1[3];     /* reserved for network id */
450Sstevel@tonic-gate         unsigned long   st_ino;
460Sstevel@tonic-gate         unsigned long   st_mode;
470Sstevel@tonic-gate         unsigned long   st_nlink;
480Sstevel@tonic-gate         long            st_uid;
490Sstevel@tonic-gate         long            st_gid;
500Sstevel@tonic-gate         unsigned long   st_rdev;
510Sstevel@tonic-gate         long            st_pad2[2];
520Sstevel@tonic-gate         long            st_size;
530Sstevel@tonic-gate         long            st_pad3;        /* future off_t expansion */
540Sstevel@tonic-gate         struct ts       st_atim;
550Sstevel@tonic-gate         struct ts       st_mtim;
560Sstevel@tonic-gate         struct ts       st_ctim;
570Sstevel@tonic-gate         long            st_blksize;
580Sstevel@tonic-gate         long            st_blocks;
590Sstevel@tonic-gate         char            st_fstype[ST_FSTYPSZ];
600Sstevel@tonic-gate         long            st_pad4[8];     /* expansion area */
613224Sraf 
620Sstevel@tonic-gate };
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void cpstatbuf(struct stat *, struct n_stat *);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate int
fstat(int fd,struct stat * buf)670Sstevel@tonic-gate fstat(int fd, struct stat *buf)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	return (bc_fstat(fd, buf));
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int
bc_fstat(int fd,struct stat * buf)730Sstevel@tonic-gate bc_fstat(int fd, struct stat *buf)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	int ret;
760Sstevel@tonic-gate 	struct n_stat nb;
773224Sraf 	extern int errno;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (buf == 0) {
800Sstevel@tonic-gate 		errno = EFAULT;
810Sstevel@tonic-gate 		return (-1);
820Sstevel@tonic-gate 	}
833224Sraf 
84*11798SRoger.Faulkner@Sun.COM 	if ((ret = _syscall(SYS_fstatat, fd, NULL, &nb, 0)) == -1)
850Sstevel@tonic-gate 		return (ret);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	cpstatbuf(buf, &nb);
880Sstevel@tonic-gate 	if (fd_get(fd) != -1) {
893224Sraf 		buf->st_size = getmodsize(buf->st_size,
900Sstevel@tonic-gate 			sizeof (struct utmpx), sizeof(struct compat_utmp));
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	return (ret);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
stat_com(int lstat,char * path,struct stat * buf)97*11798SRoger.Faulkner@Sun.COM stat_com(int lstat, char *path, struct stat *buf)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	int fd, ret;
1000Sstevel@tonic-gate 	struct n_stat nb;
101*11798SRoger.Faulkner@Sun.COM 	int follow = lstat? AT_SYMLINK_NOFOLLOW : 0;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (strcmp(path, "/etc/mtab") == 0) {
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * stat the real mnttab, or the "parsed" mtab
1060Sstevel@tonic-gate  * created by open?
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * for now, stat the real mnttab.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  *		fd = open_mnt("/etc/mnttab", "mtab", O_RDONLY);
1130Sstevel@tonic-gate  *		ret = fstat(fd, buf);
1140Sstevel@tonic-gate  *		close(fd);
1150Sstevel@tonic-gate  *		return(ret);
1160Sstevel@tonic-gate  */
117*11798SRoger.Faulkner@Sun.COM 		ret = stat_com(lstat, "/etc/mnttab", buf);
1180Sstevel@tonic-gate 		return(ret);
1193224Sraf 	}
1203224Sraf 	if (strcmp(path, "/etc/fstab") == 0) {
1210Sstevel@tonic-gate 		fd = open_mnt("/etc/vfstab", "fstab", O_RDONLY);
1223224Sraf 		if (fd < 0)
1233224Sraf 			ret = -1;
1240Sstevel@tonic-gate 		else {
1253224Sraf 			ret = fstat(fd, buf);
1263224Sraf 			close(fd);
1270Sstevel@tonic-gate 		}
1283224Sraf 		return(ret);
1293224Sraf 	}
1303224Sraf 	if (strcmp(path, "/etc/utmp") == 0 ||
1313224Sraf 	    strcmp(path, "/var/adm/utmp") == 0) {
132*11798SRoger.Faulkner@Sun.COM 		if ((ret = _syscall(SYS_fstatat, AT_FDCWD,
133*11798SRoger.Faulkner@Sun.COM 		    "/var/adm/utmpx", &nb, follow)) != -1) {
1340Sstevel@tonic-gate 			cpstatbuf(buf, &nb);
1353224Sraf 			buf->st_size = getmodsize(buf->st_size,
1363224Sraf 		       	    sizeof(struct utmpx), sizeof(struct compat_utmp));
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 		return(ret);
1390Sstevel@tonic-gate 	}
1403224Sraf 	if (strcmp(path, "/var/adm/wtmp") == 0) {
141*11798SRoger.Faulkner@Sun.COM 		if ((ret = _syscall(SYS_fstatat, AT_FDCWD,
142*11798SRoger.Faulkner@Sun.COM 		    "/var/adm/wtmpx", &nb, follow)) != -1) {
1433224Sraf 			cpstatbuf(buf, &nb);
1443224Sraf 			buf->st_size = getmodsize(buf->st_size,
1453224Sraf 		       	    sizeof(struct utmpx), sizeof(struct compat_utmp));
1463224Sraf 		}
1473224Sraf 		return(ret);
1483224Sraf 	}
1493224Sraf 	if (_strstr(path, "/lib/locale/") != 0) {
1503224Sraf 		fd = open(path, O_RDONLY);
1513224Sraf 		if (fd < 0)
1523224Sraf 			ret = -1;
1533224Sraf 		else {
1543224Sraf 			ret = fstat(fd, buf);
1553224Sraf 			close(fd);
1563224Sraf 		}
1573224Sraf 		return(ret);
1583224Sraf 	}
1593224Sraf 
160*11798SRoger.Faulkner@Sun.COM 	if ((ret = _syscall(SYS_fstatat, AT_FDCWD, path, &nb, follow)) != -1)
1613224Sraf 		cpstatbuf(buf, &nb);
1623224Sraf 	return(ret);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Common code to copy xstat buf to BSD style buf
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate static void
cpstatbuf(struct stat * bsdbuf,struct n_stat * nbuf)1700Sstevel@tonic-gate cpstatbuf(struct stat *bsdbuf, struct n_stat *nbuf)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate         bsdbuf->st_dev = (dev_t) cmpdev(nbuf->st_dev);
1730Sstevel@tonic-gate         bsdbuf->st_ino = nbuf->st_ino;
1740Sstevel@tonic-gate         bsdbuf->st_mode = (unsigned short) nbuf->st_mode;
1750Sstevel@tonic-gate         bsdbuf->st_nlink = (short) nbuf->st_nlink;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((unsigned long)nbuf->st_uid > 0xffff)
1780Sstevel@tonic-gate 		bsdbuf->st_uid = 60001;	/* UID_NOBODY */
1790Sstevel@tonic-gate 	else
1800Sstevel@tonic-gate         	bsdbuf->st_uid = (uid_t) nbuf->st_uid;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if ((unsigned long)nbuf->st_gid > 0xffff)
1830Sstevel@tonic-gate 		bsdbuf->st_gid = 60001;	/* GID_NOBODY */
1840Sstevel@tonic-gate 	else
1850Sstevel@tonic-gate         	bsdbuf->st_gid = (gid_t) nbuf->st_gid;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate         bsdbuf->st_rdev = (dev_t) cmpdev(nbuf->st_rdev);
1880Sstevel@tonic-gate         bsdbuf->st_size = nbuf->st_size;
1890Sstevel@tonic-gate         bsdbuf->st_atime = nbuf->st_atim.tv_sec;
1900Sstevel@tonic-gate         bsdbuf->st_mtime = nbuf->st_mtim.tv_sec;
1910Sstevel@tonic-gate         bsdbuf->st_ctime = nbuf->st_ctim.tv_sec;
1920Sstevel@tonic-gate         bsdbuf->st_blksize = nbuf->st_blksize;
1930Sstevel@tonic-gate         bsdbuf->st_blocks = nbuf->st_blocks;
1940Sstevel@tonic-gate }
195