1 /* $NetBSD: svr4_32_stat.c,v 1.35 2008/04/28 20:23:46 martin 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_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>
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>
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
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))
92 static void bsd_to_svr4_32_stat(struct stat
*, struct svr4_32_stat
*);
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
;
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
));
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
)
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
);
172 struct svr4_32_stat svr4_st
;
175 error
= do_sys_stat(SCARG(&cup
, path
), FOLLOW
, &st
);
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
),
191 svr4_32_sys_lstat(struct lwp
*l
, const struct svr4_32_sys_lstat_args
*uap
, register_t
*retval
)
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
);
202 struct svr4_32_stat svr4_st
;
205 error
= do_sys_stat(SCARG(&cup
, path
), NOFOLLOW
, &st
);
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
),
221 svr4_32_sys_fstat(struct lwp
*l
, const struct svr4_32_sys_fstat_args
*uap
, register_t
*retval
)
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
);
232 struct svr4_32_stat svr4_st
;
235 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
239 bsd_to_svr4_32_stat(&st
, &svr4_st
);
241 return copyout(&svr4_st
, SCARG_P32(uap
, ub
),
248 svr4_32_sys_xstat(struct lwp
*l
, const struct svr4_32_sys_xstat_args
*uap
, register_t
*retval
)
251 struct svr4_32_xstat svr4_st
;
253 const char *path
= SCARG_P32(uap
, path
);
255 error
= do_sys_stat(path
, FOLLOW
, &st
);
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
),
270 svr4_32_sys_lxstat(struct lwp
*l
, const struct svr4_32_sys_lxstat_args
*uap
, register_t
*retval
)
273 struct svr4_32_xstat svr4_st
;
275 const char *path
= SCARG_P32(uap
, path
);
277 error
= do_sys_stat(path
, NOFOLLOW
, &st
);
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
),
292 svr4_32_sys_fxstat(struct lwp
*l
, const struct svr4_32_sys_fxstat_args
*uap
, register_t
*retval
)
295 struct svr4_32_xstat svr4_st
;
298 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
302 bsd_to_svr4_32_xstat(&st
, &svr4_st
);
304 return copyout(&svr4_st
, SCARG_P32(uap
, sb
),
310 svr4_32_sys_stat64(struct lwp
*l
, const struct svr4_32_sys_stat64_args
*uap
, register_t
*retval
)
313 struct svr4_32_stat64 svr4_st
;
315 const char *path
= SCARG_P32(uap
, path
);
317 error
= do_sys_stat(path
, FOLLOW
, &st
);
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
),
332 svr4_32_sys_lstat64(struct lwp
*l
, const struct svr4_32_sys_lstat64_args
*uap
, register_t
*retval
)
335 struct svr4_32_stat64 svr4_st
;
337 const char *path
= SCARG_P32(uap
, path
);
339 error
= do_sys_stat(path
, NOFOLLOW
, &st
);
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
),
354 svr4_32_sys_fstat64(struct lwp
*l
, const struct svr4_32_sys_fstat64_args
*uap
, register_t
*retval
)
357 struct svr4_32_stat64 svr4_st
;
360 error
= do_sys_fstat(SCARG(uap
, fd
), &st
);
364 bsd_to_svr4_32_stat64(&st
, &svr4_st
);
366 return copyout(&svr4_st
, SCARG_P32(uap
, sb
),
371 struct svr4_32_ustat_args
{
372 syscallarg(svr4_dev_t
) dev
;
373 syscallarg(svr4_32_ustatp
) name
;
377 svr4_32_ustat(struct lwp
*l
, const struct svr4_32_ustat_args
*uap
, register_t
*retval
)
380 syscallarg(svr4_dev_t) dev;
381 syscallarg(svr4_32_ustatp) name;
383 struct svr4_32_ustat us
;
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
),
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
;
437 u_int rlen
= SCARG(uap
, len
);
439 switch (SCARG(uap
, what
)) {
440 case SVR4_SI_SYSNAME
:
444 case SVR4_SI_HOSTNAME
:
448 case SVR4_SI_RELEASE
:
452 case SVR4_SI_VERSION
:
456 case SVR4_SI_MACHINE
:
457 str
= "sun4m"; /* Lie, pretend we are 4m */
460 case SVR4_SI_ARCHITECTURE
:
461 #if defined(__sparc__)
468 case SVR4_SI_ISALIST
:
469 #if defined(__sparc__)
470 str
= "sparcv8 sparcv8-fsmuld sparcv7 sparc";
471 #elif defined(__i386__)
478 case SVR4_SI_HW_SERIAL
:
479 snprintf(buf
, sizeof(buf
), "%lu", hostid
);
483 case SVR4_SI_HW_PROVIDER
:
487 case SVR4_SI_SRPC_DOMAIN
:
491 case SVR4_SI_PLATFORM
:
492 #if defined(__i386__)
494 #elif defined(__sparc__)
496 extern char machine_model
[];
505 case SVR4_SI_KERB_REALM
:
509 case SVR4_SI_SET_HOSTNAME
:
510 name
[1] = KERN_HOSTNAME
;
513 case SVR4_SI_SET_SRPC_DOMAIN
:
514 name
[1] = KERN_DOMAINNAME
;
517 case SVR4_SI_SET_KERB_REALM
:
521 DPRINTF(("Bad systeminfo command %d\n", SCARG(uap
, what
)));
526 len
= strlen(str
) + 1;
530 if (SCARG_P32(uap
, buf
)) {
531 error
= copyout(str
, SCARG_P32(uap
, buf
),
536 /* make sure we are NULL terminated */
538 error
= copyout(buf
, &(((char *)
539 SCARG_P32(uap
, buf
))[rlen
- 1]), 1);
546 error
= copyinstr(SCARG_P32(uap
, buf
), buf
,
551 error
= old_sysctl(&name
[0], 1, 0, 0, buf
, len
, NULL
);
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) */
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
;
596 if (SCARG_P32(uap
, ubuf
)) {
597 if ((error
= copyin(SCARG_P32(uap
, ubuf
),
598 &ub
, sizeof(ub
))) != 0)
600 tbuf
[0].tv_sec
= ub
.actime
;
602 tbuf
[1].tv_sec
= ub
.modtime
;
608 return do_sys_utimes(l
, NULL
, SCARG_P32(uap
, path
), FOLLOW
,
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
);
625 svr4_32_to_bsd_pathconf(int name
)
628 case SVR4_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
:
640 case SVR4_PC_PATH_MAX
:
643 case SVR4_PC_PIPE_BUF
:
646 case SVR4_PC_NO_TRUNC
:
649 case SVR4_PC_VDISABLE
:
652 case SVR4_PC_CHOWN_RESTRICTED
:
653 return _PC_CHOWN_RESTRICTED
;
655 case SVR4_PC_SYNC_IO
:
658 case SVR4_PC_FILESIZEBITS
:
659 return _PC_FILESIZEBITS
;
661 case SVR4_PC_ASYNC_IO
:
662 case SVR4_PC_PRIO_IO
:
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;
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
)) {
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
)) {
713 return sys_fpathconf(l
, &ua
, retval
);