1139799Simp/*-
211394Sswallace * Copyright (c) 1995 Scott Bartram
311397Sswallace * Copyright (c) 1995 Steven Wallace
411394Sswallace * All rights reserved.
511394Sswallace *
611394Sswallace * Redistribution and use in source and binary forms, with or without
711394Sswallace * modification, are permitted provided that the following conditions
811394Sswallace * are met:
911394Sswallace * 1. Redistributions of source code must retain the above copyright
1011394Sswallace *    notice, this list of conditions and the following disclaimer.
1111394Sswallace * 2. Redistributions in binary form must reproduce the above copyright
1211394Sswallace *    notice, this list of conditions and the following disclaimer in the
1311394Sswallace *    documentation and/or other materials provided with the distribution.
1411394Sswallace * 3. The name of the author may not be used to endorse or promote products
1511394Sswallace *    derived from this software without specific prior written permission
1611394Sswallace *
1711394Sswallace * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1811394Sswallace * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1911394Sswallace * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011394Sswallace * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2111394Sswallace * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2211394Sswallace * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2311394Sswallace * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2411394Sswallace * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2511394Sswallace * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2611394Sswallace * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711394Sswallace */
2811394Sswallace
29115684Sobrien#include <sys/cdefs.h>
30115684Sobrien__FBSDID("$FreeBSD$");
31115684Sobrien
3211394Sswallace#include <sys/param.h>
3311394Sswallace#include <sys/systm.h>
3411394Sswallace#include <sys/namei.h>
3511394Sswallace#include <sys/file.h>
3611394Sswallace#include <sys/stat.h>
3711394Sswallace#include <sys/filedesc.h>
3891388Srobert#include <sys/jail.h>
3911394Sswallace#include <sys/kernel.h>
4011394Sswallace#include <sys/mount.h>
41141488Sjhb#include <sys/malloc.h>
4211394Sswallace#include <sys/vnode.h>
43141488Sjhb#include <sys/syscallsubr.h>
4411397Sswallace#include <sys/sysctl.h>
4511397Sswallace#include <sys/sysproto.h>
4611394Sswallace
4711397Sswallace#include <i386/ibcs2/ibcs2_signal.h>
4811397Sswallace#include <i386/ibcs2/ibcs2_stat.h>
4911397Sswallace#include <i386/ibcs2/ibcs2_statfs.h>
5011397Sswallace#include <i386/ibcs2/ibcs2_proto.h>
5111397Sswallace#include <i386/ibcs2/ibcs2_util.h>
5211397Sswallace#include <i386/ibcs2/ibcs2_utsname.h>
5311394Sswallace
5454655Seivind
5592761Salfredstatic void bsd_stat2ibcs_stat(struct stat *, struct ibcs2_stat *);
5692761Salfredstatic int  cvt_statfs(struct statfs *, caddr_t, int);
5711394Sswallace
5811394Sswallacestatic void
5911394Sswallacebsd_stat2ibcs_stat(st, st4)
6011397Sswallace	struct stat *st;
6111394Sswallace	struct ibcs2_stat *st4;
6211394Sswallace{
6311394Sswallace	bzero(st4, sizeof(*st4));
6411397Sswallace	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
6511397Sswallace	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
6611394Sswallace	st4->st_mode = (ibcs2_mode_t)st->st_mode;
6711397Sswallace	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
6811397Sswallace	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
6911397Sswallace	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
7011394Sswallace	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
7111397Sswallace	if (st->st_size < (quad_t)1 << 32)
7211397Sswallace		st4->st_size = (ibcs2_off_t)st->st_size;
7311397Sswallace	else
7411397Sswallace		st4->st_size = -2;
75205792Sed	st4->st_atim = (ibcs2_time_t)st->st_atim.tv_sec;
76205792Sed	st4->st_mtim = (ibcs2_time_t)st->st_mtim.tv_sec;
77205792Sed	st4->st_ctim = (ibcs2_time_t)st->st_ctim.tv_sec;
7811394Sswallace}
7911394Sswallace
8011394Sswallacestatic int
8111394Sswallacecvt_statfs(sp, buf, len)
8211394Sswallace	struct statfs *sp;
8311394Sswallace	caddr_t buf;
8411394Sswallace	int len;
8511394Sswallace{
8611394Sswallace	struct ibcs2_statfs ssfs;
8711394Sswallace
88118754Snectar	if (len < 0)
89118754Snectar		return (EINVAL);
90118754Snectar	else if (len > sizeof(ssfs))
91118754Snectar		len = sizeof(ssfs);
9211394Sswallace	bzero(&ssfs, sizeof ssfs);
9311394Sswallace	ssfs.f_fstyp = 0;
9411394Sswallace	ssfs.f_bsize = sp->f_bsize;
9511394Sswallace	ssfs.f_frsize = 0;
9611394Sswallace	ssfs.f_blocks = sp->f_blocks;
9711394Sswallace	ssfs.f_bfree = sp->f_bfree;
9811394Sswallace	ssfs.f_files = sp->f_files;
9911394Sswallace	ssfs.f_ffree = sp->f_ffree;
10011394Sswallace	ssfs.f_fname[0] = 0;
10111394Sswallace	ssfs.f_fpack[0] = 0;
10211394Sswallace	return copyout((caddr_t)&ssfs, buf, len);
10311394Sswallace}
10411394Sswallace
10511394Sswallaceint
10683366Sjulianibcs2_statfs(td, uap)
10783366Sjulian	struct thread *td;
10811394Sswallace	struct ibcs2_statfs_args *uap;
10911394Sswallace{
110141488Sjhb	struct statfs sf;
111141488Sjhb	char *path;
11211394Sswallace	int error;
11311394Sswallace
114141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
115141488Sjhb	error = kern_statfs(td, path, UIO_SYSSPACE, &sf);
116141488Sjhb	free(path, M_TEMP);
117141488Sjhb	if (error)
11811394Sswallace		return (error);
119141488Sjhb	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
12011394Sswallace}
12111394Sswallace
12211394Sswallaceint
12383366Sjulianibcs2_fstatfs(td, uap)
12483366Sjulian	struct thread *td;
12511394Sswallace	struct ibcs2_fstatfs_args *uap;
12611394Sswallace{
127141488Sjhb	struct statfs sf;
12811394Sswallace	int error;
12911394Sswallace
130141488Sjhb	error = kern_fstatfs(td, uap->fd, &sf);
131141488Sjhb	if (error)
13211394Sswallace		return (error);
133141488Sjhb	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
13411394Sswallace}
13511394Sswallace
13611394Sswallaceint
13783366Sjulianibcs2_stat(td, uap)
13883366Sjulian	struct thread *td;
13911394Sswallace	struct ibcs2_stat_args *uap;
14011394Sswallace{
141141488Sjhb	struct ibcs2_stat ibcs2_st;
14211397Sswallace	struct stat st;
143141488Sjhb	char *path;
14411394Sswallace	int error;
14511394Sswallace
146141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
14711397Sswallace
148141488Sjhb	error = kern_stat(td, path, UIO_SYSSPACE, &st);
149141488Sjhb	free(path, M_TEMP);
150141488Sjhb	if (error)
151141488Sjhb		return (error);
15211394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
153107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
15411394Sswallace		       ibcs2_stat_len);
15511394Sswallace}
15611394Sswallace
15711394Sswallaceint
15883366Sjulianibcs2_lstat(td, uap)
15983366Sjulian	struct thread *td;
16011394Sswallace	struct ibcs2_lstat_args *uap;
16111394Sswallace{
162141488Sjhb	struct ibcs2_stat ibcs2_st;
16311397Sswallace	struct stat st;
164141488Sjhb	char *path;
16511394Sswallace	int error;
16611394Sswallace
167141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
16811397Sswallace
169141488Sjhb	error = kern_lstat(td, path, UIO_SYSSPACE, &st);
170141488Sjhb	free(path, M_TEMP);
171141488Sjhb	if (error)
172141488Sjhb		return (error);
17311394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
174107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
17511394Sswallace		       ibcs2_stat_len);
17611394Sswallace}
17711394Sswallace
17811394Sswallaceint
17983366Sjulianibcs2_fstat(td, uap)
18083366Sjulian	struct thread *td;
18111394Sswallace	struct ibcs2_fstat_args *uap;
18211394Sswallace{
183141488Sjhb	struct ibcs2_stat ibcs2_st;
18411397Sswallace	struct stat st;
18511394Sswallace	int error;
18611394Sswallace
187141488Sjhb	error = kern_fstat(td, uap->fd, &st);
188141488Sjhb	if (error)
189141488Sjhb		return (error);
19011394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
191107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
19211394Sswallace		       ibcs2_stat_len);
19311394Sswallace}
19411394Sswallace
19511394Sswallaceint
19683366Sjulianibcs2_utssys(td, uap)
19783366Sjulian	struct thread *td;
19811394Sswallace	struct ibcs2_utssys_args *uap;
19911394Sswallace{
200107849Salfred	switch (uap->flag) {
20111394Sswallace	case 0:			/* uname(2) */
20211394Sswallace	{
20316193Snate		char machine_name[9], *p;
20411394Sswallace		struct ibcs2_utsname sut;
20511525Sswallace		bzero(&sut, ibcs2_utsname_len);
20611394Sswallace
20741514Sarchie		strncpy(sut.sysname,
20841514Sarchie			IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1);
20941514Sarchie		strncpy(sut.release,
21041514Sarchie			IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1);
21141514Sarchie		strncpy(sut.version,
21241514Sarchie			IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
21391393Srobert		getcredhostname(td->td_ucred, machine_name,
21491388Srobert		    sizeof(machine_name) - 1);
215229272Sed		p = strchr(machine_name, '.');
21616193Snate		if ( p )
21716193Snate			*p = '\0';
21841514Sarchie		strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);
21941514Sarchie		strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
22011394Sswallace
22111525Sswallace		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
22211525Sswallace			 sut.sysname, sut.release, sut.version, sut.nodename,
22311525Sswallace			 sut.machine));
224107849Salfred		return copyout((caddr_t)&sut, (caddr_t)uap->a1,
22511394Sswallace			       ibcs2_utsname_len);
22611394Sswallace	}
22711394Sswallace
22811394Sswallace	case 2:			/* ustat(2) */
22911394Sswallace	{
23011394Sswallace		return ENOSYS;	/* XXX - TODO */
23111394Sswallace	}
23211394Sswallace
23311394Sswallace	default:
23411394Sswallace		return ENOSYS;
23511394Sswallace	}
23611394Sswallace}
237