]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.14 1998/12/04 21:16:47 jgg Exp $
4 /* ######################################################################
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
12 ##################################################################### */
14 // Include Files /*{{{*/
16 #pragma implementation "apt-pkg/acquire-worker.h"
18 #include <apt-pkg/acquire-worker.h>
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/fileutl.h>
32 // Worker::Worker - Constructor for Queue startup /*{{{*/
33 // ---------------------------------------------------------------------
35 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
36 pkgAcquireStatus
*Log
) : Log(Log
)
46 // Worker::Worker - Constructor for method config startup /*{{{*/
47 // ---------------------------------------------------------------------
49 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
59 // Worker::Construct - Constructor helper /*{{{*/
60 // ---------------------------------------------------------------------
62 void pkgAcquire::Worker::Construct()
71 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
74 // Worker::~Worker - Destructor /*{{{*/
75 // ---------------------------------------------------------------------
77 pkgAcquire::Worker::~Worker()
85 if (waitpid(Process
,0,0) != Process
)
86 _error
->Warning("I waited but nothing was there!");
90 // Worker::Start - Start the worker process /*{{{*/
91 // ---------------------------------------------------------------------
92 /* This forks the method and inits the communication channel */
93 bool pkgAcquire::Worker::Start()
95 // Get the method path
96 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
97 if (FileExists(Method
) == false)
98 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
101 clog
<< "Starting method '" << Method
<< '\'' << endl
;
104 int Pipes
[4] = {-1,-1,-1,-1};
105 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
107 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
108 for (int I
= 0; I
!= 4; I
++)
112 for (int I
= 0; I
!= 4; I
++)
113 SetCloseExec(Pipes
[0],true);
115 // Fork off the process
119 cerr
<< "FATAL -> Failed to fork." << endl
;
123 // Spawn the subprocess
127 dup2(Pipes
[1],STDOUT_FILENO
);
128 dup2(Pipes
[2],STDIN_FILENO
);
129 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
130 SetCloseExec(STDOUT_FILENO
,false);
131 SetCloseExec(STDIN_FILENO
,false);
132 SetCloseExec(STDERR_FILENO
,false);
135 Args
[0] = Method
.c_str();
137 execv(Args
[0],(char **)Args
);
138 cerr
<< "Failed to exec method " << Args
[0] << endl
;
145 SetNonBlock(Pipes
[0],true);
146 SetNonBlock(Pipes
[3],true);
152 // Read the configuration data
153 if (WaitFd(InFd
) == false ||
154 ReadMessages() == false)
155 return _error
->Error("Method %s did not start correctly",Method
.c_str());
164 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
165 // ---------------------------------------------------------------------
167 bool pkgAcquire::Worker::ReadMessages()
169 if (::ReadMessages(InFd
,MessageQueue
) == false)
170 return MethodFailure();
174 // Worker::RunMessage - Empty the message queue /*{{{*/
175 // ---------------------------------------------------------------------
176 /* This takes the messages from the message queue and runs them through
177 the parsers in order. */
178 bool pkgAcquire::Worker::RunMessages()
180 while (MessageQueue
.empty() == false)
182 string Message
= MessageQueue
.front();
183 MessageQueue
.erase(MessageQueue
.begin());
186 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
188 // Fetch the message number
190 int Number
= strtol(Message
.c_str(),&End
,10);
191 if (End
== Message
.c_str())
192 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
194 string URI
= LookupTag(Message
,"URI");
195 pkgAcquire::Queue::QItem
*Itm
= 0;
196 if (URI
.empty() == false)
197 Itm
= OwnerQ
->FindItem(URI
,this);
199 // Determine the message number and dispatch
204 if (Capabilities(Message
) == false)
205 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
211 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
216 Status
= LookupTag(Message
,"Message");
224 _error
->Error("Method gave invalid 200 URI Start message");
230 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
231 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
244 _error
->Error("Method gave invalid 201 URI Done message");
248 pkgAcquire::Item
*Owner
= Itm
->Owner
;
249 pkgAcquire::ItemDesc Desc
= *Itm
;
250 OwnerQ
->ItemDone(Itm
);
251 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
252 LookupTag(Message
,"MD5-Hash"));
255 // Log that we are done
258 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
259 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
272 _error
->Error("Method gave invalid 400 URI Failure message");
276 pkgAcquire::Item
*Owner
= Itm
->Owner
;
277 pkgAcquire::ItemDesc Desc
= *Itm
;
278 OwnerQ
->ItemDone(Itm
);
279 Owner
->Failed(Message
);
288 // 401 General Failure
290 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
295 MediaChange(Message
);
302 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
303 // ---------------------------------------------------------------------
304 /* This parses the capabilities message and dumps it into the configuration
306 bool pkgAcquire::Worker::Capabilities(string Message
)
311 Config
->Version
= LookupTag(Message
,"Version");
312 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
313 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
314 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
315 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
320 clog
<< "Configured access method " << Config
->Access
<< endl
;
321 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
322 Config
->SingleInstance
<<
323 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
324 Config
->SendConfig
<< endl
;
330 // Worker::MediaChange - Request a media change /*{{{*/
331 // ---------------------------------------------------------------------
333 bool pkgAcquire::Worker::MediaChange(string Message
)
335 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
336 LookupTag(Message
,"Drive")) == false)
339 sprintf(S
,"603 Media Changed\nFailed: true\n\n");
341 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
348 sprintf(S
,"603 Media Changed\n\n");
350 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
356 // Worker::SendConfiguration - Send the config to the method /*{{{*/
357 // ---------------------------------------------------------------------
359 bool pkgAcquire::Worker::SendConfiguration()
361 if (Config
->SendConfig
== false)
367 string Message
= "601 Configuration\n";
368 Message
.reserve(2000);
370 /* Write out all of the configuration directives by walking the
371 configuration tree */
372 const Configuration::Item
*Top
= _config
->Tree(0);
375 if (Top
->Value
.empty() == false)
377 string Line
= "Config-Item: " + Top
->FullTag() + "=";
378 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
388 while (Top
!= 0 && Top
->Next
== 0)
396 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
403 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
404 // ---------------------------------------------------------------------
405 /* Send a URI Acquire message to the method */
406 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
411 string Message
= "600 URI Acquire\n";
412 Message
.reserve(300);
413 Message
+= "URI: " + Item
->URI
;
414 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
415 Message
+= Item
->Owner
->Custom600Headers();
419 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
426 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
427 // ---------------------------------------------------------------------
429 bool pkgAcquire::Worker::OutFdReady()
431 int Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
433 return MethodFailure();
435 // Hmm.. this should never happen.
439 OutQueue
.erase(0,Res
);
440 if (OutQueue
.empty() == true)
446 // Worker::InFdRead - In bound FD is ready /*{{{*/
447 // ---------------------------------------------------------------------
449 bool pkgAcquire::Worker::InFdReady()
451 if (ReadMessages() == false)
457 // Worker::MethodFailure - Called when the method fails /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This is called when the method is belived to have failed, probably because
461 bool pkgAcquire::Worker::MethodFailure()
463 cerr
<< "Method " << Access
<< " has died unexpectedly!" << endl
;
464 if (waitpid(Process
,0,0) != Process
)
465 _error
->Warning("I waited but nothing was there!");
474 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
479 // Worker::Pulse - Called periodically /*{{{*/
480 // ---------------------------------------------------------------------
482 void pkgAcquire::Worker::Pulse()
484 if (CurrentItem
== 0)
488 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
490 CurrentSize
= Buf
.st_size
;
493 // Worker::ItemDone - Called when the current item is finished /*{{{*/
494 // ---------------------------------------------------------------------
496 void pkgAcquire::Worker::ItemDone()