]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
More CD support
[apt.git] / apt-pkg / acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-worker.cc,v 1.12 1998/11/14 01:39:44 jgg Exp $
4 /* ######################################################################
5
6 Acquire Worker
7
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
11
12 ##################################################################### */
13 /*}}}*/
14 // Include Files /*{{{*/
15 #ifdef __GNUG__
16 #pragma implementation "apt-pkg/acquire-worker.h"
17 #endif
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 <strutl.h>
24
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <wait.h>
29 /*}}}*/
30
31 // Worker::Worker - Constructor for Queue startup /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
35 pkgAcquireStatus *Log) : Log(Log)
36 {
37 OwnerQ = Q;
38 Config = Cnf;
39 Access = Cnf->Access;
40 CurrentItem = 0;
41
42 Construct();
43 }
44 /*}}}*/
45 // Worker::Worker - Constructor for method config startup /*{{{*/
46 // ---------------------------------------------------------------------
47 /* */
48 pkgAcquire::Worker::Worker(MethodConfig *Cnf)
49 {
50 OwnerQ = 0;
51 Config = Cnf;
52 Access = Cnf->Access;
53 CurrentItem = 0;
54
55 Construct();
56 }
57 /*}}}*/
58 // Worker::Construct - Constructor helper /*{{{*/
59 // ---------------------------------------------------------------------
60 /* */
61 void pkgAcquire::Worker::Construct()
62 {
63 NextQueue = 0;
64 NextAcquire = 0;
65 Process = -1;
66 InFd = -1;
67 OutFd = -1;
68 OutReady = false;
69 InReady = false;
70 Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
71 }
72 /*}}}*/
73 // Worker::~Worker - Destructor /*{{{*/
74 // ---------------------------------------------------------------------
75 /* */
76 pkgAcquire::Worker::~Worker()
77 {
78 close(InFd);
79 close(OutFd);
80
81 if (Process > 0)
82 {
83 kill(Process,SIGINT);
84 if (waitpid(Process,0,0) != Process)
85 _error->Warning("I waited but nothing was there!");
86 }
87 }
88 /*}}}*/
89 // Worker::Start - Start the worker process /*{{{*/
90 // ---------------------------------------------------------------------
91 /* This forks the method and inits the communication channel */
92 bool pkgAcquire::Worker::Start()
93 {
94 // Get the method path
95 string Method = _config->FindDir("Dir::Bin::Methods") + Access;
96 if (FileExists(Method) == false)
97 return _error->Error("The method driver %s could not be found.",Method.c_str());
98
99 if (Debug == true)
100 clog << "Starting method '" << Method << '\'' << endl;
101
102 // Create the pipes
103 int Pipes[4] = {-1,-1,-1,-1};
104 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
105 {
106 _error->Errno("pipe","Failed to create IPC pipe to subprocess");
107 for (int I = 0; I != 4; I++)
108 close(Pipes[I]);
109 return false;
110 }
111 for (int I = 0; I != 4; I++)
112 SetCloseExec(Pipes[0],true);
113
114 // Fork off the process
115 Process = fork();
116 if (Process < 0)
117 {
118 cerr << "FATAL -> Failed to fork." << endl;
119 exit(100);
120 }
121
122 // Spawn the subprocess
123 if (Process == 0)
124 {
125 // Setup the FDs
126 dup2(Pipes[1],STDOUT_FILENO);
127 dup2(Pipes[2],STDIN_FILENO);
128 dup2(((filebuf *)clog.rdbuf())->fd(),STDERR_FILENO);
129 SetCloseExec(STDOUT_FILENO,false);
130 SetCloseExec(STDIN_FILENO,false);
131 SetCloseExec(STDERR_FILENO,false);
132
133 const char *Args[2];
134 Args[0] = Method.c_str();
135 Args[1] = 0;
136 execv(Args[0],(char **)Args);
137 cerr << "Failed to exec method " << Args[0] << endl;
138 exit(100);
139 }
140
141 // Fix up our FDs
142 InFd = Pipes[0];
143 OutFd = Pipes[3];
144 SetNonBlock(Pipes[0],true);
145 SetNonBlock(Pipes[3],true);
146 close(Pipes[1]);
147 close(Pipes[2]);
148 OutReady = false;
149 InReady = true;
150
151 // Read the configuration data
152 if (WaitFd(InFd) == false ||
153 ReadMessages() == false)
154 return _error->Error("Method %s did not start correctly",Method.c_str());
155
156 RunMessages();
157 if (OwnerQ != 0)
158 SendConfiguration();
159
160 return true;
161 }
162 /*}}}*/
163 // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
164 // ---------------------------------------------------------------------
165 /* */
166 bool pkgAcquire::Worker::ReadMessages()
167 {
168 if (::ReadMessages(InFd,MessageQueue) == false)
169 return MethodFailure();
170 return true;
171 }
172 /*}}}*/
173 // Worker::RunMessage - Empty the message queue /*{{{*/
174 // ---------------------------------------------------------------------
175 /* This takes the messages from the message queue and runs them through
176 the parsers in order. */
177 bool pkgAcquire::Worker::RunMessages()
178 {
179 while (MessageQueue.empty() == false)
180 {
181 string Message = MessageQueue.front();
182 MessageQueue.erase(MessageQueue.begin());
183
184 if (Debug == true)
185 clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
186
187 // Fetch the message number
188 char *End;
189 int Number = strtol(Message.c_str(),&End,10);
190 if (End == Message.c_str())
191 return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
192
193 string URI = LookupTag(Message,"URI");
194 pkgAcquire::Queue::QItem *Itm = 0;
195 if (URI.empty() == false)
196 Itm = OwnerQ->FindItem(URI,this);
197
198 // Determine the message number and dispatch
199 switch (Number)
200 {
201 // 100 Capabilities
202 case 100:
203 if (Capabilities(Message) == false)
204 return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
205 break;
206
207 // 101 Log
208 case 101:
209 if (Debug == true)
210 clog << " <- (log) " << LookupTag(Message,"Message") << endl;
211 break;
212
213 // 102 Status
214 case 102:
215 Status = LookupTag(Message,"Message");
216 break;
217
218 // 200 URI Start
219 case 200:
220 {
221 if (Itm == 0)
222 {
223 _error->Error("Method gave invalid 200 URI Start message");
224 break;
225 }
226
227 CurrentItem = Itm;
228 CurrentSize = 0;
229 TotalSize = atoi(LookupTag(Message,"Size","0").c_str());
230 Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
231
232 if (Log != 0)
233 Log->Fetch(*Itm);
234
235 break;
236 }
237
238 // 201 URI Done
239 case 201:
240 {
241 if (Itm == 0)
242 {
243 _error->Error("Method gave invalid 201 URI Done message");
244 break;
245 }
246
247 pkgAcquire::Item *Owner = Itm->Owner;
248 pkgAcquire::ItemDesc Desc = *Itm;
249 OwnerQ->ItemDone(Itm);
250 Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
251 LookupTag(Message,"MD5-Hash"));
252 ItemDone();
253
254 // Log that we are done
255 if (Log != 0)
256 {
257 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
258 StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
259 Log->IMSHit(Desc);
260 else
261 Log->Done(Desc);
262 }
263 break;
264 }
265
266 // 400 URI Failure
267 case 400:
268 {
269 if (Itm == 0)
270 {
271 _error->Error("Method gave invalid 400 URI Failure message");
272 break;
273 }
274
275 pkgAcquire::Item *Owner = Itm->Owner;
276 pkgAcquire::ItemDesc Desc = *Itm;
277 OwnerQ->ItemDone(Itm);
278 Owner->Failed(Message);
279 ItemDone();
280
281 if (Log != 0)
282 Log->Fail(Desc);
283
284 break;
285 }
286
287 // 401 General Failure
288 case 401:
289 _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
290 break;
291 }
292 }
293 return true;
294 }
295 /*}}}*/
296 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This parses the capabilities message and dumps it into the configuration
299 structure. */
300 bool pkgAcquire::Worker::Capabilities(string Message)
301 {
302 if (Config == 0)
303 return true;
304
305 Config->Version = LookupTag(Message,"Version");
306 Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
307 Config->PreScan = StringToBool(LookupTag(Message,"Pre-Scan"),false);
308 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
309 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
310 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
311
312 // Some debug text
313 if (Debug == true)
314 {
315 clog << "Configured access method " << Config->Access << endl;
316 clog << "Version:" << Config->Version << " SingleInstance:" <<
317 Config->SingleInstance << " PreScan: " << Config->PreScan <<
318 " Pipeline:" << Config->Pipeline << " SendConfig:" <<
319 Config->SendConfig << endl;
320 }
321
322 return true;
323 }
324 /*}}}*/
325 // Worker::SendConfiguration - Send the config to the method /*{{{*/
326 // ---------------------------------------------------------------------
327 /* */
328 bool pkgAcquire::Worker::SendConfiguration()
329 {
330 if (Config->SendConfig == false)
331 return true;
332
333 if (OutFd == -1)
334 return false;
335
336 string Message = "601 Configuration\n";
337 Message.reserve(2000);
338
339 /* Write out all of the configuration directives by walking the
340 configuration tree */
341 const Configuration::Item *Top = _config->Tree(0);
342 for (; Top != 0;)
343 {
344 if (Top->Value.empty() == false)
345 {
346 string Line = "Config-Item: " + Top->FullTag() + "=";
347 Line += QuoteString(Top->Value,"\n") + '\n';
348 Message += Line;
349 }
350
351 if (Top->Child != 0)
352 {
353 Top = Top->Child;
354 continue;
355 }
356
357 while (Top != 0 && Top->Next == 0)
358 Top = Top->Parent;
359 if (Top != 0)
360 Top = Top->Next;
361 }
362 Message += '\n';
363
364 if (Debug == true)
365 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
366 OutQueue += Message;
367 OutReady = true;
368
369 return true;
370 }
371 /*}}}*/
372 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
373 // ---------------------------------------------------------------------
374 /* Send a URI Acquire message to the method */
375 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
376 {
377 if (OutFd == -1)
378 return false;
379
380 string Message = "600 URI Acquire\n";
381 Message.reserve(300);
382 Message += "URI: " + Item->URI;
383 Message += "\nFilename: " + Item->Owner->DestFile;
384 Message += Item->Owner->Custom600Headers();
385 Message += "\n\n";
386
387 if (Debug == true)
388 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
389 OutQueue += Message;
390 OutReady = true;
391
392 return true;
393 }
394 /*}}}*/
395 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
396 // ---------------------------------------------------------------------
397 /* */
398 bool pkgAcquire::Worker::OutFdReady()
399 {
400 int Res = write(OutFd,OutQueue.begin(),OutQueue.length());
401 if (Res <= 0)
402 return MethodFailure();
403
404 // Hmm.. this should never happen.
405 if (Res < 0)
406 return true;
407
408 OutQueue.erase(0,Res);
409 if (OutQueue.empty() == true)
410 OutReady = false;
411
412 return true;
413 }
414 /*}}}*/
415 // Worker::InFdRead - In bound FD is ready /*{{{*/
416 // ---------------------------------------------------------------------
417 /* */
418 bool pkgAcquire::Worker::InFdReady()
419 {
420 if (ReadMessages() == false)
421 return false;
422 RunMessages();
423 return true;
424 }
425 /*}}}*/
426 // Worker::MethodFailure - Called when the method fails /*{{{*/
427 // ---------------------------------------------------------------------
428 /* This is called when the method is belived to have failed, probably because
429 read returned -1. */
430 bool pkgAcquire::Worker::MethodFailure()
431 {
432 cerr << "Method " << Access << " has died unexpectedly!" << endl;
433 if (waitpid(Process,0,0) != Process)
434 _error->Warning("I waited but nothing was there!");
435 Process = -1;
436 close(InFd);
437 close(OutFd);
438 InFd = -1;
439 OutFd = -1;
440 OutReady = false;
441 InReady = false;
442 OutQueue = string();
443 MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
444
445 return false;
446 }
447 /*}}}*/
448 // Worker::Pulse - Called periodically /*{{{*/
449 // ---------------------------------------------------------------------
450 /* */
451 void pkgAcquire::Worker::Pulse()
452 {
453 if (CurrentItem == 0)
454 return;
455
456
457 struct stat Buf;
458 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
459 return;
460 CurrentSize = Buf.st_size;
461 }
462 /*}}}*/
463 // Worker::ItemDone - Called when the current item is finished /*{{{*/
464 // ---------------------------------------------------------------------
465 /* */
466 void pkgAcquire::Worker::ItemDone()
467 {
468 CurrentItem = 0;
469 CurrentSize = 0;
470 TotalSize = 0;
471 Status = string();
472 }
473 /*}}}*/