]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/compat/svr4/svr4_stat.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / compat / svr4 / svr4_stat.c
1 /*-
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1994 Christos Zoulas
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/stat.h>
36 #include <sys/filedesc.h>
37 #include <sys/jail.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/unistd.h>
41 #include <sys/time.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysctl.h>
44 #include <sys/sysproto.h>
45 #include <sys/un.h>
46
47 #include <vm/vm.h>
48
49 #include <netinet/in.h>
50
51 #include <compat/svr4/svr4.h>
52 #include <compat/svr4/svr4_types.h>
53 #include <compat/svr4/svr4_signal.h>
54 #include <compat/svr4/svr4_proto.h>
55 #include <compat/svr4/svr4_util.h>
56 #include <compat/svr4/svr4_stat.h>
57 #include <compat/svr4/svr4_ustat.h>
58 #include <compat/svr4/svr4_utsname.h>
59 #include <compat/svr4/svr4_systeminfo.h>
60 #include <compat/svr4/svr4_socket.h>
61 #include <compat/svr4/svr4_time.h>
62 #if defined(NOTYET)
63 #include "svr4_fuser.h"
64 #endif
65
66 #ifdef sparc
67 /* 
68  * Solaris-2.4 on the sparc has the old stat call using the new
69  * stat data structure...
70  */
71 # define SVR4_NO_OSTAT
72 #endif
73
74 struct svr4_ustat_args {
75         svr4_dev_t              dev;
76         struct svr4_ustat * name;
77 };
78
79 static void bsd_to_svr4_xstat(struct stat *, struct svr4_xstat *);
80 static void bsd_to_svr4_stat64(struct stat *, struct svr4_stat64 *);
81 int svr4_ustat(struct thread *, struct svr4_ustat_args *);
82 static int svr4_to_bsd_pathconf(int);
83
84 /*
85  * SVR4 uses named pipes as named sockets, so we tell programs
86  * that sockets are named pipes with mode 0
87  */
88 #define BSD_TO_SVR4_MODE(mode) (S_ISSOCK(mode) ? S_IFIFO : (mode))
89
90
91 #ifndef SVR4_NO_OSTAT
92 static void bsd_to_svr4_stat(struct stat *, struct svr4_stat *);
93
94 static void
95 bsd_to_svr4_stat(st, st4)
96         struct stat             *st;
97         struct svr4_stat        *st4;
98 {
99         memset(st4, 0, sizeof(*st4));
100         st4->st_dev = bsd_to_svr4_odev_t(st->st_dev);
101         st4->st_ino = st->st_ino;
102         st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
103         st4->st_nlink = st->st_nlink;
104         st4->st_uid = st->st_uid;
105         st4->st_gid = st->st_gid;
106         st4->st_rdev = bsd_to_svr4_odev_t(st->st_rdev);
107         st4->st_size = st->st_size;
108         st4->st_atim = st->st_atimespec.tv_sec;
109         st4->st_mtim = st->st_mtimespec.tv_sec;
110         st4->st_ctim = st->st_ctimespec.tv_sec;
111 }
112 #endif
113
114
115 static void
116 bsd_to_svr4_xstat(st, st4)
117         struct stat             *st;
118         struct svr4_xstat       *st4;
119 {
120         memset(st4, 0, sizeof(*st4));
121         st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
122         st4->st_ino = st->st_ino;
123         st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
124         st4->st_nlink = st->st_nlink;
125         st4->st_uid = st->st_uid;
126         st4->st_gid = st->st_gid;
127         st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
128         st4->st_size = st->st_size;
129         st4->st_atim = st->st_atimespec;
130         st4->st_mtim = st->st_mtimespec;
131         st4->st_ctim = st->st_ctimespec;
132         st4->st_blksize = st->st_blksize;
133         st4->st_blocks = st->st_blocks;
134         strcpy(st4->st_fstype, "unknown");
135 }
136
137
138 static void
139 bsd_to_svr4_stat64(st, st4)
140         struct stat             *st;
141         struct svr4_stat64      *st4;
142 {
143         memset(st4, 0, sizeof(*st4));
144         st4->st_dev = bsd_to_svr4_dev_t(st->st_dev);
145         st4->st_ino = st->st_ino;
146         st4->st_mode = BSD_TO_SVR4_MODE(st->st_mode);
147         st4->st_nlink = st->st_nlink;
148         st4->st_uid = st->st_uid;
149         st4->st_gid = st->st_gid;
150         st4->st_rdev = bsd_to_svr4_dev_t(st->st_rdev);
151         st4->st_size = st->st_size;
152         st4->st_atim = st->st_atimespec;
153         st4->st_mtim = st->st_mtimespec;
154         st4->st_ctim = st->st_ctimespec;
155         st4->st_blksize = st->st_blksize;
156         st4->st_blocks = st->st_blocks;
157         strcpy(st4->st_fstype, "unknown");
158 }
159
160 int
161 svr4_sys_stat(td, uap)
162         struct thread *td;
163         struct svr4_sys_stat_args *uap;
164 {
165         struct svr4_stat svr4_st;
166         struct stat st;
167         char *path;
168         int error;
169
170         CHECKALTEXIST(td, uap->path, &path);
171
172         error = kern_stat(td, path, UIO_SYSSPACE, &st);
173         free(path, M_TEMP);
174         if (error)
175                 return (error);
176         bsd_to_svr4_stat(&st, &svr4_st);
177
178         if (S_ISSOCK(st.st_mode))
179                 (void) svr4_add_socket(td, uap->path, &st);
180
181         return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
182 }
183
184
185 int
186 svr4_sys_lstat(td, uap)
187         register struct thread *td;
188         struct svr4_sys_lstat_args *uap;
189 {
190         struct svr4_stat svr4_st;
191         struct stat st;
192         char *path;
193         int error;
194
195         CHECKALTEXIST(td, uap->path, &path);
196
197         error = kern_lstat(td, path, UIO_SYSSPACE, &st);
198         free(path, M_TEMP);
199         if (error)
200                 return (error);
201         bsd_to_svr4_stat(&st, &svr4_st);
202
203         if (S_ISSOCK(st.st_mode))
204                 (void) svr4_add_socket(td, uap->path, &st);
205
206         return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
207 }
208
209
210 int
211 svr4_sys_fstat(td, uap)
212         register struct thread *td;
213         struct svr4_sys_fstat_args *uap;
214 {
215         struct svr4_stat svr4_st;
216         struct stat st;
217         int error;
218
219
220         error = kern_fstat(td, uap->fd, &st);
221         if (error)
222                 return (error);
223         bsd_to_svr4_stat(&st, &svr4_st);
224         return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
225 }
226
227
228 int
229 svr4_sys_xstat(td, uap)
230         register struct thread *td;
231         struct svr4_sys_xstat_args *uap;
232 {
233         struct svr4_xstat svr4_st;
234         struct stat st;
235         char *path;
236         int error;
237
238         CHECKALTEXIST(td, uap->path, &path);
239
240         error = kern_stat(td, path, UIO_SYSSPACE, &st);
241         free(path, M_TEMP);
242         if (error)
243                 return (error);
244
245         bsd_to_svr4_xstat(&st, &svr4_st);
246
247 #if defined(SOCKET_NOTYET)
248         if (S_ISSOCK(st.st_mode))
249                 (void) svr4_add_socket(td, uap->path, &st);
250 #endif
251
252         return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
253 }
254
255 int
256 svr4_sys_lxstat(td, uap)
257         register struct thread *td;
258         struct svr4_sys_lxstat_args *uap;
259 {
260         struct svr4_xstat svr4_st;
261         struct stat st;
262         char *path;
263         int error;
264
265         CHECKALTEXIST(td, uap->path, &path);
266
267         error = kern_lstat(td, path, UIO_SYSSPACE, &st);
268         free(path, M_TEMP);
269         if (error)
270                 return (error);
271
272         bsd_to_svr4_xstat(&st, &svr4_st);
273
274 #if defined(SOCKET_NOTYET)
275         if (S_ISSOCK(st.st_mode))
276                 (void) svr4_add_socket(td, uap->path, &st);
277 #endif
278         return (copyout(&svr4_st, uap->ub, sizeof svr4_st));
279 }
280
281
282 int
283 svr4_sys_fxstat(td, uap)
284         register struct thread *td;
285         struct svr4_sys_fxstat_args *uap;
286 {
287         struct svr4_xstat svr4_st;
288         struct stat st;
289         int error;
290
291
292         error = kern_fstat(td, uap->fd, &st);
293         if (error)
294                 return (error);
295         bsd_to_svr4_xstat(&st, &svr4_st);
296         return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
297 }
298
299 int
300 svr4_sys_stat64(td, uap)
301         register struct thread *td;
302         struct svr4_sys_stat64_args *uap;
303 {
304         struct svr4_stat64 svr4_st;
305         struct stat st;
306         char *path;
307         int error;
308
309         CHECKALTEXIST(td, uap->path, &path);
310
311         error = kern_stat(td, path, UIO_SYSSPACE, &st);
312         free(path, M_TEMP);
313         if (error)
314                 return (error);
315
316         bsd_to_svr4_stat64(&st, &svr4_st);
317
318         if (S_ISSOCK(st.st_mode))
319                 (void) svr4_add_socket(td, uap->path, &st);
320
321         return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
322 }
323
324
325 int
326 svr4_sys_lstat64(td, uap)
327         register struct thread *td;
328         struct svr4_sys_lstat64_args *uap;
329 {
330         struct svr4_stat64 svr4_st;
331         struct stat st;
332         char *path;
333         int error;
334
335         CHECKALTEXIST(td, uap->path, &path);
336
337         error = kern_lstat(td, path, UIO_SYSSPACE, &st);
338         free(path, M_TEMP);
339         if (error)
340                 return (error);
341
342         bsd_to_svr4_stat64(&st, &svr4_st);
343
344         if (S_ISSOCK(st.st_mode))
345                 (void) svr4_add_socket(td, uap->path, &st);
346
347         return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
348 }
349
350
351 int
352 svr4_sys_fstat64(td, uap)
353         register struct thread *td;
354         struct svr4_sys_fstat64_args *uap;
355 {
356         struct svr4_stat64 svr4_st;
357         struct stat st;
358         int error;
359
360         error = kern_fstat(td, uap->fd, &st);
361         if (error)
362                 return (error);
363         bsd_to_svr4_stat64(&st, &svr4_st);
364         return (copyout(&svr4_st, uap->sb, sizeof svr4_st));
365 }
366
367
368 int
369 svr4_ustat(td, uap)
370         register struct thread *td;
371         struct svr4_ustat_args *uap;
372 {
373         struct svr4_ustat       us;
374         int                     error;
375
376         memset(&us, 0, sizeof us);
377
378         /*
379          * XXX: should set f_tfree and f_tinode at least
380          * How do we translate dev -> fstat? (and then to svr4_ustat)
381          */
382         if ((error = copyout(&us, uap->name, sizeof us)) != 0)
383                 return (error);
384
385         return 0;
386 }
387
388 /*extern char ostype[], hostname[], osrelease[], version[], machine[];*/
389
390 int
391 svr4_sys_uname(td, uap)
392         register struct thread *td;
393         struct svr4_sys_uname_args *uap;
394 {
395         struct svr4_utsname     sut;
396         
397         memset(&sut, 0, sizeof(sut));
398
399         strlcpy(sut.sysname, ostype, sizeof(sut.sysname));
400         getcredhostname(td->td_ucred, sut.nodename, sizeof(sut.nodename));
401         strlcpy(sut.release, osrelease, sizeof(sut.release));
402         strlcpy(sut.version, version, sizeof(sut.version));
403         strlcpy(sut.machine, machine, sizeof(sut.machine));
404
405         return copyout((caddr_t) &sut, (caddr_t) uap->name,
406                        sizeof(struct svr4_utsname));
407 }
408
409 int
410 svr4_sys_systeminfo(td, uap)
411         struct thread *td;
412         struct svr4_sys_systeminfo_args *uap;
413 {
414         char            *str = NULL;
415         int             error = 0;
416         register_t      *retval = td->td_retval;
417         size_t          len = 0;
418         char            buf[1];   /* XXX NetBSD uses 256, but that seems
419                                      like awfully excessive kstack usage
420                                      for an empty string... */
421         u_int           rlen = uap->len;
422
423         switch (uap->what) {
424         case SVR4_SI_SYSNAME:
425                 str = ostype;
426                 break;
427
428         case SVR4_SI_HOSTNAME:
429                 str = hostname;
430                 break;
431
432         case SVR4_SI_RELEASE:
433                 str = osrelease;
434                 break;
435
436         case SVR4_SI_VERSION:
437                 str = version;
438                 break;
439
440         case SVR4_SI_MACHINE:
441                 str = machine;
442                 break;
443
444         case SVR4_SI_ARCHITECTURE:
445                 str = machine;
446                 break;
447
448         case SVR4_SI_HW_SERIAL:
449                 str = "0";
450                 break;
451
452         case SVR4_SI_HW_PROVIDER:
453                 str = ostype;
454                 break;
455
456         case SVR4_SI_SRPC_DOMAIN:
457                 str = domainname;
458                 break;
459
460         case SVR4_SI_PLATFORM:
461 #ifdef __i386__
462                 str = "i86pc";
463 #else
464                 str = "unknown";
465 #endif
466                 break;
467
468         case SVR4_SI_KERB_REALM:
469                 str = "unsupported";
470                 break;
471 #if defined(WHY_DOES_AN_EMULATOR_WANT_TO_SET_HOSTNAMES)
472         case SVR4_SI_SET_HOSTNAME:
473                 name = KERN_HOSTNAME;
474                 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
475
476         case SVR4_SI_SET_SRPC_DOMAIN:
477                 name = KERN_NISDOMAINNAME;
478                 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
479 #else
480         case SVR4_SI_SET_HOSTNAME:
481         case SVR4_SI_SET_SRPC_DOMAIN:
482                 /* FALLTHROUGH */
483 #endif
484         case SVR4_SI_SET_KERB_REALM:
485                 return 0;
486
487         default:
488                 DPRINTF(("Bad systeminfo command %d\n", uap->what));
489                 return ENOSYS;
490         }
491
492         if (str) {
493                 len = strlen(str) + 1;
494                 if (len > rlen)
495                         len = rlen;
496
497                 if (uap->buf) {
498                         error = copyout(str, uap->buf, len);
499                         if (error)
500                                 return error;
501                         /* make sure we are NULL terminated */
502                         buf[0] = '\0';
503                         error = copyout(buf, &(uap->buf[len - 1]), 1);
504                 }
505                 else
506                         error = 0;
507         }
508         /* XXX NetBSD has hostname setting stuff here.  Why would an emulator
509            want to do that? */
510
511         *retval = len;
512         return error;
513 }
514
515 int
516 svr4_sys_utssys(td, uap)
517         register struct thread *td;
518         struct svr4_sys_utssys_args *uap;
519 {
520         switch (uap->sel) {
521         case 0:         /* uname(2)  */
522                 {
523                         struct svr4_sys_uname_args ua;
524                         ua.name = uap->a1;
525                         return svr4_sys_uname(td, &ua);
526                 }
527
528         case 2:         /* ustat(2)  */
529                 {
530                         struct svr4_ustat_args ua;
531                         ua.dev = (svr4_dev_t) uap->a2;
532                         ua.name = uap->a1;
533                         return svr4_ustat(td, &ua);
534                 }
535
536         case 3:         /* fusers(2) */
537                 return ENOSYS;
538
539         default:
540                 return ENOSYS;
541         }
542         return ENOSYS;
543 }
544
545
546 int
547 svr4_sys_utime(td, uap)
548         register struct thread *td;
549         struct svr4_sys_utime_args *uap;
550 {
551         struct svr4_utimbuf ub;
552         struct timeval tbuf[2], *tp;
553         char *path;
554         int error;
555      
556         if (uap->ubuf != NULL) {
557                 error = copyin(uap->ubuf, &ub, sizeof(ub));
558                 if (error)
559                         return (error);
560                 tbuf[0].tv_sec = ub.actime;
561                 tbuf[0].tv_usec = 0;
562                 tbuf[1].tv_sec = ub.modtime;
563                 tbuf[1].tv_usec = 0;
564                 tp = tbuf;
565         } else
566                 tp = NULL;
567
568         CHECKALTEXIST(td, uap->path, &path);
569         error = kern_utimes(td, path, UIO_SYSSPACE, tp, UIO_SYSSPACE);
570         free(path, M_TEMP);
571         return (error);
572 }
573
574
575 int
576 svr4_sys_utimes(td, uap)
577         register struct thread *td;
578         struct svr4_sys_utimes_args *uap;
579 {
580         char *path;
581         int error;
582
583         CHECKALTEXIST(td, uap->path, &path);
584         error = kern_utimes(td, path, UIO_SYSSPACE, uap->tptr, UIO_USERSPACE);
585         free(path, M_TEMP);
586         return (error);
587 }
588
589 static int
590 svr4_to_bsd_pathconf(name)
591         int name;
592 {
593         switch (name) {
594         case SVR4_PC_LINK_MAX:
595                 return _PC_LINK_MAX;
596
597         case SVR4_PC_MAX_CANON:
598                 return _PC_MAX_CANON;
599
600         case SVR4_PC_MAX_INPUT:
601                 return _PC_MAX_INPUT;
602
603         case SVR4_PC_NAME_MAX:
604                 return _PC_NAME_MAX;
605
606         case SVR4_PC_PATH_MAX:
607                 return _PC_PATH_MAX;
608
609         case SVR4_PC_PIPE_BUF:
610                 return _PC_PIPE_BUF;
611
612         case SVR4_PC_NO_TRUNC:
613                 return _PC_NO_TRUNC;
614
615         case SVR4_PC_VDISABLE:
616                 return _PC_VDISABLE;
617
618         case SVR4_PC_CHOWN_RESTRICTED:
619                 return _PC_CHOWN_RESTRICTED;
620         case SVR4_PC_SYNC_IO:
621 #if defined(_PC_SYNC_IO)
622                 return _PC_SYNC_IO;
623 #else
624                 return 0;
625 #endif
626         case SVR4_PC_ASYNC_IO:
627         case SVR4_PC_PRIO_IO:
628                 /* Not supported */
629                 return 0;
630
631         default:
632                 /* Invalid */
633                 return -1;
634         }
635 }
636
637
638 int
639 svr4_sys_pathconf(td, uap)
640         register struct thread *td;
641         struct svr4_sys_pathconf_args *uap;
642 {
643         char *path;
644         int error, name;
645
646         name = svr4_to_bsd_pathconf(uap->name);
647
648         switch (name) {
649         case -1:
650                 td->td_retval[0] = -1;
651                 return (EINVAL);
652         case 0:
653                 td->td_retval[0] = 0;
654                 return (0);
655         default:
656                 CHECKALTEXIST(td, uap->path, &path);
657                 error = kern_pathconf(td, path, UIO_SYSSPACE, name);
658                 free(path, M_TEMP);
659                 return (error);
660         }
661 }
662
663
664 int
665 svr4_sys_fpathconf(td, uap)
666         register struct thread *td;
667         struct svr4_sys_fpathconf_args *uap;
668 {
669         register_t      *retval = td->td_retval;
670
671         uap->name = svr4_to_bsd_pathconf(uap->name);
672
673         switch (uap->name) {
674         case -1:
675                 *retval = -1;
676                 return EINVAL;
677         case 0:
678                 *retval = 0;
679                 return 0;
680         default:
681                 return fpathconf(td, (struct fpathconf_args *)uap);
682         }
683 }