ibcs2_stat.c revision 16193
133965Sjdp/*
233965Sjdp * Copyright (c) 1995 Scott Bartram
3218822Sdim * Copyright (c) 1995 Steven Wallace
460484Sobrien * All rights reserved.
533965Sjdp *
633965Sjdp * Redistribution and use in source and binary forms, with or without
733965Sjdp * modification, are permitted provided that the following conditions
833965Sjdp * are met:
933965Sjdp * 1. Redistributions of source code must retain the above copyright
1033965Sjdp *    notice, this list of conditions and the following disclaimer.
1133965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1233965Sjdp *    notice, this list of conditions and the following disclaimer in the
1333965Sjdp *    documentation and/or other materials provided with the distribution.
1433965Sjdp * 3. The name of the author may not be used to endorse or promote products
1533965Sjdp *    derived from this software without specific prior written permission
1633965Sjdp *
1733965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1833965Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19218822Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20218822Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2133965Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2233965Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2360484Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2433965Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2533965Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2633965Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2733965Sjdp */
2833965Sjdp
29130561Sobrien#include <sys/param.h>
3060484Sobrien#include <sys/systm.h>
3133965Sjdp#include <sys/namei.h>
3260484Sobrien#include <sys/proc.h>
3333965Sjdp#include <sys/file.h>
3460484Sobrien#include <sys/stat.h>
3560484Sobrien#include <sys/filedesc.h>
3660484Sobrien#include <sys/ioctl.h>
37104834Sobrien#include <sys/kernel.h>
38104834Sobrien#include <sys/mount.h>
3933965Sjdp#include <sys/malloc.h>
4060484Sobrien#include <sys/vnode.h>
4133965Sjdp#include <sys/sysctl.h>
4233965Sjdp#include <sys/sysproto.h>
4333965Sjdp
4433965Sjdp#include <vm/vm.h>
4533965Sjdp
4633965Sjdp#include <i386/ibcs2/ibcs2_types.h>
4733965Sjdp#include <i386/ibcs2/ibcs2_fcntl.h>
4833965Sjdp#include <i386/ibcs2/ibcs2_signal.h>
4933965Sjdp#include <i386/ibcs2/ibcs2_stat.h>
5060484Sobrien#include <i386/ibcs2/ibcs2_statfs.h>
5160484Sobrien#include <i386/ibcs2/ibcs2_proto.h>
5260484Sobrien#include <i386/ibcs2/ibcs2_ustat.h>
53130561Sobrien#include <i386/ibcs2/ibcs2_util.h>
5433965Sjdp#include <i386/ibcs2/ibcs2_utsname.h>
55104834Sobrien
56104834Sobrienstatic void bsd_stat2ibcs_stat __P((struct stat *, struct ibcs2_stat *));
57104834Sobrienstatic int  cvt_statfs         __P((struct statfs *, caddr_t, int));
5860484Sobrien
59243933Seadlerstatic void
6033965Sjdpbsd_stat2ibcs_stat(st, st4)
6160484Sobrien	struct stat *st;
6260484Sobrien	struct ibcs2_stat *st4;
6333965Sjdp{
6460484Sobrien	bzero(st4, sizeof(*st4));
6589857Sobrien	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
6689857Sobrien	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
6733965Sjdp	st4->st_mode = (ibcs2_mode_t)st->st_mode;
6833965Sjdp	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
6933965Sjdp	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
7033965Sjdp	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
7133965Sjdp	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
72130561Sobrien	if (st->st_size < (quad_t)1 << 32)
73130561Sobrien		st4->st_size = (ibcs2_off_t)st->st_size;
7433965Sjdp	else
7560484Sobrien		st4->st_size = -2;
7633965Sjdp	st4->st_atim = (ibcs2_time_t)st->st_atime;
7733965Sjdp	st4->st_mtim = (ibcs2_time_t)st->st_mtime;
7833965Sjdp	st4->st_ctim = (ibcs2_time_t)st->st_ctime;
7933965Sjdp}
8033965Sjdp
8133965Sjdpstatic int
8233965Sjdpcvt_statfs(sp, buf, len)
8333965Sjdp	struct statfs *sp;
84243933Seadler	caddr_t buf;
8533965Sjdp	int len;
8633965Sjdp{
87130561Sobrien	struct ibcs2_statfs ssfs;
8833965Sjdp
8933965Sjdp	bzero(&ssfs, sizeof ssfs);
9033965Sjdp	ssfs.f_fstyp = 0;
9138889Sjdp	ssfs.f_bsize = sp->f_bsize;
9238889Sjdp	ssfs.f_frsize = 0;
9338889Sjdp	ssfs.f_blocks = sp->f_blocks;
9433965Sjdp	ssfs.f_bfree = sp->f_bfree;
9533965Sjdp	ssfs.f_files = sp->f_files;
9633965Sjdp	ssfs.f_ffree = sp->f_ffree;
9760484Sobrien	ssfs.f_fname[0] = 0;
9860484Sobrien	ssfs.f_fpack[0] = 0;
9960484Sobrien	return copyout((caddr_t)&ssfs, buf, len);
10060484Sobrien}
10160484Sobrien
10260484Sobrienint
10333965Sjdpibcs2_statfs(p, uap, retval)
10460484Sobrien	struct proc *p;
10560484Sobrien	struct ibcs2_statfs_args *uap;
10633965Sjdp	int *retval;
10789857Sobrien{
10860484Sobrien	register struct mount *mp;
109130561Sobrien	register struct statfs *sp;
11060484Sobrien	int error;
11133965Sjdp	struct nameidata nd;
11260484Sobrien	caddr_t sg = stackgap_init();
11360484Sobrien
11433965Sjdp	CHECKALTEXIST(p, &sg, SCARG(uap, path));
11560484Sobrien	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
11633965Sjdp	if (error = namei(&nd))
11733965Sjdp		return (error);
11833965Sjdp	mp = nd.ni_vp->v_mount;
11960484Sobrien	sp = &mp->mnt_stat;
120130561Sobrien	vrele(nd.ni_vp);
121130561Sobrien	if (error = VFS_STATFS(mp, sp, p))
12233965Sjdp		return (error);
123130561Sobrien	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
12433965Sjdp	return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len));
12589857Sobrien}
12689857Sobrien
127130561Sobrienint
128130561Sobrienibcs2_fstatfs(p, uap, retval)
12989857Sobrien	struct proc *p;
13089857Sobrien	struct ibcs2_fstatfs_args *uap;
13189857Sobrien	int *retval;
132130561Sobrien{
13389857Sobrien	struct file *fp;
134218822Sdim	struct mount *mp;
135218822Sdim	register struct statfs *sp;
13689857Sobrien	int error;
137218822Sdim
13889857Sobrien	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
13989857Sobrien		return (error);
14089857Sobrien	mp = ((struct vnode *)fp->f_data)->v_mount;
141130561Sobrien	sp = &mp->mnt_stat;
142130561Sobrien	if (error = VFS_STATFS(mp, sp, p))
14389857Sobrien		return (error);
144130561Sobrien	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
14589857Sobrien	return cvt_statfs(sp, (caddr_t)SCARG(uap, buf), SCARG(uap, len));
146218822Sdim}
147218822Sdim
148130561Sobrienint
149130561Sobrienibcs2_stat(p, uap, retval)
15089857Sobrien	struct proc *p;
151130561Sobrien	struct ibcs2_stat_args *uap;
152130561Sobrien	int *retval;
153130561Sobrien{
154218822Sdim	struct stat st;
15589857Sobrien	struct ibcs2_stat ibcs2_st;
156218822Sdim	struct stat_args cup;
157130561Sobrien	int error;
15889857Sobrien	caddr_t sg = stackgap_init();
159218822Sdim
160218822Sdim	CHECKALTEXIST(p, &sg, SCARG(uap, path));
16189857Sobrien	SCARG(&cup, path) = SCARG(uap, path);
16289857Sobrien	SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st));
16389857Sobrien
164218822Sdim	if (error = stat(p, &cup, retval))
165218822Sdim		return error;
166218822Sdim
167130561Sobrien	if (error = copyin(SCARG(&cup, ub), &st, sizeof(st)))
168218822Sdim		return error;
169218822Sdim	bsd_stat2ibcs_stat(&st, &ibcs2_st);
170218822Sdim	return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st),
171218822Sdim		       ibcs2_stat_len);
172218822Sdim}
173218822Sdim
174218822Sdimint
175218822Sdimibcs2_lstat(p, uap, retval)
176218822Sdim	struct proc *p;
177218822Sdim	struct ibcs2_lstat_args *uap;
178130561Sobrien	int *retval;
179130561Sobrien{
180218822Sdim	struct stat st;
181218822Sdim	struct ibcs2_stat ibcs2_st;
182218822Sdim	struct lstat_args cup;
18389857Sobrien	int error;
18489857Sobrien	caddr_t sg = stackgap_init();
18589857Sobrien
18689857Sobrien	CHECKALTEXIST(p, &sg, SCARG(uap, path));
187218822Sdim	SCARG(&cup, path) = SCARG(uap, path);
188218822Sdim	SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(st));
189218822Sdim
190130561Sobrien	if (error = lstat(p, &cup, retval))
191130561Sobrien		return error;
192218822Sdim
193130561Sobrien	if (error = copyin(SCARG(&cup, ub), &st, sizeof(st)))
194218822Sdim		return error;
195218822Sdim	bsd_stat2ibcs_stat(&st, &ibcs2_st);
196218822Sdim	return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st),
197218822Sdim		       ibcs2_stat_len);
198130561Sobrien}
199218822Sdim
200218822Sdimint
201218822Sdimibcs2_fstat(p, uap, retval)
202218822Sdim	struct proc *p;
203218822Sdim	struct ibcs2_fstat_args *uap;
204130561Sobrien	int *retval;
205218822Sdim{
206218822Sdim	struct stat st;
207130561Sobrien	struct ibcs2_stat ibcs2_st;
20889857Sobrien	struct fstat_args cup;
209104834Sobrien	int error;
210104834Sobrien	caddr_t sg = stackgap_init();
211104834Sobrien
212104834Sobrien	SCARG(&cup, fd) = SCARG(uap, fd);
213104834Sobrien	SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(st));
214104834Sobrien
215104834Sobrien	if (error = fstat(p, &cup, retval))
216104834Sobrien		return error;
217104834Sobrien
218104834Sobrien	if (error = copyin(SCARG(&cup, sb), &st, sizeof(st)))
219104834Sobrien		return error;
220104834Sobrien	bsd_stat2ibcs_stat(&st, &ibcs2_st);
221104834Sobrien	return copyout((caddr_t)&ibcs2_st, (caddr_t)SCARG(uap, st),
222104834Sobrien		       ibcs2_stat_len);
223104834Sobrien}
224104834Sobrien
22589857Sobrienint
22689857Sobrienibcs2_utssys(p, uap, retval)
227243933Seadler	struct proc *p;
22889857Sobrien	struct ibcs2_utssys_args *uap;
22989857Sobrien	int *retval;
23089857Sobrien{
23189857Sobrien	switch (SCARG(uap, flag)) {
232243933Seadler	case 0:			/* uname(2) */
23389857Sobrien	{
23489857Sobrien		char machine_name[9], *p;
23589857Sobrien		struct ibcs2_utsname sut;
23689857Sobrien		bzero(&sut, ibcs2_utsname_len);
23789857Sobrien
23889857Sobrien		strncpy(sut.sysname, IBCS2_UNAME_SYSNAME, sizeof(sut.sysname));
23989857Sobrien		strncpy(sut.release, IBCS2_UNAME_RELEASE, sizeof(sut.release));
240243933Seadler		strncpy(sut.version, IBCS2_UNAME_VERSION, sizeof(sut.version));
24189857Sobrien		strncpy(machine_name, hostname, sizeof(machine_name));
24289857Sobrien		p = index(machine_name, '.');
24389857Sobrien		if ( p )
24489857Sobrien			*p = '\0';
24560484Sobrien		strncpy(sut.nodename, machine_name, sizeof(sut.nodename));
24660484Sobrien		strncpy(sut.machine, machine, sizeof(sut.machine));
24789857Sobrien		sut.sysname[sizeof(sut.sysname)-1] = '\0';
24833965Sjdp		sut.release[sizeof(sut.release)-1] = '\0';
249218822Sdim		sut.version[sizeof(sut.version)-1] = '\0';
250218822Sdim		sut.nodename[sizeof(sut.nodename)-1] = '\0';
251218822Sdim		sut.machine[sizeof(sut.machine)-1] = '\0';
252218822Sdim
253218822Sdim		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
254218822Sdim			 sut.sysname, sut.release, sut.version, sut.nodename,
255218822Sdim			 sut.machine));
256218822Sdim		return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, a1),
257218822Sdim			       ibcs2_utsname_len);
258218822Sdim	}
259218822Sdim
260218822Sdim	case 2:			/* ustat(2) */
261218822Sdim	{
262218822Sdim		return ENOSYS;	/* XXX - TODO */
263218822Sdim	}
264218822Sdim
265218822Sdim	default:
266218822Sdim		return ENOSYS;
267218822Sdim	}
268218822Sdim}
269218822Sdim