Removed <lib>_pic.a file rules - libs are now built directly from shared
[unix-history] / sbin / newfs / mfs.c
CommitLineData
96df40f3
CR
1#ifdef MFS
2
3#include <stdlib.h>
4#include <unistd.h>
5#include <stdio.h>
6#include <sys/types.h>
7#include <sys/errno.h>
8#include <sys/signal.h>
9#include <sys/file.h>
10#include <sys/stat.h>
11#include <sys/mman.h>
12#include <sys/mount.h>
13
14
15
16static void
17sighandler()
18{
19 /*
20 * kernel notifies us that the FS has been mounted successfully.
21 */
22 exit(0);
23}
24
25
26void
27mfs_mount(addr, len, name, dir, flags)
28caddr_t addr;
29unsigned long len;
30char *name, *dir;
31int flags;
32{
33 struct mfs_args margs;
34 char nmbuf[16];
35 int pfeife[2];
36 char buf[1024];
37 int red;
38
39
40
41 signal(SIGUSR1, sighandler);
42 if (pipe(pfeife) == -1)
43 fatal("cannot create pipe: %s", strerror(errno));
44 switch (fork()) {
45 case -1:
46 fatal("cannot fork: %s", strerror(errno));
47 case 0:
48 /*
49 * child: disassociate from controlling terminal,
50 * and mount the filesystem.
51 */
52 dup2(pfeife[1], 2);
53 close(0);
54 close(1);
55 if (pfeife[0] != 2) close(pfeife[0]);
56 if (pfeife[1] != 2) close(pfeife[1]);
57 setsid();
58 (void)chdir("/");
59 if (name == 0) {
60 sprintf(nmbuf, "MFS:%d", getpid());
61 name = nmbuf;
62 }
63 margs.name = name;
64 margs.base = addr;
65 margs.size = len;
66 margs.flags = MFSMNT_SIGPPID;
67 if (mount(MOUNT_MFS, dir, flags, &margs) == -1)
68 fatal("mounting MFS: %s", strerror(errno));
69 default:
70 /*
71 * parent; if the mount system call fails, the
72 * child will write error messages to the pipe.
73 * We duplicate those messages to our stdout.
74 * If the mount succeedet, we will receive a SIGUSR1
75 * (and exit with status 0).
76 */
77 close(pfeife[1]);
78 while ((red = read(pfeife[0], buf, sizeof(buf))) > 0)
79 write(2, buf, red);
80 exit(1);
81 }
82 /* NOTREACHED */
83}
84
85
86caddr_t
87mfs_malloc(size)
88unsigned long size;
89{
90 caddr_t addr;
91
92 addr = mmap(0, size, PROT_READ | PROT_WRITE,
93 MAP_ANON | MAP_SHARED, -1, 0);
94 if (addr == (caddr_t)-1)
95 fatal("cannot allocate memory: %s", strerror(errno));
96 return(addr);
97}
98
99void
100mfs_mountfile(file, dir, flags)
101char *file, *dir;
102int flags;
103{
104 caddr_t addr;
105 int fd;
106 struct stat st;
107
108
109 fd = open(file, O_RDWR | O_EXLOCK);
110 if ((fd == -1) || (fstat(fd, &st) == -1))
111 fatal("%s: %s", file, strerror(errno));
112 addr = mmap(0, st.st_size, PROT_READ | PROT_WRITE,
113 MAP_FILE | MAP_SHARED, fd, 0);
114 if (addr == (caddr_t) -1)
115 fatal("cannot mmap file: %s", strerror(errno));
116 mfs_mount(addr, st.st_size, file, dir, flags);
117}
118
119#else
120caddr_t
121mfs_malloc()
122{
123 fatal("compiled without MFS support");
124 return(0);
125}
126void mfs_mount() {}
127void mfs_mountfile() {mfs_malloc();}
128#endif