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.
31 #include <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/unistd.h>
42 #include <sys/sysctl.h>
43 #include <netinet/in.h>
45 #include <sys/sysproto.h>
47 #include <svr4/svr4.h>
48 #include <svr4/svr4_types.h>
49 #include <svr4/svr4_signal.h>
50 #include <svr4/svr4_proto.h>
51 #include <svr4/svr4_util.h>
52 #include <svr4/svr4_stat.h>
53 #include <svr4/svr4_ustat.h>
54 #include <svr4/svr4_utsname.h>
55 #include <svr4/svr4_systeminfo.h>
56 #include <svr4/svr4_socket.h>
57 #include <svr4/svr4_time.h>
59 #include "svr4_fuser.h"
64 * Solaris-2.4 on the sparc has the old stat call using the new
65 * stat data structure...
67 # define SVR4_NO_OSTAT
70 struct svr4_ustat_args {
72 struct svr4_ustat * name;
75 static void bsd_to_svr4_xstat __P((struct stat *, struct svr4_xstat *));
76 static void bsd_to_svr4_stat64 __P((struct stat *, struct svr4_stat64 *));
77 int svr4_ustat __P((struct proc *, struct svr4_ustat_args *));
78 static int svr4_to_bsd_pathconf __P((int));
81 * SVR4 uses named pipes as named sockets, so we tell programs
82 * that sockets are named pipes with mode 0
84 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
88 static void bsd_to_svr4_stat __P((struct stat *, struct svr4_stat *));
91 bsd_to_svr4_stat(st, st4)
93 struct svr4_stat *st4;
95 memset(st4, 0, sizeof(*st4));
96 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
97 st4->st_ino = st->st_ino;
98 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
99 st4->st_nlink = st->st_nlink;
100 st4->st_uid = st->st_uid;
101 st4->st_gid = st->st_gid;
102 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
103 st4->st_size = st->st_size;
104 st4->st_atim = st->st_atimespec.tv_sec;
105 st4->st_mtim = st->st_mtimespec.tv_sec;
106 st4->st_ctim = st->st_ctimespec.tv_sec;
112 bsd_to_svr4_xstat(st, st4)
114 struct svr4_xstat *st4;
116 memset(st4, 0, sizeof(*st4));
117 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
118 st4->st_ino = st->st_ino;
119 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
120 st4->st_nlink = st->st_nlink;
121 st4->st_uid = st->st_uid;
122 st4->st_gid = st->st_gid;
123 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
124 st4->st_size = st->st_size;
125 st4->st_atim = st->st_atimespec;
126 st4->st_mtim = st->st_mtimespec;
127 st4->st_ctim = st->st_ctimespec;
128 st4->st_blksize = st->st_blksize;
129 st4->st_blocks = st->st_blocks;
130 strcpy(st4->st_fstype, "unknown");
135 bsd_to_svr4_stat64(st, st4)
137 struct svr4_stat64 *st4;
139 memset(st4, 0, sizeof(*st4));
140 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
141 st4->st_ino = st->st_ino;
142 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
143 st4->st_nlink = st->st_nlink;
144 st4->st_uid = st->st_uid;
145 st4->st_gid = st->st_gid;
146 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
147 st4->st_size = st->st_size;
148 st4->st_atim = st->st_atimespec;
149 st4->st_mtim = st->st_mtimespec;
150 st4->st_ctim = st->st_ctimespec;
151 st4->st_blksize = st->st_blksize;
152 st4->st_blocks = st->st_blocks;
153 strcpy(st4->st_fstype, "unknown");
157 svr4_sys_stat(p, uap)
159 struct svr4_sys_stat_args *uap;
162 struct svr4_stat svr4_st;
163 struct stat_args cup;
165 caddr_t sg = stackgap_init();
167 CHECKALTEXIST(p, &sg, SCARG(uap, path));
169 SCARG(&cup, path) = SCARG(uap, path);
170 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
173 if ((error = stat(p, &cup)) != 0)
176 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
179 bsd_to_svr4_stat(&st, &svr4_st);
181 if (S_ISSOCK(st.st_mode))
182 (void) svr4_add_socket(p, SCARG(uap, path), &st);
184 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
192 svr4_sys_lstat(p, uap)
193 register struct proc *p;
194 struct svr4_sys_lstat_args *uap;
197 struct svr4_stat svr4_st;
198 struct lstat_args cup;
200 caddr_t sg = stackgap_init();
202 CHECKALTEXIST(p, &sg, SCARG(uap, path));
204 SCARG(&cup, path) = SCARG(uap, path);
205 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
207 if ((error = lstat(p, &cup)) != 0)
210 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
213 bsd_to_svr4_stat(&st, &svr4_st);
215 if (S_ISSOCK(st.st_mode))
216 (void) svr4_add_socket(p, SCARG(uap, path), &st);
218 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
226 svr4_sys_fstat(p, uap)
227 register struct proc *p;
228 struct svr4_sys_fstat_args *uap;
231 struct svr4_stat svr4_st;
232 struct fstat_args cup;
234 caddr_t sg = stackgap_init();
236 SCARG(&cup, fd) = SCARG(uap, fd);
237 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
239 if ((error = fstat(p, &cup)) != 0)
242 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
245 bsd_to_svr4_stat(&st, &svr4_st);
247 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
255 svr4_sys_xstat(p, uap)
256 register struct proc *p;
257 struct svr4_sys_xstat_args *uap;
260 struct svr4_xstat svr4_st;
261 struct stat_args cup;
264 caddr_t sg = stackgap_init();
265 CHECKALTEXIST(p, &sg, SCARG(uap, path));
267 SCARG(&cup, path) = SCARG(uap, path);
268 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
270 if ((error = stat(p, &cup)) != 0)
273 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
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(p, SCARG(uap, path), &st);
283 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
290 svr4_sys_lxstat(p, uap)
291 register struct proc *p;
292 struct svr4_sys_lxstat_args *uap;
295 struct svr4_xstat svr4_st;
296 struct lstat_args cup;
298 caddr_t sg = stackgap_init();
299 CHECKALTEXIST(p, &sg, SCARG(uap, path));
301 SCARG(&cup, path) = SCARG(uap, path);
302 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
304 if ((error = lstat(p, &cup)) != 0)
307 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
310 bsd_to_svr4_xstat(&st, &svr4_st);
312 #if defined(SOCKET_NOTYET)
313 if (S_ISSOCK(st.st_mode))
314 (void) svr4_add_socket(p, SCARG(uap, path), &st);
316 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
324 svr4_sys_fxstat(p, uap)
325 register struct proc *p;
326 struct svr4_sys_fxstat_args *uap;
329 struct svr4_xstat svr4_st;
330 struct fstat_args cup;
333 caddr_t sg = stackgap_init();
335 SCARG(&cup, fd) = SCARG(uap, fd);
336 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
338 if ((error = fstat(p, &cup)) != 0)
341 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
344 bsd_to_svr4_xstat(&st, &svr4_st);
346 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
353 svr4_sys_stat64(p, uap)
354 register struct proc *p;
355 struct svr4_sys_stat64_args *uap;
358 struct svr4_stat64 svr4_st;
359 struct stat_args cup;
361 caddr_t sg = stackgap_init();
363 CHECKALTEXIST(p, &sg, SCARG(uap, path));
365 SCARG(&cup, path) = SCARG(uap, path);
366 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
368 if ((error = stat(p, &cup)) != 0)
371 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
374 bsd_to_svr4_stat64(&st, &svr4_st);
376 if (S_ISSOCK(st.st_mode))
377 (void) svr4_add_socket(p, SCARG(uap, path), &st);
379 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
387 svr4_sys_lstat64(p, uap)
388 register struct proc *p;
389 struct svr4_sys_lstat64_args *uap;
392 struct svr4_stat64 svr4_st;
393 struct stat_args cup;
395 caddr_t sg = stackgap_init();
397 CHECKALTEXIST(p, &sg, (char *) SCARG(uap, path));
399 SCARG(&cup, path) = SCARG(uap, path);
400 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
402 if ((error = lstat(p, (struct lstat_args *)&cup)) != 0)
405 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
408 bsd_to_svr4_stat64(&st, &svr4_st);
410 if (S_ISSOCK(st.st_mode))
411 (void) svr4_add_socket(p, SCARG(uap, path), &st);
413 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
421 svr4_sys_fstat64(p, uap)
422 register struct proc *p;
423 struct svr4_sys_fstat64_args *uap;
426 struct svr4_stat64 svr4_st;
427 struct fstat_args cup;
429 caddr_t sg = stackgap_init();
431 SCARG(&cup, fd) = SCARG(uap, fd);
432 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
434 if ((error = fstat(p, &cup)) != 0)
437 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
440 bsd_to_svr4_stat64(&st, &svr4_st);
442 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
451 register struct proc *p;
452 struct svr4_ustat_args *uap;
454 struct svr4_ustat us;
457 memset(&us, 0, sizeof us);
460 * XXX: should set f_tfree and f_tinode at least
461 * How do we translate dev -> fstat? (and then to svr4_ustat)
463 if ((error = copyout(&us, SCARG(uap, name), sizeof us)) != 0)
469 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
472 svr4_sys_uname(p, uap)
473 register struct proc *p;
474 struct svr4_sys_uname_args *uap;
476 struct svr4_utsname sut;
479 memset(&sut, 0, sizeof(sut));
481 strncpy(sut.sysname, ostype, sizeof(sut.sysname));
482 sut.sysname[sizeof(sut.sysname) - 1] = '\0';
484 strncpy(sut.nodename, hostname, sizeof(sut.nodename));
485 sut.nodename[sizeof(sut.nodename) - 1] = '\0';
487 strncpy(sut.release, osrelease, sizeof(sut.release));
488 sut.release[sizeof(sut.release) - 1] = '\0';
490 strncpy(sut.version, version, sizeof(sut.version));
491 sut.version[sizeof(sut.version) - 1] = '\0';
493 strncpy(sut.machine, machine, sizeof(sut.machine));
494 sut.machine[sizeof(sut.machine) - 1] = '\0';
496 return copyout((caddr_t) &sut, (caddr_t) SCARG(uap, name),
497 sizeof(struct svr4_utsname));
501 svr4_sys_systeminfo(p, uap)
503 struct svr4_sys_systeminfo_args *uap;
507 register_t *retval = p->p_retval;
509 char buf[1]; /* XXX NetBSD uses 256, but that seems
510 like awfully excessive kstack usage
511 for an empty string... */
512 u_int rlen = SCARG(uap, len);
514 switch (SCARG(uap, what)) {
515 case SVR4_SI_SYSNAME:
519 case SVR4_SI_HOSTNAME:
523 case SVR4_SI_RELEASE:
527 case SVR4_SI_VERSION:
531 case SVR4_SI_MACHINE:
535 case SVR4_SI_ARCHITECTURE:
539 case SVR4_SI_HW_SERIAL:
543 case SVR4_SI_HW_PROVIDER:
547 case SVR4_SI_SRPC_DOMAIN:
551 case SVR4_SI_PLATFORM:
559 case SVR4_SI_KERB_REALM:
562 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
563 case SVR4_SI_SET_HOSTNAME:
564 if ((error = suser(p)) != 0)
566 name = KERN_HOSTNAME;
567 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
569 case SVR4_SI_SET_SRPC_DOMAIN:
570 if ((error = suser(p)) != 0)
572 name = KERN_NISDOMAINNAME;
573 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
575 case SVR4_SI_SET_HOSTNAME:
576 case SVR4_SI_SET_SRPC_DOMAIN:
579 case SVR4_SI_SET_KERB_REALM:
583 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap, what)));
588 len = strlen(str) + 1;
592 if (SCARG(uap, buf)) {
593 error = copyout(str, SCARG(uap, buf), len);
596 /* make sure we are NULL terminated */
598 error = copyout(buf, &(SCARG(uap, buf)[len - 1]), 1);
603 /* XXX NetBSD has hostname setting stuff here. Why would an emulator
611 svr4_sys_utssys(p, uap)
612 register struct proc *p;
613 struct svr4_sys_utssys_args *uap;
615 switch (SCARG(uap, sel)) {
616 case 0: /* uname(2) */
618 struct svr4_sys_uname_args ua;
619 SCARG(&ua, name) = SCARG(uap, a1);
620 return svr4_sys_uname(p, &ua);
623 case 2: /* ustat(2) */
625 struct svr4_ustat_args ua;
626 SCARG(&ua, dev) = (svr4_dev_t) SCARG(uap, a2);
627 SCARG(&ua, name) = SCARG(uap, a1);
628 return svr4_ustat(p, &ua);
631 case 3: /* fusers(2) */
642 svr4_sys_utime(p, uap)
643 register struct proc *p;
644 struct svr4_sys_utime_args *uap;
646 struct svr4_utimbuf ub;
647 struct timeval tbuf[2];
648 struct utimes_args ap;
650 caddr_t sg = stackgap_init();
653 CHECKALTEXIST(p, &sg, SCARG(uap, path));
654 SCARG(&ap, path) = SCARG(uap, path);
655 if (SCARG(uap, ubuf) != NULL) {
656 if ((error = copyin(SCARG(uap, ubuf), &ub, sizeof(ub))) != 0)
658 tbuf[0].tv_sec = ub.actime;
660 tbuf[1].tv_sec = ub.modtime;
662 ttp = stackgap_alloc(&sg, sizeof(tbuf));
663 error = copyout(tbuf, ttp, sizeof(tbuf));
666 SCARG(&ap, tptr) = ttp;
669 SCARG(&ap, tptr) = NULL;
670 return utimes(p, &ap);
675 svr4_sys_utimes(p, uap)
676 register struct proc *p;
677 struct svr4_sys_utimes_args *uap;
679 caddr_t sg = stackgap_init();
680 CHECKALTEXIST(p, &sg, SCARG(uap, path));
681 return utimes(p, (struct utimes_args *)uap);
685 svr4_to_bsd_pathconf(name)
689 case SVR4_PC_LINK_MAX:
692 case SVR4_PC_MAX_CANON:
693 return _PC_MAX_CANON;
695 case SVR4_PC_MAX_INPUT:
696 return _PC_MAX_INPUT;
698 case SVR4_PC_NAME_MAX:
701 case SVR4_PC_PATH_MAX:
704 case SVR4_PC_PIPE_BUF:
707 case SVR4_PC_NO_TRUNC:
710 case SVR4_PC_VDISABLE:
713 case SVR4_PC_CHOWN_RESTRICTED:
714 return _PC_CHOWN_RESTRICTED;
715 case SVR4_PC_SYNC_IO:
716 #if defined(_PC_SYNC_IO)
721 case SVR4_PC_ASYNC_IO:
722 case SVR4_PC_PRIO_IO:
734 svr4_sys_pathconf(p, uap)
735 register struct proc *p;
736 struct svr4_sys_pathconf_args *uap;
738 caddr_t sg = stackgap_init();
739 register_t *retval = p->p_retval;
741 CHECKALTEXIST(p, &sg, SCARG(uap, path));
743 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
745 switch (SCARG(uap, name)) {
753 return pathconf(p, (struct pathconf_args *)uap);
759 svr4_sys_fpathconf(p, uap)
760 register struct proc *p;
761 struct svr4_sys_fpathconf_args *uap;
763 register_t *retval = p->p_retval;
765 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
767 switch (SCARG(uap, name)) {
775 return fpathconf(p, (struct fpathconf_args *)uap);