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