-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommand.m
209 lines (176 loc) · 5.71 KB
/
Command.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import "EcProcess.h"
#if !defined(EC_BASE_CLASS)
#define EC_BASE_CLASS EcCommand
#endif
/* Create a fake interface to satisfy compiler ...
*/
@interface EC_BASE_CLASS : EcProcess
@end
void
inner_main()
{
NSAutoreleasePool *arp;
NSDictionary *defs;
int status;
arp = [NSAutoreleasePool new];
cmdVersion(@"$Date: 2012-02-13 08:11:49 +0000 (Mon, 13 Feb 2012) $ $Revision: 65934 $");
defs = [NSDictionary dictionaryWithObjectsAndKeys:
@"Command", @"HomeDirectory",
@"YES", @"Daemon",
#if defined(EC_REGISTRATION_DOMAIN)
EC_REGISTRATION_DOMAIN
#endif
nil];
if (nil == [[EC_BASE_CLASS alloc] initWithDefaults: defs])
{
NSLog(@"Unable to create command object.");
exit(1);
}
[EcProc ecRun];
status = [EcProc ecQuitStatus];
[arp release];
exit(status);
}
int
main(int argc, char *argv[])
{
NSProcessInfo *pInfo;
NSArray *pArgs;
NSString *pName;
CREATE_AUTORELEASE_POOL(pool);
pInfo = [NSProcessInfo processInfo];
pArgs = [pInfo arguments];
pName = [pInfo processName];
if ([pArgs containsObject: @"--Watched"] == NO)
{
NSMutableArray *args = AUTORELEASE([pArgs mutableCopy]);
NSString *path = [[NSBundle mainBundle] executablePath];
NSEnumerator *dbg;
NSString *key;
NSAutoreleasePool *inner = nil;
BOOL done = NO;
int status = 0;
NSFileHandle *null;
NSTask *t;
[args removeObjectAtIndex: 0];
/* Pass debug settings to child
* qdddd
* q
*/
dbg = [[[NSProcessInfo processInfo] debugSet] objectEnumerator];
while (nil != (key = [dbg nextObject]))
{
NSString *arg = [@"--GNU-Debug=" stringByAppendingString: key];
[args addObject: arg];
}
if ([pArgs containsObject: @"--Watcher"] == NO)
{
/* In the top level task ... set flags to create a subtask
* to act as a watcher for other tasks, and once that has
* been created, exit to leave it running as a daemon.
*/
[args addObject: @"--Watcher"];
t = [NSTask new];
NS_DURING
{
[t setLaunchPath: path];
[t setArguments: args];
[t setEnvironment: [pInfo environment]];
null = [NSFileHandle fileHandleWithNullDevice];
[t setStandardInput: null];
[t setStandardOutput: null];
[t setStandardError: null];
[t launch];
}
NS_HANDLER
{
NSLog(@"Problem creating %@ subprocess: %@",
pName, localException);
exit(1);
}
NS_ENDHANDLER
[t release];
exit(0);
}
/* This is the watcher ... its subtasks are those which are watched.
*/
/* Set args to tell subtask task not to make itself a daemon
*/
[args addObject: @"-Daemon"];
[args addObject: @"NO"];
/* Set args to tell task it is being watched.
*/
[args removeObject: @"--Watcher"];
[args addObject: @"--Watched"];
while (NO == done)
{
DESTROY(inner);
inner = [NSAutoreleasePool new];
t = [[NSTask new] autorelease];
NS_DURING
{
[t setLaunchPath: path];
[t setArguments: args];
[t setEnvironment: [pInfo environment]];
null = [NSFileHandle fileHandleWithNullDevice];
[t setStandardInput: null];
[t setStandardOutput: null];
[t setStandardError: null];
[t launch];
[t waitUntilExit];
if (0 == [t terminationStatus])
{
done = YES;
}
else if (255 == ([t terminationStatus] & 255))
{
/* A 'restart' uses this termination status ... try
* to do it promptly.
*/
[NSThread sleepForTimeInterval: 0.5];
}
else
{
/* Subprocess died ... try to restart after 30 seconds
*/
[NSThread sleepForTimeInterval: 30.0];
}
}
NS_HANDLER
{
done = YES;
status = 1;
NSLog(@"Problem creating %@ subprocess: %@",
pName, localException);
}
NS_ENDHANDLER
}
DESTROY(inner);
DESTROY(pool);
exit(status);
}
DESTROY(pool);
inner_main();
return 0;
}