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