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