]>
Commit | Line | Data |
---|---|---|
0118833a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
0a8a80e5 | 3 | // $Id: acquire-worker.cc,v 1.4 1998/10/22 04:56:40 jgg Exp $ |
0118833a AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire Worker | |
7 | ||
3b5421b4 AL |
8 | The worker process can startup either as a Configuration prober |
9 | or as a queue runner. As a configuration prober it only reads the | |
10 | configuration message and | |
11 | ||
0118833a AL |
12 | ##################################################################### */ |
13 | /*}}}*/ | |
14 | // Include Files /*{{{*/ | |
15 | #ifdef __GNUG__ | |
16 | #pragma implementation "apt-pkg/acquire-worker.h" | |
3b5421b4 | 17 | #endif |
0118833a | 18 | #include <apt-pkg/acquire-worker.h> |
0a8a80e5 | 19 | #include <apt-pkg/acquire-item.h> |
3b5421b4 AL |
20 | #include <apt-pkg/configuration.h> |
21 | #include <apt-pkg/error.h> | |
22 | #include <apt-pkg/fileutl.h> | |
23 | #include <strutl.h> | |
24 | ||
25 | #include <unistd.h> | |
26 | #include <signal.h> | |
93641593 | 27 | #include <wait.h> |
3b5421b4 AL |
28 | /*}}}*/ |
29 | ||
30 | // Worker::Worker - Constructor for Queue startup /*{{{*/ | |
31 | // --------------------------------------------------------------------- | |
32 | /* */ | |
0a8a80e5 | 33 | pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf) |
3b5421b4 AL |
34 | { |
35 | OwnerQ = Q; | |
0a8a80e5 AL |
36 | Config = Cnf; |
37 | Access = Cnf->Access; | |
38 | CurrentItem = 0; | |
3b5421b4 AL |
39 | |
40 | Construct(); | |
41 | } | |
42 | /*}}}*/ | |
43 | // Worker::Worker - Constructor for method config startup /*{{{*/ | |
44 | // --------------------------------------------------------------------- | |
45 | /* */ | |
46 | pkgAcquire::Worker::Worker(MethodConfig *Cnf) | |
47 | { | |
48 | OwnerQ = 0; | |
49 | Config = Cnf; | |
50 | Access = Cnf->Access; | |
0a8a80e5 AL |
51 | CurrentItem = 0; |
52 | ||
3b5421b4 AL |
53 | Construct(); |
54 | } | |
55 | /*}}}*/ | |
56 | // Worker::Construct - Constructor helper /*{{{*/ | |
57 | // --------------------------------------------------------------------- | |
58 | /* */ | |
59 | void pkgAcquire::Worker::Construct() | |
60 | { | |
0a8a80e5 AL |
61 | NextQueue = 0; |
62 | NextAcquire = 0; | |
3b5421b4 AL |
63 | Process = -1; |
64 | InFd = -1; | |
65 | OutFd = -1; | |
0a8a80e5 AL |
66 | OutReady = false; |
67 | InReady = false; | |
3b5421b4 AL |
68 | Debug = _config->FindB("Debug::pkgAcquire::Worker",false); |
69 | } | |
70 | /*}}}*/ | |
71 | // Worker::~Worker - Destructor /*{{{*/ | |
72 | // --------------------------------------------------------------------- | |
73 | /* */ | |
74 | pkgAcquire::Worker::~Worker() | |
75 | { | |
76 | close(InFd); | |
77 | close(OutFd); | |
78 | ||
79 | if (Process > 0) | |
0a8a80e5 | 80 | { |
3b5421b4 | 81 | kill(Process,SIGINT); |
0a8a80e5 AL |
82 | if (waitpid(Process,0,0) != Process) |
83 | _error->Warning("I waited but nothing was there!"); | |
84 | } | |
3b5421b4 AL |
85 | } |
86 | /*}}}*/ | |
87 | // Worker::Start - Start the worker process /*{{{*/ | |
88 | // --------------------------------------------------------------------- | |
89 | /* This forks the method and inits the communication channel */ | |
90 | bool pkgAcquire::Worker::Start() | |
91 | { | |
92 | // Get the method path | |
93 | string Method = _config->FindDir("Dir::Bin::Methods") + Access; | |
94 | if (FileExists(Method) == false) | |
95 | return _error->Error("The method driver %s could not be found.",Method.c_str()); | |
96 | ||
97 | if (Debug == true) | |
98 | clog << "Starting method '" << Method << '\'' << endl; | |
99 | ||
100 | // Create the pipes | |
101 | int Pipes[4] = {-1,-1,-1,-1}; | |
102 | if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0) | |
103 | { | |
104 | _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
105 | for (int I = 0; I != 4; I++) | |
106 | close(Pipes[I]); | |
107 | return false; | |
108 | } | |
109 | ||
110 | // Fork off the process | |
111 | Process = fork(); | |
112 | if (Process < 0) | |
113 | { | |
114 | cerr << "FATAL -> Failed to fork." << endl; | |
115 | exit(100); | |
116 | } | |
117 | ||
118 | // Spawn the subprocess | |
119 | if (Process == 0) | |
120 | { | |
121 | // Setup the FDs | |
122 | dup2(Pipes[1],STDOUT_FILENO); | |
123 | dup2(Pipes[2],STDIN_FILENO); | |
124 | dup2(((filebuf *)clog.rdbuf())->fd(),STDERR_FILENO); | |
125 | for (int I = 0; I != 4; I++) | |
126 | close(Pipes[I]); | |
127 | SetCloseExec(STDOUT_FILENO,false); | |
128 | SetCloseExec(STDIN_FILENO,false); | |
129 | SetCloseExec(STDERR_FILENO,false); | |
130 | ||
131 | const char *Args[2]; | |
132 | Args[0] = Method.c_str(); | |
133 | Args[1] = 0; | |
134 | execv(Args[0],(char **)Args); | |
135 | cerr << "Failed to exec method " << Args[0] << endl; | |
136 | exit(100); | |
137 | } | |
138 | ||
139 | // Fix up our FDs | |
140 | InFd = Pipes[0]; | |
141 | OutFd = Pipes[3]; | |
142 | SetNonBlock(Pipes[0],true); | |
143 | SetNonBlock(Pipes[3],true); | |
144 | close(Pipes[1]); | |
145 | close(Pipes[2]); | |
0a8a80e5 AL |
146 | OutReady = false; |
147 | InReady = true; | |
3b5421b4 AL |
148 | |
149 | // Read the configuration data | |
150 | if (WaitFd(InFd) == false || | |
151 | ReadMessages() == false) | |
152 | return _error->Error("Method %s did not start correctly",Method.c_str()); | |
153 | ||
154 | RunMessages(); | |
0a8a80e5 | 155 | SendConfiguration(); |
3b5421b4 AL |
156 | |
157 | return true; | |
158 | } | |
159 | /*}}}*/ | |
160 | // Worker::ReadMessages - Read all pending messages into the list /*{{{*/ | |
161 | // --------------------------------------------------------------------- | |
0a8a80e5 | 162 | /* */ |
3b5421b4 AL |
163 | bool pkgAcquire::Worker::ReadMessages() |
164 | { | |
0a8a80e5 AL |
165 | if (::ReadMessages(InFd,MessageQueue) == false) |
166 | return MethodFailure(); | |
3b5421b4 AL |
167 | return true; |
168 | } | |
169 | /*}}}*/ | |
170 | ||
171 | // Worker::RunMessage - Empty the message queue /*{{{*/ | |
172 | // --------------------------------------------------------------------- | |
173 | /* This takes the messages from the message queue and runs them through | |
174 | the parsers in order. */ | |
175 | bool pkgAcquire::Worker::RunMessages() | |
176 | { | |
177 | while (MessageQueue.empty() == false) | |
178 | { | |
179 | string Message = MessageQueue.front(); | |
180 | MessageQueue.erase(MessageQueue.begin()); | |
0a8a80e5 AL |
181 | |
182 | if (Debug == true) | |
183 | clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl; | |
3b5421b4 AL |
184 | |
185 | // Fetch the message number | |
186 | char *End; | |
187 | int Number = strtol(Message.c_str(),&End,10); | |
188 | if (End == Message.c_str()) | |
189 | return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str()); | |
190 | ||
191 | // Determine the message number and dispatch | |
192 | switch (Number) | |
193 | { | |
0a8a80e5 | 194 | // 100 Capabilities |
3b5421b4 AL |
195 | case 100: |
196 | if (Capabilities(Message) == false) | |
197 | return _error->Error("Unable to process Capabilities message from %s",Access.c_str()); | |
198 | break; | |
0a8a80e5 AL |
199 | |
200 | // 101 Log | |
201 | case 101: | |
202 | if (Debug == true) | |
203 | clog << " <- (log) " << LookupTag(Message,"Message") << endl; | |
204 | break; | |
205 | ||
206 | // 102 Status | |
207 | case 102: | |
208 | Status = LookupTag(Message,"Message"); | |
209 | break; | |
210 | ||
211 | // 200 URI Start | |
212 | case 200: | |
213 | break; | |
214 | ||
215 | // 201 URI Done | |
216 | case 201: | |
217 | break; | |
218 | ||
219 | // 400 URI Failure | |
220 | case 400: | |
221 | break; | |
222 | ||
223 | // 401 General Failure | |
224 | case 401: | |
225 | _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str()); | |
226 | break; | |
3b5421b4 AL |
227 | } |
228 | } | |
229 | return true; | |
230 | } | |
231 | /*}}}*/ | |
232 | // Worker::Capabilities - 100 Capabilities handler /*{{{*/ | |
233 | // --------------------------------------------------------------------- | |
234 | /* This parses the capabilities message and dumps it into the configuration | |
235 | structure. */ | |
236 | bool pkgAcquire::Worker::Capabilities(string Message) | |
237 | { | |
238 | if (Config == 0) | |
239 | return true; | |
240 | ||
241 | Config->Version = LookupTag(Message,"Version"); | |
242 | Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false); | |
243 | Config->PreScan = StringToBool(LookupTag(Message,"Pre-Scan"),false); | |
0a8a80e5 AL |
244 | Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false); |
245 | Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false); | |
3b5421b4 AL |
246 | |
247 | // Some debug text | |
248 | if (Debug == true) | |
249 | { | |
250 | clog << "Configured access method " << Config->Access << endl; | |
0a8a80e5 AL |
251 | clog << "Version:" << Config->Version << " SingleInstance:" << |
252 | Config->SingleInstance << " PreScan: " << Config->PreScan << | |
253 | " Pipeline:" << Config->Pipeline << " SendConfig:" << | |
254 | Config->SendConfig << endl; | |
3b5421b4 AL |
255 | } |
256 | ||
257 | return true; | |
258 | } | |
0118833a | 259 | /*}}}*/ |
0a8a80e5 AL |
260 | // Worker::SendConfiguration - Send the config to the method /*{{{*/ |
261 | // --------------------------------------------------------------------- | |
262 | /* */ | |
263 | bool pkgAcquire::Worker::SendConfiguration() | |
264 | { | |
265 | if (Config->SendConfig == false) | |
266 | return true; | |
267 | ||
268 | if (OutFd == -1) | |
269 | return false; | |
270 | ||
271 | string Message = "601 Configuration\n"; | |
272 | Message.reserve(2000); | |
273 | ||
274 | /* Write out all of the configuration directives by walking the | |
275 | configuration tree */ | |
276 | const Configuration::Item *Top = _config->Tree(0); | |
277 | for (; Top != 0;) | |
278 | { | |
279 | if (Top->Value.empty() == false) | |
280 | { | |
281 | string Line = "Config-Item: " + Top->FullTag() + "="; | |
282 | Line += QuoteString(Top->Value,"\n") + '\n'; | |
283 | Message += Line; | |
284 | } | |
285 | ||
286 | if (Top->Child != 0) | |
287 | { | |
288 | Top = Top->Child; | |
289 | continue; | |
290 | } | |
291 | ||
292 | while (Top != 0 && Top->Next == 0) | |
293 | Top = Top->Parent; | |
294 | if (Top != 0) | |
295 | Top = Top->Next; | |
296 | } | |
297 | Message += '\n'; | |
298 | ||
299 | if (Debug == true) | |
300 | clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl; | |
301 | OutQueue += Message; | |
302 | OutReady = true; | |
303 | ||
304 | return true; | |
305 | } | |
306 | /*}}}*/ | |
307 | // Worker::QueueItem - Add an item to the outbound queue /*{{{*/ | |
308 | // --------------------------------------------------------------------- | |
309 | /* Send a URI Acquire message to the method */ | |
310 | bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item) | |
311 | { | |
312 | if (OutFd == -1) | |
313 | return false; | |
314 | ||
315 | string Message = "600 URI Acquire\n"; | |
316 | Message.reserve(300); | |
317 | Message += "URI: " + Item->URI; | |
318 | Message += "\nFilename: " + Item->Owner->DestFile; | |
319 | Message += Item->Owner->Custom600Headers(); | |
320 | Message += "\n\n"; | |
321 | ||
322 | if (Debug == true) | |
323 | clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl; | |
324 | OutQueue += Message; | |
325 | OutReady = true; | |
326 | ||
327 | return true; | |
328 | } | |
329 | /*}}}*/ | |
330 | // Worker::OutFdRead - Out bound FD is ready /*{{{*/ | |
331 | // --------------------------------------------------------------------- | |
332 | /* */ | |
333 | bool pkgAcquire::Worker::OutFdReady() | |
334 | { | |
335 | int Res = write(OutFd,OutQueue.begin(),OutQueue.length()); | |
336 | if (Res <= 0) | |
337 | return MethodFailure(); | |
338 | ||
339 | // Hmm.. this should never happen. | |
340 | if (Res < 0) | |
341 | return true; | |
342 | ||
343 | OutQueue.erase(0,Res); | |
344 | if (OutQueue.empty() == true) | |
345 | OutReady = false; | |
346 | ||
347 | return true; | |
348 | } | |
349 | /*}}}*/ | |
350 | // Worker::InFdRead - In bound FD is ready /*{{{*/ | |
351 | // --------------------------------------------------------------------- | |
352 | /* */ | |
353 | bool pkgAcquire::Worker::InFdReady() | |
354 | { | |
355 | if (ReadMessages() == false) | |
356 | return false; | |
357 | RunMessages(); | |
358 | return true; | |
359 | } | |
360 | /*}}}*/ | |
361 | // Worker::MethodFailure - Called when the method fails /*{{{*/ | |
362 | // --------------------------------------------------------------------- | |
363 | /* This is called when the method is belived to have failed, probably because | |
364 | read returned -1. */ | |
365 | bool pkgAcquire::Worker::MethodFailure() | |
366 | { | |
367 | cerr << "Method " << Access << " has died unexpectedly!" << endl; | |
368 | if (waitpid(Process,0,0) != Process) | |
369 | _error->Warning("I waited but nothing was there!"); | |
370 | Process = -1; | |
371 | close(InFd); | |
372 | close(OutFd); | |
373 | InFd = -1; | |
374 | OutFd = -1; | |
375 | OutReady = false; | |
376 | InReady = false; | |
377 | OutQueue = string(); | |
378 | MessageQueue.erase(MessageQueue.begin(),MessageQueue.end()); | |
379 | ||
380 | return false; | |
381 | } | |
382 | /*}}}*/ |