Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/3d/fork3d.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1989-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Eduardo Krell <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
#include "3d.h"
25
26
#if FS
27
28
#include <cs.h>
29
30
/*
31
* called in the child process by fork()
32
*/
33
34
static void
35
forked(pid_t pid)
36
{
37
register int fd;
38
register Fs_t* fs;
39
register Mount_t* mp;
40
char* type;
41
long addr;
42
long port;
43
long clone;
44
int nfd;
45
pid_t ppid;
46
char* b;
47
48
reclaim();
49
ppid = state.pid;
50
state.pid = pid;
51
state.channel.internal = 0;
52
for (fs = state.fs; fs < state.fs + elementsof(state.fs); fs++)
53
{
54
if ((fs->flags & FS_FORK) && fs->set)
55
(*fs->set)(fs, state.null, 0, "fork", 4);
56
if ((fs->flags & (FS_ERROR|FS_ON|FS_OPEN|FS_UNIQUE)) == (FS_ON|FS_OPEN|FS_UNIQUE))
57
fsdrop(fs, 0);
58
}
59
for (mp = state.mount; mp < state.mount + elementsof(state.mount); mp++)
60
if (mp->fs)
61
switch (mp->fs->flags & (FS_ACTIVE|FS_ERROR|FS_GLOBAL|FS_MONITOR|FS_NAME|FS_ON))
62
{
63
case FS_ACTIVE|FS_ON:
64
if (!fscall(mp, MSG_INIT(MSG_fork, 01511, 0), ppid, state.path.name, sizeof(state.path.name)) && state.ret > 0)
65
{
66
type = b = state.path.name;
67
while (*b && *b != ' ') b++;
68
if (*b) *b++ = 0;
69
if (streq(type, "fdp")) fd = csrecv(&cs, mp->fs->fd, NiL, &nfd, 1) == 1 ? nfd : -1;
70
else
71
{
72
addr = strtol(b, &b, 0);
73
port = strtol(b, &b, 0);
74
clone = strtol(b, &b, 0);
75
fd = csbind(&cs, type, addr, port, clone);
76
}
77
if (fd >= 0)
78
{
79
FCNTL(fd, F_DUPFD, mp->fs->fd);
80
CLOSE(fd);
81
}
82
}
83
break;
84
case FS_ACTIVE|FS_GLOBAL|FS_MONITOR|FS_ON:
85
case FS_GLOBAL|FS_MONITOR|FS_ON:
86
case FS_ACTIVE|FS_MONITOR|FS_ON:
87
case FS_MONITOR|FS_ON:
88
if (fssys(mp, MSG_fork))
89
fscall(mp, MSG_fork, ppid);
90
break;
91
}
92
93
if (state.cache)
94
for (fd = 0; fd <= state.cache; fd++)
95
if ((mp = state.file[fd].mount) && !(mp->fs->flags & FS_ACTIVE) && fssys(mp, MSG_dup))
96
fscall(mp, MSG_dup, fd, fd);
97
}
98
99
#endif
100
101
#ifdef fork3d
102
103
pid_t
104
fork3d(void)
105
{
106
pid_t pid;
107
108
initialize();
109
if (!(pid = FORK()) || pid == state.pid || pid == getpid())
110
{
111
pid = 0;
112
#if FS
113
forked(getpid());
114
#else
115
state.pid = getpid();
116
#endif
117
}
118
#if FS
119
else
120
{
121
register Mount_t* mp;
122
123
for (mp = state.global; mp; mp = mp->global)
124
if (fssys(mp, MSG_fork))
125
fscall(mp, MSG_fork, pid);
126
}
127
#endif
128
return(pid);
129
}
130
131
#else
132
133
NoN(fork)
134
135
#endif
136
137