~ubuntu-branches/ubuntu/utopic/9base/utopic

« back to all changes in this revision

Viewing changes to rc/havefork.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-08-20 17:34:06 UTC
  • mfrom: (6.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090820173406-xpwqa9ruyevvc0ut
* Updating maintainer field.
* Updating vcs fields.
* Updating package to standards version 3.8.3.
* Updatin variables writing in rules to consistent style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <u.h>
2
2
#include <signal.h>
 
3
#if defined(PLAN9PORT) && defined(__sun__)
 
4
#       define BSD_COMP /* sigh.  for TIOCNOTTY */
 
5
#endif
 
6
#include <sys/ioctl.h>
3
7
#include "rc.h"
4
8
#include "getflags.h"
5
9
#include "exec.h"
12
16
Xasync(void)
13
17
{
14
18
        int null = open("/dev/null", 0);
 
19
        int tty;
15
20
        int pid;
16
 
        int tcpgrp, pgrp;
17
21
        char npid[10];
18
 
 
19
22
        if(null<0){
20
23
                Xerror("Can't open /dev/null\n");
21
24
                return;
26
29
                Xerror("try again");
27
30
                break;
28
31
        case 0:
 
32
                clearwaitpids();
29
33
                /*
30
 
                 * Should make reads of tty fail, writes succeed.
 
34
                 * I don't know what the right thing to do here is,
 
35
                 * so this is all experimentally determined.
 
36
                 * If we just dup /dev/null onto 0, then running
 
37
                 * ssh foo & will reopen /dev/tty, try to read a password,
 
38
                 * get a signal, and repeat, in a tight loop, forever.
 
39
                 * Arguably this is a bug in ssh (it behaves the same
 
40
                 * way under bash as under rc) but I'm fixing it here 
 
41
                 * anyway.  If we dissociate the process from the tty,
 
42
                 * then it won't be able to open /dev/tty ever again.
 
43
                 * The SIG_IGN on SIGTTOU makes writing the tty
 
44
                 * (via fd 1 or 2, for example) succeed even though 
 
45
                 * our pgrp is not the terminal's controlling pgrp.
31
46
                 */
32
 
                signal(SIGTTIN, SIG_IGN);
33
 
                signal(SIGTTOU, SIG_IGN);
34
 
 
35
 
                pushredir(ROPEN, null, 0);
 
47
                if((tty = open("/dev/tty", OREAD)) >= 0){
 
48
                        /*
 
49
                         * Should make reads of tty fail, writes succeed.
 
50
                         */
 
51
                        signal(SIGTTIN, SIG_IGN);
 
52
                        signal(SIGTTOU, SIG_IGN);
 
53
                        ioctl(tty, TIOCNOTTY);
 
54
                        close(tty);
 
55
                }
 
56
                if(isatty(0))
 
57
                        pushredir(ROPEN, null, 0);
 
58
                else
 
59
                        close(null);
36
60
                start(runq->code, runq->pc+1, runq->local);
37
61
                runq->ret = 0;
38
62
                break;
39
63
        default:
 
64
                addwaitpid(pid);
40
65
                close(null);
41
66
                runq->pc = runq->code[runq->pc].i;
42
67
                inttoascii(npid, pid);
62
87
                Xerror("try again");
63
88
                break;
64
89
        case 0:
 
90
                clearwaitpids();
65
91
                start(p->code, pc+2, runq->local);
66
92
                runq->ret = 0;
67
93
                close(pfd[PRD]);
68
94
                pushredir(ROPEN, pfd[PWR], lfd);
69
95
                break;
70
96
        default:
 
97
                addwaitpid(forkid);
71
98
                start(p->code, p->code[pc].i, runq->local);
72
99
                close(pfd[PWR]);
73
100
                pushredir(ROPEN, pfd[PRD], rfd);
103
130
                close(pfd[PWR]);
104
131
                return;
105
132
        case 0:
 
133
                clearwaitpids();
106
134
                close(pfd[PRD]);
107
135
                start(runq->code, runq->pc+1, runq->local);
108
136
                pushredir(ROPEN, pfd[PWR], 1);
109
137
                return;
110
138
        default:
 
139
                addwaitpid(pid);
111
140
                close(pfd[PWR]);
112
141
                f = openfd(pfd[PRD]);
113
142
                s = wd;
144
173
Xpipefd(void)
145
174
{
146
175
        struct thread *p = runq;
147
 
        int pc = p->pc;
 
176
        int pc = p->pc, pid;
148
177
        char name[40];
149
178
        int pfd[2];
150
179
        int sidefd, mainfd;
160
189
                sidefd = pfd[PRD];
161
190
                mainfd = pfd[PWR];
162
191
        }
163
 
        switch(fork()){
 
192
        switch(pid = fork()){
164
193
        case -1:
165
194
                Xerror("try again");
166
195
                break;
167
196
        case 0:
 
197
                clearwaitpids();
168
198
                start(p->code, pc+2, runq->local);
169
199
                close(mainfd);
170
200
                pushredir(ROPEN, sidefd, p->code[pc].i==READ?1:0);
171
201
                runq->ret = 0;
172
202
                break;
173
203
        default:
 
204
                addwaitpid(pid);
174
205
                close(sidefd);
175
206
                pushredir(ROPEN, mainfd, mainfd);       /* isn't this a noop? */
176
207
                strcpy(name, Fdprefix);
190
221
                Xerror("try again");
191
222
                break;
192
223
        case 0:
 
224
                clearwaitpids();
193
225
                start(runq->code, runq->pc+1, runq->local);
194
226
                runq->ret = 0;
195
227
                break;
196
228
        default:
 
229
                addwaitpid(pid);
197
230
                Waitfor(pid, 1);
198
231
                runq->pc = runq->code[runq->pc].i;
199
232
                break;
211
244
        case -1:
212
245
                return -1;
213
246
        case 0:
 
247
                clearwaitpids();
214
248
                pushword("exec");
215
249
                execexec();
216
250
                strcpy(buf, "can't exec: ");
218
252
                errstr(buf+n, ERRMAX-n);
219
253
                Exit(buf);
220
254
        }
 
255
        addwaitpid(pid);
221
256
        return pid;
222
257
}