]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-worker.cc
Fixed sign conversion
[apt.git] / apt-pkg / acquire-worker.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-worker.cc,v 1.27 1999/08/28 01:49:56 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 if (TotalSize != 0 &&
252 (unsigned)atoi(LookupTag(Message,"Size","0").c_str()) != TotalSize)
253 _error->Warning("Bizzar Error - File size is not what the server reported %s %u",
254 LookupTag(Message,"Size","0").c_str(),TotalSize);
255
256 Owner->Done(Message,atoi(LookupTag(Message,"Size","0").c_str()),
257 LookupTag(Message,"MD5-Hash"));
258 ItemDone();
259
260 // Log that we are done
261 if (Log != 0)
262 {
263 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true ||
264 StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
265 {
266 /* Hide 'hits' for local only sources - we also manage to
267 hide gets */
268 if (Config->LocalOnly == false)
269 Log->IMSHit(Desc);
270 }
271 else
272 Log->Done(Desc);
273 }
274 break;
275 }
276
277 // 400 URI Failure
278 case 400:
279 {
280 if (Itm == 0)
281 {
282 _error->Error("Method gave invalid 400 URI Failure message");
283 break;
284 }
285
286 pkgAcquire::Item *Owner = Itm->Owner;
287 pkgAcquire::ItemDesc Desc = *Itm;
288 OwnerQ->ItemDone(Itm);
289 Owner->Failed(Message,Config);
290 ItemDone();
291
292 if (Log != 0)
293 Log->Fail(Desc);
294
295 break;
296 }
297
298 // 401 General Failure
299 case 401:
300 _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
301 break;
302
303 // 403 Media Change
304 case 403:
305 MediaChange(Message);
306 break;
307 }
308 }
309 return true;
310 }
311 /*}}}*/
312 // Worker::Capabilities - 100 Capabilities handler /*{{{*/
313 // ---------------------------------------------------------------------
314 /* This parses the capabilities message and dumps it into the configuration
315 structure. */
316 bool pkgAcquire::Worker::Capabilities(string Message)
317 {
318 if (Config == 0)
319 return true;
320
321 Config->Version = LookupTag(Message,"Version");
322 Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
323 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
324 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
325 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
326
327 // Some debug text
328 if (Debug == true)
329 {
330 clog << "Configured access method " << Config->Access << endl;
331 clog << "Version:" << Config->Version << " SingleInstance:" <<
332 Config->SingleInstance <<
333 " Pipeline:" << Config->Pipeline << " SendConfig:" <<
334 Config->SendConfig << endl;
335 }
336
337 return true;
338 }
339 /*}}}*/
340 // Worker::MediaChange - Request a media change /*{{{*/
341 // ---------------------------------------------------------------------
342 /* */
343 bool pkgAcquire::Worker::MediaChange(string Message)
344 {
345 if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
346 LookupTag(Message,"Drive")) == false)
347 {
348 char S[300];
349 sprintf(S,"603 Media Changed\nFailed: true\n\n");
350 if (Debug == true)
351 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
352 OutQueue += S;
353 OutReady = true;
354 return true;
355 }
356
357 char S[300];
358 sprintf(S,"603 Media Changed\n\n");
359 if (Debug == true)
360 clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
361 OutQueue += S;
362 OutReady = true;
363 return true;
364 }
365 /*}}}*/
366 // Worker::SendConfiguration - Send the config to the method /*{{{*/
367 // ---------------------------------------------------------------------
368 /* */
369 bool pkgAcquire::Worker::SendConfiguration()
370 {
371 if (Config->SendConfig == false)
372 return true;
373
374 if (OutFd == -1)
375 return false;
376
377 string Message = "601 Configuration\n";
378 Message.reserve(2000);
379
380 /* Write out all of the configuration directives by walking the
381 configuration tree */
382 const Configuration::Item *Top = _config->Tree(0);
383 for (; Top != 0;)
384 {
385 if (Top->Value.empty() == false)
386 {
387 string Line = "Config-Item: " + Top->FullTag() + "=";
388 Line += QuoteString(Top->Value,"\n") + '\n';
389 Message += Line;
390 }
391
392 if (Top->Child != 0)
393 {
394 Top = Top->Child;
395 continue;
396 }
397
398 while (Top != 0 && Top->Next == 0)
399 Top = Top->Parent;
400 if (Top != 0)
401 Top = Top->Next;
402 }
403 Message += '\n';
404
405 if (Debug == true)
406 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
407 OutQueue += Message;
408 OutReady = true;
409
410 return true;
411 }
412 /*}}}*/
413 // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
414 // ---------------------------------------------------------------------
415 /* Send a URI Acquire message to the method */
416 bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
417 {
418 if (OutFd == -1)
419 return false;
420
421 string Message = "600 URI Acquire\n";
422 Message.reserve(300);
423 Message += "URI: " + Item->URI;
424 Message += "\nFilename: " + Item->Owner->DestFile;
425 Message += Item->Owner->Custom600Headers();
426 Message += "\n\n";
427
428 if (Debug == true)
429 clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
430 OutQueue += Message;
431 OutReady = true;
432
433 return true;
434 }
435 /*}}}*/
436 // Worker::OutFdRead - Out bound FD is ready /*{{{*/
437 // ---------------------------------------------------------------------
438 /* */
439 bool pkgAcquire::Worker::OutFdReady()
440 {
441 int Res;
442 do
443 {
444 Res = write(OutFd,OutQueue.begin(),OutQueue.length());
445 }
446 while (Res < 0 && errno == EINTR);
447
448 if (Res <= 0)
449 return MethodFailure();
450
451 // Hmm.. this should never happen.
452 if (Res < 0)
453 return true;
454
455 OutQueue.erase(0,Res);
456 if (OutQueue.empty() == true)
457 OutReady = false;
458
459 return true;
460 }
461 /*}}}*/
462 // Worker::InFdRead - In bound FD is ready /*{{{*/
463 // ---------------------------------------------------------------------
464 /* */
465 bool pkgAcquire::Worker::InFdReady()
466 {
467 if (ReadMessages() == false)
468 return false;
469 RunMessages();
470 return true;
471 }
472 /*}}}*/
473 // Worker::MethodFailure - Called when the method fails /*{{{*/
474 // ---------------------------------------------------------------------
475 /* This is called when the method is belived to have failed, probably because
476 read returned -1. */
477 bool pkgAcquire::Worker::MethodFailure()
478 {
479 _error->Error("Method %s has died unexpectedly!",Access.c_str());
480
481 ExecWait(Process,Access.c_str(),true);
482 Process = -1;
483 close(InFd);
484 close(OutFd);
485 InFd = -1;
486 OutFd = -1;
487 OutReady = false;
488 InReady = false;
489 OutQueue = string();
490 MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
491
492 return false;
493 }
494 /*}}}*/
495 // Worker::Pulse - Called periodically /*{{{*/
496 // ---------------------------------------------------------------------
497 /* */
498 void pkgAcquire::Worker::Pulse()
499 {
500 if (CurrentItem == 0)
501 return;
502
503 struct stat Buf;
504 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
505 return;
506 CurrentSize = Buf.st_size;
507
508 // Hmm? Should not happen...
509 if (CurrentSize > TotalSize && TotalSize != 0)
510 TotalSize = CurrentSize;
511 }
512 /*}}}*/
513 // Worker::ItemDone - Called when the current item is finished /*{{{*/
514 // ---------------------------------------------------------------------
515 /* */
516 void pkgAcquire::Worker::ItemDone()
517 {
518 CurrentItem = 0;
519 CurrentSize = 0;
520 TotalSize = 0;
521 Status = string();
522 }
523 /*}}}*/