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