ibcs2_stat.c revision 141488
1151497Sru/*-
2151497Sru * Copyright (c) 1995 Scott Bartram
3151497Sru * Copyright (c) 1995 Steven Wallace
4151497Sru * All rights reserved.
5151497Sru *
6151497Sru * Redistribution and use in source and binary forms, with or without
7151497Sru * modification, are permitted provided that the following conditions
8151497Sru * are met:
9151497Sru * 1. Redistributions of source code must retain the above copyright
10151497Sru *    notice, this list of conditions and the following disclaimer.
11151497Sru * 2. Redistributions in binary form must reproduce the above copyright
12151497Sru *    notice, this list of conditions and the following disclaimer in the
13151497Sru *    documentation and/or other materials provided with the distribution.
14151497Sru * 3. The name of the author may not be used to endorse or promote products
15151497Sru *    derived from this software without specific prior written permission
16151497Sru *
17151497Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18151497Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19151497Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20151497Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21151497Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22151497Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23151497Sru * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24151497Sru * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25151497Sru * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26151497Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27151497Sru */
28151497Sru
29151497Sru#include <sys/cdefs.h>
30151497Sru__FBSDID("$FreeBSD: head/sys/i386/ibcs2/ibcs2_stat.c 141488 2005-02-07 22:02:18Z jhb $");
31151497Sru
32151497Sru#include <sys/param.h>
33151497Sru#include <sys/systm.h>
34151497Sru#include <sys/namei.h>
35151497Sru#include <sys/file.h>
36151497Sru#include <sys/stat.h>
37151497Sru#include <sys/filedesc.h>
38151497Sru#include <sys/jail.h>
39151497Sru#include <sys/kernel.h>
40151497Sru#include <sys/mount.h>
41151497Sru#include <sys/malloc.h>
42151497Sru#include <sys/vnode.h>
43151497Sru#include <sys/syscallsubr.h>
44151497Sru#include <sys/sysctl.h>
45151497Sru#include <sys/sysproto.h>
46151497Sru
47151497Sru#include <i386/ibcs2/ibcs2_signal.h>
48151497Sru#include <i386/ibcs2/ibcs2_stat.h>
49151497Sru#include <i386/ibcs2/ibcs2_statfs.h>
50151497Sru#include <i386/ibcs2/ibcs2_proto.h>
51151497Sru#include <i386/ibcs2/ibcs2_util.h>
52151497Sru#include <i386/ibcs2/ibcs2_utsname.h>
53151497Sru
54151497Sru
55151497Srustatic void bsd_stat2ibcs_stat(struct stat *, struct ibcs2_stat *);
56151497Srustatic int  cvt_statfs(struct statfs *, caddr_t, int);
57151497Sru
58151497Srustatic void
59151497Srubsd_stat2ibcs_stat(st, st4)
60151497Sru	struct stat *st;
61151497Sru	struct ibcs2_stat *st4;
62151497Sru{
63151497Sru	bzero(st4, sizeof(*st4));
64151497Sru	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
65151497Sru	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
66151497Sru	st4->st_mode = (ibcs2_mode_t)st->st_mode;
67151497Sru	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
68151497Sru	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
69151497Sru	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
70151497Sru	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
71151497Sru	if (st->st_size < (quad_t)1 << 32)
72151497Sru		st4->st_size = (ibcs2_off_t)st->st_size;
73151497Sru	else
74151497Sru		st4->st_size = -2;
75151497Sru	st4->st_atim = (ibcs2_time_t)st->st_atime;
76151497Sru	st4->st_mtim = (ibcs2_time_t)st->st_mtime;
77151497Sru	st4->st_ctim = (ibcs2_time_t)st->st_ctime;
78151497Sru}
79151497Sru
80151497Srustatic int
81151497Srucvt_statfs(sp, buf, len)
82151497Sru	struct statfs *sp;
83151497Sru	caddr_t buf;
84	int len;
85{
86	struct ibcs2_statfs ssfs;
87
88	if (len < 0)
89		return (EINVAL);
90	else if (len > sizeof(ssfs))
91		len = sizeof(ssfs);
92	bzero(&ssfs, sizeof ssfs);
93	ssfs.f_fstyp = 0;
94	ssfs.f_bsize = sp->f_bsize;
95	ssfs.f_frsize = 0;
96	ssfs.f_blocks = sp->f_blocks;
97	ssfs.f_bfree = sp->f_bfree;
98	ssfs.f_files = sp->f_files;
99	ssfs.f_ffree = sp->f_ffree;
100	ssfs.f_fname[0] = 0;
101	ssfs.f_fpack[0] = 0;
102	return copyout((caddr_t)&ssfs, buf, len);
103}
104
105int
106ibcs2_statfs(td, uap)
107	struct thread *td;
108	struct ibcs2_statfs_args *uap;
109{
110	struct statfs sf;
111	char *path;
112	int error;
113
114	CHECKALTEXIST(td, uap->path, &path);
115	error = kern_statfs(td, path, UIO_SYSSPACE, &sf);
116	free(path, M_TEMP);
117	if (error)
118		return (error);
119	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
120}
121
122int
123ibcs2_fstatfs(td, uap)
124	struct thread *td;
125	struct ibcs2_fstatfs_args *uap;
126{
127	struct statfs sf;
128	int error;
129
130	error = kern_fstatfs(td, uap->fd, &sf);
131	if (error)
132		return (error);
133	return cvt_statfs(&sf, (caddr_t)uap->buf, uap->len);
134}
135
136int
137ibcs2_stat(td, uap)
138	struct thread *td;
139	struct ibcs2_stat_args *uap;
140{
141	struct ibcs2_stat ibcs2_st;
142	struct stat st;
143	char *path;
144	int error;
145
146	CHECKALTEXIST(td, uap->path, &path);
147
148	error = kern_stat(td, path, UIO_SYSSPACE, &st);
149	free(path, M_TEMP);
150	if (error)
151		return (error);
152	bsd_stat2ibcs_stat(&st, &ibcs2_st);
153	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
154		       ibcs2_stat_len);
155}
156
157int
158ibcs2_lstat(td, uap)
159	struct thread *td;
160	struct ibcs2_lstat_args *uap;
161{
162	struct ibcs2_stat ibcs2_st;
163	struct stat st;
164	char *path;
165	int error;
166
167	CHECKALTEXIST(td, uap->path, &path);
168
169	error = kern_lstat(td, path, UIO_SYSSPACE, &st);
170	free(path, M_TEMP);
171	if (error)
172		return (error);
173	bsd_stat2ibcs_stat(&st, &ibcs2_st);
174	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
175		       ibcs2_stat_len);
176}
177
178int
179ibcs2_fstat(td, uap)
180	struct thread *td;
181	struct ibcs2_fstat_args *uap;
182{
183	struct ibcs2_stat ibcs2_st;
184	struct stat st;
185	int error;
186
187	error = kern_fstat(td, uap->fd, &st);
188	if (error)
189		return (error);
190	bsd_stat2ibcs_stat(&st, &ibcs2_st);
191	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
192		       ibcs2_stat_len);
193}
194
195int
196ibcs2_utssys(td, uap)
197	struct thread *td;
198	struct ibcs2_utssys_args *uap;
199{
200	switch (uap->flag) {
201	case 0:			/* uname(2) */
202	{
203		char machine_name[9], *p;
204		struct ibcs2_utsname sut;
205		bzero(&sut, ibcs2_utsname_len);
206
207		strncpy(sut.sysname,
208			IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1);
209		strncpy(sut.release,
210			IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1);
211		strncpy(sut.version,
212			IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
213		getcredhostname(td->td_ucred, machine_name,
214		    sizeof(machine_name) - 1);
215		p = index(machine_name, '.');
216		if ( p )
217			*p = '\0';
218		strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);
219		strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
220
221		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
222			 sut.sysname, sut.release, sut.version, sut.nodename,
223			 sut.machine));
224		return copyout((caddr_t)&sut, (caddr_t)uap->a1,
225			       ibcs2_utsname_len);
226	}
227
228	case 2:			/* ustat(2) */
229	{
230		return ENOSYS;	/* XXX - TODO */
231	}
232
233	default:
234		return ENOSYS;
235	}
236}
237