Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / svr4 / svr4_stat.c
blobd73833fead25c92e02df69ee9fb15e8667d1bd36
1 /* $NetBSD: svr4_stat.c,v 1.67.2.2 2008/11/01 21:22:26 christos 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_stat.c,v 1.67.2.2 2008/11/01 21:22:26 christos 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>
49 #include <sys/time.h>
50 #include <sys/ucred.h>
51 #include <uvm/uvm_extern.h>
52 #include <sys/sysctl.h>
54 #include <sys/syscallargs.h>
55 #include <sys/vfs_syscalls.h>
57 #include <compat/svr4/svr4_types.h>
58 #include <compat/svr4/svr4_signal.h>
59 #include <compat/svr4/svr4_lwp.h>
60 #include <compat/svr4/svr4_ucontext.h>
61 #include <compat/svr4/svr4_syscallargs.h>
62 #include <compat/svr4/svr4_util.h>
63 #include <compat/svr4/svr4_stat.h>
64 #include <compat/svr4/svr4_ustat.h>
65 #include <compat/svr4/svr4_fuser.h>
66 #include <compat/svr4/svr4_utsname.h>
67 #include <compat/svr4/svr4_systeminfo.h>
68 #include <compat/svr4/svr4_time.h>
69 #include <compat/svr4/svr4_socket.h>
71 #ifdef __sparc__
73 * Solaris-2.4 on the sparc has the old stat call using the new
74 * stat data structure...
76 # define SVR4_NO_OSTAT
77 #endif
79 static void bsd_to_svr4_xstat(struct stat *, struct svr4_xstat *);
80 static void bsd_to_svr4_stat64(struct stat *, struct svr4_stat64 *);
82 struct svr4_ustat_args {
83 syscallarg(svr4_dev_t) dev;
84 syscallarg(struct svr4_ustat *) name;
87 int svr4_ustat(struct lwp *, const struct svr4_ustat_args *, register_t *);
88 static int svr4_to_bsd_pathconf(int);
91 * SVR4 uses named pipes as named sockets, so we tell programs
92 * that sockets are named pipes with mode 0
94 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
97 #ifndef SVR4_NO_OSTAT
98 static void bsd_to_svr4_stat(struct stat *, struct svr4_stat *);
100 static void
101 bsd_to_svr4_stat(struct stat *st, struct svr4_stat *st4)
103 memset(st4, 0, sizeof(*st4));
104 st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
105 st4->st_ino = st->st_ino;
106 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
107 st4->st_nlink = st->st_nlink;
108 st4->st_uid = st->st_uid;
109 st4->st_gid = st->st_gid;
110 st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
111 st4->st_size = st->st_size;
112 st4->st_atim = st->st_atimespec.tv_sec;
113 st4->st_mtim = st->st_mtimespec.tv_sec;
114 st4->st_ctim = st->st_ctimespec.tv_sec;
116 #endif
119 static void
120 bsd_to_svr4_xstat(struct stat *st, struct svr4_xstat *st4)
122 memset(st4, 0, sizeof(*st4));
123 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
124 st4->st_ino = st->st_ino;
125 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
126 st4->st_nlink = st->st_nlink;
127 st4->st_uid = st->st_uid;
128 st4->st_gid = st->st_gid;
129 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
130 st4->st_size = st->st_size;
131 st4->st_atim.tv_sec = st->st_atimespec.tv_sec;
132 st4->st_atim.tv_nsec = st->st_atimespec.tv_nsec;
133 st4->st_mtim.tv_sec = st->st_mtimespec.tv_sec;
134 st4->st_mtim.tv_nsec = st->st_mtimespec.tv_nsec;
135 st4->st_ctim.tv_sec = st->st_ctimespec.tv_sec;
136 st4->st_ctim.tv_nsec = st->st_ctimespec.tv_nsec;
137 st4->st_blksize = st->st_blksize;
138 st4->st_blocks = st->st_blocks;
139 strlcpy(st4->st_fstype, "unknown", sizeof(st4->st_fstype));
143 static void
144 bsd_to_svr4_stat64(struct stat *st, struct svr4_stat64 *st4)
146 memset(st4, 0, sizeof(*st4));
147 st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
148 st4->st_ino = st->st_ino;
149 st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
150 st4->st_nlink = st->st_nlink;
151 st4->st_uid = st->st_uid;
152 st4->st_gid = st->st_gid;
153 st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
154 st4->st_size = st->st_size;
155 st4->st_atim.tv_sec = st->st_atimespec.tv_sec;
156 st4->st_atim.tv_nsec = st->st_atimespec.tv_nsec;
157 st4->st_mtim.tv_sec = st->st_mtimespec.tv_sec;
158 st4->st_mtim.tv_nsec = st->st_mtimespec.tv_nsec;
159 st4->st_ctim.tv_sec = st->st_ctimespec.tv_sec;
160 st4->st_ctim.tv_nsec = st->st_ctimespec.tv_nsec;
161 st4->st_blksize = st->st_blksize;
162 st4->st_blocks = st->st_blocks;
163 strlcpy(st4->st_fstype, "unknown", sizeof(st4->st_fstype));
168 svr4_sys_stat(struct lwp *l, const struct svr4_sys_stat_args *uap, register_t *retval)
170 #ifdef SVR4_NO_OSTAT
171 struct svr4_sys_xstat_args cup;
173 SCARG(&cup, two) = 2;
174 SCARG(&cup, path) = SCARG(uap, path);
175 SCARG(&cup, ub) = (struct svr4_xstat *) SCARG(uap, ub);
176 return svr4_sys_xstat(l, &cup, retval);
177 #else
178 struct stat st;
179 struct svr4_stat svr4_st;
180 int error;
182 error = do_sys_stat(SCARG(uap, path), FOLLOW, &st);
183 if (error != 0)
184 return error;
186 bsd_to_svr4_stat(&st, &svr4_st);
188 if (S_ISSOCK(st.st_mode))
189 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
191 return copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st);
192 #endif
197 svr4_sys_lstat(struct lwp *l, const struct svr4_sys_lstat_args *uap, register_t *retval)
199 #ifdef SVR4_NO_OSTAT
200 struct svr4_sys_lxstat_args cup;
202 SCARG(&cup, two) = 2;
203 SCARG(&cup, path) = SCARG(uap, path);
204 SCARG(&cup, ub) = (struct svr4_xstat *) SCARG(uap, ub);
205 return svr4_sys_lxstat(l, &cup, retval);
206 #else
207 struct stat st;
208 struct svr4_stat svr4_st;
209 int error;
211 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &st);
212 if (error != 0)
213 return error;
215 bsd_to_svr4_stat(&st, &svr4_st);
217 if (S_ISSOCK(st.st_mode))
218 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
220 return copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st);
221 #endif
226 svr4_sys_fstat(struct lwp *l, const struct svr4_sys_fstat_args *uap, register_t *retval)
228 #ifdef SVR4_NO_OSTAT
229 struct svr4_sys_fxstat_args cup;
231 SCARG(&cup, two) = 2;
232 SCARG(&cup, fd) = SCARG(uap, fd);
233 SCARG(&cup, sb) = (struct svr4_xstat *) SCARG(uap, sb);
234 return svr4_sys_fxstat(l, &cup, retval);
235 #else
236 struct stat st;
237 struct svr4_stat svr4_st;
238 int error;
240 error = do_sys_fstat(SCARG(uap, fd), &st);
241 if (error != 0)
242 return error;
244 bsd_to_svr4_stat(&st, &svr4_st);
246 return copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st);
247 #endif
252 svr4_sys_xstat(struct lwp *l, const struct svr4_sys_xstat_args *uap, register_t *retval)
254 struct stat st;
255 struct svr4_xstat svr4_st;
256 int error;
258 error = do_sys_stat(SCARG(uap, path), FOLLOW, &st);
259 if (error != 0)
260 return error;
262 bsd_to_svr4_xstat(&st, &svr4_st);
264 if (S_ISSOCK(st.st_mode))
265 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
267 return copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st);
272 svr4_sys_lxstat(struct lwp *l, const struct svr4_sys_lxstat_args *uap, register_t *retval)
274 struct stat st;
275 struct svr4_xstat svr4_st;
276 int error;
278 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &st);
279 if (error != 0)
280 return error;
282 bsd_to_svr4_xstat(&st, &svr4_st);
284 if (S_ISSOCK(st.st_mode))
285 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
287 return copyout(&svr4_st, SCARG(uap, ub), sizeof svr4_st);
292 svr4_sys_fxstat(struct lwp *l, const struct svr4_sys_fxstat_args *uap, register_t *retval)
294 struct stat st;
295 struct svr4_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_xstat(&st, &svr4_st);
304 return copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st);
309 svr4_sys_stat64(struct lwp *l, const struct svr4_sys_stat64_args *uap, register_t *retval)
311 struct stat st;
312 struct svr4_stat64 svr4_st;
313 int error;
315 error = do_sys_stat(SCARG(uap, path), FOLLOW, &st);
316 if (error != 0)
317 return error;
319 bsd_to_svr4_stat64(&st, &svr4_st);
321 if (S_ISSOCK(st.st_mode))
322 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
324 return copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st);
329 svr4_sys_lstat64(struct lwp *l, const struct svr4_sys_lstat64_args *uap, register_t *retval)
331 struct stat st;
332 struct svr4_stat64 svr4_st;
333 int error;
335 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &st);
336 if (error != 0)
337 return error;
339 bsd_to_svr4_stat64(&st, &svr4_st);
341 if (S_ISSOCK(st.st_mode))
342 (void) svr4_add_socket(l->l_proc, SCARG(uap, path), &st);
344 return copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st);
349 svr4_sys_fstat64(struct lwp *l, const struct svr4_sys_fstat64_args *uap, register_t *retval)
351 struct svr4_stat64 svr4_st;
352 struct stat st;
353 int error;
355 error = do_sys_fstat(SCARG(uap, fd), &st);
356 if (error != 0)
357 return error;
359 bsd_to_svr4_stat64(&st, &svr4_st);
361 return copyout(&svr4_st, SCARG(uap, sb), sizeof svr4_st);
366 svr4_ustat(struct lwp *l, const struct svr4_ustat_args *uap, register_t *retval)
368 /* {
369 syscallarg(svr4_dev_t) dev;
370 syscallarg(struct svr4_ustat *) name;
371 } */
372 struct svr4_ustat us;
373 int error;
375 memset(&us, 0, sizeof us);
378 * XXX: should set f_tfree and f_tinode at least
379 * How do we translate dev -> fstat? (and then to svr4_ustat)
381 if ((error = copyout(&us, SCARG(uap, name), sizeof us)) != 0)
382 return (error);
384 return 0;
390 svr4_sys_uname(struct lwp *l, const struct svr4_sys_uname_args *uap, register_t *retval)
392 struct svr4_utsname *sut;
393 int error;
395 sut = malloc(sizeof(*sut), M_TEMP, M_WAITOK|M_ZERO);
397 (void)strncpy(sut->sysname, ostype, sizeof(sut->sysname));
398 sut->sysname[sizeof(sut->sysname) - 1] = '\0';
400 (void)strncpy(sut->nodename, hostname, sizeof(sut->nodename));
401 sut->nodename[sizeof(sut->nodename) - 1] = '\0';
403 (void)strncpy(sut->release, osrelease, sizeof(sut->release));
404 sut->release[sizeof(sut->release) - 1] = '\0';
406 (void)strncpy(sut->version, version, sizeof(sut->version));
407 sut->version[sizeof(sut->version) - 1] = '\0';
409 (void)strncpy(sut->machine, machine, sizeof(sut->machine));
410 sut->machine[sizeof(sut->machine) - 1] = '\0';
412 error = copyout(sut, SCARG(uap, name), sizeof(*sut));
413 free(sut, M_TEMP);
414 return error;
419 svr4_sys_systeminfo(struct lwp *l, const struct svr4_sys_systeminfo_args *uap, register_t *retval)
421 const char *str = NULL;
422 int name[2];
423 int error;
424 size_t len;
425 char buf[256];
427 u_int rlen = SCARG(uap, len);
429 switch (SCARG(uap, what)) {
430 case SVR4_SI_SYSNAME:
431 str = ostype;
432 break;
434 case SVR4_SI_HOSTNAME:
435 str = hostname;
436 break;
438 case SVR4_SI_RELEASE:
439 str = osrelease;
440 break;
442 case SVR4_SI_VERSION:
443 str = version;
444 break;
446 case SVR4_SI_MACHINE:
447 str = machine;
448 break;
450 case SVR4_SI_ARCHITECTURE:
451 str = machine_arch;
452 break;
454 case SVR4_SI_ISALIST:
455 #if defined(__sparc__)
456 str = "sparcv9 sparcv9-fsmuld sparcv8 sparcv8-fsmuld sparcv7 sparc";
457 #elif defined(__i386__)
458 str = "i386";
459 #else
460 str = "unknown";
461 #endif
462 break;
464 case SVR4_SI_HW_SERIAL:
465 snprintf(buf, sizeof(buf), "%lu", hostid);
466 str = buf;
467 break;
469 case SVR4_SI_HW_PROVIDER:
470 str = ostype;
471 break;
473 case SVR4_SI_SRPC_DOMAIN:
474 str = domainname;
475 break;
477 case SVR4_SI_PLATFORM:
478 #if defined(__i386__)
479 str = "i86pc";
480 #elif defined(__sparc__)
481 #elif defined(__sparc__)
483 extern char machine_model[];
485 str = machine_model;
487 #else
488 str = "unknown";
489 #endif
490 break;
492 case SVR4_SI_KERB_REALM:
493 str = "unsupported";
494 break;
496 case SVR4_SI_SET_HOSTNAME:
497 name[1] = KERN_HOSTNAME;
498 break;
500 case SVR4_SI_SET_SRPC_DOMAIN:
501 name[1] = KERN_DOMAINNAME;
502 break;
504 case SVR4_SI_SET_KERB_REALM:
505 return 0;
507 default:
508 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap, what)));
509 return ENOSYS;
512 if (str) {
513 len = strlen(str) + 1;
514 if (len < rlen)
515 rlen = len;
517 if (SCARG(uap, buf)) {
518 error = copyout(str, SCARG(uap, buf), rlen);
519 if (error)
520 return error;
521 if (rlen > 0) {
522 /* make sure we are NULL terminated */
523 buf[0] = '\0';
524 error = copyout(buf,
525 &(SCARG(uap, buf)[rlen - 1]), 1);
528 else
529 error = 0;
531 else {
533 * looks redundant to do this, but actually it retrieves "len"
535 error = copyinstr(SCARG(uap, buf), buf, sizeof(buf), &len);
536 if (error)
537 return error;
538 name[0] = CTL_KERN;
539 error = old_sysctl(&name[0], 2, 0, 0, SCARG(uap, buf), len, l);
542 *retval = len;
543 return error;
548 svr4_sys_utssys(struct lwp *l, const struct svr4_sys_utssys_args *uap, register_t *retval)
551 switch (SCARG(uap, sel)) {
552 case 0: /* uname(2) */
554 struct svr4_sys_uname_args ua;
555 SCARG(&ua, name) = SCARG(uap, a1);
556 return svr4_sys_uname(l, &ua, retval);
559 case 2: /* ustat(2) */
561 struct svr4_ustat_args ua;
562 SCARG(&ua, dev) = (svr4_dev_t) SCARG(uap, a2);
563 SCARG(&ua, name) = SCARG(uap, a1);
564 return svr4_ustat(l, &ua, retval);
567 case 3: /* fusers(2) */
568 return ENOSYS;
570 default:
571 return ENOSYS;
577 svr4_sys_utime(struct lwp *l, const struct svr4_sys_utime_args *uap, register_t *retval)
579 struct svr4_utimbuf ub;
580 struct timeval tbuf[2], *tvp;
581 int error;
583 if (SCARG(uap, ubuf) != NULL) {
584 if ((error = copyin(SCARG(uap, ubuf), &ub, sizeof(ub))) != 0)
585 return error;
586 tbuf[0].tv_sec = ub.actime;
587 tbuf[0].tv_usec = 0;
588 tbuf[1].tv_sec = ub.modtime;
589 tbuf[1].tv_usec = 0;
590 tvp = tbuf;
591 } else
592 tvp = NULL;
594 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
595 tvp, UIO_SYSSPACE);
600 svr4_sys_utimes(struct lwp *l, const struct svr4_sys_utimes_args *uap, register_t *retval)
602 return compat_50_sys_utimes(l, (const void *)uap, retval);
606 static int
607 svr4_to_bsd_pathconf(int name)
609 switch (name) {
610 case SVR4_PC_LINK_MAX:
611 return _PC_LINK_MAX;
613 case SVR4_PC_MAX_CANON:
614 return _PC_MAX_CANON;
616 case SVR4_PC_MAX_INPUT:
617 return _PC_MAX_INPUT;
619 case SVR4_PC_NAME_MAX:
620 return _PC_NAME_MAX;
622 case SVR4_PC_PATH_MAX:
623 return _PC_PATH_MAX;
625 case SVR4_PC_PIPE_BUF:
626 return _PC_PIPE_BUF;
628 case SVR4_PC_NO_TRUNC:
629 return _PC_NO_TRUNC;
631 case SVR4_PC_VDISABLE:
632 return _PC_VDISABLE;
634 case SVR4_PC_CHOWN_RESTRICTED:
635 return _PC_CHOWN_RESTRICTED;
637 case SVR4_PC_SYNC_IO:
638 return _PC_SYNC_IO;
640 case SVR4_PC_FILESIZEBITS:
641 return _PC_FILESIZEBITS;
643 case SVR4_PC_ASYNC_IO:
644 case SVR4_PC_PRIO_IO:
645 /* Not supported */
646 return 0;
648 default:
649 /* Invalid */
650 return -1;
656 svr4_sys_pathconf(struct lwp *l, const struct svr4_sys_pathconf_args *uap, register_t *retval)
658 struct sys_pathconf_args bsd_ua;
660 SCARG(&bsd_ua, path) = SCARG(uap, path);
661 SCARG(&bsd_ua, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
663 switch (SCARG(&bsd_ua, name)) {
664 case -1:
665 *retval = -1;
666 return EINVAL;
667 case 0:
668 *retval = 0;
669 return 0;
670 default:
671 return sys_pathconf(l, &bsd_ua, retval);
677 svr4_sys_fpathconf(struct lwp *l, const struct svr4_sys_fpathconf_args *uap, register_t *retval)
679 struct sys_fpathconf_args bsd_ua;
681 SCARG(&bsd_ua, fd) = SCARG(uap, fd);
682 SCARG(&bsd_ua, name) = svr4_to_bsd_pathconf(SCARG(uap, name));
684 switch (SCARG(&bsd_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_fpathconf(l, &bsd_ua, retval);