Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / sys / compat / svr4_32 / svr4_32_stat.c
blobc2dc3bed9f2b704ac1e693b33246098a0528d0fa
1 /* $NetBSD: svr4_32_stat.c,v 1.35 2008/04/28 20:23:46 martin Exp $ */
3 /*-
4 * Copyright (c) 1994, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_32_stat.c,v 1.35 2008/04/28 20:23:46 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/proc.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/filedesc.h>
42 #include <sys/ioctl.h>
43 #include <sys/kernel.h>
44 #include <sys/mount.h>
45 #include <sys/malloc.h>
46 #include <sys/namei.h>
47 #include <sys/unistd.h>
48 #include <sys/kauth.h>
50 #include <sys/time.h>
51 #include <sys/ucred.h>
52 #include <uvm/uvm_extern.h>
53 #include <sys/sysctl.h>
54 #include <sys/vfs_syscalls.h>
56 #include <sys/syscallargs.h>
58 #include <compat/svr4_32/svr4_32_types.h>
59 #include <compat/svr4_32/svr4_32_signal.h>
60 #include <compat/svr4_32/svr4_32_lwp.h>
61 #include <compat/svr4_32/svr4_32_ucontext.h>
62 #include <compat/svr4_32/svr4_32_syscallargs.h>
63 #include <compat/svr4_32/svr4_32_util.h>
64 #include <compat/svr4_32/svr4_32_stat.h>
65 #include <compat/svr4_32/svr4_32_ustat.h>
66 #include <compat/svr4_32/svr4_32_fuser.h>
67 #include <compat/svr4/svr4_utsname.h>
68 #include <compat/svr4/svr4_systeminfo.h>
69 #include <compat/svr4_32/svr4_32_time.h>
70 #include <compat/svr4_32/svr4_32_socket.h>
72 #if defined(__sparc__) || defined(__sparc_v9__) || defined(__sparc64__)
74 * Solaris-2.4 on the sparc has the old stat call using the new
75 * stat data structure...
77 # define SVR4_NO_OSTAT
78 #endif
80 static void bsd_to_svr4_32_xstat(struct stat *, struct svr4_32_xstat *);
81 static void bsd_to_svr4_32_stat64(struct stat *, struct svr4_32_stat64 *);
82 static int svr4_32_to_bsd_pathconf(int);
85 * SVR4 uses named pipes as named sockets, so we tell programs
86 * that sockets are named pipes with mode 0
88 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
91 #ifndef SVR4_NO_OSTAT
92 static void bsd_to_svr4_32_stat(struct stat *, struct svr4_32_stat *);
94 static void
95 bsd_to_svr4_32_stat(struct stat *st, struct svr4_32_stat *st4)
97 memset(st4, 0, sizeof(*st4));
98 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
99 st4->st_ino = st->st_ino;
100 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
101 st4->st_nlink = st->st_nlink;
102 st4->st_uid = st->st_uid;
103 st4->st_gid = st->st_gid;
104 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
105 st4->st_size = st->st_size;
106 st4->st_atim = st->st_atimespec.tv_sec;
107 st4->st_mtim = st->st_mtimespec.tv_sec;
108 st4->st_ctim = st->st_ctimespec.tv_sec;
110 #endif
113 static void
114 bsd_to_svr4_32_xstat(struct stat *st, struct svr4_32_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.tv_sec = st->st_atimespec.tv_sec;
126 st4->st_atim.tv_nsec = st->st_atimespec.tv_nsec;
127 st4->st_mtim.tv_sec = st->st_mtimespec.tv_sec;
128 st4->st_mtim.tv_nsec = st->st_mtimespec.tv_nsec;
129 st4->st_ctim.tv_sec = st->st_ctimespec.tv_sec;
130 st4->st_ctim.tv_nsec = st->st_ctimespec.tv_nsec;
131 st4->st_blksize = st->st_blksize;
132 st4->st_blocks = st->st_blocks;
133 strlcpy(st4->st_fstype, "unknown", sizeof(st4->st_fstype));
137 static void
138 bsd_to_svr4_32_stat64(struct stat *st, struct svr4_32_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.tv_sec = st->st_atimespec.tv_sec;
150 st4->st_atim.tv_nsec = st->st_atimespec.tv_nsec;
151 st4->st_mtim.tv_sec = st->st_mtimespec.tv_sec;
152 st4->st_mtim.tv_nsec = st->st_mtimespec.tv_nsec;
153 st4->st_ctim.tv_sec = st->st_ctimespec.tv_sec;
154 st4->st_ctim.tv_nsec = st->st_ctimespec.tv_nsec;
155 st4->st_blocks = st->st_blocks;
156 strlcpy(st4->st_fstype, "unknown", sizeof(st4->st_fstype));
161 svr4_32_sys_stat(struct lwp *l, const struct svr4_32_sys_stat_args *uap, register_t *retval)
163 #ifdef SVR4_NO_OSTAT
164 struct svr4_32_sys_xstat_args cup;
166 SCARG(&cup, two) = 2;
167 SCARG(&cup, path) = SCARG(uap, path);
168 SCARG(&cup, ub) = SCARG(uap, ub);
169 return svr4_32_sys_xstat(l, &cup, retval);
170 #else
171 struct stat st;
172 struct svr4_32_stat svr4_st;
173 int error;
175 error = do_sys_stat(SCARG(&cup, path), FOLLOW, &st);
176 if (error != 0)
177 return error;
179 bsd_to_svr4_32_stat(&st, &svr4_st);
181 if (S_ISSOCK(st.st_mode))
182 (void) svr4_add_socket(p, SCARG(&cup, path), &st);
184 return copyout(&svr4_st, SCARG_P32(uap, ub),
185 sizeof svr4_st);
186 #endif
191 svr4_32_sys_lstat(struct lwp *l, const struct svr4_32_sys_lstat_args *uap, register_t *retval)
193 #ifdef SVR4_NO_OSTAT
194 struct svr4_32_sys_lxstat_args cup;
196 SCARG(&cup, two) = 2;
197 SCARG(&cup, path) = SCARG(uap, path);
198 SCARG(&cup, ub) = SCARG(uap, ub);
199 return svr4_32_sys_lxstat(l, &cup, retval);
200 #else
201 struct stat st;
202 struct svr4_32_stat svr4_st;
203 int error;
205 error = do_sys_stat(SCARG(&cup, path), NOFOLLOW, &st);
206 if (error != 0)
207 return error;
209 bsd_to_svr4_32_stat(&st, &svr4_st);
211 if (S_ISSOCK(st.st_mode))
212 (void) svr4_add_socket(p, SCARG(&cup, path), &st);
214 return copyout(&svr4_st, SCARG_P32(uap, ub),
215 sizeof svr4_st);
216 #endif
221 svr4_32_sys_fstat(struct lwp *l, const struct svr4_32_sys_fstat_args *uap, register_t *retval)
223 #ifdef SVR4_NO_OSTAT
224 struct svr4_32_sys_fxstat_args cup;
226 SCARG(&cup, two) = 2;
227 SCARG(&cup, fd) = SCARG(uap, fd);
228 SCARG(&cup, sb) = SCARG(uap, sb);
229 return svr4_32_sys_fxstat(l, &cup, retval);
230 #else
231 struct stat st;
232 struct svr4_32_stat svr4_st;
233 int error;
235 error = do_sys_fstat(SCARG(uap, fd), &st);
236 if (error != 0)
237 return error;
239 bsd_to_svr4_32_stat(&st, &svr4_st);
241 return copyout(&svr4_st, SCARG_P32(uap, ub),
242 sizeof svr4_st);
243 #endif
248 svr4_32_sys_xstat(struct lwp *l, const struct svr4_32_sys_xstat_args *uap, register_t *retval)
250 struct stat st;
251 struct svr4_32_xstat svr4_st;
252 int error;
253 const char *path = SCARG_P32(uap, path);
255 error = do_sys_stat(path, FOLLOW, &st);
256 if (error != 0)
257 return error;
259 bsd_to_svr4_32_xstat(&st, &svr4_st);
261 if (S_ISSOCK(st.st_mode))
262 (void) svr4_add_socket(l->l_proc, path, &st);
264 return copyout(&svr4_st, SCARG_P32(uap, ub),
265 sizeof svr4_st);
270 svr4_32_sys_lxstat(struct lwp *l, const struct svr4_32_sys_lxstat_args *uap, register_t *retval)
272 struct stat st;
273 struct svr4_32_xstat svr4_st;
274 int error;
275 const char *path = SCARG_P32(uap, path);
277 error = do_sys_stat(path, NOFOLLOW, &st);
278 if (error != 0)
279 return error;
281 bsd_to_svr4_32_xstat(&st, &svr4_st);
283 if (S_ISSOCK(st.st_mode))
284 (void) svr4_add_socket(l->l_proc, path, &st);
286 return copyout(&svr4_st, SCARG_P32(uap, ub),
287 sizeof svr4_st);
292 svr4_32_sys_fxstat(struct lwp *l, const struct svr4_32_sys_fxstat_args *uap, register_t *retval)
294 struct stat st;
295 struct svr4_32_xstat svr4_st;
296 int error;
298 error = do_sys_fstat(SCARG(uap, fd), &st);
299 if (error != 0)
300 return error;
302 bsd_to_svr4_32_xstat(&st, &svr4_st);
304 return copyout(&svr4_st, SCARG_P32(uap, sb),
305 sizeof svr4_st);
310 svr4_32_sys_stat64(struct lwp *l, const struct svr4_32_sys_stat64_args *uap, register_t *retval)
312 struct stat st;
313 struct svr4_32_stat64 svr4_st;
314 int error;
315 const char *path = SCARG_P32(uap, path);
317 error = do_sys_stat(path, FOLLOW, &st);
318 if (error != 0)
319 return error;
321 bsd_to_svr4_32_stat64(&st, &svr4_st);
323 if (S_ISSOCK(st.st_mode))
324 (void) svr4_add_socket(l->l_proc, path, &st);
326 return copyout(&svr4_st, SCARG_P32(uap, sb),
327 sizeof svr4_st);
332 svr4_32_sys_lstat64(struct lwp *l, const struct svr4_32_sys_lstat64_args *uap, register_t *retval)
334 struct stat st;
335 struct svr4_32_stat64 svr4_st;
336 int error;
337 const char *path = SCARG_P32(uap, path);
339 error = do_sys_stat(path, NOFOLLOW, &st);
340 if (error != 0)
341 return error;
343 bsd_to_svr4_32_stat64(&st, &svr4_st);
345 if (S_ISSOCK(st.st_mode))
346 (void) svr4_add_socket(l->l_proc, path, &st);
348 return copyout(&svr4_st, SCARG_P32(uap, sb),
349 sizeof svr4_st);
354 svr4_32_sys_fstat64(struct lwp *l, const struct svr4_32_sys_fstat64_args *uap, register_t *retval)
356 struct stat st;
357 struct svr4_32_stat64 svr4_st;
358 int error;
360 error = do_sys_fstat(SCARG(uap, fd), &st);
361 if (error != 0)
362 return error;
364 bsd_to_svr4_32_stat64(&st, &svr4_st);
366 return copyout(&svr4_st, SCARG_P32(uap, sb),
367 sizeof svr4_st);
371 struct svr4_32_ustat_args {
372 syscallarg(svr4_dev_t) dev;
373 syscallarg(svr4_32_ustatp) name;
376 static int
377 svr4_32_ustat(struct lwp *l, const struct svr4_32_ustat_args *uap, register_t *retval)
379 /* {
380 syscallarg(svr4_dev_t) dev;
381 syscallarg(svr4_32_ustatp) name;
382 } */
383 struct svr4_32_ustat us;
384 int error;
386 memset(&us, 0, sizeof us);
389 * XXX: should set f_tfree and f_tinode at least
390 * How do we translate dev -> fstat? (and then to svr4_32_ustat)
392 if ((error = copyout(&us, SCARG_P32(uap, name),
393 sizeof us)) != 0)
394 return (error);
396 return 0;
402 svr4_32_sys_uname(struct lwp *l, const struct svr4_32_sys_uname_args *uap, register_t *retval)
404 struct svr4_utsname sut;
406 memset(&sut, 0, sizeof(sut));
408 strncpy(sut.sysname, ostype, sizeof(sut.sysname));
409 sut.sysname[sizeof(sut.sysname) - 1] = '\0';
411 strncpy(sut.nodename, hostname, sizeof(sut.nodename));
412 sut.nodename[sizeof(sut.nodename) - 1] = '\0';
414 strncpy(sut.release, osrelease, sizeof(sut.release));
415 sut.release[sizeof(sut.release) - 1] = '\0';
417 strncpy(sut.version, version, sizeof(sut.version));
418 sut.version[sizeof(sut.version) - 1] = '\0';
420 strncpy(sut.machine, machine, sizeof(sut.machine));
421 sut.machine[sizeof(sut.machine) - 1] = '\0';
423 return copyout((void *) &sut, SCARG_P32(uap, name),
424 sizeof(struct svr4_utsname));
429 svr4_32_sys_systeminfo(struct lwp *l, const struct svr4_32_sys_systeminfo_args *uap, register_t *retval)
431 const char *str = NULL;
432 int name[2];
433 int error;
434 size_t len;
435 char buf[256];
437 u_int rlen = SCARG(uap, len);
439 switch (SCARG(uap, what)) {
440 case SVR4_SI_SYSNAME:
441 str = ostype;
442 break;
444 case SVR4_SI_HOSTNAME:
445 str = hostname;
446 break;
448 case SVR4_SI_RELEASE:
449 str = osrelease;
450 break;
452 case SVR4_SI_VERSION:
453 str = version;
454 break;
456 case SVR4_SI_MACHINE:
457 str = "sun4m"; /* Lie, pretend we are 4m */
458 break;
460 case SVR4_SI_ARCHITECTURE:
461 #if defined(__sparc__)
462 str = "sparc";
463 #else
464 str = machine_arch;
465 #endif
466 break;
468 case SVR4_SI_ISALIST:
469 #if defined(__sparc__)
470 str = "sparcv8 sparcv8-fsmuld sparcv7 sparc";
471 #elif defined(__i386__)
472 str = "i386";
473 #else
474 str = "unknown";
475 #endif
476 break;
478 case SVR4_SI_HW_SERIAL:
479 snprintf(buf, sizeof(buf), "%lu", hostid);
480 str = buf;
481 break;
483 case SVR4_SI_HW_PROVIDER:
484 str = ostype;
485 break;
487 case SVR4_SI_SRPC_DOMAIN:
488 str = domainname;
489 break;
491 case SVR4_SI_PLATFORM:
492 #if defined(__i386__)
493 str = "i86pc";
494 #elif defined(__sparc__)
496 extern char machine_model[];
498 str = machine_model;
500 #else
501 str = "unknown";
502 #endif
503 break;
505 case SVR4_SI_KERB_REALM:
506 str = "unsupported";
507 break;
509 case SVR4_SI_SET_HOSTNAME:
510 name[1] = KERN_HOSTNAME;
511 break;
513 case SVR4_SI_SET_SRPC_DOMAIN:
514 name[1] = KERN_DOMAINNAME;
515 break;
517 case SVR4_SI_SET_KERB_REALM:
518 return 0;
520 default:
521 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap, what)));
522 return ENOSYS;
525 if (str) {
526 len = strlen(str) + 1;
527 if (len < rlen)
528 rlen = len;
530 if (SCARG_P32(uap, buf)) {
531 error = copyout(str, SCARG_P32(uap, buf),
532 rlen);
533 if (error)
534 return error;
535 if (rlen > 0) {
536 /* make sure we are NULL terminated */
537 buf[0] = '\0';
538 error = copyout(buf, &(((char *)
539 SCARG_P32(uap, buf))[rlen - 1]), 1);
542 else
543 error = 0;
545 else {
546 error = copyinstr(SCARG_P32(uap, buf), buf,
547 sizeof(buf), &len);
548 if (error)
549 return error;
550 name[0] = CTL_KERN;
551 error = old_sysctl(&name[0], 1, 0, 0, buf, len, NULL);
554 *retval = len;
555 return error;
560 svr4_32_sys_utssys(struct lwp *l, const struct svr4_32_sys_utssys_args *uap, register_t *retval)
563 switch (SCARG(uap, sel)) {
564 case 0: /* uname(2) */
566 struct svr4_32_sys_uname_args ua;
567 SCARG(&ua, name) = SCARG(uap, a1);
568 return svr4_32_sys_uname(l, &ua, retval);
571 case 2: /* ustat(2) */
573 struct svr4_32_ustat_args ua;
574 SCARG(&ua, dev) = (uintptr_t)SCARG_P32(uap, a2);
575 SCARG(&ua, name) = SCARG(uap, a1);
576 return svr4_32_ustat(l, &ua, retval);
579 case 3: /* fusers(2) */
580 return ENOSYS;
582 default:
583 return ENOSYS;
585 return ENOSYS;
590 svr4_32_sys_utime(struct lwp *l, const struct svr4_32_sys_utime_args *uap, register_t *retval)
592 struct svr4_32_utimbuf ub;
593 struct timeval tbuf[2], *tvp;
594 int error;
596 if (SCARG_P32(uap, ubuf)) {
597 if ((error = copyin(SCARG_P32(uap, ubuf),
598 &ub, sizeof(ub))) != 0)
599 return error;
600 tbuf[0].tv_sec = ub.actime;
601 tbuf[0].tv_usec = 0;
602 tbuf[1].tv_sec = ub.modtime;
603 tbuf[1].tv_usec = 0;
604 tvp = tbuf;
606 else
607 tvp = NULL;
608 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
609 tvp, UIO_SYSSPACE);
614 svr4_32_sys_utimes(struct lwp *l, const struct svr4_32_sys_utimes_args *uap, register_t *retval)
616 struct compat_50_sys_utimes_args ua;
617 SCARG(&ua, path) = SCARG_P32(uap, path);
618 SCARG(&ua, tptr) = SCARG_P32(uap, tptr);
620 return compat_50_sys_utimes(l, &ua, retval);
624 static int
625 svr4_32_to_bsd_pathconf(int name)
627 switch (name) {
628 case SVR4_PC_LINK_MAX:
629 return _PC_LINK_MAX;
631 case SVR4_PC_MAX_CANON:
632 return _PC_MAX_CANON;
634 case SVR4_PC_MAX_INPUT:
635 return _PC_MAX_INPUT;
637 case SVR4_PC_NAME_MAX:
638 return _PC_NAME_MAX;
640 case SVR4_PC_PATH_MAX:
641 return _PC_PATH_MAX;
643 case SVR4_PC_PIPE_BUF:
644 return _PC_PIPE_BUF;
646 case SVR4_PC_NO_TRUNC:
647 return _PC_NO_TRUNC;
649 case SVR4_PC_VDISABLE:
650 return _PC_VDISABLE;
652 case SVR4_PC_CHOWN_RESTRICTED:
653 return _PC_CHOWN_RESTRICTED;
655 case SVR4_PC_SYNC_IO:
656 return _PC_SYNC_IO;
658 case SVR4_PC_FILESIZEBITS:
659 return _PC_FILESIZEBITS;
661 case SVR4_PC_ASYNC_IO:
662 case SVR4_PC_PRIO_IO:
663 /* Not supported */
664 return 0;
666 default:
667 /* Invalid */
668 return -1;
674 svr4_32_sys_pathconf(struct lwp *l, const struct svr4_32_sys_pathconf_args *uap, register_t *retval)
676 struct sys_pathconf_args /* {
677 syscallarg(char *) path;
678 syscallarg(int) name;
679 } */ ua;
681 SCARG(&ua, path) = SCARG_P32(uap, path);
682 SCARG(&ua, name) = svr4_32_to_bsd_pathconf(SCARG(&ua, name));
684 switch (SCARG(&ua, name)) {
685 case -1:
686 *retval = -1;
687 return EINVAL;
688 case 0:
689 *retval = 0;
690 return 0;
691 default:
692 return sys_pathconf(l, &ua, retval);
698 svr4_32_sys_fpathconf(struct lwp *l, const struct svr4_32_sys_fpathconf_args *uap, register_t *retval)
700 struct sys_fpathconf_args ua;
702 SCARG(&ua, fd) = SCARG(uap, fd);
703 SCARG(&ua, name) = svr4_32_to_bsd_pathconf(SCARG(uap, name));
705 switch (SCARG(&ua, name)) {
706 case -1:
707 *retval = -1;
708 return EINVAL;
709 case 0:
710 *retval = 0;
711 return 0;
712 default:
713 return sys_fpathconf(l, &ua, retval);