2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/filedesc.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/unistd.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysctl.h>
44 #include <sys/sysproto.h>
46 #include <sys/vimage.h>
50 #include <netinet/in.h>
52 #include <compat/svr4/svr4.h>
53 #include <compat/svr4/svr4_types.h>
54 #include <compat/svr4/svr4_signal.h>
55 #include <compat/svr4/svr4_proto.h>
56 #include <compat/svr4/svr4_util.h>
57 #include <compat/svr4/svr4_stat.h>
58 #include <compat/svr4/svr4_ustat.h>
59 #include <compat/svr4/svr4_utsname.h>
60 #include <compat/svr4/svr4_systeminfo.h>
61 #include <compat/svr4/svr4_socket.h>
62 #include <compat/svr4/svr4_time.h>
64 #include "svr4_fuser.h"
69 * Solaris-2.4 on the sparc has the old stat call using the new
70 * stat data structure...
72 # define SVR4_NO_OSTAT
75 struct svr4_ustat_args {
77 struct svr4_ustat * name;
80 static void bsd_to_svr4_xstat(struct stat *, struct svr4_xstat *);
81 static void bsd_to_svr4_stat64(struct stat *, struct svr4_stat64 *);
82 int svr4_ustat(struct thread *, struct svr4_ustat_args *);
83 static int svr4_to_bsd_pathconf(int);
86 * SVR4 uses named pipes as named sockets, so we tell programs
87 * that sockets are named pipes with mode 0
89 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
93 static void bsd_to_svr4_stat(struct stat *, struct svr4_stat *);
96 bsd_to_svr4_stat(st, st4)
98 struct svr4_stat *st4;
100 memset(st4, 0, sizeof(*st4));
101 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
102 st4->st_ino = st->st_ino;
103 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
104 st4->st_nlink = st->st_nlink;
105 st4->st_uid = st->st_uid;
106 st4->st_gid = st->st_gid;
107 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
108 st4->st_size = st->st_size;
109 st4->st_atim = st->st_atimespec.tv_sec;
110 st4->st_mtim = st->st_mtimespec.tv_sec;
111 st4->st_ctim = st->st_ctimespec.tv_sec;
117 bsd_to_svr4_xstat(st, st4)
119 struct svr4_xstat *st4;
121 memset(st4, 0, sizeof(*st4));
122 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
123 st4->st_ino = st->st_ino;
124 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
125 st4->st_nlink = st->st_nlink;
126 st4->st_uid = st->st_uid;
127 st4->st_gid = st->st_gid;
128 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
129 st4->st_size = st->st_size;
130 st4->st_atim = st->st_atimespec;
131 st4->st_mtim = st->st_mtimespec;
132 st4->st_ctim = st->st_ctimespec;
133 st4->st_blksize = st->st_blksize;
134 st4->st_blocks = st->st_blocks;
135 strcpy(st4->st_fstype, "unknown");
140 bsd_to_svr4_stat64(st, st4)
142 struct svr4_stat64 *st4;
144 memset(st4, 0, sizeof(*st4));
145 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
146 st4->st_ino = st->st_ino;
147 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
148 st4->st_nlink = st->st_nlink;
149 st4->st_uid = st->st_uid;
150 st4->st_gid = st->st_gid;
151 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
152 st4->st_size = st->st_size;
153 st4->st_atim = st->st_atimespec;
154 st4->st_mtim = st->st_mtimespec;
155 st4->st_ctim = st->st_ctimespec;
156 st4->st_blksize = st->st_blksize;
157 st4->st_blocks = st->st_blocks;
158 strcpy(st4->st_fstype, "unknown");
162 svr4_sys_stat(td, uap)
164 struct svr4_sys_stat_args *uap;
166 struct svr4_stat svr4_st;
171 CHECKALTEXIST(td, uap->path, &path);
173 error = kern_stat(td, path, UIO_SYSSPACE, &st);
177 bsd_to_svr4_stat(&st, &svr4_st);
179 if (S_ISSOCK(st.st_mode))
180 (void) svr4_add_socket(td, uap->path, &st);
182 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
187 svr4_sys_lstat(td, uap)
188 register struct thread *td;
189 struct svr4_sys_lstat_args *uap;
191 struct svr4_stat svr4_st;
196 CHECKALTEXIST(td, uap->path, &path);
198 error = kern_lstat(td, path, UIO_SYSSPACE, &st);
202 bsd_to_svr4_stat(&st, &svr4_st);
204 if (S_ISSOCK(st.st_mode))
205 (void) svr4_add_socket(td, uap->path, &st);
207 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
212 svr4_sys_fstat(td, uap)
213 register struct thread *td;
214 struct svr4_sys_fstat_args *uap;
216 struct svr4_stat svr4_st;
221 error = kern_fstat(td, uap->fd, &st);
224 bsd_to_svr4_stat(&st, &svr4_st);
225 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
230 svr4_sys_xstat(td, uap)
231 register struct thread *td;
232 struct svr4_sys_xstat_args *uap;
234 struct svr4_xstat svr4_st;
239 CHECKALTEXIST(td, uap->path, &path);
241 error = kern_stat(td, path, UIO_SYSSPACE, &st);
246 bsd_to_svr4_xstat(&st, &svr4_st);
248 #if defined(SOCKET_NOTYET)
249 if (S_ISSOCK(st.st_mode))
250 (void) svr4_add_socket(td, uap->path, &st);
253 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
257 svr4_sys_lxstat(td, uap)
258 register struct thread *td;
259 struct svr4_sys_lxstat_args *uap;
261 struct svr4_xstat svr4_st;
266 CHECKALTEXIST(td, uap->path, &path);
268 error = kern_lstat(td, path, UIO_SYSSPACE, &st);
273 bsd_to_svr4_xstat(&st, &svr4_st);
275 #if defined(SOCKET_NOTYET)
276 if (S_ISSOCK(st.st_mode))
277 (void) svr4_add_socket(td, uap->path, &st);
279 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
284 svr4_sys_fxstat(td, uap)
285 register struct thread *td;
286 struct svr4_sys_fxstat_args *uap;
288 struct svr4_xstat svr4_st;
293 error = kern_fstat(td, uap->fd, &st);
296 bsd_to_svr4_xstat(&st, &svr4_st);
297 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
301 svr4_sys_stat64(td, uap)
302 register struct thread *td;
303 struct svr4_sys_stat64_args *uap;
305 struct svr4_stat64 svr4_st;
310 CHECKALTEXIST(td, uap->path, &path);
312 error = kern_stat(td, path, UIO_SYSSPACE, &st);
317 bsd_to_svr4_stat64(&st, &svr4_st);
319 if (S_ISSOCK(st.st_mode))
320 (void) svr4_add_socket(td, uap->path, &st);
322 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
327 svr4_sys_lstat64(td, uap)
328 register struct thread *td;
329 struct svr4_sys_lstat64_args *uap;
331 struct svr4_stat64 svr4_st;
336 CHECKALTEXIST(td, uap->path, &path);
338 error = kern_lstat(td, path, UIO_SYSSPACE, &st);
343 bsd_to_svr4_stat64(&st, &svr4_st);
345 if (S_ISSOCK(st.st_mode))
346 (void) svr4_add_socket(td, uap->path, &st);
348 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
353 svr4_sys_fstat64(td, uap)
354 register struct thread *td;
355 struct svr4_sys_fstat64_args *uap;
357 struct svr4_stat64 svr4_st;
361 error = kern_fstat(td, uap->fd, &st);
364 bsd_to_svr4_stat64(&st, &svr4_st);
365 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
371 register struct thread *td;
372 struct svr4_ustat_args *uap;
374 struct svr4_ustat us;
377 memset(&us, 0, sizeof us);
380 * XXX: should set f_tfree and f_tinode at least
381 * How do we translate dev -> fstat? (and then to svr4_ustat)
383 if ((error = copyout(&us, uap->name, sizeof us)) != 0)
389 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
392 svr4_sys_uname(td, uap)
393 register struct thread *td;
394 struct svr4_sys_uname_args *uap;
396 struct svr4_utsname sut;
398 memset(&sut, 0, sizeof(sut));
400 strlcpy(sut.sysname, ostype, sizeof(sut.sysname));
401 getcredhostname(td->td_ucred, sut.nodename, sizeof(sut.nodename));
402 strlcpy(sut.release, osrelease, sizeof(sut.release));
403 strlcpy(sut.version, version, sizeof(sut.version));
404 strlcpy(sut.machine, machine, sizeof(sut.machine));
406 return copyout((caddr_t) &sut, (caddr_t) uap->name,
407 sizeof(struct svr4_utsname));
411 svr4_sys_systeminfo(td, uap)
413 struct svr4_sys_systeminfo_args *uap;
417 register_t *retval = td->td_retval;
419 char buf[1]; /* XXX NetBSD uses 256, but that seems
420 like awfully excessive kstack usage
421 for an empty string... */
422 u_int rlen = uap->len;
425 case SVR4_SI_SYSNAME:
429 case SVR4_SI_HOSTNAME:
433 case SVR4_SI_RELEASE:
437 case SVR4_SI_VERSION:
441 case SVR4_SI_MACHINE:
445 case SVR4_SI_ARCHITECTURE:
449 case SVR4_SI_HW_SERIAL:
453 case SVR4_SI_HW_PROVIDER:
457 case SVR4_SI_SRPC_DOMAIN:
458 /* XXXRW: locking? */
462 case SVR4_SI_PLATFORM:
470 case SVR4_SI_KERB_REALM:
473 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
474 case SVR4_SI_SET_HOSTNAME:
475 name = KERN_HOSTNAME;
476 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
478 case SVR4_SI_SET_SRPC_DOMAIN:
479 name = KERN_NISDOMAINNAME;
480 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
482 case SVR4_SI_SET_HOSTNAME:
483 case SVR4_SI_SET_SRPC_DOMAIN:
486 case SVR4_SI_SET_KERB_REALM:
490 DPRINTF(("Bad systeminfo command %d\n", uap->what));
495 len = strlen(str) + 1;
500 error = copyout(str, uap->buf, len);
503 /* make sure we are NULL terminated */
505 error = copyout(buf, &(uap->buf[len - 1]), 1);
510 /* XXX NetBSD has hostname setting stuff here. Why would an emulator
518 svr4_sys_utssys(td, uap)
519 register struct thread *td;
520 struct svr4_sys_utssys_args *uap;
523 case 0: /* uname(2) */
525 struct svr4_sys_uname_args ua;
527 return svr4_sys_uname(td, &ua);
530 case 2: /* ustat(2) */
532 struct svr4_ustat_args ua;
533 ua.dev = (svr4_dev_t) uap->a2;
535 return svr4_ustat(td, &ua);
538 case 3: /* fusers(2) */
549 svr4_sys_utime(td, uap)
550 register struct thread *td;
551 struct svr4_sys_utime_args *uap;
553 struct svr4_utimbuf ub;
554 struct timeval tbuf[2], *tp;
558 if (uap->ubuf != NULL) {
559 error = copyin(uap->ubuf, &ub, sizeof(ub));
562 tbuf[0].tv_sec = ub.actime;
564 tbuf[1].tv_sec = ub.modtime;
570 CHECKALTEXIST(td, uap->path, &path);
571 error = kern_utimes(td, path, UIO_SYSSPACE, tp, UIO_SYSSPACE);
578 svr4_sys_utimes(td, uap)
579 register struct thread *td;
580 struct svr4_sys_utimes_args *uap;
585 CHECKALTEXIST(td, uap->path, &path);
586 error = kern_utimes(td, path, UIO_SYSSPACE, uap->tptr, UIO_USERSPACE);
592 svr4_to_bsd_pathconf(name)
596 case SVR4_PC_LINK_MAX:
599 case SVR4_PC_MAX_CANON:
600 return _PC_MAX_CANON;
602 case SVR4_PC_MAX_INPUT:
603 return _PC_MAX_INPUT;
605 case SVR4_PC_NAME_MAX:
608 case SVR4_PC_PATH_MAX:
611 case SVR4_PC_PIPE_BUF:
614 case SVR4_PC_NO_TRUNC:
617 case SVR4_PC_VDISABLE:
620 case SVR4_PC_CHOWN_RESTRICTED:
621 return _PC_CHOWN_RESTRICTED;
622 case SVR4_PC_SYNC_IO:
623 #if defined(_PC_SYNC_IO)
628 case SVR4_PC_ASYNC_IO:
629 case SVR4_PC_PRIO_IO:
641 svr4_sys_pathconf(td, uap)
642 register struct thread *td;
643 struct svr4_sys_pathconf_args *uap;
648 name = svr4_to_bsd_pathconf(uap->name);
652 td->td_retval[0] = -1;
655 td->td_retval[0] = 0;
658 CHECKALTEXIST(td, uap->path, &path);
659 error = kern_pathconf(td, path, UIO_SYSSPACE, name);
667 svr4_sys_fpathconf(td, uap)
668 register struct thread *td;
669 struct svr4_sys_fpathconf_args *uap;
671 register_t *retval = td->td_retval;
673 uap->name = svr4_to_bsd_pathconf(uap->name);
683 return fpathconf(td, (struct fpathconf_args *)uap);