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: releng/11.0/sys/i386/ibcs2/ibcs2_stat.c 274476 2014-11-13 18:01:51Z kib $");
31115684Sobrien
3211394Sswallace#include <sys/param.h>
3311394Sswallace#include <sys/systm.h>
3411394Sswallace#include <sys/namei.h>
35274476Skib#include <sys/fcntl.h>
3611394Sswallace#include <sys/file.h>
3711394Sswallace#include <sys/stat.h>
3811394Sswallace#include <sys/filedesc.h>
3991388Srobert#include <sys/jail.h>
4011394Sswallace#include <sys/kernel.h>
4111394Sswallace#include <sys/mount.h>
42141488Sjhb#include <sys/malloc.h>
4311394Sswallace#include <sys/vnode.h>
44141488Sjhb#include <sys/syscallsubr.h>
4511397Sswallace#include <sys/sysctl.h>
4611397Sswallace#include <sys/sysproto.h>
4711394Sswallace
4811397Sswallace#include <i386/ibcs2/ibcs2_signal.h>
4911397Sswallace#include <i386/ibcs2/ibcs2_stat.h>
5011397Sswallace#include <i386/ibcs2/ibcs2_statfs.h>
5111397Sswallace#include <i386/ibcs2/ibcs2_proto.h>
5211397Sswallace#include <i386/ibcs2/ibcs2_util.h>
5311397Sswallace#include <i386/ibcs2/ibcs2_utsname.h>
5411394Sswallace
5554655Seivind
5692761Salfredstatic void bsd_stat2ibcs_stat(struct stat *, struct ibcs2_stat *);
5792761Salfredstatic int  cvt_statfs(struct statfs *, caddr_t, int);
5811394Sswallace
5911394Sswallacestatic void
6011394Sswallacebsd_stat2ibcs_stat(st, st4)
6111397Sswallace	struct stat *st;
6211394Sswallace	struct ibcs2_stat *st4;
6311394Sswallace{
6411394Sswallace	bzero(st4, sizeof(*st4));
6511397Sswallace	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
6611397Sswallace	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
6711394Sswallace	st4->st_mode = (ibcs2_mode_t)st->st_mode;
6811397Sswallace	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
6911397Sswallace	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
7011397Sswallace	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
7111394Sswallace	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
7211397Sswallace	if (st->st_size < (quad_t)1 << 32)
7311397Sswallace		st4->st_size = (ibcs2_off_t)st->st_size;
7411397Sswallace	else
7511397Sswallace		st4->st_size = -2;
76205792Sed	st4->st_atim = (ibcs2_time_t)st->st_atim.tv_sec;
77205792Sed	st4->st_mtim = (ibcs2_time_t)st->st_mtim.tv_sec;
78205792Sed	st4->st_ctim = (ibcs2_time_t)st->st_ctim.tv_sec;
7911394Sswallace}
8011394Sswallace
8111394Sswallacestatic int
8211394Sswallacecvt_statfs(sp, buf, len)
8311394Sswallace	struct statfs *sp;
8411394Sswallace	caddr_t buf;
8511394Sswallace	int len;
8611394Sswallace{
8711394Sswallace	struct ibcs2_statfs ssfs;
8811394Sswallace
89118754Snectar	if (len < 0)
90118754Snectar		return (EINVAL);
91118754Snectar	else if (len > sizeof(ssfs))
92118754Snectar		len = sizeof(ssfs);
9311394Sswallace	bzero(&ssfs, sizeof ssfs);
9411394Sswallace	ssfs.f_fstyp = 0;
9511394Sswallace	ssfs.f_bsize = sp->f_bsize;
9611394Sswallace	ssfs.f_frsize = 0;
9711394Sswallace	ssfs.f_blocks = sp->f_blocks;
9811394Sswallace	ssfs.f_bfree = sp->f_bfree;
9911394Sswallace	ssfs.f_files = sp->f_files;
10011394Sswallace	ssfs.f_ffree = sp->f_ffree;
10111394Sswallace	ssfs.f_fname[0] = 0;
10211394Sswallace	ssfs.f_fpack[0] = 0;
10311394Sswallace	return copyout((caddr_t)&ssfs, buf, len);
10411394Sswallace}
10511394Sswallace
10611394Sswallaceint
10783366Sjulianibcs2_statfs(td, uap)
10883366Sjulian	struct thread *td;
10911394Sswallace	struct ibcs2_statfs_args *uap;
11011394Sswallace{
111141488Sjhb	struct statfs sf;
112141488Sjhb	char *path;
11311394Sswallace	int error;
11411394Sswallace
115141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
116141488Sjhb	error = kern_statfs(td, path, UIO_SYSSPACE, &sf);
117141488Sjhb	free(path, M_TEMP);
118141488Sjhb	if (error)
11911394Sswallace		return (error);
120141488Sjhb	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
12111394Sswallace}
12211394Sswallace
12311394Sswallaceint
12483366Sjulianibcs2_fstatfs(td, uap)
12583366Sjulian	struct thread *td;
12611394Sswallace	struct ibcs2_fstatfs_args *uap;
12711394Sswallace{
128141488Sjhb	struct statfs sf;
12911394Sswallace	int error;
13011394Sswallace
131141488Sjhb	error = kern_fstatfs(td, uap->fd, &sf);
132141488Sjhb	if (error)
13311394Sswallace		return (error);
134141488Sjhb	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
13511394Sswallace}
13611394Sswallace
13711394Sswallaceint
13883366Sjulianibcs2_stat(td, uap)
13983366Sjulian	struct thread *td;
14011394Sswallace	struct ibcs2_stat_args *uap;
14111394Sswallace{
142141488Sjhb	struct ibcs2_stat ibcs2_st;
14311397Sswallace	struct stat st;
144141488Sjhb	char *path;
14511394Sswallace	int error;
14611394Sswallace
147141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
14811397Sswallace
149274476Skib	error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, NULL);
150141488Sjhb	free(path, M_TEMP);
151141488Sjhb	if (error)
152141488Sjhb		return (error);
15311394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
154107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
15511394Sswallace		       ibcs2_stat_len);
15611394Sswallace}
15711394Sswallace
15811394Sswallaceint
15983366Sjulianibcs2_lstat(td, uap)
16083366Sjulian	struct thread *td;
16111394Sswallace	struct ibcs2_lstat_args *uap;
16211394Sswallace{
163141488Sjhb	struct ibcs2_stat ibcs2_st;
16411397Sswallace	struct stat st;
165141488Sjhb	char *path;
16611394Sswallace	int error;
16711394Sswallace
168141488Sjhb	CHECKALTEXIST(td, uap->path, &path);
16911397Sswallace
170274476Skib	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
171274476Skib	    UIO_SYSSPACE, &st, NULL);
172141488Sjhb	free(path, M_TEMP);
173141488Sjhb	if (error)
174141488Sjhb		return (error);
17511394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
176107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
17711394Sswallace		       ibcs2_stat_len);
17811394Sswallace}
17911394Sswallace
18011394Sswallaceint
18183366Sjulianibcs2_fstat(td, uap)
18283366Sjulian	struct thread *td;
18311394Sswallace	struct ibcs2_fstat_args *uap;
18411394Sswallace{
185141488Sjhb	struct ibcs2_stat ibcs2_st;
18611397Sswallace	struct stat st;
18711394Sswallace	int error;
18811394Sswallace
189141488Sjhb	error = kern_fstat(td, uap->fd, &st);
190141488Sjhb	if (error)
191141488Sjhb		return (error);
19211394Sswallace	bsd_stat2ibcs_stat(&st, &ibcs2_st);
193107849Salfred	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
19411394Sswallace		       ibcs2_stat_len);
19511394Sswallace}
19611394Sswallace
19711394Sswallaceint
19883366Sjulianibcs2_utssys(td, uap)
19983366Sjulian	struct thread *td;
20011394Sswallace	struct ibcs2_utssys_args *uap;
20111394Sswallace{
202107849Salfred	switch (uap->flag) {
20311394Sswallace	case 0:			/* uname(2) */
20411394Sswallace	{
20516193Snate		char machine_name[9], *p;
20611394Sswallace		struct ibcs2_utsname sut;
20711525Sswallace		bzero(&sut, ibcs2_utsname_len);
20811394Sswallace
20941514Sarchie		strncpy(sut.sysname,
21041514Sarchie			IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1);
21141514Sarchie		strncpy(sut.release,
21241514Sarchie			IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1);
21341514Sarchie		strncpy(sut.version,
21441514Sarchie			IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
21591393Srobert		getcredhostname(td->td_ucred, machine_name,
21691388Srobert		    sizeof(machine_name) - 1);
217229272Sed		p = strchr(machine_name, '.');
21816193Snate		if ( p )
21916193Snate			*p = '\0';
22041514Sarchie		strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);
22141514Sarchie		strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
22211394Sswallace
22311525Sswallace		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
22411525Sswallace			 sut.sysname, sut.release, sut.version, sut.nodename,
22511525Sswallace			 sut.machine));
226107849Salfred		return copyout((caddr_t)&sut, (caddr_t)uap->a1,
22711394Sswallace			       ibcs2_utsname_len);
22811394Sswallace	}
22911394Sswallace
23011394Sswallace	case 2:			/* ustat(2) */
23111394Sswallace	{
23211394Sswallace		return ENOSYS;	/* XXX - TODO */
23311394Sswallace	}
23411394Sswallace
23511394Sswallace	default:
23611394Sswallace		return ENOSYS;
23711394Sswallace	}
23811394Sswallace}
239