]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.20 1999/03/16 00:43:55 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>
23 #include <apt-pkg/strutl.h>
34 // Worker::Worker - Constructor for Queue startup /*{{{*/
35 // ---------------------------------------------------------------------
37 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
,
38 pkgAcquireStatus
*Log
) : Log(Log
)
48 // Worker::Worker - Constructor for method config startup /*{{{*/
49 // ---------------------------------------------------------------------
51 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
61 // Worker::Construct - Constructor helper /*{{{*/
62 // ---------------------------------------------------------------------
64 void pkgAcquire::Worker::Construct()
73 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
76 // Worker::~Worker - Destructor /*{{{*/
77 // ---------------------------------------------------------------------
79 pkgAcquire::Worker::~Worker()
87 if (waitpid(Process
,0,0) != Process
)
88 _error
->Warning("I waited but nothing was there!");
92 // Worker::Start - Start the worker process /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This forks the method and inits the communication channel */
95 bool pkgAcquire::Worker::Start()
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());
103 clog
<< "Starting method '" << Method
<< '\'' << endl
;
106 int Pipes
[4] = {-1,-1,-1,-1};
107 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
109 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
110 for (int I
= 0; I
!= 4; I
++)
114 for (int I
= 0; I
!= 4; I
++)
115 SetCloseExec(Pipes
[0],true);
117 // Fork off the process
121 cerr
<< "FATAL -> Failed to fork." << endl
;
125 // Spawn the subprocess
129 dup2(Pipes
[1],STDOUT_FILENO
);
130 dup2(Pipes
[2],STDIN_FILENO
);
131 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
132 SetCloseExec(STDOUT_FILENO
,false);
133 SetCloseExec(STDIN_FILENO
,false);
134 SetCloseExec(STDERR_FILENO
,false);
136 signal(SIGPIPE
,SIG_DFL
);
137 signal(SIGQUIT
,SIG_DFL
);
138 signal(SIGINT
,SIG_DFL
);
139 signal(SIGWINCH
,SIG_DFL
);
140 signal(SIGCONT
,SIG_DFL
);
141 signal(SIGTSTP
,SIG_DFL
);
143 // Close all of our FDs - just in case
144 for (int K
= 3; K
!= 40; K
++)
145 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
148 Args
[0] = Method
.c_str();
150 execv(Args
[0],(char **)Args
);
151 cerr
<< "Failed to exec method " << Args
[0] << endl
;
158 SetNonBlock(Pipes
[0],true);
159 SetNonBlock(Pipes
[3],true);
165 // Read the configuration data
166 if (WaitFd(InFd
) == false ||
167 ReadMessages() == false)
168 return _error
->Error("Method %s did not start correctly",Method
.c_str());
177 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
178 // ---------------------------------------------------------------------
180 bool pkgAcquire::Worker::ReadMessages()
182 if (::ReadMessages(InFd
,MessageQueue
) == false)
183 return MethodFailure();
187 // Worker::RunMessage - Empty the message queue /*{{{*/
188 // ---------------------------------------------------------------------
189 /* This takes the messages from the message queue and runs them through
190 the parsers in order. */
191 bool pkgAcquire::Worker::RunMessages()
193 while (MessageQueue
.empty() == false)
195 string Message
= MessageQueue
.front();
196 MessageQueue
.erase(MessageQueue
.begin());
199 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
201 // Fetch the message number
203 int Number
= strtol(Message
.c_str(),&End
,10);
204 if (End
== Message
.c_str())
205 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
207 string URI
= LookupTag(Message
,"URI");
208 pkgAcquire::Queue::QItem
*Itm
= 0;
209 if (URI
.empty() == false)
210 Itm
= OwnerQ
->FindItem(URI
,this);
212 // Determine the message number and dispatch
217 if (Capabilities(Message
) == false)
218 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
224 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
229 Status
= LookupTag(Message
,"Message");
237 _error
->Error("Method gave invalid 200 URI Start message");
243 TotalSize
= atoi(LookupTag(Message
,"Size","0").c_str());
244 Itm
->Owner
->Start(Message
,atoi(LookupTag(Message
,"Size","0").c_str()));
257 _error
->Error("Method gave invalid 201 URI Done message");
261 pkgAcquire::Item
*Owner
= Itm
->Owner
;
262 pkgAcquire::ItemDesc Desc
= *Itm
;
263 OwnerQ
->ItemDone(Itm
);
264 Owner
->Done(Message
,atoi(LookupTag(Message
,"Size","0").c_str()),
265 LookupTag(Message
,"MD5-Hash"));
268 // Log that we are done
271 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true ||
272 StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
274 /* Hide 'hits' for local only sources - we also manage to
276 if (Config
->LocalOnly
== false)
290 _error
->Error("Method gave invalid 400 URI Failure message");
294 pkgAcquire::Item
*Owner
= Itm
->Owner
;
295 pkgAcquire::ItemDesc Desc
= *Itm
;
296 OwnerQ
->ItemDone(Itm
);
297 Owner
->Failed(Message
,Config
);
306 // 401 General Failure
308 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
313 MediaChange(Message
);
320 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
321 // ---------------------------------------------------------------------
322 /* This parses the capabilities message and dumps it into the configuration
324 bool pkgAcquire::Worker::Capabilities(string Message
)
329 Config
->Version
= LookupTag(Message
,"Version");
330 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
331 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
332 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
333 Config
->LocalOnly
= StringToBool(LookupTag(Message
,"Local-Only"),false);
338 clog
<< "Configured access method " << Config
->Access
<< endl
;
339 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
340 Config
->SingleInstance
<<
341 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
342 Config
->SendConfig
<< endl
;
348 // Worker::MediaChange - Request a media change /*{{{*/
349 // ---------------------------------------------------------------------
351 bool pkgAcquire::Worker::MediaChange(string Message
)
353 if (Log
== 0 || Log
->MediaChange(LookupTag(Message
,"Media"),
354 LookupTag(Message
,"Drive")) == false)
357 sprintf(S
,"603 Media Changed\nFailed: true\n\n");
359 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
366 sprintf(S
,"603 Media Changed\n\n");
368 clog
<< " -> " << Access
<< ':' << QuoteString(S
,"\n") << endl
;
374 // Worker::SendConfiguration - Send the config to the method /*{{{*/
375 // ---------------------------------------------------------------------
377 bool pkgAcquire::Worker::SendConfiguration()
379 if (Config
->SendConfig
== false)
385 string Message
= "601 Configuration\n";
386 Message
.reserve(2000);
388 /* Write out all of the configuration directives by walking the
389 configuration tree */
390 const Configuration::Item
*Top
= _config
->Tree(0);
393 if (Top
->Value
.empty() == false)
395 string Line
= "Config-Item: " + Top
->FullTag() + "=";
396 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
406 while (Top
!= 0 && Top
->Next
== 0)
414 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
421 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
422 // ---------------------------------------------------------------------
423 /* Send a URI Acquire message to the method */
424 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
429 string Message
= "600 URI Acquire\n";
430 Message
.reserve(300);
431 Message
+= "URI: " + Item
->URI
;
432 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
433 Message
+= Item
->Owner
->Custom600Headers();
437 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
444 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
445 // ---------------------------------------------------------------------
447 bool pkgAcquire::Worker::OutFdReady()
452 Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
454 while (Res
< 0 && errno
== EINTR
);
457 return MethodFailure();
459 // Hmm.. this should never happen.
463 OutQueue
.erase(0,Res
);
464 if (OutQueue
.empty() == true)
470 // Worker::InFdRead - In bound FD is ready /*{{{*/
471 // ---------------------------------------------------------------------
473 bool pkgAcquire::Worker::InFdReady()
475 if (ReadMessages() == false)
481 // Worker::MethodFailure - Called when the method fails /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This is called when the method is belived to have failed, probably because
485 bool pkgAcquire::Worker::MethodFailure()
487 _error
->Error("Method %s has died unexpectedly!",Access
.c_str());
489 if (waitpid(Process
,0,0) != Process
)
490 _error
->Warning("I waited but nothing was there!");
499 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());
504 // Worker::Pulse - Called periodically /*{{{*/
505 // ---------------------------------------------------------------------
507 void pkgAcquire::Worker::Pulse()
509 if (CurrentItem
== 0)
513 if (stat(CurrentItem
->Owner
->DestFile
.c_str(),&Buf
) != 0)
515 CurrentSize
= Buf
.st_size
;
518 // Worker::ItemDone - Called when the current item is finished /*{{{*/
519 // ---------------------------------------------------------------------
521 void pkgAcquire::Worker::ItemDone()