ibcs2_stat.c revision 116678
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
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/i386/ibcs2/ibcs2_stat.c 116678 2003-06-22 08:41:43Z phk $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/namei.h>
35#include <sys/file.h>
36#include <sys/stat.h>
37#include <sys/filedesc.h>
38#include <sys/jail.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
53static void bsd_stat2ibcs_stat(struct stat *, struct ibcs2_stat *);
54static int  cvt_statfs(struct statfs *, caddr_t, int);
55
56static void
57bsd_stat2ibcs_stat(st, st4)
58	struct stat *st;
59	struct ibcs2_stat *st4;
60{
61	bzero(st4, sizeof(*st4));
62	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
63	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
64	st4->st_mode = (ibcs2_mode_t)st->st_mode;
65	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
66	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
67	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
68	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
69	if (st->st_size < (quad_t)1 << 32)
70		st4->st_size = (ibcs2_off_t)st->st_size;
71	else
72		st4->st_size = -2;
73	st4->st_atim = (ibcs2_time_t)st->st_atime;
74	st4->st_mtim = (ibcs2_time_t)st->st_mtime;
75	st4->st_ctim = (ibcs2_time_t)st->st_ctime;
76}
77
78static int
79cvt_statfs(sp, buf, len)
80	struct statfs *sp;
81	caddr_t buf;
82	int len;
83{
84	struct ibcs2_statfs ssfs;
85
86	bzero(&ssfs, sizeof ssfs);
87	ssfs.f_fstyp = 0;
88	ssfs.f_bsize = sp->f_bsize;
89	ssfs.f_frsize = 0;
90	ssfs.f_blocks = sp->f_blocks;
91	ssfs.f_bfree = sp->f_bfree;
92	ssfs.f_files = sp->f_files;
93	ssfs.f_ffree = sp->f_ffree;
94	ssfs.f_fname[0] = 0;
95	ssfs.f_fpack[0] = 0;
96	return copyout((caddr_t)&ssfs, buf, len);
97}
98
99int
100ibcs2_statfs(td, uap)
101	struct thread *td;
102	struct ibcs2_statfs_args *uap;
103{
104	register struct mount *mp;
105	register struct statfs *sp;
106	int error;
107	struct nameidata nd;
108	caddr_t sg = stackgap_init();
109
110	CHECKALTEXIST(td, &sg, uap->path);
111	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 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)uap->buf, uap->len);
122}
123
124int
125ibcs2_fstatfs(td, uap)
126	struct thread *td;
127	struct ibcs2_fstatfs_args *uap;
128{
129	struct file *fp;
130	struct mount *mp;
131	register struct statfs *sp;
132	int error;
133
134	if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
135		return (error);
136	mp = fp->f_vnode->v_mount;
137	sp = &mp->mnt_stat;
138	error = VFS_STATFS(mp, sp, td);
139	fdrop(fp, td);
140	if (error != 0)
141		return (error);
142	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
143	return cvt_statfs(sp, (caddr_t)uap->buf, uap->len);
144}
145
146int
147ibcs2_stat(td, uap)
148	struct thread *td;
149	struct ibcs2_stat_args *uap;
150{
151	struct stat st;
152	struct ibcs2_stat ibcs2_st;
153	struct stat_args cup;
154	int error;
155	caddr_t sg = stackgap_init();
156
157	CHECKALTEXIST(td, &sg, uap->path);
158	cup.path = uap->path;
159	cup.ub = stackgap_alloc(&sg, sizeof(st));
160
161	if ((error = stat(td, &cup)) != 0)
162		return error;
163
164	if ((error = copyin(cup.ub, &st, sizeof(st))) != 0)
165		return error;
166	bsd_stat2ibcs_stat(&st, &ibcs2_st);
167	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
168		       ibcs2_stat_len);
169}
170
171int
172ibcs2_lstat(td, uap)
173	struct thread *td;
174	struct ibcs2_lstat_args *uap;
175{
176	struct stat st;
177	struct ibcs2_stat ibcs2_st;
178	struct lstat_args cup;
179	int error;
180	caddr_t sg = stackgap_init();
181
182	CHECKALTEXIST(td, &sg, uap->path);
183	cup.path = uap->path;
184	cup.ub = stackgap_alloc(&sg, sizeof(st));
185
186	if ((error = lstat(td, &cup)) != 0)
187		return error;
188
189	if ((error = copyin(cup.ub, &st, sizeof(st))) != 0)
190		return error;
191	bsd_stat2ibcs_stat(&st, &ibcs2_st);
192	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
193		       ibcs2_stat_len);
194}
195
196int
197ibcs2_fstat(td, uap)
198	struct thread *td;
199	struct ibcs2_fstat_args *uap;
200{
201	struct stat st;
202	struct ibcs2_stat ibcs2_st;
203	struct fstat_args cup;
204	int error;
205	caddr_t sg = stackgap_init();
206
207	cup.fd = uap->fd;
208	cup.sb = stackgap_alloc(&sg, sizeof(st));
209
210	if ((error = fstat(td, &cup)) != 0)
211		return error;
212
213	if ((error = copyin(cup.sb, &st, sizeof(st))) != 0)
214		return error;
215	bsd_stat2ibcs_stat(&st, &ibcs2_st);
216	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
217		       ibcs2_stat_len);
218}
219
220int
221ibcs2_utssys(td, uap)
222	struct thread *td;
223	struct ibcs2_utssys_args *uap;
224{
225	switch (uap->flag) {
226	case 0:			/* uname(2) */
227	{
228		char machine_name[9], *p;
229		struct ibcs2_utsname sut;
230		bzero(&sut, ibcs2_utsname_len);
231
232		strncpy(sut.sysname,
233			IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1);
234		strncpy(sut.release,
235			IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1);
236		strncpy(sut.version,
237			IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
238		getcredhostname(td->td_ucred, machine_name,
239		    sizeof(machine_name) - 1);
240		p = index(machine_name, '.');
241		if ( p )
242			*p = '\0';
243		strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);
244		strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
245
246		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
247			 sut.sysname, sut.release, sut.version, sut.nodename,
248			 sut.machine));
249		return copyout((caddr_t)&sut, (caddr_t)uap->a1,
250			       ibcs2_utsname_len);
251	}
252
253	case 2:			/* ustat(2) */
254	{
255		return ENOSYS;	/* XXX - TODO */
256	}
257
258	default:
259		return ENOSYS;
260	}
261}
262