]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-worker.cc,v 1.2 1998/10/20 02:39:13 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>
28 // Worker::Worker - Constructor for Queue startup /*{{{*/
29 // ---------------------------------------------------------------------
31 pkgAcquire::Worker::Worker(Queue
*Q
,string Acc
)
40 // Worker::Worker - Constructor for method config startup /*{{{*/
41 // ---------------------------------------------------------------------
43 pkgAcquire::Worker::Worker(MethodConfig
*Cnf
)
52 // Worker::Construct - Constructor helper /*{{{*/
53 // ---------------------------------------------------------------------
55 void pkgAcquire::Worker::Construct()
61 Debug
= _config
->FindB("Debug::pkgAcquire::Worker",false);
64 // Worker::~Worker - Destructor /*{{{*/
65 // ---------------------------------------------------------------------
67 pkgAcquire::Worker::~Worker()
76 // Worker::Start - Start the worker process /*{{{*/
77 // ---------------------------------------------------------------------
78 /* This forks the method and inits the communication channel */
79 bool pkgAcquire::Worker::Start()
81 // Get the method path
82 string Method
= _config
->FindDir("Dir::Bin::Methods") + Access
;
83 if (FileExists(Method
) == false)
84 return _error
->Error("The method driver %s could not be found.",Method
.c_str());
87 clog
<< "Starting method '" << Method
<< '\'' << endl
;
90 int Pipes
[4] = {-1,-1,-1,-1};
91 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
93 _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
94 for (int I
= 0; I
!= 4; I
++)
99 // Fork off the process
103 cerr
<< "FATAL -> Failed to fork." << endl
;
107 // Spawn the subprocess
111 dup2(Pipes
[1],STDOUT_FILENO
);
112 dup2(Pipes
[2],STDIN_FILENO
);
113 dup2(((filebuf
*)clog
.rdbuf())->fd(),STDERR_FILENO
);
114 for (int I
= 0; I
!= 4; I
++)
116 SetCloseExec(STDOUT_FILENO
,false);
117 SetCloseExec(STDIN_FILENO
,false);
118 SetCloseExec(STDERR_FILENO
,false);
121 Args
[0] = Method
.c_str();
123 execv(Args
[0],(char **)Args
);
124 cerr
<< "Failed to exec method " << Args
[0] << endl
;
131 SetNonBlock(Pipes
[0],true);
132 SetNonBlock(Pipes
[3],true);
136 // Read the configuration data
137 if (WaitFd(InFd
) == false ||
138 ReadMessages() == false)
139 return _error
->Error("Method %s did not start correctly",Method
.c_str());
146 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This pulls full messages from the input FD into the message buffer.
149 It assumes that messages will not pause during transit so no
150 fancy buffering is used. */
151 bool pkgAcquire::Worker::ReadMessages()
158 int Res
= read(InFd
,End
,sizeof(Buffer
) - (End
-Buffer
));
160 // Process is dead, this is kind of bad..
163 if (waitpid(Process
,0,0) != Process
)
164 _error
->Warning("I waited but nothing was there!");
179 // Look for the end of the message
180 for (char *I
= Buffer
; I
< End
; I
++)
182 if (I
[0] != '\n' || I
[1] != '\n')
185 // Pull the message out
186 string
Message(Buffer
,0,I
-Buffer
);
189 for (; I
< End
&& *I
== '\n'; I
++);
191 memmove(Buffer
,I
,End
-Buffer
);
195 clog
<< "Message " << Access
<< ':' << QuoteString(Message
,"\n") << endl
;
197 MessageQueue
.push_back(Message
);
202 if (WaitFd(InFd
) == false)
210 // Worker::RunMessage - Empty the message queue /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This takes the messages from the message queue and runs them through
213 the parsers in order. */
214 bool pkgAcquire::Worker::RunMessages()
216 while (MessageQueue
.empty() == false)
218 string Message
= MessageQueue
.front();
219 MessageQueue
.erase(MessageQueue
.begin());
221 // Fetch the message number
223 int Number
= strtol(Message
.c_str(),&End
,10);
224 if (End
== Message
.c_str())
225 return _error
->Error("Invalid message from method %s: %s",Access
.c_str(),Message
.c_str());
227 // Determine the message number and dispatch
231 if (Capabilities(Message
) == false)
232 return _error
->Error("Unable to process Capabilities message from %s",Access
.c_str());
239 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
240 // ---------------------------------------------------------------------
241 /* This parses the capabilities message and dumps it into the configuration
243 bool pkgAcquire::Worker::Capabilities(string Message
)
248 Config
->Version
= LookupTag(Message
,"Version");
249 Config
->SingleInstance
= StringToBool(LookupTag(Message
,"Single-Instance"),false);
250 Config
->PreScan
= StringToBool(LookupTag(Message
,"Pre-Scan"),false);
255 clog
<< "Configured access method " << Config
->Access
<< endl
;
256 clog
<< "Version: " << Config
->Version
<< " SingleInstance: " <<
257 Config
->SingleInstance
<< " PreScan: " << Config
->PreScan
<< endl
;