ibcs2_stat.c revision 91388
1219820Sjeff/* 2219820Sjeff * Copyright (c) 1995 Scott Bartram 3219820Sjeff * Copyright (c) 1995 Steven Wallace 4219820Sjeff * All rights reserved. 5219820Sjeff * 6219820Sjeff * Redistribution and use in source and binary forms, with or without 7219820Sjeff * modification, are permitted provided that the following conditions 8219820Sjeff * are met: 9219820Sjeff * 1. Redistributions of source code must retain the above copyright 10219820Sjeff * notice, this list of conditions and the following disclaimer. 11219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 12219820Sjeff * notice, this list of conditions and the following disclaimer in the 13219820Sjeff * documentation and/or other materials provided with the distribution. 14219820Sjeff * 3. The name of the author may not be used to endorse or promote products 15219820Sjeff * derived from this software without specific prior written permission 16219820Sjeff * 17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27219820Sjeff * 28219820Sjeff * $FreeBSD: head/sys/i386/ibcs2/ibcs2_stat.c 91388 2002-02-27 15:23:01Z robert $ 29219820Sjeff */ 30219820Sjeff 31219820Sjeff#include <sys/param.h> 32219820Sjeff#include <sys/systm.h> 33219820Sjeff#include <sys/namei.h> 34219820Sjeff#include <sys/file.h> 35219820Sjeff#include <sys/stat.h> 36219820Sjeff#include <sys/filedesc.h> 37219820Sjeff#include <sys/jail.h> 38219820Sjeff#include <sys/kernel.h> 39219820Sjeff#include <sys/mount.h> 40219820Sjeff#include <sys/vnode.h> 41219820Sjeff#include <sys/sysctl.h> 42219820Sjeff#include <sys/sysproto.h> 43219820Sjeff 44219820Sjeff#include <i386/ibcs2/ibcs2_signal.h> 45219820Sjeff#include <i386/ibcs2/ibcs2_stat.h> 46219820Sjeff#include <i386/ibcs2/ibcs2_statfs.h> 47219820Sjeff#include <i386/ibcs2/ibcs2_proto.h> 48219820Sjeff#include <i386/ibcs2/ibcs2_util.h> 49219820Sjeff#include <i386/ibcs2/ibcs2_utsname.h> 50219820Sjeff 51219820Sjeff 52219820Sjeffstatic void bsd_stat2ibcs_stat __P((struct stat *, struct ibcs2_stat *)); 53219820Sjeffstatic int cvt_statfs __P((struct statfs *, caddr_t, int)); 54219820Sjeff 55219820Sjeffstatic void 56219820Sjeffbsd_stat2ibcs_stat(st, st4) 57219820Sjeff struct stat *st; 58219820Sjeff struct ibcs2_stat *st4; 59219820Sjeff{ 60219820Sjeff bzero(st4, sizeof(*st4)); 61219820Sjeff st4->st_dev = (ibcs2_dev_t)st->st_dev; 62219820Sjeff st4->st_ino = (ibcs2_ino_t)st->st_ino; 63219820Sjeff st4->st_mode = (ibcs2_mode_t)st->st_mode; 64219820Sjeff st4->st_nlink= (ibcs2_nlink_t)st->st_nlink; 65219820Sjeff st4->st_uid = (ibcs2_uid_t)st->st_uid; 66219820Sjeff st4->st_gid = (ibcs2_gid_t)st->st_gid; 67219820Sjeff st4->st_rdev = (ibcs2_dev_t)st->st_rdev; 68219820Sjeff if (st->st_size < (quad_t)1 << 32) 69219820Sjeff st4->st_size = (ibcs2_off_t)st->st_size; 70219820Sjeff else 71219820Sjeff st4->st_size = -2; 72219820Sjeff st4->st_atim = (ibcs2_time_t)st->st_atime; 73219820Sjeff st4->st_mtim = (ibcs2_time_t)st->st_mtime; 74219820Sjeff st4->st_ctim = (ibcs2_time_t)st->st_ctime; 75219820Sjeff} 76219820Sjeff 77219820Sjeffstatic int 78219820Sjeffcvt_statfs(sp, buf, len) 79219820Sjeff struct statfs *sp; 80219820Sjeff caddr_t buf; 81219820Sjeff int len; 82219820Sjeff{ 83219820Sjeff struct ibcs2_statfs ssfs; 84219820Sjeff 85219820Sjeff bzero(&ssfs, sizeof ssfs); 86219820Sjeff ssfs.f_fstyp = 0; 87219820Sjeff ssfs.f_bsize = sp->f_bsize; 88219820Sjeff ssfs.f_frsize = 0; 89219820Sjeff ssfs.f_blocks = sp->f_blocks; 90219820Sjeff ssfs.f_bfree = sp->f_bfree; 91219820Sjeff ssfs.f_files = sp->f_files; 92219820Sjeff ssfs.f_ffree = sp->f_ffree; 93219820Sjeff ssfs.f_fname[0] = 0; 94219820Sjeff ssfs.f_fpack[0] = 0; 95219820Sjeff return copyout((caddr_t)&ssfs, buf, len); 96219820Sjeff} 97219820Sjeff 98219820Sjeffint 99219820Sjeffibcs2_statfs(td, uap) 100219820Sjeff struct thread *td; 101219820Sjeff struct ibcs2_statfs_args *uap; 102219820Sjeff{ 103219820Sjeff register struct mount *mp; 104219820Sjeff register struct statfs *sp; 105219820Sjeff int error; 106219820Sjeff struct nameidata nd; 107219820Sjeff caddr_t sg = stackgap_init(); 108219820Sjeff 109219820Sjeff CHECKALTEXIST(td, &sg, SCARG(uap, path)); 110219820Sjeff NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td); 111219820Sjeff if ((error = namei(&nd)) != 0) 112219820Sjeff return (error); 113219820Sjeff NDFREE(&nd, NDF_ONLY_PNBUF); 114219820Sjeff mp = nd.ni_vp->v_mount; 115219820Sjeff sp = &mp->mnt_stat; 116219820Sjeff vrele(nd.ni_vp); 117219820Sjeff if ((error = VFS_STATFS(mp, sp, td)) != 0) 118219820Sjeff return (error); 119219820Sjeff sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 120219820Sjeff return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len)); 121219820Sjeff} 122219820Sjeff 123219820Sjeffint 124219820Sjeffibcs2_fstatfs(td, uap) 125219820Sjeff struct thread *td; 126219820Sjeff struct ibcs2_fstatfs_args *uap; 127219820Sjeff{ 128219820Sjeff struct file *fp; 129219820Sjeff struct mount *mp; 130219820Sjeff register struct statfs *sp; 131219820Sjeff int error; 132219820Sjeff 133219820Sjeff if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0) 134219820Sjeff return (error); 135219820Sjeff mp = ((struct vnode *)fp->f_data)->v_mount; 136219820Sjeff sp = &mp->mnt_stat; 137219820Sjeff error = VFS_STATFS(mp, sp, td); 138219820Sjeff fdrop(fp, td); 139219820Sjeff if (error != 0) 140219820Sjeff return (error); 141219820Sjeff sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 142219820Sjeff return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len)); 143219820Sjeff} 144219820Sjeff 145219820Sjeffint 146219820Sjeffibcs2_stat(td, uap) 147219820Sjeff struct thread *td; 148219820Sjeff struct ibcs2_stat_args *uap; 149219820Sjeff{ 150219820Sjeff struct stat st; 151219820Sjeff struct ibcs2_stat ibcs2_st; 152219820Sjeff struct stat_args cup; 153219820Sjeff int error; 154219820Sjeff caddr_t sg = stackgap_init(); 155219820Sjeff 156219820Sjeff CHECKALTEXIST(td, &sg, SCARG(uap, path)); 157219820Sjeff SCARG(&cup, path) = SCARG(uap, path); 158219820Sjeff SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st)); 159219820Sjeff 160219820Sjeff if ((error = stat(td, &cup)) != 0) 161219820Sjeff return error; 162219820Sjeff 163219820Sjeff if ((error = copyin(SCARG(&cup, ub), &st, sizeof(st))) != 0) 164219820Sjeff return error; 165219820Sjeff bsd_stat2ibcs_stat(&st, &ibcs2_st); 166219820Sjeff return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), 167219820Sjeff ibcs2_stat_len); 168219820Sjeff} 169219820Sjeff 170219820Sjeffint 171219820Sjeffibcs2_lstat(td, uap) 172219820Sjeff struct thread *td; 173219820Sjeff struct ibcs2_lstat_args *uap; 174219820Sjeff{ 175219820Sjeff struct stat st; 176219820Sjeff struct ibcs2_stat ibcs2_st; 177219820Sjeff struct lstat_args cup; 178219820Sjeff int error; 179219820Sjeff caddr_t sg = stackgap_init(); 180219820Sjeff 181219820Sjeff CHECKALTEXIST(td, &sg, SCARG(uap, path)); 182219820Sjeff SCARG(&cup, path) = SCARG(uap, path); 183219820Sjeff SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st)); 184219820Sjeff 185219820Sjeff if ((error = lstat(td, &cup)) != 0) 186219820Sjeff return error; 187219820Sjeff 188219820Sjeff if ((error = copyin(SCARG(&cup, ub), &st, sizeof(st))) != 0) 189219820Sjeff return error; 190219820Sjeff bsd_stat2ibcs_stat(&st, &ibcs2_st); 191219820Sjeff return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), 192219820Sjeff ibcs2_stat_len); 193219820Sjeff} 194219820Sjeff 195219820Sjeffint 196219820Sjeffibcs2_fstat(td, uap) 197219820Sjeff struct thread *td; 198219820Sjeff struct ibcs2_fstat_args *uap; 199219820Sjeff{ 200219820Sjeff struct stat st; 201219820Sjeff struct ibcs2_stat ibcs2_st; 202219820Sjeff struct fstat_args cup; 203219820Sjeff int error; 204219820Sjeff caddr_t sg = stackgap_init(); 205219820Sjeff 206219820Sjeff SCARG(&cup, fd) = SCARG(uap, fd); 207219820Sjeff SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(st)); 208219820Sjeff 209219820Sjeff if ((error = fstat(td, &cup)) != 0) 210219820Sjeff return error; 211219820Sjeff 212219820Sjeff if ((error = copyin(SCARG(&cup, sb), &st, sizeof(st))) != 0) 213219820Sjeff return error; 214219820Sjeff bsd_stat2ibcs_stat(&st, &ibcs2_st); 215219820Sjeff return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), 216219820Sjeff ibcs2_stat_len); 217219820Sjeff} 218219820Sjeff 219219820Sjeffint 220219820Sjeffibcs2_utssys(td, uap) 221219820Sjeff struct thread *td; 222219820Sjeff struct ibcs2_utssys_args *uap; 223219820Sjeff{ 224219820Sjeff switch (SCARG(uap, flag)) { 225219820Sjeff case 0: /* uname(2) */ 226219820Sjeff { 227219820Sjeff char machine_name[9], *p; 228219820Sjeff struct ibcs2_utsname sut; 229219820Sjeff bzero(&sut, ibcs2_utsname_len); 230219820Sjeff 231219820Sjeff strncpy(sut.sysname, 232219820Sjeff IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1); 233219820Sjeff strncpy(sut.release, 234219820Sjeff IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1); 235219820Sjeff strncpy(sut.version, 236219820Sjeff IBCS2_UNAME_VERSION, sizeof(sut.version) - 1); 237219820Sjeff strncpy(machine_name, getcredhostname(td->td_ucred), 238219820Sjeff sizeof(machine_name) - 1); 239219820Sjeff machine_name[sizeof(machine_name) - 1] = 0; 240219820Sjeff p = index(machine_name, '.'); 241219820Sjeff if ( p ) 242219820Sjeff *p = '\0'; 243219820Sjeff strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1); 244219820Sjeff strncpy(sut.machine, machine, sizeof(sut.machine) - 1); 245219820Sjeff 246219820Sjeff DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n", 247219820Sjeff sut.sysname, sut.release, sut.version, sut.nodename, 248219820Sjeff sut.machine)); 249219820Sjeff return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, a1), 250219820Sjeff ibcs2_utsname_len); 251219820Sjeff } 252219820Sjeff 253219820Sjeff case 2: /* ustat(2) */ 254219820Sjeff { 255219820Sjeff return ENOSYS; /* XXX - TODO */ 256219820Sjeff } 257219820Sjeff 258219820Sjeff default: 259219820Sjeff return ENOSYS; 260219820Sjeff } 261219820Sjeff} 262219820Sjeff