Small segv bug fix
[ntk/apt.git] / apt-pkg / acquire-worker.cc
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
01408959 3// $Id: acquire-worker.cc,v 1.25 1999/08/03 02:25:38 jgg Exp $
0118833a
AL
4/* ######################################################################
5
6 Acquire Worker
7
3b5421b4
AL
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
0118833a
AL
12 ##################################################################### */
13 /*}}}*/
14// Include Files /*{{{*/
15#ifdef __GNUG__
16#pragma implementation "apt-pkg/acquire-worker.h"
3b5421b4 17#endif
0118833a 18#include <apt-pkg/acquire-worker.h>
0a8a80e5 19#include <apt-pkg/acquire-item.h>
3b5421b4
AL
20#include <apt-pkg/configuration.h>
21#include <apt-pkg/error.h>
22#include <apt-pkg/fileutl.h>
cdcc6d34 23#include <apt-pkg/strutl.h>
3b5421b4 24
8267fe24 25#include <sys/stat.h>
3b5421b4 26#include <unistd.h>
b0db36b1 27#include <fcntl.h>
3b5421b4 28#include <signal.h>
542ec555 29#include <stdio.h>
b0db36b1 30#include <errno.h>
3b5421b4
AL
31 /*}}}*/
32
33// Worker::Worker - Constructor for Queue startup /*{{{*/
34// ---------------------------------------------------------------------
35/* */
8267fe24
AL
36pkgAcquire::Worker::Worker(Queue *Q,MethodConfig *Cnf,
37 pkgAcquireStatus *Log) : Log(Log)
3b5421b4
AL
38{
39 OwnerQ = Q;
0a8a80e5
AL
40 Config = Cnf;
41 Access = Cnf->Access;
42 CurrentItem = 0;
18ef0a78
AL
43 TotalSize = 0;
44 CurrentSize = 0;
45
3b5421b4
AL
46 Construct();
47}
48 /*}}}*/
49// Worker::Worker - Constructor for method config startup /*{{{*/
50// ---------------------------------------------------------------------
51/* */
52pkgAcquire::Worker::Worker(MethodConfig *Cnf)
53{
54 OwnerQ = 0;
55 Config = Cnf;
56 Access = Cnf->Access;
0a8a80e5 57 CurrentItem = 0;
18ef0a78
AL
58 TotalSize = 0;
59 CurrentSize = 0;
0a8a80e5 60
3b5421b4
AL
61 Construct();
62}
63 /*}}}*/
64// Worker::Construct - Constructor helper /*{{{*/
65// ---------------------------------------------------------------------
66/* */
67void pkgAcquire::Worker::Construct()
68{
0a8a80e5
AL
69 NextQueue = 0;
70 NextAcquire = 0;
3b5421b4
AL
71 Process = -1;
72 InFd = -1;
73 OutFd = -1;
0a8a80e5
AL
74 OutReady = false;
75 InReady = false;
3b5421b4
AL
76 Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
77}
78 /*}}}*/
79// Worker::~Worker - Destructor /*{{{*/
80// ---------------------------------------------------------------------
81/* */
82pkgAcquire::Worker::~Worker()
83{
84 close(InFd);
85 close(OutFd);
86
87 if (Process > 0)
0a8a80e5 88 {
3b5421b4 89 kill(Process,SIGINT);
ddc1d8d0 90 ExecWait(Process,Access.c_str(),true);
0a8a80e5 91 }
3b5421b4
AL
92}
93 /*}}}*/
94// Worker::Start - Start the worker process /*{{{*/
95// ---------------------------------------------------------------------
96/* This forks the method and inits the communication channel */
97bool 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 }
8b89e57f
AL
116 for (int I = 0; I != 4; I++)
117 SetCloseExec(Pipes[0],true);
118
3b5421b4 119 // Fork off the process
54676e1a 120 Process = ExecFork();
3b5421b4
AL
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);
3b5421b4
AL
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;
0dbb95d8 138 _exit(100);
3b5421b4
AL
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]);
0a8a80e5
AL
148 OutReady = false;
149 InReady = true;
3b5421b4
AL
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();
8b89e57f
AL
157 if (OwnerQ != 0)
158 SendConfiguration();
3b5421b4
AL
159
160 return true;
161}
162 /*}}}*/
163// Worker::ReadMessages - Read all pending messages into the list /*{{{*/
164// ---------------------------------------------------------------------
0a8a80e5 165/* */
3b5421b4
AL
166bool pkgAcquire::Worker::ReadMessages()
167{
0a8a80e5
AL
168 if (::ReadMessages(InFd,MessageQueue) == false)
169 return MethodFailure();
3b5421b4
AL
170 return true;
171}
172 /*}}}*/
3b5421b4
AL
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. */
177bool pkgAcquire::Worker::RunMessages()
178{
179 while (MessageQueue.empty() == false)
180 {
181 string Message = MessageQueue.front();
182 MessageQueue.erase(MessageQueue.begin());
0a8a80e5
AL
183
184 if (Debug == true)
185 clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
3b5421b4
AL
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
c88edf1d
AL
193 string URI = LookupTag(Message,"URI");
194 pkgAcquire::Queue::QItem *Itm = 0;
195 if (URI.empty() == false)
196 Itm = OwnerQ->FindItem(URI,this);
bfd22fc0 197
3b5421b4
AL
198 // Determine the message number and dispatch
199 switch (Number)
200 {
0a8a80e5 201 // 100 Capabilities
3b5421b4
AL
202 case 100:
203 if (Capabilities(Message) == false)
204 return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
205 break;
0a8a80e5
AL
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:
c88edf1d
AL
220 {
221 if (Itm == 0)
222 {
93bf083d 223 _error->Error("Method gave invalid 200 URI Start message");
c88edf1d
AL
224 break;
225 }
8267fe24 226
c88edf1d
AL
227 CurrentItem = Itm;
228 CurrentSize = 0;
229 TotalSize = atoi(LookupTag(Message,"Size","0").c_str());
8b75eb1c 230 ResumePoint = atoi(LookupTag(Message,"Resume-Point","0").c_str());
8267fe24 231 Itm->Owner->Start(Message,atoi(LookupTag(Message,"Size","0").c_str()));
8b75eb1c 232
8267fe24
AL
233 if (Log != 0)
234 Log->Fetch(*Itm);
235
c88edf1d
AL
236 break;
237 }
0a8a80e5
AL
238
239 // 201 URI Done
240 case 201:
c88edf1d
AL
241 {
242 if (Itm == 0)
243 {
93bf083d 244 _error->Error("Method gave invalid 201 URI Done message");
c88edf1d
AL
245 break;
246 }
247
01408959 248 Pulse();
bfd22fc0 249 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 250 pkgAcquire::ItemDesc Desc = *Itm;
be4401bf 251 OwnerQ->ItemDone(Itm);
18ef0a78
AL
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"));
8267fe24
AL
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)
c46824ce
AL
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 }
8267fe24
AL
272 else
273 Log->Done(Desc);
18ef0a78 274 }
c88edf1d
AL
275 break;
276 }
0a8a80e5
AL
277
278 // 400 URI Failure
279 case 400:
c88edf1d
AL
280 {
281 if (Itm == 0)
282 {
93bf083d 283 _error->Error("Method gave invalid 400 URI Failure message");
c88edf1d
AL
284 break;
285 }
286
bfd22fc0 287 pkgAcquire::Item *Owner = Itm->Owner;
8267fe24 288 pkgAcquire::ItemDesc Desc = *Itm;
c88edf1d 289 OwnerQ->ItemDone(Itm);
7d8afa39 290 Owner->Failed(Message,Config);
8267fe24 291 ItemDone();
7d8afa39 292
8267fe24
AL
293 if (Log != 0)
294 Log->Fail(Desc);
7d8afa39 295
c88edf1d
AL
296 break;
297 }
0a8a80e5
AL
298
299 // 401 General Failure
300 case 401:
301 _error->Error("Method %s General failure: %s",LookupTag(Message,"Message").c_str());
302 break;
542ec555
AL
303
304 // 403 Media Change
305 case 403:
306 MediaChange(Message);
307 break;
3b5421b4
AL
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. */
317bool 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);
0a8a80e5
AL
324 Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
325 Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
e331f6ed 326 Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
3b5421b4
AL
327
328 // Some debug text
329 if (Debug == true)
330 {
331 clog << "Configured access method " << Config->Access << endl;
0a8a80e5 332 clog << "Version:" << Config->Version << " SingleInstance:" <<
a72ace20 333 Config->SingleInstance <<
0a8a80e5
AL
334 " Pipeline:" << Config->Pipeline << " SendConfig:" <<
335 Config->SendConfig << endl;
3b5421b4
AL
336 }
337
542ec555
AL
338 return true;
339}
340 /*}}}*/
341// Worker::MediaChange - Request a media change /*{{{*/
342// ---------------------------------------------------------------------
343/* */
344bool 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;
3b5421b4
AL
364 return true;
365}
0118833a 366 /*}}}*/
0a8a80e5
AL
367// Worker::SendConfiguration - Send the config to the method /*{{{*/
368// ---------------------------------------------------------------------
369/* */
370bool 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 */
417bool 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/* */
440bool pkgAcquire::Worker::OutFdReady()
441{
b0db36b1
AL
442 int Res;
443 do
444 {
445 Res = write(OutFd,OutQueue.begin(),OutQueue.length());
446 }
447 while (Res < 0 && errno == EINTR);
448
0a8a80e5
AL
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/* */
466bool 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. */
478bool pkgAcquire::Worker::MethodFailure()
479{
76d97c26
AL
480 _error->Error("Method %s has died unexpectedly!",Access.c_str());
481
ddc1d8d0 482 ExecWait(Process,Access.c_str(),true);
0a8a80e5
AL
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 /*}}}*/
8267fe24
AL
496// Worker::Pulse - Called periodically /*{{{*/
497// ---------------------------------------------------------------------
498/* */
499void pkgAcquire::Worker::Pulse()
500{
501 if (CurrentItem == 0)
502 return;
542ec555 503
8267fe24
AL
504 struct stat Buf;
505 if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
506 return;
507 CurrentSize = Buf.st_size;
18ef0a78
AL
508
509 // Hmm? Should not happen...
510 if (CurrentSize > TotalSize && TotalSize != 0)
511 TotalSize = CurrentSize;
8267fe24
AL
512}
513 /*}}}*/
514// Worker::ItemDone - Called when the current item is finished /*{{{*/
515// ---------------------------------------------------------------------
516/* */
517void pkgAcquire::Worker::ItemDone()
518{
519 CurrentItem = 0;
520 CurrentSize = 0;
521 TotalSize = 0;
522 Status = string();
523}
524 /*}}}*/