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.
28 * $FreeBSD: src/sys/svr4/svr4_stat.c,v 1.6 1999/12/08 12:00:48 newton Exp $
29 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_stat.c,v 1.5 2003/07/26 18:12:46 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/unistd.h>
40 #include <sys/sysctl.h>
41 #include <sys/sysproto.h>
45 #include <netinet/in.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 {
73 struct svr4_ustat * name;
76 static void bsd_to_svr4_xstat __P((struct stat *, struct svr4_xstat *));
77 static void bsd_to_svr4_stat64 __P((struct stat *, struct svr4_stat64 *));
78 int svr4_ustat __P((struct svr4_ustat_args *));
79 static int svr4_to_bsd_pathconf __P((int));
82 * SVR4 uses named pipes as named sockets, so we tell programs
83 * that sockets are named pipes with mode 0
85 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
89 static void bsd_to_svr4_stat __P((struct stat *, struct svr4_stat *));
92 bsd_to_svr4_stat(st, st4)
94 struct svr4_stat *st4;
96 memset(st4, 0, sizeof(*st4));
97 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
98 st4->st_ino = st->st_ino;
99 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
100 st4->st_nlink = st->st_nlink;
101 st4->st_uid = st->st_uid;
102 st4->st_gid = st->st_gid;
103 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
104 st4->st_size = st->st_size;
105 st4->st_atim = st->st_atimespec.tv_sec;
106 st4->st_mtim = st->st_mtimespec.tv_sec;
107 st4->st_ctim = st->st_ctimespec.tv_sec;
113 bsd_to_svr4_xstat(st, st4)
115 struct svr4_xstat *st4;
117 memset(st4, 0, sizeof(*st4));
118 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
119 st4->st_ino = st->st_ino;
120 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
121 st4->st_nlink = st->st_nlink;
122 st4->st_uid = st->st_uid;
123 st4->st_gid = st->st_gid;
124 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
125 st4->st_size = st->st_size;
126 st4->st_atim = st->st_atimespec;
127 st4->st_mtim = st->st_mtimespec;
128 st4->st_ctim = st->st_ctimespec;
129 st4->st_blksize = st->st_blksize;
130 st4->st_blocks = st->st_blocks;
131 strcpy(st4->st_fstype, "unknown");
136 bsd_to_svr4_stat64(st, st4)
138 struct svr4_stat64 *st4;
140 memset(st4, 0, sizeof(*st4));
141 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
142 st4->st_ino = st->st_ino;
143 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
144 st4->st_nlink = st->st_nlink;
145 st4->st_uid = st->st_uid;
146 st4->st_gid = st->st_gid;
147 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
148 st4->st_size = st->st_size;
149 st4->st_atim = st->st_atimespec;
150 st4->st_mtim = st->st_mtimespec;
151 st4->st_ctim = st->st_ctimespec;
152 st4->st_blksize = st->st_blksize;
153 st4->st_blocks = st->st_blocks;
154 strcpy(st4->st_fstype, "unknown");
158 svr4_sys_stat(struct svr4_sys_stat_args *uap)
160 struct thread *td = curthread;
162 struct svr4_stat svr4_st;
163 struct stat_args cup;
165 caddr_t sg = stackgap_init();
167 CHECKALTEXIST(&sg, SCARG(uap, path));
169 SCARG(&cup, path) = SCARG(uap, path);
170 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
171 cup.lmsg.u.ms_result = 0;
173 if ((error = stat(&cup)) != 0)
176 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
179 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
181 bsd_to_svr4_stat(&st, &svr4_st);
183 if (S_ISSOCK(st.st_mode))
184 (void) svr4_add_socket(td, SCARG(uap, path), &st);
186 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
194 svr4_sys_lstat(struct svr4_sys_lstat_args *uap)
196 struct thread *td = curthread;
198 struct svr4_stat svr4_st;
199 struct lstat_args cup;
201 caddr_t sg = stackgap_init();
203 CHECKALTEXIST(&sg, SCARG(uap, path));
205 SCARG(&cup, path) = SCARG(uap, path);
206 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
207 cup.lmsg.u.ms_result = 0;
209 if ((error = lstat(&cup)) != 0)
212 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
215 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
217 bsd_to_svr4_stat(&st, &svr4_st);
219 if (S_ISSOCK(st.st_mode))
220 (void) svr4_add_socket(td, SCARG(uap, path), &st);
222 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
230 svr4_sys_fstat(struct svr4_sys_fstat_args *uap)
233 struct svr4_stat svr4_st;
234 struct fstat_args cup;
236 caddr_t sg = stackgap_init();
238 SCARG(&cup, fd) = SCARG(uap, fd);
239 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
240 cup.lmsg.u.ms_result = 0;
242 if ((error = fstat(&cup)) != 0)
245 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
248 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
250 bsd_to_svr4_stat(&st, &svr4_st);
252 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
260 svr4_sys_xstat(struct svr4_sys_xstat_args *uap)
263 struct svr4_xstat svr4_st;
264 struct stat_args cup;
267 caddr_t sg = stackgap_init();
268 CHECKALTEXIST(&sg, SCARG(uap, path));
270 SCARG(&cup, path) = SCARG(uap, path);
271 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
272 cup.lmsg.u.ms_result = 0;
274 if ((error = stat(&cup)) != 0)
277 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
280 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
282 bsd_to_svr4_xstat(&st, &svr4_st);
284 #if defined(SOCKET_NOTYET)
285 if (S_ISSOCK(st.st_mode))
286 (void) svr4_add_socket(p, SCARG(uap, path), &st);
289 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
296 svr4_sys_lxstat(struct svr4_sys_lxstat_args *uap)
299 struct svr4_xstat svr4_st;
300 struct lstat_args cup;
302 caddr_t sg = stackgap_init();
303 CHECKALTEXIST(&sg, SCARG(uap, path));
305 SCARG(&cup, path) = SCARG(uap, path);
306 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
307 cup.lmsg.u.ms_result = 0;
309 if ((error = lstat(&cup)) != 0)
312 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
315 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
317 bsd_to_svr4_xstat(&st, &svr4_st);
319 #if defined(SOCKET_NOTYET)
320 if (S_ISSOCK(st.st_mode))
321 (void) svr4_add_socket(p, SCARG(uap, path), &st);
323 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
331 svr4_sys_fxstat(struct svr4_sys_fxstat_args *uap)
334 struct svr4_xstat svr4_st;
335 struct fstat_args cup;
338 caddr_t sg = stackgap_init();
340 SCARG(&cup, fd) = SCARG(uap, fd);
341 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
342 cup.lmsg.u.ms_result = 0;
344 if ((error = fstat(&cup)) != 0)
347 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
350 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
352 bsd_to_svr4_xstat(&st, &svr4_st);
354 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
361 svr4_sys_stat64(struct svr4_sys_stat64_args *uap)
363 struct thread *td = curthread;
365 struct svr4_stat64 svr4_st;
366 struct stat_args cup;
368 caddr_t sg = stackgap_init();
370 CHECKALTEXIST(&sg, SCARG(uap, path));
372 SCARG(&cup, path) = SCARG(uap, path);
373 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
374 cup.lmsg.u.ms_result = 0;
376 if ((error = stat(&cup)) != 0)
379 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
382 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
384 bsd_to_svr4_stat64(&st, &svr4_st);
386 if (S_ISSOCK(st.st_mode))
387 (void) svr4_add_socket(td, SCARG(uap, path), &st);
389 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
397 svr4_sys_lstat64(struct svr4_sys_lstat64_args *uap)
399 struct thread *td = curthread;
401 struct svr4_stat64 svr4_st;
402 struct stat_args cup;
404 caddr_t sg = stackgap_init();
406 CHECKALTEXIST(&sg, (char *) SCARG(uap, path));
408 SCARG(&cup, path) = SCARG(uap, path);
409 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
410 cup.lmsg.u.ms_result = 0;
412 if ((error = lstat((struct lstat_args *)&cup)) != 0)
415 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
418 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
420 bsd_to_svr4_stat64(&st, &svr4_st);
422 if (S_ISSOCK(st.st_mode))
423 (void) svr4_add_socket(td, SCARG(uap, path), &st);
425 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
433 svr4_sys_fstat64(struct svr4_sys_fstat64_args *uap)
436 struct svr4_stat64 svr4_st;
437 struct fstat_args cup;
439 caddr_t sg = stackgap_init();
441 SCARG(&cup, fd) = SCARG(uap, fd);
442 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
443 cup.lmsg.u.ms_result = 0;
445 if ((error = fstat(&cup)) != 0)
448 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
451 uap->lmsg.u.ms_result = cup.lmsg.u.ms_result;
453 bsd_to_svr4_stat64(&st, &svr4_st);
455 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
463 svr4_ustat(struct svr4_ustat_args *uap)
465 struct svr4_ustat us;
468 memset(&us, 0, sizeof us);
471 * XXX: should set f_tfree and f_tinode at least
472 * How do we translate dev -> fstat? (and then to svr4_ustat)
474 if ((error = copyout(&us, SCARG(uap, name), sizeof us)) != 0)
480 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
483 svr4_sys_uname(struct svr4_sys_uname_args *uap)
485 struct svr4_utsname sut;
487 memset(&sut, 0, sizeof(sut));
489 strncpy(sut.sysname, ostype, sizeof(sut.sysname));
490 sut.sysname[sizeof(sut.sysname) - 1] = '\0';
492 strncpy(sut.nodename, hostname, sizeof(sut.nodename));
493 sut.nodename[sizeof(sut.nodename) - 1] = '\0';
495 strncpy(sut.release, osrelease, sizeof(sut.release));
496 sut.release[sizeof(sut.release) - 1] = '\0';
498 strncpy(sut.version, version, sizeof(sut.version));
499 sut.version[sizeof(sut.version) - 1] = '\0';
501 strncpy(sut.machine, machine, sizeof(sut.machine));
502 sut.machine[sizeof(sut.machine) - 1] = '\0';
504 return copyout((caddr_t) &sut, (caddr_t) SCARG(uap, name),
505 sizeof(struct svr4_utsname));
509 svr4_sys_systeminfo(struct svr4_sys_systeminfo_args *uap)
513 register_t *retval = &uap->lmsg.u.ms_result;
515 char buf[1]; /* XXX NetBSD uses 256, but that seems
516 like awfully excessive kstack usage
517 for an empty string... */
518 u_int rlen = SCARG(uap, len);
520 switch (SCARG(uap, what)) {
521 case SVR4_SI_SYSNAME:
525 case SVR4_SI_HOSTNAME:
529 case SVR4_SI_RELEASE:
533 case SVR4_SI_VERSION:
537 case SVR4_SI_MACHINE:
541 case SVR4_SI_ARCHITECTURE:
545 case SVR4_SI_HW_SERIAL:
549 case SVR4_SI_HW_PROVIDER:
553 case SVR4_SI_SRPC_DOMAIN:
557 case SVR4_SI_PLATFORM:
565 case SVR4_SI_KERB_REALM:
568 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
569 case SVR4_SI_SET_HOSTNAME:
570 if ((error = suser(td)) != 0)
572 name = KERN_HOSTNAME;
573 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
575 case SVR4_SI_SET_SRPC_DOMAIN:
576 if ((error = suser(td)) != 0)
578 name = KERN_NISDOMAINNAME;
579 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
581 case SVR4_SI_SET_HOSTNAME:
582 case SVR4_SI_SET_SRPC_DOMAIN:
585 case SVR4_SI_SET_KERB_REALM:
589 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap, what)));
594 len = strlen(str) + 1;
598 if (SCARG(uap, buf)) {
599 error = copyout(str, SCARG(uap, buf), len);
602 /* make sure we are NULL terminated */
604 error = copyout(buf, &(SCARG(uap, buf)[len - 1]), 1);
609 /* XXX NetBSD has hostname setting stuff here. Why would an emulator
617 svr4_sys_utssys(struct svr4_sys_utssys_args *uap)
619 switch (SCARG(uap, sel)) {
620 case 0: /* uname(2) */
622 struct svr4_sys_uname_args ua;
625 SCARG(&ua, name) = SCARG(uap, a1);
626 ua.lmsg.u.ms_result = 0;
627 error = svr4_sys_uname(&ua);
628 uap->lmsg.u.ms_result = ua.lmsg.u.ms_result;
632 case 2: /* ustat(2) */
634 struct svr4_ustat_args ua;
637 SCARG(&ua, dev) = (svr4_dev_t) SCARG(uap, a2);
638 SCARG(&ua, name) = SCARG(uap, a1);
639 ua.lmsg.u.ms_result = 0;
640 error = svr4_ustat(&ua);
641 uap->lmsg.u.ms_result = ua.lmsg.u.ms_result;
645 case 3: /* fusers(2) */
656 svr4_sys_utime(struct svr4_sys_utime_args *uap)
658 struct svr4_utimbuf ub;
659 struct timeval tbuf[2];
660 struct utimes_args ap;
662 caddr_t sg = stackgap_init();
665 CHECKALTEXIST(&sg, SCARG(uap, path));
666 SCARG(&ap, path) = SCARG(uap, path);
667 if (SCARG(uap, ubuf) != NULL) {
668 if ((error = copyin(SCARG(uap, ubuf), &ub, sizeof(ub))) != 0)
670 tbuf[0].tv_sec = ub.actime;
672 tbuf[1].tv_sec = ub.modtime;
674 ttp = stackgap_alloc(&sg, sizeof(tbuf));
675 error = copyout(tbuf, ttp, sizeof(tbuf));
678 SCARG(&ap, tptr) = ttp;
680 SCARG(&ap, tptr) = NULL;
682 ap.lmsg.u.ms_result = 0;
684 uap->lmsg.u.ms_result = ap.lmsg.u.ms_result;
690 svr4_sys_utimes(struct svr4_sys_utimes_args *uap)
692 caddr_t sg = stackgap_init();
693 CHECKALTEXIST(&sg, SCARG(uap, path));
694 return utimes((struct utimes_args *)uap);
698 svr4_to_bsd_pathconf(name)
702 case SVR4_PC_LINK_MAX:
705 case SVR4_PC_MAX_CANON:
706 return _PC_MAX_CANON;
708 case SVR4_PC_MAX_INPUT:
709 return _PC_MAX_INPUT;
711 case SVR4_PC_NAME_MAX:
714 case SVR4_PC_PATH_MAX:
717 case SVR4_PC_PIPE_BUF:
720 case SVR4_PC_NO_TRUNC:
723 case SVR4_PC_VDISABLE:
726 case SVR4_PC_CHOWN_RESTRICTED:
727 return _PC_CHOWN_RESTRICTED;
728 case SVR4_PC_SYNC_IO:
729 #if defined(_PC_SYNC_IO)
734 case SVR4_PC_ASYNC_IO:
735 case SVR4_PC_PRIO_IO:
747 svr4_sys_pathconf(struct svr4_sys_pathconf_args *uap)
749 caddr_t sg = stackgap_init();
750 register_t *retval = &uap->lmsg.u.ms_result;
752 CHECKALTEXIST(&sg, SCARG(uap, path));
754 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
756 switch (SCARG(uap, name)) {
764 return pathconf((struct pathconf_args *)uap);
770 svr4_sys_fpathconf(struct svr4_sys_fpathconf_args *uap)
772 register_t *retval = &uap->lmsg.u.ms_result;
774 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
776 switch (SCARG(uap, name)) {
784 return fpathconf((struct fpathconf_args *)uap);