1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1995 Scott Bartram
5  * Copyright (c) 1995 Steven Wallace
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/12/sys/i386/ibcs2/ibcs2_stat.c 326260 2017-11-27 15:08:52Z pfg $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/fcntl.h>
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 #include <sys/filedesc.h>
41 #include <sys/jail.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/malloc.h>
46 #include <sys/vnode.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysproto.h>
50 
51 #include <i386/ibcs2/ibcs2_signal.h>
52 #include <i386/ibcs2/ibcs2_stat.h>
53 #include <i386/ibcs2/ibcs2_statfs.h>
54 #include <i386/ibcs2/ibcs2_proto.h>
55 #include <i386/ibcs2/ibcs2_util.h>
56 #include <i386/ibcs2/ibcs2_utsname.h>
57 
58 
59 static void bsd_stat2ibcs_stat(struct stat *, struct ibcs2_stat *);
60 static int  cvt_statfs(struct statfs *, caddr_t, int);
61 
62 static void
bsd_stat2ibcs_stat(st,st4)63 bsd_stat2ibcs_stat(st, st4)
64 	struct stat *st;
65 	struct ibcs2_stat *st4;
66 {
67 	bzero(st4, sizeof(*st4));
68 	st4->st_dev  = (ibcs2_dev_t)st->st_dev;
69 	st4->st_ino  = (ibcs2_ino_t)st->st_ino;
70 	st4->st_mode = (ibcs2_mode_t)st->st_mode;
71 	st4->st_nlink= (ibcs2_nlink_t)st->st_nlink;
72 	st4->st_uid  = (ibcs2_uid_t)st->st_uid;
73 	st4->st_gid  = (ibcs2_gid_t)st->st_gid;
74 	st4->st_rdev = (ibcs2_dev_t)st->st_rdev;
75 	if (st->st_size < (quad_t)1 << 32)
76 		st4->st_size = (ibcs2_off_t)st->st_size;
77 	else
78 		st4->st_size = -2;
79 	st4->st_atim = (ibcs2_time_t)st->st_atim.tv_sec;
80 	st4->st_mtim = (ibcs2_time_t)st->st_mtim.tv_sec;
81 	st4->st_ctim = (ibcs2_time_t)st->st_ctim.tv_sec;
82 }
83 
84 static int
cvt_statfs(sp,buf,len)85 cvt_statfs(sp, buf, len)
86 	struct statfs *sp;
87 	caddr_t buf;
88 	int len;
89 {
90 	struct ibcs2_statfs ssfs;
91 
92 	if (len < 0)
93 		return (EINVAL);
94 	else if (len > sizeof(ssfs))
95 		len = sizeof(ssfs);
96 	bzero(&ssfs, sizeof ssfs);
97 	ssfs.f_fstyp = 0;
98 	ssfs.f_bsize = sp->f_bsize;
99 	ssfs.f_frsize = 0;
100 	ssfs.f_blocks = sp->f_blocks;
101 	ssfs.f_bfree = sp->f_bfree;
102 	ssfs.f_files = sp->f_files;
103 	ssfs.f_ffree = sp->f_ffree;
104 	ssfs.f_fname[0] = 0;
105 	ssfs.f_fpack[0] = 0;
106 	return copyout((caddr_t)&ssfs, buf, len);
107 }
108 
109 int
ibcs2_statfs(td,uap)110 ibcs2_statfs(td, uap)
111 	struct thread *td;
112 	struct ibcs2_statfs_args *uap;
113 {
114 	struct statfs *sf;
115 	char *path;
116 	int error;
117 
118 	CHECKALTEXIST(td, uap->path, &path);
119 	sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
120 	error = kern_statfs(td, path, UIO_SYSSPACE, sf);
121 	free(path, M_TEMP);
122 	if (error == 0)
123 		error = cvt_statfs(sf, (caddr_t)uap->buf, uap->len);
124 	free(sf, M_STATFS);
125 	return (error);
126 }
127 
128 int
ibcs2_fstatfs(td,uap)129 ibcs2_fstatfs(td, uap)
130 	struct thread *td;
131 	struct ibcs2_fstatfs_args *uap;
132 {
133 	struct statfs *sf;
134 	int error;
135 
136 	sf = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
137 	error = kern_fstatfs(td, uap->fd, sf);
138 	if (error == 0)
139 		error = cvt_statfs(sf, (caddr_t)uap->buf, uap->len);
140 	free(sf, M_STATFS);
141 	return (error);
142 }
143 
144 int
ibcs2_stat(td,uap)145 ibcs2_stat(td, uap)
146 	struct thread *td;
147 	struct ibcs2_stat_args *uap;
148 {
149 	struct ibcs2_stat ibcs2_st;
150 	struct stat st;
151 	char *path;
152 	int error;
153 
154 	CHECKALTEXIST(td, uap->path, &path);
155 
156 	error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, NULL);
157 	free(path, M_TEMP);
158 	if (error)
159 		return (error);
160 	bsd_stat2ibcs_stat(&st, &ibcs2_st);
161 	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
162 		       ibcs2_stat_len);
163 }
164 
165 int
ibcs2_lstat(td,uap)166 ibcs2_lstat(td, uap)
167 	struct thread *td;
168 	struct ibcs2_lstat_args *uap;
169 {
170 	struct ibcs2_stat ibcs2_st;
171 	struct stat st;
172 	char *path;
173 	int error;
174 
175 	CHECKALTEXIST(td, uap->path, &path);
176 
177 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
178 	    UIO_SYSSPACE, &st, NULL);
179 	free(path, M_TEMP);
180 	if (error)
181 		return (error);
182 	bsd_stat2ibcs_stat(&st, &ibcs2_st);
183 	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
184 		       ibcs2_stat_len);
185 }
186 
187 int
ibcs2_fstat(td,uap)188 ibcs2_fstat(td, uap)
189 	struct thread *td;
190 	struct ibcs2_fstat_args *uap;
191 {
192 	struct ibcs2_stat ibcs2_st;
193 	struct stat st;
194 	int error;
195 
196 	error = kern_fstat(td, uap->fd, &st);
197 	if (error)
198 		return (error);
199 	bsd_stat2ibcs_stat(&st, &ibcs2_st);
200 	return copyout((caddr_t)&ibcs2_st, (caddr_t)uap->st,
201 		       ibcs2_stat_len);
202 }
203 
204 int
ibcs2_utssys(td,uap)205 ibcs2_utssys(td, uap)
206 	struct thread *td;
207 	struct ibcs2_utssys_args *uap;
208 {
209 	switch (uap->flag) {
210 	case 0:			/* uname(2) */
211 	{
212 		char machine_name[9], *p;
213 		struct ibcs2_utsname sut;
214 		bzero(&sut, ibcs2_utsname_len);
215 
216 		strncpy(sut.sysname,
217 			IBCS2_UNAME_SYSNAME, sizeof(sut.sysname) - 1);
218 		strncpy(sut.release,
219 			IBCS2_UNAME_RELEASE, sizeof(sut.release) - 1);
220 		strncpy(sut.version,
221 			IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
222 		getcredhostname(td->td_ucred, machine_name,
223 		    sizeof(machine_name) - 1);
224 		p = strchr(machine_name, '.');
225 		if ( p )
226 			*p = '\0';
227 		strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);
228 		strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
229 
230 		DPRINTF(("IBCS2 uname: sys=%s rel=%s ver=%s node=%s mach=%s\n",
231 			 sut.sysname, sut.release, sut.version, sut.nodename,
232 			 sut.machine));
233 		return copyout((caddr_t)&sut, (caddr_t)uap->a1,
234 			       ibcs2_utsname_len);
235 	}
236 
237 	case 2:			/* ustat(2) */
238 	{
239 		return ENOSYS;	/* XXX - TODO */
240 	}
241 
242 	default:
243 		return ENOSYS;
244 	}
245 }
246