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 $
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/unistd.h>
39 #include <sys/sysctl.h>
40 #include <sys/sysproto.h>
44 #include <netinet/in.h>
46 #include <svr4/svr4.h>
47 #include <svr4/svr4_types.h>
48 #include <svr4/svr4_signal.h>
49 #include <svr4/svr4_proto.h>
50 #include <svr4/svr4_util.h>
51 #include <svr4/svr4_stat.h>
52 #include <svr4/svr4_ustat.h>
53 #include <svr4/svr4_utsname.h>
54 #include <svr4/svr4_systeminfo.h>
55 #include <svr4/svr4_socket.h>
56 #include <svr4/svr4_time.h>
58 #include "svr4_fuser.h"
63 * Solaris-2.4 on the sparc has the old stat call using the new
64 * stat data structure...
66 # define SVR4_NO_OSTAT
69 struct svr4_ustat_args {
71 struct svr4_ustat * name;
74 static void bsd_to_svr4_xstat __P((struct stat *, struct svr4_xstat *));
75 static void bsd_to_svr4_stat64 __P((struct stat *, struct svr4_stat64 *));
76 int svr4_ustat __P((struct proc *, struct svr4_ustat_args *));
77 static int svr4_to_bsd_pathconf __P((int));
80 * SVR4 uses named pipes as named sockets, so we tell programs
81 * that sockets are named pipes with mode 0
83 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
87 static void bsd_to_svr4_stat __P((struct stat *, struct svr4_stat *));
90 bsd_to_svr4_stat(st, st4)
92 struct svr4_stat *st4;
94 memset(st4, 0, sizeof(*st4));
95 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
96 st4->st_ino = st->st_ino;
97 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
98 st4->st_nlink = st->st_nlink;
99 st4->st_uid = st->st_uid;
100 st4->st_gid = st->st_gid;
101 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
102 st4->st_size = st->st_size;
103 st4->st_atim = st->st_atimespec.tv_sec;
104 st4->st_mtim = st->st_mtimespec.tv_sec;
105 st4->st_ctim = st->st_ctimespec.tv_sec;
111 bsd_to_svr4_xstat(st, st4)
113 struct svr4_xstat *st4;
115 memset(st4, 0, sizeof(*st4));
116 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
117 st4->st_ino = st->st_ino;
118 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
119 st4->st_nlink = st->st_nlink;
120 st4->st_uid = st->st_uid;
121 st4->st_gid = st->st_gid;
122 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
123 st4->st_size = st->st_size;
124 st4->st_atim = st->st_atimespec;
125 st4->st_mtim = st->st_mtimespec;
126 st4->st_ctim = st->st_ctimespec;
127 st4->st_blksize = st->st_blksize;
128 st4->st_blocks = st->st_blocks;
129 strcpy(st4->st_fstype, "unknown");
134 bsd_to_svr4_stat64(st, st4)
136 struct svr4_stat64 *st4;
138 memset(st4, 0, sizeof(*st4));
139 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
140 st4->st_ino = st->st_ino;
141 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
142 st4->st_nlink = st->st_nlink;
143 st4->st_uid = st->st_uid;
144 st4->st_gid = st->st_gid;
145 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
146 st4->st_size = st->st_size;
147 st4->st_atim = st->st_atimespec;
148 st4->st_mtim = st->st_mtimespec;
149 st4->st_ctim = st->st_ctimespec;
150 st4->st_blksize = st->st_blksize;
151 st4->st_blocks = st->st_blocks;
152 strcpy(st4->st_fstype, "unknown");
156 svr4_sys_stat(p, uap)
158 struct svr4_sys_stat_args *uap;
161 struct svr4_stat svr4_st;
162 struct stat_args cup;
164 caddr_t sg = stackgap_init();
166 CHECKALTEXIST(p, &sg, SCARG(uap, path));
168 SCARG(&cup, path) = SCARG(uap, path);
169 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
172 if ((error = stat(p, &cup)) != 0)
175 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
178 bsd_to_svr4_stat(&st, &svr4_st);
180 if (S_ISSOCK(st.st_mode))
181 (void) svr4_add_socket(p, SCARG(uap, path), &st);
183 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
191 svr4_sys_lstat(p, uap)
192 register struct proc *p;
193 struct svr4_sys_lstat_args *uap;
196 struct svr4_stat svr4_st;
197 struct lstat_args cup;
199 caddr_t sg = stackgap_init();
201 CHECKALTEXIST(p, &sg, SCARG(uap, path));
203 SCARG(&cup, path) = SCARG(uap, path);
204 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
206 if ((error = lstat(p, &cup)) != 0)
209 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
212 bsd_to_svr4_stat(&st, &svr4_st);
214 if (S_ISSOCK(st.st_mode))
215 (void) svr4_add_socket(p, SCARG(uap, path), &st);
217 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
225 svr4_sys_fstat(p, uap)
226 register struct proc *p;
227 struct svr4_sys_fstat_args *uap;
230 struct svr4_stat svr4_st;
231 struct fstat_args cup;
233 caddr_t sg = stackgap_init();
235 SCARG(&cup, fd) = SCARG(uap, fd);
236 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
238 if ((error = fstat(p, &cup)) != 0)
241 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
244 bsd_to_svr4_stat(&st, &svr4_st);
246 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
254 svr4_sys_xstat(p, uap)
255 register struct proc *p;
256 struct svr4_sys_xstat_args *uap;
259 struct svr4_xstat svr4_st;
260 struct stat_args cup;
263 caddr_t sg = stackgap_init();
264 CHECKALTEXIST(p, &sg, SCARG(uap, path));
266 SCARG(&cup, path) = SCARG(uap, path);
267 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
269 if ((error = stat(p, &cup)) != 0)
272 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
275 bsd_to_svr4_xstat(&st, &svr4_st);
277 #if defined(SOCKET_NOTYET)
278 if (S_ISSOCK(st.st_mode))
279 (void) svr4_add_socket(p, SCARG(uap, path), &st);
282 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
289 svr4_sys_lxstat(p, uap)
290 register struct proc *p;
291 struct svr4_sys_lxstat_args *uap;
294 struct svr4_xstat svr4_st;
295 struct lstat_args cup;
297 caddr_t sg = stackgap_init();
298 CHECKALTEXIST(p, &sg, SCARG(uap, path));
300 SCARG(&cup, path) = SCARG(uap, path);
301 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
303 if ((error = lstat(p, &cup)) != 0)
306 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
309 bsd_to_svr4_xstat(&st, &svr4_st);
311 #if defined(SOCKET_NOTYET)
312 if (S_ISSOCK(st.st_mode))
313 (void) svr4_add_socket(p, SCARG(uap, path), &st);
315 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
323 svr4_sys_fxstat(p, uap)
324 register struct proc *p;
325 struct svr4_sys_fxstat_args *uap;
328 struct svr4_xstat svr4_st;
329 struct fstat_args cup;
332 caddr_t sg = stackgap_init();
334 SCARG(&cup, fd) = SCARG(uap, fd);
335 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
337 if ((error = fstat(p, &cup)) != 0)
340 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
343 bsd_to_svr4_xstat(&st, &svr4_st);
345 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
352 svr4_sys_stat64(p, uap)
353 register struct proc *p;
354 struct svr4_sys_stat64_args *uap;
357 struct svr4_stat64 svr4_st;
358 struct stat_args cup;
360 caddr_t sg = stackgap_init();
362 CHECKALTEXIST(p, &sg, SCARG(uap, path));
364 SCARG(&cup, path) = SCARG(uap, path);
365 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
367 if ((error = stat(p, &cup)) != 0)
370 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
373 bsd_to_svr4_stat64(&st, &svr4_st);
375 if (S_ISSOCK(st.st_mode))
376 (void) svr4_add_socket(p, SCARG(uap, path), &st);
378 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
386 svr4_sys_lstat64(p, uap)
387 register struct proc *p;
388 struct svr4_sys_lstat64_args *uap;
391 struct svr4_stat64 svr4_st;
392 struct stat_args cup;
394 caddr_t sg = stackgap_init();
396 CHECKALTEXIST(p, &sg, (char *) SCARG(uap, path));
398 SCARG(&cup, path) = SCARG(uap, path);
399 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
401 if ((error = lstat(p, (struct lstat_args *)&cup)) != 0)
404 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
407 bsd_to_svr4_stat64(&st, &svr4_st);
409 if (S_ISSOCK(st.st_mode))
410 (void) svr4_add_socket(p, SCARG(uap, path), &st);
412 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
420 svr4_sys_fstat64(p, uap)
421 register struct proc *p;
422 struct svr4_sys_fstat64_args *uap;
425 struct svr4_stat64 svr4_st;
426 struct fstat_args cup;
428 caddr_t sg = stackgap_init();
430 SCARG(&cup, fd) = SCARG(uap, fd);
431 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
433 if ((error = fstat(p, &cup)) != 0)
436 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
439 bsd_to_svr4_stat64(&st, &svr4_st);
441 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
450 register struct proc *p;
451 struct svr4_ustat_args *uap;
453 struct svr4_ustat us;
456 memset(&us, 0, sizeof us);
459 * XXX: should set f_tfree and f_tinode at least
460 * How do we translate dev -> fstat? (and then to svr4_ustat)
462 if ((error = copyout(&us, SCARG(uap, name), sizeof us)) != 0)
468 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
471 svr4_sys_uname(p, uap)
472 register struct proc *p;
473 struct svr4_sys_uname_args *uap;
475 struct svr4_utsname sut;
478 memset(&sut, 0, sizeof(sut));
480 strncpy(sut.sysname, ostype, sizeof(sut.sysname));
481 sut.sysname[sizeof(sut.sysname) - 1] = '\0';
483 strncpy(sut.nodename, hostname, sizeof(sut.nodename));
484 sut.nodename[sizeof(sut.nodename) - 1] = '\0';
486 strncpy(sut.release, osrelease, sizeof(sut.release));
487 sut.release[sizeof(sut.release) - 1] = '\0';
489 strncpy(sut.version, version, sizeof(sut.version));
490 sut.version[sizeof(sut.version) - 1] = '\0';
492 strncpy(sut.machine, machine, sizeof(sut.machine));
493 sut.machine[sizeof(sut.machine) - 1] = '\0';
495 return copyout((caddr_t) &sut, (caddr_t) SCARG(uap, name),
496 sizeof(struct svr4_utsname));
500 svr4_sys_systeminfo(p, uap)
502 struct svr4_sys_systeminfo_args *uap;
506 register_t *retval = p->p_retval;
508 char buf[1]; /* XXX NetBSD uses 256, but that seems
509 like awfully excessive kstack usage
510 for an empty string... */
511 u_int rlen = SCARG(uap, len);
513 switch (SCARG(uap, what)) {
514 case SVR4_SI_SYSNAME:
518 case SVR4_SI_HOSTNAME:
522 case SVR4_SI_RELEASE:
526 case SVR4_SI_VERSION:
530 case SVR4_SI_MACHINE:
534 case SVR4_SI_ARCHITECTURE:
538 case SVR4_SI_HW_SERIAL:
542 case SVR4_SI_HW_PROVIDER:
546 case SVR4_SI_SRPC_DOMAIN:
550 case SVR4_SI_PLATFORM:
558 case SVR4_SI_KERB_REALM:
561 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
562 case SVR4_SI_SET_HOSTNAME:
563 if ((error = suser(p)) != 0)
565 name = KERN_HOSTNAME;
566 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
568 case SVR4_SI_SET_SRPC_DOMAIN:
569 if ((error = suser(p)) != 0)
571 name = KERN_NISDOMAINNAME;
572 return kern_sysctl(&name, 1, 0, 0, SCARG(uap, buf), rlen, p);
574 case SVR4_SI_SET_HOSTNAME:
575 case SVR4_SI_SET_SRPC_DOMAIN:
578 case SVR4_SI_SET_KERB_REALM:
582 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap, what)));
587 len = strlen(str) + 1;
591 if (SCARG(uap, buf)) {
592 error = copyout(str, SCARG(uap, buf), len);
595 /* make sure we are NULL terminated */
597 error = copyout(buf, &(SCARG(uap, buf)[len - 1]), 1);
602 /* XXX NetBSD has hostname setting stuff here. Why would an emulator
610 svr4_sys_utssys(p, uap)
611 register struct proc *p;
612 struct svr4_sys_utssys_args *uap;
614 switch (SCARG(uap, sel)) {
615 case 0: /* uname(2) */
617 struct svr4_sys_uname_args ua;
618 SCARG(&ua, name) = SCARG(uap, a1);
619 return svr4_sys_uname(p, &ua);
622 case 2: /* ustat(2) */
624 struct svr4_ustat_args ua;
625 SCARG(&ua, dev) = (svr4_dev_t) SCARG(uap, a2);
626 SCARG(&ua, name) = SCARG(uap, a1);
627 return svr4_ustat(p, &ua);
630 case 3: /* fusers(2) */
641 svr4_sys_utime(p, uap)
642 register struct proc *p;
643 struct svr4_sys_utime_args *uap;
645 struct svr4_utimbuf ub;
646 struct timeval tbuf[2];
647 struct utimes_args ap;
649 caddr_t sg = stackgap_init();
652 CHECKALTEXIST(p, &sg, SCARG(uap, path));
653 SCARG(&ap, path) = SCARG(uap, path);
654 if (SCARG(uap, ubuf) != NULL) {
655 if ((error = copyin(SCARG(uap, ubuf), &ub, sizeof(ub))) != 0)
657 tbuf[0].tv_sec = ub.actime;
659 tbuf[1].tv_sec = ub.modtime;
661 ttp = stackgap_alloc(&sg, sizeof(tbuf));
662 error = copyout(tbuf, ttp, sizeof(tbuf));
665 SCARG(&ap, tptr) = ttp;
668 SCARG(&ap, tptr) = NULL;
669 return utimes(p, &ap);
674 svr4_sys_utimes(p, uap)
675 register struct proc *p;
676 struct svr4_sys_utimes_args *uap;
678 caddr_t sg = stackgap_init();
679 CHECKALTEXIST(p, &sg, SCARG(uap, path));
680 return utimes(p, (struct utimes_args *)uap);
684 svr4_to_bsd_pathconf(name)
688 case SVR4_PC_LINK_MAX:
691 case SVR4_PC_MAX_CANON:
692 return _PC_MAX_CANON;
694 case SVR4_PC_MAX_INPUT:
695 return _PC_MAX_INPUT;
697 case SVR4_PC_NAME_MAX:
700 case SVR4_PC_PATH_MAX:
703 case SVR4_PC_PIPE_BUF:
706 case SVR4_PC_NO_TRUNC:
709 case SVR4_PC_VDISABLE:
712 case SVR4_PC_CHOWN_RESTRICTED:
713 return _PC_CHOWN_RESTRICTED;
714 case SVR4_PC_SYNC_IO:
715 #if defined(_PC_SYNC_IO)
720 case SVR4_PC_ASYNC_IO:
721 case SVR4_PC_PRIO_IO:
733 svr4_sys_pathconf(p, uap)
734 register struct proc *p;
735 struct svr4_sys_pathconf_args *uap;
737 caddr_t sg = stackgap_init();
738 register_t *retval = p->p_retval;
740 CHECKALTEXIST(p, &sg, SCARG(uap, path));
742 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
744 switch (SCARG(uap, name)) {
752 return pathconf(p, (struct pathconf_args *)uap);
758 svr4_sys_fpathconf(p, uap)
759 register struct proc *p;
760 struct svr4_sys_fpathconf_args *uap;
762 register_t *retval = p->p_retval;
764 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
766 switch (SCARG(uap, name)) {
774 return fpathconf(p, (struct fpathconf_args *)uap);