1 /* $NetBSD: svr4_stat.c,v 1.67.2.2 2008/11/01 21:22:26 christos Exp $ */
4 * Copyright (c) 1994, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
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>
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>
73 * Solaris-2.4 on the sparc has the old stat call using the new
74 * stat data structure...
76 # define SVR4_NO_OSTAT
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))
98 static void bsd_to_svr4_stat(struct stat
*, struct svr4_stat
*);
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
;
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
));
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
)
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
);
179 struct svr4_stat svr4_st
;
182 error
= do_sys_stat(SCARG(uap
, path
), FOLLOW
, &st
);
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
);
197 svr4_sys_lstat(struct lwp
*l
, const struct svr4_sys_lstat_args
*uap
, register_t
*retval
)
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
);
208 struct svr4_stat svr4_st
;
211 error
= do_sys_stat(SCARG(uap
, path
), NOFOLLOW
, &st
);
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
);
226 svr4_sys_fstat(struct lwp
*l
, const struct svr4_sys_fstat_args
*uap
, register_t
*retval
)
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
);
237 struct svr4_stat svr4_st
;
240 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
244 bsd_to_svr4_stat(&st
, &svr4_st
);
246 return copyout(&svr4_st
, SCARG(uap
, sb
), sizeof svr4_st
);
252 svr4_sys_xstat(struct lwp
*l
, const struct svr4_sys_xstat_args
*uap
, register_t
*retval
)
255 struct svr4_xstat svr4_st
;
258 error
= do_sys_stat(SCARG(uap
, path
), FOLLOW
, &st
);
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
)
275 struct svr4_xstat svr4_st
;
278 error
= do_sys_stat(SCARG(uap
, path
), NOFOLLOW
, &st
);
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
)
295 struct svr4_xstat svr4_st
;
298 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
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
)
312 struct svr4_stat64 svr4_st
;
315 error
= do_sys_stat(SCARG(uap
, path
), FOLLOW
, &st
);
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
)
332 struct svr4_stat64 svr4_st
;
335 error
= do_sys_stat(SCARG(uap
, path
), NOFOLLOW
, &st
);
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
;
355 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
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
)
369 syscallarg(svr4_dev_t) dev;
370 syscallarg(struct svr4_ustat *) name;
372 struct svr4_ustat us
;
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)
390 svr4_sys_uname(struct lwp
*l
, const struct svr4_sys_uname_args
*uap
, register_t
*retval
)
392 struct svr4_utsname
*sut
;
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
));
419 svr4_sys_systeminfo(struct lwp
*l
, const struct svr4_sys_systeminfo_args
*uap
, register_t
*retval
)
421 const char *str
= NULL
;
427 u_int rlen
= SCARG(uap
, len
);
429 switch (SCARG(uap
, what
)) {
430 case SVR4_SI_SYSNAME
:
434 case SVR4_SI_HOSTNAME
:
438 case SVR4_SI_RELEASE
:
442 case SVR4_SI_VERSION
:
446 case SVR4_SI_MACHINE
:
450 case SVR4_SI_ARCHITECTURE
:
454 case SVR4_SI_ISALIST
:
455 #if defined(__sparc__)
456 str
= "sparcv9 sparcv9-fsmuld sparcv8 sparcv8-fsmuld sparcv7 sparc";
457 #elif defined(__i386__)
464 case SVR4_SI_HW_SERIAL
:
465 snprintf(buf
, sizeof(buf
), "%lu", hostid
);
469 case SVR4_SI_HW_PROVIDER
:
473 case SVR4_SI_SRPC_DOMAIN
:
477 case SVR4_SI_PLATFORM
:
478 #if defined(__i386__)
480 #elif defined(__sparc__)
481 #elif defined(__sparc__)
483 extern char machine_model
[];
492 case SVR4_SI_KERB_REALM
:
496 case SVR4_SI_SET_HOSTNAME
:
497 name
[1] = KERN_HOSTNAME
;
500 case SVR4_SI_SET_SRPC_DOMAIN
:
501 name
[1] = KERN_DOMAINNAME
;
504 case SVR4_SI_SET_KERB_REALM
:
508 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap
, what
)));
513 len
= strlen(str
) + 1;
517 if (SCARG(uap
, buf
)) {
518 error
= copyout(str
, SCARG(uap
, buf
), rlen
);
522 /* make sure we are NULL terminated */
525 &(SCARG(uap
, buf
)[rlen
- 1]), 1);
533 * looks redundant to do this, but actually it retrieves "len"
535 error
= copyinstr(SCARG(uap
, buf
), buf
, sizeof(buf
), &len
);
539 error
= old_sysctl(&name
[0], 2, 0, 0, SCARG(uap
, buf
), len
, l
);
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) */
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
;
583 if (SCARG(uap
, ubuf
) != NULL
) {
584 if ((error
= copyin(SCARG(uap
, ubuf
), &ub
, sizeof(ub
))) != 0)
586 tbuf
[0].tv_sec
= ub
.actime
;
588 tbuf
[1].tv_sec
= ub
.modtime
;
594 return do_sys_utimes(l
, NULL
, SCARG(uap
, path
), FOLLOW
,
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
);
607 svr4_to_bsd_pathconf(int name
)
610 case SVR4_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
:
622 case SVR4_PC_PATH_MAX
:
625 case SVR4_PC_PIPE_BUF
:
628 case SVR4_PC_NO_TRUNC
:
631 case SVR4_PC_VDISABLE
:
634 case SVR4_PC_CHOWN_RESTRICTED
:
635 return _PC_CHOWN_RESTRICTED
;
637 case SVR4_PC_SYNC_IO
:
640 case SVR4_PC_FILESIZEBITS
:
641 return _PC_FILESIZEBITS
;
643 case SVR4_PC_ASYNC_IO
:
644 case SVR4_PC_PRIO_IO
:
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
)) {
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
)) {
692 return sys_fpathconf(l
, &bsd_ua
, retval
);