]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
688c5e2202d5f5e8bccf0b427ebd52eb829a2bae
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.3 1998/10/20 04:33:12 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/configuration.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/fileutl.h>
29 // Worker::Worker - Constructor for Queue startup /*{{{*/
30 // ---------------------------------------------------------------------
32 pkgAcquire::Worker::Worker(Queue
*Q
,string Acc
)
41 // Worker::Worker - Constructor for method config startup /*{{{*/
42 // ---------------------------------------------------------------------
44 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
53 // Worker::Construct - Constructor helper /*{{{*/
54 // ---------------------------------------------------------------------
56 void pkgAcquire::Worker::Construct()
62 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
65 // Worker::~Worker - Destructor /*{{{*/
66 // ---------------------------------------------------------------------
68 pkgAcquire::Worker::~Worker()
77 // Worker::Start - Start the worker process /*{{{*/
78 // ---------------------------------------------------------------------
79 /* This forks the method and inits the communication channel */
80 bool pkgAcquire::Worker::Start()
82 // Get the method path
83 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
84 if (FileExists(Method
) == false)
85 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
88 clog
<< "Starting method '" << Method
<< '\'' << endl
;
91 int Pipes
[4] = {-1,-1,-1,-1};
92 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
94 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
95 for (int I
= 0; I
!= 4; I
++)
100 // Fork off the process
104 cerr
<< "FATAL -> Failed to fork." << endl
;
108 // Spawn the subprocess
112 dup2(Pipes
[1],STDOUT_FILENO
);
113 dup2(Pipes
[2],STDIN_FILENO
);
114 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
115 for (int I
= 0; I
!= 4; I
++)
117 SetCloseExec(STDOUT_FILENO
,false);
118 SetCloseExec(STDIN_FILENO
,false);
119 SetCloseExec(STDERR_FILENO
,false);
122 Args
[0] = Method
.c_str();
124 execv(Args
[0],(char **)Args
);
125 cerr
<< "Failed to exec method " << Args
[0] << endl
;
132 SetNonBlock(Pipes
[0],true);
133 SetNonBlock(Pipes
[3],true);
137 // Read the configuration data
138 if (WaitFd(InFd
) == false ||
139 ReadMessages() == false)
140 return _error
->Error("Method %s did not start correctly",Method
.c_str());
147 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This pulls full messages from the input FD into the message buffer.
150 It assumes that messages will not pause during transit so no
151 fancy buffering is used. */
152 bool pkgAcquire::Worker::ReadMessages()
159 int Res
= read(InFd
,End
,sizeof(Buffer
) - (End
-Buffer
));
161 // Process is dead, this is kind of bad..
164 if (waitpid(Process
,0,0) != Process
)
165 _error
->Warning("I waited but nothing was there!");
180 // Look for the end of the message
181 for (char *I
= Buffer
; I
< End
; I
++)
183 if (I
[0] != '\n' || I
[1] != '\n')
186 // Pull the message out
187 string
Message(Buffer
,0,I
-Buffer
);
190 for (; I
< End
&& *I
== '\n'; I
++);
192 memmove(Buffer
,I
,End
-Buffer
);
196 clog
<< "Message " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
198 MessageQueue
.push_back(Message
);
203 if (WaitFd(InFd
) == false)
211 // Worker::RunMessage - Empty the message queue /*{{{*/
212 // ---------------------------------------------------------------------
213 /* This takes the messages from the message queue and runs them through
214 the parsers in order. */
215 bool pkgAcquire::Worker::RunMessages()
217 while (MessageQueue
.empty() == false)
219 string Message
= MessageQueue
.front();
220 MessageQueue
.erase(MessageQueue
.begin());
222 // Fetch the message number
224 int Number
= strtol(Message
.c_str(),&End
,10);
225 if (End
== Message
.c_str())
226 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
228 // Determine the message number and dispatch
232 if (Capabilities(Message
) == false)
233 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
240 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
241 // ---------------------------------------------------------------------
242 /* This parses the capabilities message and dumps it into the configuration
244 bool pkgAcquire::Worker::Capabilities(string Message
)
249 Config
->Version
= LookupTag(Message
,"Version");
250 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
251 Config
->PreScan
= StringToBool(LookupTag(Message
,"Pre-Scan"),false);
256 clog
<< "Configured access method " << Config
->Access
<< endl
;
257 clog
<< "Version: " << Config
->Version
<< " SingleInstance: " <<
258 Config
->SingleInstance
<< " PreScan: " << Config
->PreScan
<< endl
;