]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: acquire-worker.cc,v 1.28 1999/10/18 00:37:35 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Acquire Worker | |
7 | ||
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 | ||
12 | ##################################################################### */ | |
13 | /*}}}*/ | |
14 | // Include Files /*{{{*/ | |
15 | #ifdef __GNUG__ | |
16 | #pragma implementation "apt-pkg/acquire-worker.h" | |
17 | #endif | |
18 | #include <apt-pkg/acquire-worker.h> | |
19 | #include <apt-pkg/acquire-item.h> | |
20 | #include <apt-pkg/configuration.h> | |
21 | #include <apt-pkg/error.h> | |
22 | #include <apt-pkg/fileutl.h> | |
23 | #include <apt-pkg/strutl.h> | |
24 | ||
25 | #include <sys/stat.h> | |
26 | #include <unistd.h> | |
27 | #include <fcntl.h> | |
28 | #include <signal.h> | |
29 | #include <stdio.h> | |
30 | #include <errno.h> | |
31 | /*}}}*/ | |
32 | ||
33 | // Worker::Worker - Constructor for Queue startup /*{{{*/ | |
34 | // --------------------------------------------------------------------- | |
35 | /* */ | |
36 | pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf, | |
37 | pkgAcquireStatus *Log) : Log(Log) | |
38 | { | |
39 | OwnerQ = Q; | |
40 | Config = Cnf; | |
41 | Access = Cnf->Access; | |
42 | CurrentItem = 0; | |
43 | TotalSize = 0; | |
44 | CurrentSize = 0; | |
45 | ||
46 | Construct(); | |
47 | } | |
48 | /*}}}*/ | |
49 | // Worker::Worker - Constructor for method config startup /*{{{*/ | |
50 | // --------------------------------------------------------------------- | |
51 | /* */ | |
52 | pkgAcquire::Worker::Worker(MethodConfig *Cnf) | |
53 | { | |
54 | OwnerQ = 0; | |
55 | Config = Cnf; | |
56 | Access = Cnf->Access; | |
57 | CurrentItem = 0; | |
58 | TotalSize = 0; | |
59 | CurrentSize = 0; | |
60 | ||
61 | Construct(); | |
62 | } | |
63 | /*}}}*/ | |
64 | // Worker::Construct - Constructor helper /*{{{*/ | |
65 | // --------------------------------------------------------------------- | |
66 | /* */ | |
67 | void pkgAcquire::Worker::Construct() | |
68 | { | |
69 | NextQueue = 0; | |
70 | NextAcquire = 0; | |
71 | Process = -1; | |
72 | InFd = -1; | |
73 | OutFd = -1; | |
74 | OutReady = false; | |
75 | InReady = false; | |
76 | Debug = _config->FindB("Debug::pkgAcquire::Worker",false); | |
77 | } | |
78 | /*}}}*/ | |
79 | // Worker::~Worker - Destructor /*{{{*/ | |
80 | // --------------------------------------------------------------------- | |
81 | /* */ | |
82 | pkgAcquire::Worker::~Worker() | |
83 | { | |
84 | close(InFd); | |
85 | close(OutFd); | |
86 | ||
87 | if (Process > 0) | |
88 | { | |
89 | /* Closing of stdin is the signal to exit and die when the process | |
90 | indicates it needs cleanup */ | |
91 | if (Config->NeedsCleanup == false) | |
92 | kill(Process,SIGINT); | |
93 | ExecWait(Process,Access.c_str(),true); | |
94 | } | |
95 | } | |
96 | /*}}}*/ | |
97 | // Worker::Start - Start the worker process /*{{{*/ | |
98 | // --------------------------------------------------------------------- | |
99 | /* This forks the method and inits the communication channel */ | |
100 | bool pkgAcquire::Worker::Start() | |
101 | { | |
102 | // Get the method path | |
103 | string Method = _config->FindDir("Dir::Bin::Methods") + Access; | |
104 | if (FileExists(Method) == false) | |
105 | return _error->Error("The method driver %s could not be found.",Method.c_str()); | |
106 | ||
107 | if (Debug == true) | |
108 | clog << "Starting method '" << Method << '\'' << endl; | |
109 | ||
110 | // Create the pipes | |
111 | int Pipes[4] = {-1,-1,-1,-1}; | |
112 | if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0) | |
113 | { | |
114 | _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
115 | for (int I = 0; I != 4; I++) | |
116 | close(Pipes[I]); | |
117 | return false; | |
118 | } | |
119 | for (int I = 0; I != 4; I++) | |
120 | SetCloseExec(Pipes[0],true); | |
121 | ||
122 | // Fork off the process | |
123 | Process = ExecFork(); | |
124 | ||
125 | // Spawn the subprocess | |
126 | if (Process == 0) | |
127 | { | |
128 | // Setup the FDs | |
129 | dup2(Pipes[1],STDOUT_FILENO); | |
130 | dup2(Pipes[2],STDIN_FILENO); | |
131 | dup2(((filebuf *)clog.rdbuf())->fd(),STDERR_FILENO); | |
132 | SetCloseExec(STDOUT_FILENO,false); | |
133 | SetCloseExec(STDIN_FILENO,false); | |
134 | SetCloseExec(STDERR_FILENO,false); | |
135 | ||
136 | const char *Args[2]; | |
137 | Args[0] = Method.c_str(); | |
138 | Args[1] = 0; | |
139 | execv(Args[0],(char **)Args); | |
140 | cerr << "Failed to exec method " << Args[0] << endl; | |
141 | _exit(100); | |
142 | } | |
143 | ||
144 | // Fix up our FDs | |
145 | InFd = Pipes[0]; | |
146 | OutFd = Pipes[3]; | |
147 | SetNonBlock(Pipes[0],true); | |
148 | SetNonBlock(Pipes[3],true); | |
149 | close(Pipes[1]); | |
150 | close(Pipes[2]); | |
151 | OutReady = false; | |
152 | InReady = true; | |
153 | ||
154 | // Read the configuration data | |
155 | if (WaitFd(InFd) == false || | |
156 | ReadMessages() == false) | |
157 | return _error->Error("Method %s did not start correctly",Method.c_str()); | |
158 | ||
159 | RunMessages(); | |
160 | if (OwnerQ != 0) | |
161 | SendConfiguration(); | |
162 | ||
163 | return true; | |
164 | } | |
165 | /*}}}*/ | |
166 | // Worker::ReadMessages - Read all pending messages into the list /*{{{*/ | |
167 | // --------------------------------------------------------------------- | |
168 | /* */ | |
169 | bool pkgAcquire::Worker::ReadMessages() | |
170 | { | |
171 | if (::ReadMessages(InFd,MessageQueue) == false) | |
172 | return MethodFailure(); | |
173 | return true; | |
174 | } | |
175 | /*}}}*/ | |
176 | // Worker::RunMessage - Empty the message queue /*{{{*/ | |
177 | // --------------------------------------------------------------------- | |
178 | /* This takes the messages from the message queue and runs them through | |
179 | the parsers in order. */ | |
180 | bool pkgAcquire::Worker::RunMessages() | |
181 | { | |
182 | while (MessageQueue.empty() == false) | |
183 | { | |
184 | string Message = MessageQueue.front(); | |
185 | MessageQueue.erase(MessageQueue.begin()); | |
186 | ||
187 | if (Debug == true) | |
188 | clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl; | |
189 | ||
190 | // Fetch the message number | |
191 | char *End; | |
192 | int Number = strtol(Message.c_str(),&End,10); | |
193 | if (End == Message.c_str()) | |
194 | return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str()); | |
195 | ||
196 | string URI = LookupTag(Message,"URI"); | |
197 | pkgAcquire::Queue::QItem *Itm = 0; | |
198 | if (URI.empty() == false) | |
199 | Itm = OwnerQ->FindItem(URI,this); | |
200 | ||
201 | // Determine the message number and dispatch | |
202 | switch (Number) | |
203 | { | |
204 | // 100 Capabilities | |
205 | case 100: | |
206 | if (Capabilities(Message) == false) | |
207 | return _error->Error("Unable to process Capabilities message from %s",Access.c_str()); | |
208 | break; | |
209 | ||
210 | // 101 Log | |
211 | case 101: | |
212 | if (Debug == true) | |
213 | clog << " <- (log) " << LookupTag(Message,"Message") << endl; | |
214 | break; | |
215 | ||
216 | // 102 Status | |
217 | case 102: | |
218 | Status = LookupTag(Message,"Message"); | |
219 | break; | |
220 | ||
221 | // 200 URI Start | |
222 | case 200: | |
223 | { | |
224 | if (Itm == 0) | |
225 | { | |
226 | _error->Error("Method gave invalid 200 URI Start message"); | |
227 | break; | |
228 | } | |
229 | ||
230 | CurrentItem = Itm; | |
231 | CurrentSize = 0; | |
232 | TotalSize = atoi(LookupTag(Message,"Size","0").c_str()); | |
233 | ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str()); | |
234 | Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str())); | |
235 | ||
236 | if (Log != 0) | |
237 | Log->Fetch(*Itm); | |
238 | ||
239 | break; | |
240 | } | |
241 | ||
242 | // 201 URI Done | |
243 | case 201: | |
244 | { | |
245 | if (Itm == 0) | |
246 | { | |
247 | _error->Error("Method gave invalid 201 URI Done message"); | |
248 | break; | |
249 | } | |
250 | ||
251 | pkgAcquire::Item *Owner = Itm->Owner; | |
252 | pkgAcquire::ItemDesc Desc = *Itm; | |
253 | OwnerQ->ItemDone(Itm); | |
254 | if (TotalSize != 0 && | |
255 | (unsigned)atoi(LookupTag(Message,"Size","0").c_str()) != TotalSize) | |
256 | _error->Warning("Bizzar Error - File size is not what the server reported %s %u", | |
257 | LookupTag(Message,"Size","0").c_str(),TotalSize); | |
258 | ||
259 | Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()), | |
260 | LookupTag(Message,"MD5-Hash")); | |
261 | ItemDone(); | |
262 | ||
263 | // Log that we are done | |
264 | if (Log != 0) | |
265 | { | |
266 | if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true || | |
267 | StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true) | |
268 | { | |
269 | /* Hide 'hits' for local only sources - we also manage to | |
270 | hide gets */ | |
271 | if (Config->LocalOnly == false) | |
272 | Log->IMSHit(Desc); | |
273 | } | |
274 | else | |
275 | Log->Done(Desc); | |
276 | } | |
277 | break; | |
278 | } | |
279 | ||
280 | // 400 URI Failure | |
281 | case 400: | |
282 | { | |
283 | if (Itm == 0) | |
284 | { | |
285 | _error->Error("Method gave invalid 400 URI Failure message"); | |
286 | break; | |
287 | } | |
288 | ||
289 | pkgAcquire::Item *Owner = Itm->Owner; | |
290 | pkgAcquire::ItemDesc Desc = *Itm; | |
291 | OwnerQ->ItemDone(Itm); | |
292 | Owner->Failed(Message,Config); | |
293 | ItemDone(); | |
294 | ||
295 | if (Log != 0) | |
296 | Log->Fail(Desc); | |
297 | ||
298 | break; | |
299 | } | |
300 | ||
301 | // 401 General Failure | |
302 | case 401: | |
303 | _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str()); | |
304 | break; | |
305 | ||
306 | // 403 Media Change | |
307 | case 403: | |
308 | MediaChange(Message); | |
309 | break; | |
310 | } | |
311 | } | |
312 | return true; | |
313 | } | |
314 | /*}}}*/ | |
315 | // Worker::Capabilities - 100 Capabilities handler /*{{{*/ | |
316 | // --------------------------------------------------------------------- | |
317 | /* This parses the capabilities message and dumps it into the configuration | |
318 | structure. */ | |
319 | bool pkgAcquire::Worker::Capabilities(string Message) | |
320 | { | |
321 | if (Config == 0) | |
322 | return true; | |
323 | ||
324 | Config->Version = LookupTag(Message,"Version"); | |
325 | Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false); | |
326 | Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false); | |
327 | Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false); | |
328 | Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false); | |
329 | Config->NeedsCleanup = StringToBool(LookupTag(Message,"Needs-Cleanup"),false); | |
330 | ||
331 | // Some debug text | |
332 | if (Debug == true) | |
333 | { | |
334 | clog << "Configured access method " << Config->Access << endl; | |
335 | clog << "Version:" << Config->Version << " SingleInstance:" << | |
336 | Config->SingleInstance << | |
337 | " Pipeline:" << Config->Pipeline << " SendConfig:" << | |
338 | Config->SendConfig << endl; | |
339 | } | |
340 | ||
341 | return true; | |
342 | } | |
343 | /*}}}*/ | |
344 | // Worker::MediaChange - Request a media change /*{{{*/ | |
345 | // --------------------------------------------------------------------- | |
346 | /* */ | |
347 | bool pkgAcquire::Worker::MediaChange(string Message) | |
348 | { | |
349 | if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"), | |
350 | LookupTag(Message,"Drive")) == false) | |
351 | { | |
352 | char S[300]; | |
353 | sprintf(S,"603 Media Changed\nFailed: true\n\n"); | |
354 | if (Debug == true) | |
355 | clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl; | |
356 | OutQueue += S; | |
357 | OutReady = true; | |
358 | return true; | |
359 | } | |
360 | ||
361 | char S[300]; | |
362 | sprintf(S,"603 Media Changed\n\n"); | |
363 | if (Debug == true) | |
364 | clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl; | |
365 | OutQueue += S; | |
366 | OutReady = true; | |
367 | return true; | |
368 | } | |
369 | /*}}}*/ | |
370 | // Worker::SendConfiguration - Send the config to the method /*{{{*/ | |
371 | // --------------------------------------------------------------------- | |
372 | /* */ | |
373 | bool pkgAcquire::Worker::SendConfiguration() | |
374 | { | |
375 | if (Config->SendConfig == false) | |
376 | return true; | |
377 | ||
378 | if (OutFd == -1) | |
379 | return false; | |
380 | ||
381 | string Message = "601 Configuration\n"; | |
382 | Message.reserve(2000); | |
383 | ||
384 | /* Write out all of the configuration directives by walking the | |
385 | configuration tree */ | |
386 | const Configuration::Item *Top = _config->Tree(0); | |
387 | for (; Top != 0;) | |
388 | { | |
389 | if (Top->Value.empty() == false) | |
390 | { | |
391 | string Line = "Config-Item: " + Top->FullTag() + "="; | |
392 | Line += QuoteString(Top->Value,"\n") + '\n'; | |
393 | Message += Line; | |
394 | } | |
395 | ||
396 | if (Top->Child != 0) | |
397 | { | |
398 | Top = Top->Child; | |
399 | continue; | |
400 | } | |
401 | ||
402 | while (Top != 0 && Top->Next == 0) | |
403 | Top = Top->Parent; | |
404 | if (Top != 0) | |
405 | Top = Top->Next; | |
406 | } | |
407 | Message += '\n'; | |
408 | ||
409 | if (Debug == true) | |
410 | clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl; | |
411 | OutQueue += Message; | |
412 | OutReady = true; | |
413 | ||
414 | return true; | |
415 | } | |
416 | /*}}}*/ | |
417 | // Worker::QueueItem - Add an item to the outbound queue /*{{{*/ | |
418 | // --------------------------------------------------------------------- | |
419 | /* Send a URI Acquire message to the method */ | |
420 | bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item) | |
421 | { | |
422 | if (OutFd == -1) | |
423 | return false; | |
424 | ||
425 | string Message = "600 URI Acquire\n"; | |
426 | Message.reserve(300); | |
427 | Message += "URI: " + Item->URI; | |
428 | Message += "\nFilename: " + Item->Owner->DestFile; | |
429 | Message += Item->Owner->Custom600Headers(); | |
430 | Message += "\n\n"; | |
431 | ||
432 | if (Debug == true) | |
433 | clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl; | |
434 | OutQueue += Message; | |
435 | OutReady = true; | |
436 | ||
437 | return true; | |
438 | } | |
439 | /*}}}*/ | |
440 | // Worker::OutFdRead - Out bound FD is ready /*{{{*/ | |
441 | // --------------------------------------------------------------------- | |
442 | /* */ | |
443 | bool pkgAcquire::Worker::OutFdReady() | |
444 | { | |
445 | int Res; | |
446 | do | |
447 | { | |
448 | Res = write(OutFd,OutQueue.begin(),OutQueue.length()); | |
449 | } | |
450 | while (Res < 0 && errno == EINTR); | |
451 | ||
452 | if (Res <= 0) | |
453 | return MethodFailure(); | |
454 | ||
455 | // Hmm.. this should never happen. | |
456 | if (Res < 0) | |
457 | return true; | |
458 | ||
459 | OutQueue.erase(0,Res); | |
460 | if (OutQueue.empty() == true) | |
461 | OutReady = false; | |
462 | ||
463 | return true; | |
464 | } | |
465 | /*}}}*/ | |
466 | // Worker::InFdRead - In bound FD is ready /*{{{*/ | |
467 | // --------------------------------------------------------------------- | |
468 | /* */ | |
469 | bool pkgAcquire::Worker::InFdReady() | |
470 | { | |
471 | if (ReadMessages() == false) | |
472 | return false; | |
473 | RunMessages(); | |
474 | return true; | |
475 | } | |
476 | /*}}}*/ | |
477 | // Worker::MethodFailure - Called when the method fails /*{{{*/ | |
478 | // --------------------------------------------------------------------- | |
479 | /* This is called when the method is belived to have failed, probably because | |
480 | read returned -1. */ | |
481 | bool pkgAcquire::Worker::MethodFailure() | |
482 | { | |
483 | _error->Error("Method %s has died unexpectedly!",Access.c_str()); | |
484 | ||
485 | ExecWait(Process,Access.c_str(),true); | |
486 | Process = -1; | |
487 | close(InFd); | |
488 | close(OutFd); | |
489 | InFd = -1; | |
490 | OutFd = -1; | |
491 | OutReady = false; | |
492 | InReady = false; | |
493 | OutQueue = string(); | |
494 | MessageQueue.erase(MessageQueue.begin(),MessageQueue.end()); | |
495 | ||
496 | return false; | |
497 | } | |
498 | /*}}}*/ | |
499 | // Worker::Pulse - Called periodically /*{{{*/ | |
500 | // --------------------------------------------------------------------- | |
501 | /* */ | |
502 | void pkgAcquire::Worker::Pulse() | |
503 | { | |
504 | if (CurrentItem == 0) | |
505 | return; | |
506 | ||
507 | struct stat Buf; | |
508 | if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0) | |
509 | return; | |
510 | CurrentSize = Buf.st_size; | |
511 | ||
512 | // Hmm? Should not happen... | |
513 | if (CurrentSize > TotalSize && TotalSize != 0) | |
514 | TotalSize = CurrentSize; | |
515 | } | |
516 | /*}}}*/ | |
517 | // Worker::ItemDone - Called when the current item is finished /*{{{*/ | |
518 | // --------------------------------------------------------------------- | |
519 | /* */ | |
520 | void pkgAcquire::Worker::ItemDone() | |
521 | { | |
522 | CurrentItem = 0; | |
523 | CurrentSize = 0; | |
524 | TotalSize = 0; | |
525 | Status = string(); | |
526 | } | |
527 | /*}}}*/ |