| 1 | /* |
| 2 | * Copyright (c) 1995 Scott Bartram |
| 3 | * Copyright (c) 1995 Steven Wallace |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * $FreeBSD: src/sys/i386/ibcs2/ibcs2_stat.c,v 1.10 1999/12/15 23:01:45 eivind Exp $ |
| 29 | * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_stat.c,v 1.4 2003/06/25 03:55:53 dillon Exp $ |
| 30 | */ |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/file.h> |
| 35 | #include <sys/proc.h> |
| 36 | #include <sys/namei.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/filedesc.h> |
| 39 | #include <sys/kernel.h> |
| 40 | #include <sys/mount.h> |
| 41 | #include <sys/vnode.h> |
| 42 | #include <sys/sysctl.h> |
| 43 | #include <sys/sysproto.h> |
| 44 | |
| 45 | #include <i386/ibcs2/ibcs2_signal.h> |
| 46 | #include <i386/ibcs2/ibcs2_stat.h> |
| 47 | #include <i386/ibcs2/ibcs2_statfs.h> |
| 48 | #include <i386/ibcs2/ibcs2_proto.h> |
| 49 | #include <i386/ibcs2/ibcs2_util.h> |
| 50 | #include <i386/ibcs2/ibcs2_utsname.h> |
| 51 | |
| 52 | #include <vm/vm_zone.h> |
| 53 | |
| 54 | static void bsd_stat2ibcs_stat __P((struct stat *, struct ibcs2_stat *)); |
| 55 | static int cvt_statfs __P((struct statfs *, caddr_t, int)); |
| 56 | |
| 57 | static void |
| 58 | bsd_stat2ibcs_stat(st, st4) |
| 59 | struct stat *st; |
| 60 | struct ibcs2_stat *st4; |
| 61 | { |
| 62 | bzero(st4, sizeof(*st4)); |
| 63 | st4->st_dev = (ibcs2_dev_t)st->st_dev; |
| 64 | st4->st_ino = (ibcs2_ino_t)st->st_ino; |
| 65 | st4->st_mode = (ibcs2_mode_t)st->st_mode; |
| 66 | st4->st_nlink= (ibcs2_nlink_t)st->st_nlink; |
| 67 | st4->st_uid = (ibcs2_uid_t)st->st_uid; |
| 68 | st4->st_gid = (ibcs2_gid_t)st->st_gid; |
| 69 | st4->st_rdev = (ibcs2_dev_t)st->st_rdev; |
| 70 | if (st->st_size < (quad_t)1 << 32) |
| 71 | st4->st_size = (ibcs2_off_t)st->st_size; |
| 72 | else |
| 73 | st4->st_size = -2; |
| 74 | st4->st_atim = (ibcs2_time_t)st->st_atime; |
| 75 | st4->st_mtim = (ibcs2_time_t)st->st_mtime; |
| 76 | st4->st_ctim = (ibcs2_time_t)st->st_ctime; |
| 77 | } |
| 78 | |
| 79 | static int |
| 80 | cvt_statfs(sp, buf, len) |
| 81 | struct statfs *sp; |
| 82 | caddr_t buf; |
| 83 | int len; |
| 84 | { |
| 85 | struct ibcs2_statfs ssfs; |
| 86 | |
| 87 | bzero(&ssfs, sizeof ssfs); |
| 88 | ssfs.f_fstyp = 0; |
| 89 | ssfs.f_bsize = sp->f_bsize; |
| 90 | ssfs.f_frsize = 0; |
| 91 | ssfs.f_blocks = sp->f_blocks; |
| 92 | ssfs.f_bfree = sp->f_bfree; |
| 93 | ssfs.f_files = sp->f_files; |
| 94 | ssfs.f_ffree = sp->f_ffree; |
| 95 | ssfs.f_fname[0] = 0; |
| 96 | ssfs.f_fpack[0] = 0; |
| 97 | return copyout((caddr_t)&ssfs, buf, len); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | ibcs2_statfs(struct ibcs2_statfs_args *uap) |
| 102 | { |
| 103 | struct thread *td = curthread; /* XXX */ |
| 104 | struct mount *mp; |
| 105 | struct statfs *sp; |
| 106 | int error; |
| 107 | struct nameidata nd; |
| 108 | caddr_t sg = stackgap_init(); |
| 109 | |
| 110 | CHECKALTEXIST(&sg, SCARG(uap, path)); |
| 111 | NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td); |
| 112 | if ((error = namei(&nd)) != 0) |
| 113 | return (error); |
| 114 | NDFREE(&nd, NDF_ONLY_PNBUF); |
| 115 | mp = nd.ni_vp->v_mount; |
| 116 | sp = &mp->mnt_stat; |
| 117 | vrele(nd.ni_vp); |
| 118 | if ((error = VFS_STATFS(mp, sp, td)) != 0) |
| 119 | return (error); |
| 120 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; |
| 121 | return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len)); |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | ibcs2_fstatfs(struct ibcs2_fstatfs_args *uap) |
| 126 | { |
| 127 | struct thread *td = curthread; /* XXX */ |
| 128 | struct file *fp; |
| 129 | struct mount *mp; |
| 130 | register struct statfs *sp; |
| 131 | int error; |
| 132 | |
| 133 | KKASSERT(td->td_proc); |
| 134 | if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0) |
| 135 | return (error); |
| 136 | mp = ((struct vnode *)fp->f_data)->v_mount; |
| 137 | sp = &mp->mnt_stat; |
| 138 | if ((error = VFS_STATFS(mp, sp, td)) != 0) |
| 139 | return (error); |
| 140 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; |
| 141 | return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len)); |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | ibcs2_stat(struct ibcs2_stat_args *uap) |
| 146 | { |
| 147 | struct stat st; |
| 148 | struct ibcs2_stat ibcs2_st; |
| 149 | struct stat_args cup; |
| 150 | int error; |
| 151 | caddr_t sg = stackgap_init(); |
| 152 | |
| 153 | CHECKALTEXIST(&sg, SCARG(uap, path)); |
| 154 | SCARG(&cup, path) = SCARG(uap, path); |
| 155 | SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st)); |
| 156 | |
| 157 | if ((error = stat(&cup)) != 0) |
| 158 | return error; |
| 159 | |
| 160 | if ((error = copyin(SCARG(&cup, ub), &st, sizeof(st))) != 0) |
| 161 | return error; |
| 162 | bsd_stat2ibcs_stat(&st, &ibcs2_st); |
| 163 | return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), |
| 164 | ibcs2_stat_len); |
| 165 | } |
| 166 | |
| 167 | int |
| 168 | ibcs2_lstat(struct ibcs2_lstat_args *uap) |
| 169 | { |
| 170 | struct stat st; |
| 171 | struct ibcs2_stat ibcs2_st; |
| 172 | struct lstat_args cup; |
| 173 | int error; |
| 174 | caddr_t sg = stackgap_init(); |
| 175 | |
| 176 | CHECKALTEXIST(&sg, SCARG(uap, path)); |
| 177 | SCARG(&cup, path) = SCARG(uap, path); |
| 178 | SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st)); |
| 179 | |
| 180 | if ((error = lstat(&cup)) != 0) |
| 181 | return error; |
| 182 | |
| 183 | if ((error = copyin(SCARG(&cup, ub), &st, sizeof(st))) != 0) |
| 184 | return error; |
| 185 | bsd_stat2ibcs_stat(&st, &ibcs2_st); |
| 186 | return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), |
| 187 | ibcs2_stat_len); |
| 188 | } |
| 189 | |
| 190 | int |
| 191 | ibcs2_fstat(struct ibcs2_fstat_args *uap) |
| 192 | { |
| 193 | struct stat st; |
| 194 | struct ibcs2_stat ibcs2_st; |
| 195 | struct fstat_args cup; |
| 196 | int error; |
| 197 | caddr_t sg = stackgap_init(); |
| 198 | |
| 199 | SCARG(&cup, fd) = SCARG(uap, fd); |
| 200 | SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(st)); |
| 201 | |
| 202 | if ((error = fstat(&cup)) != 0) |
| 203 | return error; |
| 204 | |
| 205 | if ((error = copyin(SCARG(&cup, sb), &st, sizeof(st))) != 0) |
| 206 | return error; |
| 207 | bsd_stat2ibcs_stat(&st, &ibcs2_st); |
| 208 | return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st), |
| 209 | ibcs2_stat_len); |
| 210 | } |
| 211 | |
| 212 | int |
| 213 | ibcs2_utssys(struct ibcs2_utssys_args *uap) |
| 214 | { |
| 215 | switch (SCARG(uap, flag)) { |
| 216 | case 0: /* uname(2) */ |
| 217 | { |
| 218 | char machine_name[9], *p; |
| 219 | struct ibcs2_utsname sut; |
| 220 | bzero(&sut, ibcs2_utsname_len); |
| 221 | |
| 222 | strncpy(sut.sysname, |
| 223 | IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1); |
| 224 | strncpy(sut.release, |
| 225 | IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1); |
| 226 | strncpy(sut.version, |
| 227 | IBCS2_UNAME_VERSION, sizeof(sut.version) - 1); |
| 228 | strncpy(machine_name, hostname, sizeof(machine_name) - 1); |
| 229 | machine_name[sizeof(machine_name) - 1] = 0; |
| 230 | p = index(machine_name, '.'); |
| 231 | if ( p ) |
| 232 | *p = '\0'; |
| 233 | strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1); |
| 234 | strncpy(sut.machine, machine, sizeof(sut.machine) - 1); |
| 235 | |
| 236 | DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n", |
| 237 | sut.sysname, sut.release, sut.version, sut.nodename, |
| 238 | sut.machine)); |
| 239 | return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, a1), |
| 240 | ibcs2_utsname_len); |
| 241 | } |
| 242 | |
| 243 | case 2: /* ustat(2) */ |
| 244 | { |
| 245 | return ENOSYS; /* XXX - TODO */ |
| 246 | } |
| 247 | |
| 248 | default: |
| 249 | return ENOSYS; |
| 250 | } |
| 251 | } |