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