]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
166055bfcf8df7d6428b82c792a5e6ff44e2261c
[apt.git] / apt-pkg / acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-worker.cc,v 1.24 1999/07/30 05:36:52 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 <apt-pkg/strutl.h>
24
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <errno.h>
31 /*}}}*/
32
33 // Worker::Worker - Constructor for Queue startup /*{{{*/
34 // ---------------------------------------------------------------------
35 /* */
36 pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
37 pkgAcquireStatus *Log) : Log(Log)
38 {
39 OwnerQ = Q;
40 Config = Cnf;
41 Access = Cnf->Access;
42 CurrentItem = 0;
43 TotalSize = 0;
44 CurrentSize = 0;
45
46 Construct();
47 }
48 /*}}}*/
49 // Worker::Worker - Constructor for method config startup /*{{{*/
50 // ---------------------------------------------------------------------
51 /* */
52 pkgAcquire::Worker::Worker(MethodConfig *Cnf)
53 {
54 OwnerQ = 0;
55 Config = Cnf;
56 Access = Cnf->Access;
57 CurrentItem = 0;
58 TotalSize = 0;
59 CurrentSize = 0;
60
61 Construct();
62 }
63 /*}}}*/
64 // Worker::Construct - Constructor helper /*{{{*/
65 // ---------------------------------------------------------------------
66 /* */
67 void pkgAcquire::Worker::Construct()
68 {
69 NextQueue = 0;
70 NextAcquire = 0;
71 Process = -1;
72 InFd = -1;
73 OutFd = -1;
74 OutReady = false;
75 InReady = false;
76 Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
77 }
78 /*}}}*/
79 // Worker::~Worker - Destructor /*{{{*/
80 // ---------------------------------------------------------------------
81 /* */
82 pkgAcquire::Worker::~Worker()
83 {
84 close(InFd);
85 close(OutFd);
86
87 if (Process > 0)
88 {
89 kill(Process,SIGINT);
90 ExecWait(Process,Access.c_str(),true);
91 }
92 }
93 /*}}}*/
94 // Worker::Start - Start the worker process /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This forks the method and inits the communication channel */
97 bool pkgAcquire::Worker::Start()
98 {
99 // Get the method path
100 string Method = _config->FindDir("Dir::Bin::Methods") + Access;
101 if (FileExists(Method) == false)
102 return _error->Error("The method driver %s could not be found.",Method.c_str());
103
104 if (Debug == true)
105 clog << "Starting method '" << Method << '\'' << endl;
106
107 // Create the pipes
108 int Pipes[4] = {-1,-1,-1,-1};
109 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
110 {
111 _error->Errno("pipe","Failed to create IPC pipe to subprocess");
112 for (int I = 0; I != 4; I++)
113 close(Pipes[I]);
114 return false;
115 }
116 for (int I = 0; I != 4; I++)
117 SetCloseExec(Pipes[0],true);
118
119 // Fork off the process
120 Process = ExecFork();
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 ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str());
231 Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
232
233 if (Log != 0)
234 Log->Fetch(*Itm);
235
236 break;
237 }
238
239 // 201 URI Done
240 case 201:
241 {
242 if (Itm == 0)
243 {
244 _error->Error("Method gave invalid 201 URI Done message");
245 break;
246 }
247
248 pkgAcquire::Item *Owner = Itm->Owner;
249 pkgAcquire::ItemDesc Desc = *Itm;
250 OwnerQ->ItemDone(Itm);
251 Pulse();
252 if (TotalSize != 0 &&
253 atoi(LookupTag(Message,"Size","0").c_str()) != TotalSize)
254 _error->Warning("Bizzar Error - File size is not what the server reported %s %u",
255 LookupTag(Message,"Size","0").c_str(),TotalSize);
256
257 Owner->Done(Message,CurrentSize,
258 LookupTag(Message,"MD5-Hash"));
259 ItemDone();
260
261 // Log that we are done
262 if (Log != 0)
263 {
264 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
265 StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
266 {
267 /* Hide 'hits' for local only sources - we also manage to
268 hide gets */
269 if (Config->LocalOnly == false)
270 Log->IMSHit(Desc);
271 }
272 else
273 Log->Done(Desc);
274 }
275 break;
276 }
277
278 // 400 URI Failure
279 case 400:
280 {
281 if (Itm == 0)
282 {
283 _error->Error("Method gave invalid 400 URI Failure message");
284 break;
285 }
286
287 pkgAcquire::Item *Owner = Itm->Owner;
288 pkgAcquire::ItemDesc Desc = *Itm;
289 OwnerQ->ItemDone(Itm);
290 Owner->Failed(Message,Config);
291 ItemDone();
292
293 if (Log != 0)
294 Log->Fail(Desc);
295
296 break;
297 }
298
299 // 401 General Failure
300 case 401:
301 _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
302 break;
303
304 // 403 Media Change
305 case 403:
306 MediaChange(Message);
307 break;
308 }
309 }
310 return true;
311 }
312 /*}}}*/
313 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
314 // ---------------------------------------------------------------------
315 /* This parses the capabilities message and dumps it into the configuration
316 structure. */
317 bool pkgAcquire::Worker::Capabilities(string Message)
318 {
319 if (Config == 0)
320 return true;
321
322 Config->Version = LookupTag(Message,"Version");
323 Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
324 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
325 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
326 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
327
328 // Some debug text
329 if (Debug == true)
330 {
331 clog << "Configured access method " << Config->Access << endl;
332 clog << "Version:" << Config->Version << " SingleInstance:" <<
333 Config->SingleInstance <<
334 " Pipeline:" << Config->Pipeline << " SendConfig:" <<
335 Config->SendConfig << endl;
336 }
337
338 return true;
339 }
340 /*}}}*/
341 // Worker::MediaChange - Request a media change /*{{{*/
342 // ---------------------------------------------------------------------
343 /* */
344 bool pkgAcquire::Worker::MediaChange(string Message)
345 {
346 if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
347 LookupTag(Message,"Drive")) == false)
348 {
349 char S[300];
350 sprintf(S,"603 Media Changed\nFailed: true\n\n");
351 if (Debug == true)
352 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
353 OutQueue += S;
354 OutReady = true;
355 return true;
356 }
357
358 char S[300];
359 sprintf(S,"603 Media Changed\n\n");
360 if (Debug == true)
361 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
362 OutQueue += S;
363 OutReady = true;
364 return true;
365 }
366 /*}}}*/
367 // Worker::SendConfiguration - Send the config to the method /*{{{*/
368 // ---------------------------------------------------------------------
369 /* */
370 bool pkgAcquire::Worker::SendConfiguration()
371 {
372 if (Config->SendConfig == false)
373 return true;
374
375 if (OutFd == -1)
376 return false;
377
378 string Message = "601 Configuration\n";
379 Message.reserve(2000);
380
381 /* Write out all of the configuration directives by walking the
382 configuration tree */
383 const Configuration::Item *Top = _config->Tree(0);
384 for (; Top != 0;)
385 {
386 if (Top->Value.empty() == false)
387 {
388 string Line = "Config-Item: " + Top->FullTag() + "=";
389 Line += QuoteString(Top->Value,"\n") + '\n';
390 Message += Line;
391 }
392
393 if (Top->Child != 0)
394 {
395 Top = Top->Child;
396 continue;
397 }
398
399 while (Top != 0 && Top->Next == 0)
400 Top = Top->Parent;
401 if (Top != 0)
402 Top = Top->Next;
403 }
404 Message += '\n';
405
406 if (Debug == true)
407 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
408 OutQueue += Message;
409 OutReady = true;
410
411 return true;
412 }
413 /*}}}*/
414 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
415 // ---------------------------------------------------------------------
416 /* Send a URI Acquire message to the method */
417 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
418 {
419 if (OutFd == -1)
420 return false;
421
422 string Message = "600 URI Acquire\n";
423 Message.reserve(300);
424 Message += "URI: " + Item->URI;
425 Message += "\nFilename: " + Item->Owner->DestFile;
426 Message += Item->Owner->Custom600Headers();
427 Message += "\n\n";
428
429 if (Debug == true)
430 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
431 OutQueue += Message;
432 OutReady = true;
433
434 return true;
435 }
436 /*}}}*/
437 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
438 // ---------------------------------------------------------------------
439 /* */
440 bool pkgAcquire::Worker::OutFdReady()
441 {
442 int Res;
443 do
444 {
445 Res = write(OutFd,OutQueue.begin(),OutQueue.length());
446 }
447 while (Res < 0 && errno == EINTR);
448
449 if (Res <= 0)
450 return MethodFailure();
451
452 // Hmm.. this should never happen.
453 if (Res < 0)
454 return true;
455
456 OutQueue.erase(0,Res);
457 if (OutQueue.empty() == true)
458 OutReady = false;
459
460 return true;
461 }
462 /*}}}*/
463 // Worker::InFdRead - In bound FD is ready /*{{{*/
464 // ---------------------------------------------------------------------
465 /* */
466 bool pkgAcquire::Worker::InFdReady()
467 {
468 if (ReadMessages() == false)
469 return false;
470 RunMessages();
471 return true;
472 }
473 /*}}}*/
474 // Worker::MethodFailure - Called when the method fails /*{{{*/
475 // ---------------------------------------------------------------------
476 /* This is called when the method is belived to have failed, probably because
477 read returned -1. */
478 bool pkgAcquire::Worker::MethodFailure()
479 {
480 _error->Error("Method %s has died unexpectedly!",Access.c_str());
481
482 ExecWait(Process,Access.c_str(),true);
483 Process = -1;
484 close(InFd);
485 close(OutFd);
486 InFd = -1;
487 OutFd = -1;
488 OutReady = false;
489 InReady = false;
490 OutQueue = string();
491 MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
492
493 return false;
494 }
495 /*}}}*/
496 // Worker::Pulse - Called periodically /*{{{*/
497 // ---------------------------------------------------------------------
498 /* */
499 void pkgAcquire::Worker::Pulse()
500 {
501 if (CurrentItem == 0)
502 return;
503
504 struct stat Buf;
505 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
506 return;
507 CurrentSize = Buf.st_size;
508
509 // Hmm? Should not happen...
510 if (CurrentSize > TotalSize && TotalSize != 0)
511 TotalSize = CurrentSize;
512 }
513 /*}}}*/
514 // Worker::ItemDone - Called when the current item is finished /*{{{*/
515 // ---------------------------------------------------------------------
516 /* */
517 void pkgAcquire::Worker::ItemDone()
518 {
519 CurrentItem = 0;
520 CurrentSize = 0;
521 TotalSize = 0;
522 Status = string();
523 }
524 /*}}}*/