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>
37 #include <sys/fcntl.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/namei.h>
42 #include <sys/unistd.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysproto.h>
51 #include <netinet/in.h>
53 #include <compat/svr4/svr4.h>
54 #include <compat/svr4/svr4_types.h>
55 #include <compat/svr4/svr4_signal.h>
56 #include <compat/svr4/svr4_proto.h>
57 #include <compat/svr4/svr4_util.h>
58 #include <compat/svr4/svr4_stat.h>
59 #include <compat/svr4/svr4_ustat.h>
60 #include <compat/svr4/svr4_utsname.h>
61 #include <compat/svr4/svr4_systeminfo.h>
62 #include <compat/svr4/svr4_socket.h>
63 #include <compat/svr4/svr4_time.h>
65 #include "svr4_fuser.h"
70 * Solaris-2.4 on the sparc has the old stat call using the new
71 * stat data structure...
73 # define SVR4_NO_OSTAT
76 struct svr4_ustat_args {
78 struct svr4_ustat * name;
81 static void bsd_to_svr4_xstat(struct stat *, struct svr4_xstat *);
82 static void bsd_to_svr4_stat64(struct stat *, struct svr4_stat64 *);
83 int svr4_ustat(struct thread *, struct svr4_ustat_args *);
84 static int svr4_to_bsd_pathconf(int);
87 * SVR4 uses named pipes as named sockets, so we tell programs
88 * that sockets are named pipes with mode 0
90 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
94 static void bsd_to_svr4_stat(struct stat *, struct svr4_stat *);
97 bsd_to_svr4_stat(st, st4)
99 struct svr4_stat *st4;
101 memset(st4, 0, sizeof(*st4));
102 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
103 st4->st_ino = st->st_ino;
104 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
105 st4->st_nlink = st->st_nlink;
106 st4->st_uid = st->st_uid;
107 st4->st_gid = st->st_gid;
108 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
109 st4->st_size = st->st_size;
110 st4->st_atim = st->st_atim.tv_sec;
111 st4->st_mtim = st->st_mtim.tv_sec;
112 st4->st_ctim = st->st_ctim.tv_sec;
118 bsd_to_svr4_xstat(st, st4)
120 struct svr4_xstat *st4;
122 memset(st4, 0, sizeof(*st4));
123 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
124 st4->st_ino = st->st_ino;
125 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
126 st4->st_nlink = st->st_nlink;
127 st4->st_uid = st->st_uid;
128 st4->st_gid = st->st_gid;
129 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
130 st4->st_size = st->st_size;
131 st4->st_atim = st->st_atim;
132 st4->st_mtim = st->st_mtim;
133 st4->st_ctim = st->st_ctim;
134 st4->st_blksize = st->st_blksize;
135 st4->st_blocks = st->st_blocks;
136 strcpy(st4->st_fstype, "unknown");
141 bsd_to_svr4_stat64(st, st4)
143 struct svr4_stat64 *st4;
145 memset(st4, 0, sizeof(*st4));
146 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
147 st4->st_ino = st->st_ino;
148 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
149 st4->st_nlink = st->st_nlink;
150 st4->st_uid = st->st_uid;
151 st4->st_gid = st->st_gid;
152 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
153 st4->st_size = st->st_size;
154 st4->st_atim = st->st_atim;
155 st4->st_mtim = st->st_mtim;
156 st4->st_ctim = st->st_ctim;
157 st4->st_blksize = st->st_blksize;
158 st4->st_blocks = st->st_blocks;
159 strcpy(st4->st_fstype, "unknown");
163 svr4_sys_stat(td, uap)
165 struct svr4_sys_stat_args *uap;
167 struct svr4_stat svr4_st;
172 CHECKALTEXIST(td, uap->path, &path);
174 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, NULL);
178 bsd_to_svr4_stat(&st, &svr4_st);
180 if (S_ISSOCK(st.st_mode))
181 (void) svr4_add_socket(td, uap->path, &st);
183 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
188 svr4_sys_lstat(td, uap)
190 struct svr4_sys_lstat_args *uap;
192 struct svr4_stat svr4_st;
197 CHECKALTEXIST(td, uap->path, &path);
199 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
200 UIO_SYSSPACE, &st, NULL);
204 bsd_to_svr4_stat(&st, &svr4_st);
206 if (S_ISSOCK(st.st_mode))
207 (void) svr4_add_socket(td, uap->path, &st);
209 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
214 svr4_sys_fstat(td, uap)
216 struct svr4_sys_fstat_args *uap;
218 struct svr4_stat svr4_st;
223 error = kern_fstat(td, uap->fd, &st);
226 bsd_to_svr4_stat(&st, &svr4_st);
227 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
232 svr4_sys_xstat(td, uap)
234 struct svr4_sys_xstat_args *uap;
236 struct svr4_xstat svr4_st;
241 CHECKALTEXIST(td, uap->path, &path);
243 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, NULL);
248 bsd_to_svr4_xstat(&st, &svr4_st);
250 #if defined(SOCKET_NOTYET)
251 if (S_ISSOCK(st.st_mode))
252 (void) svr4_add_socket(td, uap->path, &st);
255 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
259 svr4_sys_lxstat(td, uap)
261 struct svr4_sys_lxstat_args *uap;
263 struct svr4_xstat svr4_st;
268 CHECKALTEXIST(td, uap->path, &path);
270 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
271 UIO_SYSSPACE, &st, NULL);
276 bsd_to_svr4_xstat(&st, &svr4_st);
278 #if defined(SOCKET_NOTYET)
279 if (S_ISSOCK(st.st_mode))
280 (void) svr4_add_socket(td, uap->path, &st);
282 return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
287 svr4_sys_fxstat(td, uap)
289 struct svr4_sys_fxstat_args *uap;
291 struct svr4_xstat svr4_st;
296 error = kern_fstat(td, uap->fd, &st);
299 bsd_to_svr4_xstat(&st, &svr4_st);
300 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
304 svr4_sys_stat64(td, uap)
306 struct svr4_sys_stat64_args *uap;
308 struct svr4_stat64 svr4_st;
313 CHECKALTEXIST(td, uap->path, &path);
315 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, NULL);
320 bsd_to_svr4_stat64(&st, &svr4_st);
322 if (S_ISSOCK(st.st_mode))
323 (void) svr4_add_socket(td, uap->path, &st);
325 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
330 svr4_sys_lstat64(td, uap)
332 struct svr4_sys_lstat64_args *uap;
334 struct svr4_stat64 svr4_st;
339 CHECKALTEXIST(td, uap->path, &path);
341 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
342 UIO_SYSSPACE, &st, NULL);
347 bsd_to_svr4_stat64(&st, &svr4_st);
349 if (S_ISSOCK(st.st_mode))
350 (void) svr4_add_socket(td, uap->path, &st);
352 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
357 svr4_sys_fstat64(td, uap)
359 struct svr4_sys_fstat64_args *uap;
361 struct svr4_stat64 svr4_st;
365 error = kern_fstat(td, uap->fd, &st);
368 bsd_to_svr4_stat64(&st, &svr4_st);
369 return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
376 struct svr4_ustat_args *uap;
378 struct svr4_ustat us;
381 memset(&us, 0, sizeof us);
384 * XXX: should set f_tfree and f_tinode at least
385 * How do we translate dev -> fstat? (and then to svr4_ustat)
387 if ((error = copyout(&us, uap->name, sizeof us)) != 0)
393 /*extern char ostype[], osrelease[], version[], machine[];*/
396 svr4_sys_uname(td, uap)
398 struct svr4_sys_uname_args *uap;
400 struct svr4_utsname sut;
402 memset(&sut, 0, sizeof(sut));
404 strlcpy(sut.sysname, ostype, sizeof(sut.sysname));
405 getcredhostname(td->td_ucred, sut.nodename, sizeof(sut.nodename));
406 strlcpy(sut.release, osrelease, sizeof(sut.release));
407 strlcpy(sut.version, version, sizeof(sut.version));
408 strlcpy(sut.machine, machine, sizeof(sut.machine));
410 return copyout((caddr_t) &sut, (caddr_t) uap->name,
411 sizeof(struct svr4_utsname));
415 svr4_sys_systeminfo(td, uap)
417 struct svr4_sys_systeminfo_args *uap;
421 register_t *retval = td->td_retval;
424 char buf[MAXHOSTNAMELEN];
425 u_int rlen = uap->len;
428 case SVR4_SI_SYSNAME:
432 case SVR4_SI_HOSTNAME:
433 getcredhostname(td->td_ucred, buf, sizeof(buf));
437 case SVR4_SI_RELEASE:
441 case SVR4_SI_VERSION:
445 case SVR4_SI_MACHINE:
449 case SVR4_SI_ARCHITECTURE:
453 case SVR4_SI_ISALIST:
454 #if defined(__sparc__)
455 str = "sparcv9 sparcv9-fsmuld sparcv8 sparcv8-fsmuld sparcv7 sparc";
456 #elif defined(__i386__)
458 #elif defined(__amd64__)
465 case SVR4_SI_HW_SERIAL:
466 getcredhostid(td->td_ucred, &hostid);
467 snprintf(buf, sizeof(buf), "%lu", hostid);
471 case SVR4_SI_HW_PROVIDER:
475 case SVR4_SI_SRPC_DOMAIN:
476 getcreddomainname(td->td_ucred, buf, sizeof(buf));
480 case SVR4_SI_PLATFORM:
481 #if defined(__i386__)
488 case SVR4_SI_KERB_REALM:
491 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
492 case SVR4_SI_SET_HOSTNAME:
493 name = KERN_HOSTNAME;
494 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
496 case SVR4_SI_SET_SRPC_DOMAIN:
497 name = KERN_NISDOMAINNAME;
498 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
500 case SVR4_SI_SET_HOSTNAME:
501 case SVR4_SI_SET_SRPC_DOMAIN:
504 case SVR4_SI_SET_KERB_REALM:
508 DPRINTF(("Bad systeminfo command %d\n", uap->what));
513 len = strlen(str) + 1;
518 error = copyout(str, uap->buf, len);
521 /* make sure we are NULL terminated */
523 error = copyout(buf, &(uap->buf[len - 1]), 1);
528 /* XXX NetBSD has hostname setting stuff here. Why would an emulator
536 svr4_sys_utssys(td, uap)
538 struct svr4_sys_utssys_args *uap;
541 case 0: /* uname(2) */
543 struct svr4_sys_uname_args ua;
545 return svr4_sys_uname(td, &ua);
548 case 2: /* ustat(2) */
550 struct svr4_ustat_args ua;
551 ua.dev = (svr4_dev_t) uap->a2;
553 return svr4_ustat(td, &ua);
556 case 3: /* fusers(2) */
567 svr4_sys_utime(td, uap)
569 struct svr4_sys_utime_args *uap;
571 struct svr4_utimbuf ub;
572 struct timeval tbuf[2], *tp;
576 if (uap->ubuf != NULL) {
577 error = copyin(uap->ubuf, &ub, sizeof(ub));
580 tbuf[0].tv_sec = ub.actime;
582 tbuf[1].tv_sec = ub.modtime;
588 CHECKALTEXIST(td, uap->path, &path);
589 error = kern_utimesat(td, AT_FDCWD, path, UIO_SYSSPACE,
597 svr4_sys_utimes(td, uap)
599 struct svr4_sys_utimes_args *uap;
604 CHECKALTEXIST(td, uap->path, &path);
605 error = kern_utimesat(td, AT_FDCWD, path, UIO_SYSSPACE,
606 uap->tptr, UIO_USERSPACE);
612 svr4_to_bsd_pathconf(name)
616 case SVR4_PC_LINK_MAX:
619 case SVR4_PC_MAX_CANON:
620 return _PC_MAX_CANON;
622 case SVR4_PC_MAX_INPUT:
623 return _PC_MAX_INPUT;
625 case SVR4_PC_NAME_MAX:
628 case SVR4_PC_PATH_MAX:
631 case SVR4_PC_PIPE_BUF:
634 case SVR4_PC_NO_TRUNC:
637 case SVR4_PC_VDISABLE:
640 case SVR4_PC_CHOWN_RESTRICTED:
641 return _PC_CHOWN_RESTRICTED;
642 case SVR4_PC_SYNC_IO:
643 #if defined(_PC_SYNC_IO)
648 case SVR4_PC_ASYNC_IO:
649 case SVR4_PC_PRIO_IO:
661 svr4_sys_pathconf(td, uap)
663 struct svr4_sys_pathconf_args *uap;
668 name = svr4_to_bsd_pathconf(uap->name);
672 td->td_retval[0] = -1;
675 td->td_retval[0] = 0;
678 CHECKALTEXIST(td, uap->path, &path);
679 error = kern_pathconf(td, path, UIO_SYSSPACE, name, FOLLOW);
687 svr4_sys_fpathconf(td, uap)
689 struct svr4_sys_fpathconf_args *uap;
691 register_t *retval = td->td_retval;
693 uap->name = svr4_to_bsd_pathconf(uap->name);
703 return sys_fpathconf(td, (struct fpathconf_args *)uap);