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