]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-worker.cc
do not call resolver twice on (dist-)upgrade
[apt.git] / apt-pkg / acquire-worker.cc
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
24a0d63a 3// $Id: acquire-worker.cc,v 1.34 2001/05/22 04:42:54 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 /*{{{*/
ea542140
DK
15#include <config.h>
16
453b82a3 17#include <apt-pkg/acquire.h>
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>
cdcc6d34 23#include <apt-pkg/strutl.h>
453b82a3 24#include <apt-pkg/hashes.h>
3b5421b4 25
453b82a3
DK
26#include <string>
27#include <vector>
24a0d63a 28#include <iostream>
80a26ed1 29#include <sstream>
ea542140 30
8267fe24 31#include <sys/stat.h>
453b82a3 32#include <stdlib.h>
3b5421b4
AL
33#include <unistd.h>
34#include <signal.h>
542ec555 35#include <stdio.h>
b0db36b1 36#include <errno.h>
ea542140
DK
37
38#include <apti18n.h>
3b5421b4
AL
39 /*}}}*/
40
24a0d63a
AL
41using namespace std;
42
3b5421b4
AL
43// Worker::Worker - Constructor for Queue startup /*{{{*/
44// ---------------------------------------------------------------------
45/* */
8267fe24
AL
46pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
47 pkgAcquireStatus *Log) : Log(Log)
3b5421b4
AL
48{
49 OwnerQ = Q;
0a8a80e5
AL
50 Config = Cnf;
51 Access = Cnf->Access;
52 CurrentItem = 0;
18ef0a78
AL
53 TotalSize = 0;
54 CurrentSize = 0;
55
3b5421b4
AL
56 Construct();
57}
58 /*}}}*/
59// Worker::Worker - Constructor for method config startup /*{{{*/
60// ---------------------------------------------------------------------
61/* */
62pkgAcquire::Worker::Worker(MethodConfig *Cnf)
63{
64 OwnerQ = 0;
65 Config = Cnf;
66 Access = Cnf->Access;
0a8a80e5 67 CurrentItem = 0;
18ef0a78
AL
68 TotalSize = 0;
69 CurrentSize = 0;
0a8a80e5 70
3b5421b4
AL
71 Construct();
72}
73 /*}}}*/
74// Worker::Construct - Constructor helper /*{{{*/
75// ---------------------------------------------------------------------
76/* */
77void pkgAcquire::Worker::Construct()
78{
0a8a80e5
AL
79 NextQueue = 0;
80 NextAcquire = 0;
3b5421b4
AL
81 Process = -1;
82 InFd = -1;
83 OutFd = -1;
0a8a80e5
AL
84 OutReady = false;
85 InReady = false;
3b5421b4
AL
86 Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
87}
88 /*}}}*/
89// Worker::~Worker - Destructor /*{{{*/
90// ---------------------------------------------------------------------
91/* */
92pkgAcquire::Worker::~Worker()
93{
94 close(InFd);
95 close(OutFd);
96
97 if (Process > 0)
0a8a80e5 98 {
8e5fc8f5
AL
99 /* Closing of stdin is the signal to exit and die when the process
100 indicates it needs cleanup */
101 if (Config->NeedsCleanup == false)
102 kill(Process,SIGINT);
ddc1d8d0 103 ExecWait(Process,Access.c_str(),true);
0a8a80e5 104 }
3b5421b4
AL
105}
106 /*}}}*/
107// Worker::Start - Start the worker process /*{{{*/
108// ---------------------------------------------------------------------
109/* This forks the method and inits the communication channel */
110bool pkgAcquire::Worker::Start()
111{
112 // Get the method path
113 string Method = _config->FindDir("Dir::Bin::Methods") + Access;
114 if (FileExists(Method) == false)
9082a1fc
DK
115 {
116 _error->Error(_("The method driver %s could not be found."),Method.c_str());
117 if (Access == "https")
118 _error->Notice(_("Is the package %s installed?"), "apt-transport-https");
119 return false;
120 }
3b5421b4
AL
121
122 if (Debug == true)
123 clog << "Starting method '" << Method << '\'' << endl;
124
125 // Create the pipes
126 int Pipes[4] = {-1,-1,-1,-1};
127 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
128 {
129 _error->Errno("pipe","Failed to create IPC pipe to subprocess");
130 for (int I = 0; I != 4; I++)
131 close(Pipes[I]);
132 return false;
133 }
8b89e57f 134 for (int I = 0; I != 4; I++)
4490f2de 135 SetCloseExec(Pipes[I],true);
8b89e57f 136
3b5421b4 137 // Fork off the process
54676e1a 138 Process = ExecFork();
3b5421b4
AL
139 if (Process == 0)
140 {
141 // Setup the FDs
142 dup2(Pipes[1],STDOUT_FILENO);
143 dup2(Pipes[2],STDIN_FILENO);
3b5421b4
AL
144 SetCloseExec(STDOUT_FILENO,false);
145 SetCloseExec(STDIN_FILENO,false);
146 SetCloseExec(STDERR_FILENO,false);
147
148 const char *Args[2];
149 Args[0] = Method.c_str();
150 Args[1] = 0;
151 execv(Args[0],(char **)Args);
152 cerr << "Failed to exec method " << Args[0] << endl;
0dbb95d8 153 _exit(100);
3b5421b4
AL
154 }
155
156 // Fix up our FDs
157 InFd = Pipes[0];
158 OutFd = Pipes[3];
159 SetNonBlock(Pipes[0],true);
160 SetNonBlock(Pipes[3],true);
161 close(Pipes[1]);
162 close(Pipes[2]);
0a8a80e5
AL
163 OutReady = false;
164 InReady = true;
3b5421b4
AL
165
166 // Read the configuration data
167 if (WaitFd(InFd) == false ||
168 ReadMessages() == false)
b2e465d6 169 return _error->Error(_("Method %s did not start correctly"),Method.c_str());
3b5421b4
AL
170
171 RunMessages();
8b89e57f
AL
172 if (OwnerQ != 0)
173 SendConfiguration();
3b5421b4
AL
174
175 return true;
176}
177 /*}}}*/
178// Worker::ReadMessages - Read all pending messages into the list /*{{{*/
179// ---------------------------------------------------------------------
0a8a80e5 180/* */
3b5421b4
AL
181bool pkgAcquire::Worker::ReadMessages()
182{
0a8a80e5
AL
183 if (::ReadMessages(InFd,MessageQueue) == false)
184 return MethodFailure();
3b5421b4
AL
185 return true;
186}
187 /*}}}*/
3b5421b4
AL
188// Worker::RunMessage - Empty the message queue /*{{{*/
189// ---------------------------------------------------------------------
190/* This takes the messages from the message queue and runs them through
191 the parsers in order. */
192bool pkgAcquire::Worker::RunMessages()
193{
194 while (MessageQueue.empty() == false)
195 {
196 string Message = MessageQueue.front();
197 MessageQueue.erase(MessageQueue.begin());
0a8a80e5
AL
198
199 if (Debug == true)
200 clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
3b5421b4
AL
201
202 // Fetch the message number
203 char *End;
204 int Number = strtol(Message.c_str(),&End,10);
205 if (End == Message.c_str())
206 return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
207
c88edf1d
AL
208 string URI = LookupTag(Message,"URI");
209 pkgAcquire::Queue::QItem *Itm = 0;
210 if (URI.empty() == false)
211 Itm = OwnerQ->FindItem(URI,this);
196fd136
MV
212
213 // update used mirror
214 string UsedMirror = LookupTag(Message,"UsedMirror", "");
215 if (!UsedMirror.empty() &&
216 Itm &&
217 Itm->Description.find(" ") != string::npos)
218 {
219 Itm->Description.replace(0, Itm->Description.find(" "), UsedMirror);
220 // FIXME: will we need this as well?
221 //Itm->ShortDesc = UsedMirror;
222 }
bfd22fc0 223
3b5421b4
AL
224 // Determine the message number and dispatch
225 switch (Number)
226 {
0a8a80e5 227 // 100 Capabilities
3b5421b4
AL
228 case 100:
229 if (Capabilities(Message) == false)
230 return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
231 break;
0a8a80e5
AL
232
233 // 101 Log
234 case 101:
235 if (Debug == true)
236 clog << " <- (log) " << LookupTag(Message,"Message") << endl;
237 break;
238
239 // 102 Status
240 case 102:
241 Status = LookupTag(Message,"Message");
242 break;
243
15d7e515
MV
244 // 103 Redirect
245 case 103:
246 {
247 if (Itm == 0)
248 {
249 _error->Error("Method gave invalid 103 Redirect message");
250 break;
251 }
252
253 string NewURI = LookupTag(Message,"New-URI",URI.c_str());
254 Itm->URI = NewURI;
5674f6b3
RG
255
256 ItemDone();
257
258 pkgAcquire::Item *Owner = Itm->Owner;
259 pkgAcquire::ItemDesc Desc = *Itm;
260
261 // Change the status so that it can be dequeued
262 Owner->Status = pkgAcquire::Item::StatIdle;
263 // Mark the item as done (taking care of all queues)
264 // and then put it in the main queue again
265 OwnerQ->ItemDone(Itm);
266 OwnerQ->Owner->Enqueue(Desc);
267
268 if (Log != 0)
269 Log->Done(Desc);
15d7e515
MV
270 break;
271 }
272
0a8a80e5
AL
273 // 200 URI Start
274 case 200:
c88edf1d
AL
275 {
276 if (Itm == 0)
277 {
93bf083d 278 _error->Error("Method gave invalid 200 URI Start message");
c88edf1d
AL
279 break;
280 }
8267fe24 281
c88edf1d
AL
282 CurrentItem = Itm;
283 CurrentSize = 0;
650faab0
DK
284 TotalSize = strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10);
285 ResumePoint = strtoull(LookupTag(Message,"Resume-Point","0").c_str(), NULL, 10);
286 Itm->Owner->Start(Message,strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10));
8b75eb1c 287
c5ccf175
AL
288 // Display update before completion
289 if (Log != 0 && Log->MorePulses == true)
290 Log->Pulse(Itm->Owner->GetOwner());
291
8267fe24
AL
292 if (Log != 0)
293 Log->Fetch(*Itm);
294
c88edf1d
AL
295 break;
296 }
0a8a80e5
AL
297
298 // 201 URI Done
299 case 201:
c88edf1d
AL
300 {
301 if (Itm == 0)
302 {
93bf083d 303 _error->Error("Method gave invalid 201 URI Done message");
c88edf1d
AL
304 break;
305 }
c5ccf175 306
bfd22fc0 307 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 308 pkgAcquire::ItemDesc Desc = *Itm;
c5ccf175
AL
309
310 // Display update before completion
311 if (Log != 0 && Log->MorePulses == true)
312 Log->Pulse(Owner->GetOwner());
313
be4401bf 314 OwnerQ->ItemDone(Itm);
650faab0 315 unsigned long long const ServerSize = strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10);
1dea08eb
MV
316 bool isHit = StringToBool(LookupTag(Message,"IMS-Hit"),false) ||
317 StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false);
318 // Using the https method the server might return 200, but the
319 // If-Modified-Since condition is not satsified, libcurl will
320 // discard the download. In this case, however, TotalSize will be
321 // set to the actual size of the file, while ServerSize will be set
322 // to 0. Therefore, if the item is marked as a hit and the
323 // downloaded size (ServerSize) is 0, we ignore TotalSize.
324 if (TotalSize != 0 && (!isHit || ServerSize != 0) && ServerSize != TotalSize)
73da43e9 325 _error->Warning("Size of file %s is not what the server reported %s %llu",
378fcbd6 326 Owner->DestFile.c_str(), LookupTag(Message,"Size","0").c_str(),TotalSize);
bdae53f1 327
8a8feb29
MV
328 // see if there is a hash to verify
329 string RecivedHash;
330 HashString expectedHash(Owner->HashSum());
331 if(!expectedHash.empty())
332 {
333 string hashTag = expectedHash.HashType()+"-Hash";
95f45727
MV
334 string hashSum = LookupTag(Message, hashTag.c_str());
335 if(!hashSum.empty())
336 RecivedHash = expectedHash.HashType() + ":" + hashSum;
8a8feb29
MV
337 if(_config->FindB("Debug::pkgAcquire::Auth", false) == true)
338 {
339 clog << "201 URI Done: " << Owner->DescURI() << endl
340 << "RecivedHash: " << RecivedHash << endl
341 << "ExpectedHash: " << expectedHash.toStr()
342 << endl << endl;
343 }
344 }
378fcbd6 345 Owner->Done(Message, ServerSize, RecivedHash.c_str(), Config);
8267fe24
AL
346 ItemDone();
347
348 // Log that we are done
349 if (Log != 0)
350 {
1dea08eb 351 if (isHit)
c46824ce
AL
352 {
353 /* Hide 'hits' for local only sources - we also manage to
354 hide gets */
355 if (Config->LocalOnly == false)
356 Log->IMSHit(Desc);
357 }
8267fe24
AL
358 else
359 Log->Done(Desc);
18ef0a78 360 }
c88edf1d
AL
361 break;
362 }
0a8a80e5
AL
363
364 // 400 URI Failure
365 case 400:
c88edf1d
AL
366 {
367 if (Itm == 0)
368 {
93bf083d 369 _error->Error("Method gave invalid 400 URI Failure message");
c88edf1d
AL
370 break;
371 }
372
c5ccf175
AL
373 // Display update before completion
374 if (Log != 0 && Log->MorePulses == true)
375 Log->Pulse(Itm->Owner->GetOwner());
376
bfd22fc0 377 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 378 pkgAcquire::ItemDesc Desc = *Itm;
c88edf1d 379 OwnerQ->ItemDone(Itm);
7e5f33eb
MV
380
381 // set some status
382 if(LookupTag(Message,"FailReason") == "Timeout" ||
383 LookupTag(Message,"FailReason") == "TmpResolveFailure" ||
f0983ff2 384 LookupTag(Message,"FailReason") == "ResolveFailure" ||
7e5f33eb
MV
385 LookupTag(Message,"FailReason") == "ConnectionRefused")
386 Owner->Status = pkgAcquire::Item::StatTransientNetworkError;
387
7d8afa39 388 Owner->Failed(Message,Config);
8267fe24 389 ItemDone();
7d8afa39 390
8267fe24
AL
391 if (Log != 0)
392 Log->Fail(Desc);
7d8afa39 393
c88edf1d
AL
394 break;
395 }
0a8a80e5
AL
396
397 // 401 General Failure
398 case 401:
b2e465d6 399 _error->Error("Method %s General failure: %s",Access.c_str(),LookupTag(Message,"Message").c_str());
0a8a80e5 400 break;
542ec555
AL
401
402 // 403 Media Change
403 case 403:
404 MediaChange(Message);
405 break;
3b5421b4
AL
406 }
407 }
408 return true;
409}
410 /*}}}*/
411// Worker::Capabilities - 100 Capabilities handler /*{{{*/
412// ---------------------------------------------------------------------
413/* This parses the capabilities message and dumps it into the configuration
414 structure. */
415bool pkgAcquire::Worker::Capabilities(string Message)
416{
417 if (Config == 0)
418 return true;
419
420 Config->Version = LookupTag(Message,"Version");
421 Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
0a8a80e5
AL
422 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
423 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
e331f6ed 424 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
8e5fc8f5 425 Config->NeedsCleanup = StringToBool(LookupTag(Message,"Needs-Cleanup"),false);
459681d3 426 Config->Removable = StringToBool(LookupTag(Message,"Removable"),false);
3b5421b4
AL
427
428 // Some debug text
429 if (Debug == true)
430 {
431 clog << "Configured access method " << Config->Access << endl;
459681d3
AL
432 clog << "Version:" << Config->Version <<
433 " SingleInstance:" << Config->SingleInstance <<
434 " Pipeline:" << Config->Pipeline <<
435 " SendConfig:" << Config->SendConfig <<
436 " LocalOnly: " << Config->LocalOnly <<
437 " NeedsCleanup: " << Config->NeedsCleanup <<
438 " Removable: " << Config->Removable << endl;
3b5421b4
AL
439 }
440
542ec555
AL
441 return true;
442}
443 /*}}}*/
444// Worker::MediaChange - Request a media change /*{{{*/
445// ---------------------------------------------------------------------
446/* */
447bool pkgAcquire::Worker::MediaChange(string Message)
448{
80a26ed1
MV
449 int status_fd = _config->FindI("APT::Status-Fd",-1);
450 if(status_fd > 0)
451 {
452 string Media = LookupTag(Message,"Media");
453 string Drive = LookupTag(Message,"Drive");
454 ostringstream msg,status;
1a82c63e
MV
455 ioprintf(msg,_("Please insert the disc labeled: "
456 "'%s' "
457 "in the drive '%s' and press enter."),
458 Media.c_str(),Drive.c_str());
80a26ed1 459 status << "media-change: " // message
1a82c63e
MV
460 << Media << ":" // media
461 << Drive << ":" // drive
462 << msg.str() // l10n message
80a26ed1 463 << endl;
31bda500
DK
464
465 std::string const dlstatus = status.str();
d68d65ad 466 FileFd::Write(status_fd, dlstatus.c_str(), dlstatus.size());
80a26ed1
MV
467 }
468
542ec555
AL
469 if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
470 LookupTag(Message,"Drive")) == false)
471 {
472 char S[300];
96bc43c4 473 snprintf(S,sizeof(S),"603 Media Changed\nFailed: true\n\n");
542ec555
AL
474 if (Debug == true)
475 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
476 OutQueue += S;
477 OutReady = true;
478 return true;
479 }
480
481 char S[300];
96bc43c4 482 snprintf(S,sizeof(S),"603 Media Changed\n\n");
542ec555
AL
483 if (Debug == true)
484 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
485 OutQueue += S;
486 OutReady = true;
3b5421b4
AL
487 return true;
488}
0118833a 489 /*}}}*/
0a8a80e5
AL
490// Worker::SendConfiguration - Send the config to the method /*{{{*/
491// ---------------------------------------------------------------------
492/* */
493bool pkgAcquire::Worker::SendConfiguration()
494{
495 if (Config->SendConfig == false)
496 return true;
497
498 if (OutFd == -1)
499 return false;
0a8a80e5 500
d280d03a 501 /* Write out all of the configuration directives by walking the
0a8a80e5 502 configuration tree */
d280d03a
DK
503 std::ostringstream Message;
504 Message << "601 Configuration\n";
505 _config->Dump(Message, NULL, "Config-Item: %F=%V\n", false);
506 Message << '\n';
0a8a80e5
AL
507
508 if (Debug == true)
d280d03a
DK
509 clog << " -> " << Access << ':' << QuoteString(Message.str(),"\n") << endl;
510 OutQueue += Message.str();
511 OutReady = true;
512
0a8a80e5
AL
513 return true;
514}
515 /*}}}*/
516// Worker::QueueItem - Add an item to the outbound queue /*{{{*/
517// ---------------------------------------------------------------------
518/* Send a URI Acquire message to the method */
519bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
520{
521 if (OutFd == -1)
522 return false;
523
524 string Message = "600 URI Acquire\n";
525 Message.reserve(300);
526 Message += "URI: " + Item->URI;
527 Message += "\nFilename: " + Item->Owner->DestFile;
528 Message += Item->Owner->Custom600Headers();
529 Message += "\n\n";
530
531 if (Debug == true)
532 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
533 OutQueue += Message;
534 OutReady = true;
535
536 return true;
537}
538 /*}}}*/
539// Worker::OutFdRead - Out bound FD is ready /*{{{*/
540// ---------------------------------------------------------------------
541/* */
542bool pkgAcquire::Worker::OutFdReady()
543{
ee7af1bd
YY
544 int Res;
545 do
546 {
547 Res = write(OutFd,OutQueue.c_str(),OutQueue.length());
548 }
549 while (Res < 0 && errno == EINTR);
550
551 if (Res <= 0)
0a8a80e5 552 return MethodFailure();
ee7af1bd
YY
553
554 OutQueue.erase(0,Res);
0a8a80e5
AL
555 if (OutQueue.empty() == true)
556 OutReady = false;
557
558 return true;
559}
560 /*}}}*/
561// Worker::InFdRead - In bound FD is ready /*{{{*/
562// ---------------------------------------------------------------------
563/* */
564bool pkgAcquire::Worker::InFdReady()
565{
566 if (ReadMessages() == false)
567 return false;
568 RunMessages();
569 return true;
570}
571 /*}}}*/
572// Worker::MethodFailure - Called when the method fails /*{{{*/
573// ---------------------------------------------------------------------
1e3f4083 574/* This is called when the method is believed to have failed, probably because
0a8a80e5
AL
575 read returned -1. */
576bool pkgAcquire::Worker::MethodFailure()
577{
76d97c26
AL
578 _error->Error("Method %s has died unexpectedly!",Access.c_str());
579
ab7f4d7c
MV
580 // do not reap the child here to show meaningfull error to the user
581 ExecWait(Process,Access.c_str(),false);
0a8a80e5
AL
582 Process = -1;
583 close(InFd);
584 close(OutFd);
585 InFd = -1;
586 OutFd = -1;
587 OutReady = false;
588 InReady = false;
589 OutQueue = string();
590 MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
591
592 return false;
593}
594 /*}}}*/
8267fe24
AL
595// Worker::Pulse - Called periodically /*{{{*/
596// ---------------------------------------------------------------------
597/* */
598void pkgAcquire::Worker::Pulse()
599{
600 if (CurrentItem == 0)
601 return;
542ec555 602
8267fe24
AL
603 struct stat Buf;
604 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
605 return;
606 CurrentSize = Buf.st_size;
18ef0a78
AL
607
608 // Hmm? Should not happen...
609 if (CurrentSize > TotalSize && TotalSize != 0)
610 TotalSize = CurrentSize;
8267fe24
AL
611}
612 /*}}}*/
613// Worker::ItemDone - Called when the current item is finished /*{{{*/
614// ---------------------------------------------------------------------
615/* */
616void pkgAcquire::Worker::ItemDone()
617{
618 CurrentItem = 0;
619 CurrentSize = 0;
620 TotalSize = 0;
621 Status = string();
622}
623 /*}}}*/