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.10 2005/12/10 16:06:20 swildner 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>
48 #include "svr4_types.h"
49 #include "svr4_signal.h"
50 #include "svr4_proto.h"
51 #include "svr4_util.h"
52 #include "svr4_stat.h"
53 #include "svr4_ustat.h"
54 #include "svr4_utsname.h"
55 #include "svr4_systeminfo.h"
56 #include "svr4_socket.h"
57 #include "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 (struct stat *, struct svr4_xstat *);
77 static void bsd_to_svr4_stat64 (struct stat *, struct svr4_stat64 *);
78 int svr4_ustat (struct svr4_ustat_args *);
79 static int svr4_to_bsd_pathconf (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 (struct stat *, struct svr4_stat *);
92 bsd_to_svr4_stat(struct stat *st, 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(struct stat *st, struct svr4_xstat *st4)
113 memset(st4, 0, sizeof(*st4));
114 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
115 st4->st_ino = st->st_ino;
116 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
117 st4->st_nlink = st->st_nlink;
118 st4->st_uid = st->st_uid;
119 st4->st_gid = st->st_gid;
120 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
121 st4->st_size = st->st_size;
122 st4->st_atim = st->st_atimespec;
123 st4->st_mtim = st->st_mtimespec;
124 st4->st_ctim = st->st_ctimespec;
125 st4->st_blksize = st->st_blksize;
126 st4->st_blocks = st->st_blocks;
127 strcpy(st4->st_fstype, "unknown");
132 bsd_to_svr4_stat64(struct stat *st, struct svr4_stat64 *st4)
134 memset(st4, 0, sizeof(*st4));
135 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
136 st4->st_ino = st->st_ino;
137 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
138 st4->st_nlink = st->st_nlink;
139 st4->st_uid = st->st_uid;
140 st4->st_gid = st->st_gid;
141 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
142 st4->st_size = st->st_size;
143 st4->st_atim = st->st_atimespec;
144 st4->st_mtim = st->st_mtimespec;
145 st4->st_ctim = st->st_ctimespec;
146 st4->st_blksize = st->st_blksize;
147 st4->st_blocks = st->st_blocks;
148 strcpy(st4->st_fstype, "unknown");
152 svr4_sys_stat(struct svr4_sys_stat_args *uap)
154 struct thread *td = curthread;
156 struct svr4_stat svr4_st;
157 struct stat_args cup;
159 caddr_t sg = stackgap_init();
161 CHECKALTEXIST(&sg, SCARG(uap, path));
163 SCARG(&cup, path) = SCARG(uap, path);
164 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
165 cup.sysmsg_result = 0;
167 if ((error = stat(&cup)) != 0)
170 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
173 uap->sysmsg_result = cup.sysmsg_result;
175 bsd_to_svr4_stat(&st, &svr4_st);
177 if (S_ISSOCK(st.st_mode))
178 (void) svr4_add_socket(td, SCARG(uap, path), &st);
180 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
188 svr4_sys_lstat(struct svr4_sys_lstat_args *uap)
190 struct thread *td = curthread;
192 struct svr4_stat svr4_st;
193 struct lstat_args cup;
195 caddr_t sg = stackgap_init();
197 CHECKALTEXIST(&sg, SCARG(uap, path));
199 SCARG(&cup, path) = SCARG(uap, path);
200 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
201 cup.sysmsg_result = 0;
203 if ((error = lstat(&cup)) != 0)
206 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
209 uap->sysmsg_result = cup.sysmsg_result;
211 bsd_to_svr4_stat(&st, &svr4_st);
213 if (S_ISSOCK(st.st_mode))
214 (void) svr4_add_socket(td, SCARG(uap, path), &st);
216 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
224 svr4_sys_fstat(struct svr4_sys_fstat_args *uap)
227 struct svr4_stat svr4_st;
228 struct fstat_args cup;
230 caddr_t sg = stackgap_init();
232 SCARG(&cup, fd) = SCARG(uap, fd);
233 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
234 cup.sysmsg_result = 0;
236 if ((error = fstat(&cup)) != 0)
239 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
242 uap->sysmsg_result = cup.sysmsg_result;
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(struct svr4_sys_xstat_args *uap)
257 struct svr4_xstat svr4_st;
258 struct stat_args cup;
261 caddr_t sg = stackgap_init();
262 CHECKALTEXIST(&sg, SCARG(uap, path));
264 SCARG(&cup, path) = SCARG(uap, path);
265 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
266 cup.sysmsg_result = 0;
268 if ((error = stat(&cup)) != 0)
271 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
274 uap->sysmsg_result = cup.sysmsg_result;
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(struct svr4_sys_lxstat_args *uap)
293 struct svr4_xstat svr4_st;
294 struct lstat_args cup;
296 caddr_t sg = stackgap_init();
297 CHECKALTEXIST(&sg, SCARG(uap, path));
299 SCARG(&cup, path) = SCARG(uap, path);
300 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
301 cup.sysmsg_result = 0;
303 if ((error = lstat(&cup)) != 0)
306 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
309 uap->sysmsg_result = cup.sysmsg_result;
311 bsd_to_svr4_xstat(&st, &svr4_st);
313 #if defined(SOCKET_NOTYET)
314 if (S_ISSOCK(st.st_mode))
315 (void) svr4_add_socket(p, SCARG(uap, path), &st);
317 if ((error = copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st)) != 0)
325 svr4_sys_fxstat(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));
336 cup.sysmsg_result = 0;
338 if ((error = fstat(&cup)) != 0)
341 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
344 uap->sysmsg_result = cup.sysmsg_result;
346 bsd_to_svr4_xstat(&st, &svr4_st);
348 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
355 svr4_sys_stat64(struct svr4_sys_stat64_args *uap)
357 struct thread *td = curthread;
359 struct svr4_stat64 svr4_st;
360 struct stat_args cup;
362 caddr_t sg = stackgap_init();
364 CHECKALTEXIST(&sg, SCARG(uap, path));
366 SCARG(&cup, path) = SCARG(uap, path);
367 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
368 cup.sysmsg_result = 0;
370 if ((error = stat(&cup)) != 0)
373 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
376 uap->sysmsg_result = cup.sysmsg_result;
378 bsd_to_svr4_stat64(&st, &svr4_st);
380 if (S_ISSOCK(st.st_mode))
381 (void) svr4_add_socket(td, SCARG(uap, path), &st);
383 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
391 svr4_sys_lstat64(struct svr4_sys_lstat64_args *uap)
393 struct thread *td = curthread;
395 struct svr4_stat64 svr4_st;
396 struct stat_args cup;
398 caddr_t sg = stackgap_init();
400 CHECKALTEXIST(&sg, (char *) SCARG(uap, path));
402 SCARG(&cup, path) = SCARG(uap, path);
403 SCARG(&cup, ub) = stackgap_alloc(&sg, sizeof(struct stat));
404 cup.sysmsg_result = 0;
406 if ((error = lstat((struct lstat_args *)&cup)) != 0)
409 if ((error = copyin(SCARG(&cup, ub), &st, sizeof st)) != 0)
412 uap->sysmsg_result = cup.sysmsg_result;
414 bsd_to_svr4_stat64(&st, &svr4_st);
416 if (S_ISSOCK(st.st_mode))
417 (void) svr4_add_socket(td, SCARG(uap, path), &st);
419 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
427 svr4_sys_fstat64(struct svr4_sys_fstat64_args *uap)
430 struct svr4_stat64 svr4_st;
431 struct fstat_args cup;
433 caddr_t sg = stackgap_init();
435 SCARG(&cup, fd) = SCARG(uap, fd);
436 SCARG(&cup, sb) = stackgap_alloc(&sg, sizeof(struct stat));
437 cup.sysmsg_result = 0;
439 if ((error = fstat(&cup)) != 0)
442 if ((error = copyin(SCARG(&cup, sb), &st, sizeof st)) != 0)
445 uap->sysmsg_result = cup.sysmsg_result;
447 bsd_to_svr4_stat64(&st, &svr4_st);
449 if ((error = copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st)) != 0)
457 svr4_ustat(struct svr4_ustat_args *uap)
459 struct svr4_ustat us;
462 memset(&us, 0, sizeof us);
465 * XXX: should set f_tfree and f_tinode at least
466 * How do we translate dev -> fstat? (and then to svr4_ustat)
468 if ((error = copyout(&us, SCARG(uap, name), sizeof us)) != 0)
474 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
477 svr4_sys_uname(struct svr4_sys_uname_args *uap)
479 struct svr4_utsname sut;
481 memset(&sut, 0, sizeof(sut));
483 strncpy(sut.sysname, ostype, sizeof(sut.sysname));
484 sut.sysname[sizeof(sut.sysname) - 1] = '\0';
486 strncpy(sut.nodename, hostname, sizeof(sut.nodename));
487 sut.nodename[sizeof(sut.nodename) - 1] = '\0';
489 strncpy(sut.release, osrelease, sizeof(sut.release));
490 sut.release[sizeof(sut.release) - 1] = '\0';
492 strncpy(sut.version, version, sizeof(sut.version));
493 sut.version[sizeof(sut.version) - 1] = '\0';
495 strncpy(sut.machine, machine, sizeof(sut.machine));
496 sut.machine[sizeof(sut.machine) - 1] = '\0';
498 return copyout((caddr_t) &sut, (caddr_t) SCARG(uap, name),
499 sizeof(struct svr4_utsname));
503 svr4_sys_systeminfo(struct svr4_sys_systeminfo_args *uap)
507 register_t *retval = &uap->sysmsg_result;
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(td)) != 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(td)) != 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(struct svr4_sys_utssys_args *uap)
613 switch (SCARG(uap, sel)) {
614 case 0: /* uname(2) */
616 struct svr4_sys_uname_args ua;
619 SCARG(&ua, name) = SCARG(uap, a1);
620 ua.sysmsg_result = 0;
621 error = svr4_sys_uname(&ua);
622 uap->sysmsg_result = ua.sysmsg_result;
626 case 2: /* ustat(2) */
628 struct svr4_ustat_args ua;
631 SCARG(&ua, dev) = (svr4_dev_t) SCARG(uap, a2);
632 SCARG(&ua, name) = SCARG(uap, a1);
633 ua.sysmsg_result = 0;
634 error = svr4_ustat(&ua);
635 uap->sysmsg_result = ua.sysmsg_result;
639 case 3: /* fusers(2) */
650 svr4_sys_utime(struct svr4_sys_utime_args *uap)
652 struct svr4_utimbuf ub;
653 struct timeval tbuf[2];
654 struct utimes_args ap;
656 caddr_t sg = stackgap_init();
659 CHECKALTEXIST(&sg, SCARG(uap, path));
660 SCARG(&ap, path) = SCARG(uap, path);
661 if (SCARG(uap, ubuf) != NULL) {
662 if ((error = copyin(SCARG(uap, ubuf), &ub, sizeof(ub))) != 0)
664 tbuf[0].tv_sec = ub.actime;
666 tbuf[1].tv_sec = ub.modtime;
668 ttp = stackgap_alloc(&sg, sizeof(tbuf));
669 error = copyout(tbuf, ttp, sizeof(tbuf));
672 SCARG(&ap, tptr) = ttp;
674 SCARG(&ap, tptr) = NULL;
676 ap.sysmsg_result = 0;
678 uap->sysmsg_result = ap.sysmsg_result;
684 svr4_sys_utimes(struct svr4_sys_utimes_args *uap)
686 caddr_t sg = stackgap_init();
687 CHECKALTEXIST(&sg, SCARG(uap, path));
688 return utimes((struct utimes_args *)uap);
692 svr4_to_bsd_pathconf(int name)
695 case SVR4_PC_LINK_MAX:
698 case SVR4_PC_MAX_CANON:
699 return _PC_MAX_CANON;
701 case SVR4_PC_MAX_INPUT:
702 return _PC_MAX_INPUT;
704 case SVR4_PC_NAME_MAX:
707 case SVR4_PC_PATH_MAX:
710 case SVR4_PC_PIPE_BUF:
713 case SVR4_PC_NO_TRUNC:
716 case SVR4_PC_VDISABLE:
719 case SVR4_PC_CHOWN_RESTRICTED:
720 return _PC_CHOWN_RESTRICTED;
721 case SVR4_PC_SYNC_IO:
722 #if defined(_PC_SYNC_IO)
727 case SVR4_PC_ASYNC_IO:
728 case SVR4_PC_PRIO_IO:
740 svr4_sys_pathconf(struct svr4_sys_pathconf_args *uap)
742 caddr_t sg = stackgap_init();
743 register_t *retval = &uap->sysmsg_result;
745 CHECKALTEXIST(&sg, SCARG(uap, path));
747 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
749 switch (SCARG(uap, name)) {
757 return pathconf((struct pathconf_args *)uap);
763 svr4_sys_fpathconf(struct svr4_sys_fpathconf_args *uap)
765 register_t *retval = &uap->sysmsg_result;
767 SCARG(uap, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
769 switch (SCARG(uap, name)) {
777 return fpathconf((struct fpathconf_args *)uap);