]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/svr4/svr4_stat.c
This commit was generated by cvs2svn to compensate for changes in r153877,
[FreeBSD/FreeBSD.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                 if ((error = suser(td)) != 0)
474                         return error;
475                 name = KERN_HOSTNAME;
476                 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
477
478         case SVR4_SI_SET_SRPC_DOMAIN:
479                 if ((error = suser(td)) != 0)
480                         return error;
481                 name = KERN_NISDOMAINNAME;
482                 return kern_sysctl(&name, 1, 0, 0, uap->buf, rlen, td);
483 #else
484         case SVR4_SI_SET_HOSTNAME:
485         case SVR4_SI_SET_SRPC_DOMAIN:
486                 /* FALLTHROUGH */
487 #endif
488         case SVR4_SI_SET_KERB_REALM:
489                 return 0;
490
491         default:
492                 DPRINTF(("Bad systeminfo command %d\n", uap->what));
493                 return ENOSYS;
494         }
495
496         if (str) {
497                 len = strlen(str) + 1;
498                 if (len > rlen)
499                         len = rlen;
500
501                 if (uap->buf) {
502                         error = copyout(str, uap->buf, len);
503                         if (error)
504                                 return error;
505                         /* make sure we are NULL terminated */
506                         buf[0] = '\0';
507                         error = copyout(buf, &(uap->buf[len - 1]), 1);
508                 }
509                 else
510                         error = 0;
511         }
512         /* XXX NetBSD has hostname setting stuff here.  Why would an emulator
513            want to do that? */
514
515         *retval = len;
516         return error;
517 }
518
519 int
520 svr4_sys_utssys(td, uap)
521         register struct thread *td;
522         struct svr4_sys_utssys_args *uap;
523 {
524         switch (uap->sel) {
525         case 0:         /* uname(2)  */
526                 {
527                         struct svr4_sys_uname_args ua;
528                         ua.name = uap->a1;
529                         return svr4_sys_uname(td, &ua);
530                 }
531
532         case 2:         /* ustat(2)  */
533                 {
534                         struct svr4_ustat_args ua;
535                         ua.dev = (svr4_dev_t) uap->a2;
536                         ua.name = uap->a1;
537                         return svr4_ustat(td, &ua);
538                 }
539
540         case 3:         /* fusers(2) */
541                 return ENOSYS;
542
543         default:
544                 return ENOSYS;
545         }
546         return ENOSYS;
547 }
548
549
550 int
551 svr4_sys_utime(td, uap)
552         register struct thread *td;
553         struct svr4_sys_utime_args *uap;
554 {
555         struct svr4_utimbuf ub;
556         struct timeval tbuf[2], *tp;
557         char *path;
558         int error;
559      
560         if (uap->ubuf != NULL) {
561                 error = copyin(uap->ubuf, &ub, sizeof(ub));
562                 if (error)
563                         return (error);
564                 tbuf[0].tv_sec = ub.actime;
565                 tbuf[0].tv_usec = 0;
566                 tbuf[1].tv_sec = ub.modtime;
567                 tbuf[1].tv_usec = 0;
568                 tp = tbuf;
569         } else
570                 tp = NULL;
571
572         CHECKALTEXIST(td, uap->path, &path);
573         error = kern_utimes(td, path, UIO_SYSSPACE, tp, UIO_SYSSPACE);
574         free(path, M_TEMP);
575         return (error);
576 }
577
578
579 int
580 svr4_sys_utimes(td, uap)
581         register struct thread *td;
582         struct svr4_sys_utimes_args *uap;
583 {
584         char *path;
585         int error;
586
587         CHECKALTEXIST(td, uap->path, &path);
588         error = kern_utimes(td, path, UIO_SYSSPACE, uap->tptr, UIO_USERSPACE);
589         free(path, M_TEMP);
590         return (error);
591 }
592
593 static int
594 svr4_to_bsd_pathconf(name)
595         int name;
596 {
597         switch (name) {
598         case SVR4_PC_LINK_MAX:
599                 return _PC_LINK_MAX;
600
601         case SVR4_PC_MAX_CANON:
602                 return _PC_MAX_CANON;
603
604         case SVR4_PC_MAX_INPUT:
605                 return _PC_MAX_INPUT;
606
607         case SVR4_PC_NAME_MAX:
608                 return _PC_NAME_MAX;
609
610         case SVR4_PC_PATH_MAX:
611                 return _PC_PATH_MAX;
612
613         case SVR4_PC_PIPE_BUF:
614                 return _PC_PIPE_BUF;
615
616         case SVR4_PC_NO_TRUNC:
617                 return _PC_NO_TRUNC;
618
619         case SVR4_PC_VDISABLE:
620                 return _PC_VDISABLE;
621
622         case SVR4_PC_CHOWN_RESTRICTED:
623                 return _PC_CHOWN_RESTRICTED;
624         case SVR4_PC_SYNC_IO:
625 #if defined(_PC_SYNC_IO)
626                 return _PC_SYNC_IO;
627 #else
628                 return 0;
629 #endif
630         case SVR4_PC_ASYNC_IO:
631         case SVR4_PC_PRIO_IO:
632                 /* Not supported */
633                 return 0;
634
635         default:
636                 /* Invalid */
637                 return -1;
638         }
639 }
640
641
642 int
643 svr4_sys_pathconf(td, uap)
644         register struct thread *td;
645         struct svr4_sys_pathconf_args *uap;
646 {
647         char *path;
648         int error, name;
649
650         name = svr4_to_bsd_pathconf(uap->name);
651
652         switch (name) {
653         case -1:
654                 td->td_retval[0] = -1;
655                 return (EINVAL);
656         case 0:
657                 td->td_retval[0] = 0;
658                 return (0);
659         default:
660                 CHECKALTEXIST(td, uap->path, &path);
661                 error = kern_pathconf(td, path, UIO_SYSSPACE, name);
662                 free(path, M_TEMP);
663                 return (error);
664         }
665 }
666
667
668 int
669 svr4_sys_fpathconf(td, uap)
670         register struct thread *td;
671         struct svr4_sys_fpathconf_args *uap;
672 {
673         register_t      *retval = td->td_retval;
674
675         uap->name = svr4_to_bsd_pathconf(uap->name);
676
677         switch (uap->name) {
678         case -1:
679                 *retval = -1;
680                 return EINVAL;
681         case 0:
682                 *retval = 0;
683                 return 0;
684         default:
685                 return fpathconf(td, (struct fpathconf_args *)uap);
686         }
687 }