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