]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
756b309594efebd21d5131ab6e635dee684edb01
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.4 1998/10/22 04:56:40 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>
30 // Worker::Worker - Constructor for Queue startup /*{{{*/
31 // ---------------------------------------------------------------------
33 pkgAcquire::Worker::Worker(Queue
*Q
,MethodConfig
*Cnf
)
43 // Worker::Worker - Constructor for method config startup /*{{{*/
44 // ---------------------------------------------------------------------
46 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
56 // Worker::Construct - Constructor helper /*{{{*/
57 // ---------------------------------------------------------------------
59 void pkgAcquire::Worker::Construct()
68 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
71 // Worker::~Worker - Destructor /*{{{*/
72 // ---------------------------------------------------------------------
74 pkgAcquire::Worker::~Worker()
82 if (waitpid(Process
,0,0) != Process
)
83 _error
->Warning("I waited but nothing was there!");
87 // Worker::Start - Start the worker process /*{{{*/
88 // ---------------------------------------------------------------------
89 /* This forks the method and inits the communication channel */
90 bool pkgAcquire::Worker::Start()
92 // Get the method path
93 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
94 if (FileExists(Method
) == false)
95 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
98 clog
<< "Starting method '" << Method
<< '\'' << endl
;
101 int Pipes
[4] = {-1,-1,-1,-1};
102 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
104 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
105 for (int I
= 0; I
!= 4; I
++)
110 // Fork off the process
114 cerr
<< "FATAL -> Failed to fork." << endl
;
118 // Spawn the subprocess
122 dup2(Pipes
[1],STDOUT_FILENO
);
123 dup2(Pipes
[2],STDIN_FILENO
);
124 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
125 for (int I
= 0; I
!= 4; I
++)
127 SetCloseExec(STDOUT_FILENO
,false);
128 SetCloseExec(STDIN_FILENO
,false);
129 SetCloseExec(STDERR_FILENO
,false);
132 Args
[0] = Method
.c_str();
134 execv(Args
[0],(char **)Args
);
135 cerr
<< "Failed to exec method " << Args
[0] << endl
;
142 SetNonBlock(Pipes
[0],true);
143 SetNonBlock(Pipes
[3],true);
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());
160 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
161 // ---------------------------------------------------------------------
163 bool pkgAcquire::Worker::ReadMessages()
165 if (::ReadMessages(InFd
,MessageQueue
) == false)
166 return MethodFailure();
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. */
175 bool pkgAcquire::Worker::RunMessages()
177 while (MessageQueue
.empty() == false)
179 string Message
= MessageQueue
.front();
180 MessageQueue
.erase(MessageQueue
.begin());
183 clog
<< " <- " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
185 // Fetch the message number
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());
191 // Determine the message number and dispatch
196 if (Capabilities(Message
) == false)
197 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
203 clog
<< " <- (log) " << LookupTag(Message
,"Message") << endl
;
208 Status
= LookupTag(Message
,"Message");
223 // 401 General Failure
225 _error
->Error("Method %s General failure: %s",LookupTag(Message
,"Message").c_str());
232 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
233 // ---------------------------------------------------------------------
234 /* This parses the capabilities message and dumps it into the configuration
236 bool pkgAcquire::Worker::Capabilities(string Message
)
241 Config
->Version
= LookupTag(Message
,"Version");
242 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
243 Config
->PreScan
= StringToBool(LookupTag(Message
,"Pre-Scan"),false);
244 Config
->Pipeline
= StringToBool(LookupTag(Message
,"Pipeline"),false);
245 Config
->SendConfig
= StringToBool(LookupTag(Message
,"Send-Config"),false);
250 clog
<< "Configured access method " << Config
->Access
<< endl
;
251 clog
<< "Version:" << Config
->Version
<< " SingleInstance:" <<
252 Config
->SingleInstance
<< " PreScan: " << Config
->PreScan
<<
253 " Pipeline:" << Config
->Pipeline
<< " SendConfig:" <<
254 Config
->SendConfig
<< endl
;
260 // Worker::SendConfiguration - Send the config to the method /*{{{*/
261 // ---------------------------------------------------------------------
263 bool pkgAcquire::Worker::SendConfiguration()
265 if (Config
->SendConfig
== false)
271 string Message
= "601 Configuration\n";
272 Message
.reserve(2000);
274 /* Write out all of the configuration directives by walking the
275 configuration tree */
276 const Configuration::Item
*Top
= _config
->Tree(0);
279 if (Top
->Value
.empty() == false)
281 string Line
= "Config-Item: " + Top
->FullTag() + "=";
282 Line
+= QuoteString(Top
->Value
,"\n") + '\n';
292 while (Top
!= 0 && Top
->Next
== 0)
300 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
307 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
308 // ---------------------------------------------------------------------
309 /* Send a URI Acquire message to the method */
310 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem
*Item
)
315 string Message
= "600 URI Acquire\n";
316 Message
.reserve(300);
317 Message
+= "URI: " + Item
->URI
;
318 Message
+= "\nFilename: " + Item
->Owner
->DestFile
;
319 Message
+= Item
->Owner
->Custom600Headers();
323 clog
<< " -> " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
330 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
331 // ---------------------------------------------------------------------
333 bool pkgAcquire::Worker::OutFdReady()
335 int Res
= write(OutFd
,OutQueue
.begin(),OutQueue
.length());
337 return MethodFailure();
339 // Hmm.. this should never happen.
343 OutQueue
.erase(0,Res
);
344 if (OutQueue
.empty() == true)
350 // Worker::InFdRead - In bound FD is ready /*{{{*/
351 // ---------------------------------------------------------------------
353 bool pkgAcquire::Worker::InFdReady()
355 if (ReadMessages() == false)
361 // Worker::MethodFailure - Called when the method fails /*{{{*/
362 // ---------------------------------------------------------------------
363 /* This is called when the method is belived to have failed, probably because
365 bool pkgAcquire::Worker::MethodFailure()
367 cerr
<< "Method " << Access
<< " has died unexpectedly!" << endl
;
368 if (waitpid(Process
,0,0) != Process
)
369 _error
->Warning("I waited but nothing was there!");
378 MessageQueue
.erase(MessageQueue
.begin(),MessageQueue
.end());