]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/dpkgpm.cc
* some WS fixes
[apt.git] / apt-pkg / deb / dpkgpm.cc
CommitLineData
c0c0b100 1// -*- mode: cpp; mode: fold -*-
03e39e59 2// Description /*{{{*/
7f9a6360 3// $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz Exp $
03e39e59
AL
4/* ######################################################################
5
6 DPKG Package Manager - Provide an interface to dpkg
7
8 ##################################################################### */
9 /*}}}*/
10// Includes /*{{{*/
11#ifdef __GNUG__
12#pragma implementation "apt-pkg/dpkgpm.h"
13#endif
14#include <apt-pkg/dpkgpm.h>
15#include <apt-pkg/error.h>
16#include <apt-pkg/configuration.h>
b2e465d6
AL
17#include <apt-pkg/depcache.h>
18#include <apt-pkg/strutl.h>
233b185f 19
03e39e59
AL
20#include <unistd.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <signal.h>
26#include <errno.h>
db0c350f 27#include <stdio.h>
233b185f 28#include <iostream>
b0ebdef5 29 /*}}}*/
233b185f
AL
30
31using namespace std;
03e39e59
AL
32
33// DPkgPM::pkgDPkgPM - Constructor /*{{{*/
34// ---------------------------------------------------------------------
35/* */
b2e465d6 36pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
03e39e59
AL
37{
38}
39 /*}}}*/
40// DPkgPM::pkgDPkgPM - Destructor /*{{{*/
41// ---------------------------------------------------------------------
42/* */
43pkgDPkgPM::~pkgDPkgPM()
44{
45}
46 /*}}}*/
47// DPkgPM::Install - Install a package /*{{{*/
48// ---------------------------------------------------------------------
49/* Add an install operation to the sequence list */
50bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
51{
52 if (File.empty() == true || Pkg.end() == true)
53 return _error->Error("Internal Error, No file name for %s",Pkg.Name());
54
55 List.push_back(Item(Item::Install,Pkg,File));
56 return true;
57}
58 /*}}}*/
59// DPkgPM::Configure - Configure a package /*{{{*/
60// ---------------------------------------------------------------------
61/* Add a configure operation to the sequence list */
62bool pkgDPkgPM::Configure(PkgIterator Pkg)
63{
64 if (Pkg.end() == true)
65 return false;
66
67 List.push_back(Item(Item::Configure,Pkg));
68 return true;
69}
70 /*}}}*/
71// DPkgPM::Remove - Remove a package /*{{{*/
72// ---------------------------------------------------------------------
73/* Add a remove operation to the sequence list */
fc4b5c9f 74bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
03e39e59
AL
75{
76 if (Pkg.end() == true)
77 return false;
78
fc4b5c9f
AL
79 if (Purge == true)
80 List.push_back(Item(Item::Purge,Pkg));
81 else
82 List.push_back(Item(Item::Remove,Pkg));
6dd55be7
AL
83 return true;
84}
85 /*}}}*/
86// DPkgPM::RunScripts - Run a set of scripts /*{{{*/
87// ---------------------------------------------------------------------
88/* This looks for a list of script sto run from the configuration file,
89 each one is run with system from a forked child. */
90bool pkgDPkgPM::RunScripts(const char *Cnf)
91{
92 Configuration::Item const *Opts = _config->Tree(Cnf);
93 if (Opts == 0 || Opts->Child == 0)
94 return true;
95 Opts = Opts->Child;
96
97 // Fork for running the system calls
54676e1a 98 pid_t Child = ExecFork();
6dd55be7
AL
99
100 // This is the child
101 if (Child == 0)
102 {
6dd55be7
AL
103 if (chdir("/tmp/") != 0)
104 _exit(100);
105
6dd55be7
AL
106 unsigned int Count = 1;
107 for (; Opts != 0; Opts = Opts->Next, Count++)
108 {
109 if (Opts->Value.empty() == true)
110 continue;
111
112 if (system(Opts->Value.c_str()) != 0)
113 _exit(100+Count);
114 }
115 _exit(0);
116 }
117
118 // Wait for the child
119 int Status = 0;
120 while (waitpid(Child,&Status,0) != Child)
121 {
122 if (errno == EINTR)
123 continue;
124 return _error->Errno("waitpid","Couldn't wait for subprocess");
125 }
126
127 // Restore sig int/quit
128 signal(SIGQUIT,SIG_DFL);
129 signal(SIGINT,SIG_DFL);
ddc1d8d0 130
6dd55be7
AL
131 // Check for an error code.
132 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
133 {
134 unsigned int Count = WEXITSTATUS(Status);
135 if (Count > 100)
136 {
137 Count -= 100;
138 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
cf544e14 139 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
6dd55be7
AL
140 }
141
142 return _error->Error("Sub-process returned an error code");
143 }
144
03e39e59
AL
145 return true;
146}
db0c350f 147 /*}}}*/
b2e465d6
AL
148// DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
149// ---------------------------------------------------------------------
150/* This is part of the helper script communication interface, it sends
151 very complete information down to the other end of the pipe.*/
152bool pkgDPkgPM::SendV2Pkgs(FILE *F)
153{
154 fprintf(F,"VERSION 2\n");
155
156 /* Write out all of the configuration directives by walking the
157 configuration tree */
158 const Configuration::Item *Top = _config->Tree(0);
159 for (; Top != 0;)
160 {
161 if (Top->Value.empty() == false)
162 {
163 fprintf(F,"%s=%s\n",
164 QuoteString(Top->FullTag(),"=\"\n").c_str(),
165 QuoteString(Top->Value,"\n").c_str());
166 }
167
168 if (Top->Child != 0)
169 {
170 Top = Top->Child;
171 continue;
172 }
173
174 while (Top != 0 && Top->Next == 0)
175 Top = Top->Parent;
176 if (Top != 0)
177 Top = Top->Next;
178 }
179 fprintf(F,"\n");
180
181 // Write out the package actions in order.
182 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
183 {
184 pkgDepCache::StateCache &S = Cache[I->Pkg];
185
186 fprintf(F,"%s ",I->Pkg.Name());
187 // Current version
188 if (I->Pkg->CurrentVer == 0)
189 fprintf(F,"- ");
190 else
191 fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
192
193 // Show the compare operator
194 // Target version
195 if (S.InstallVer != 0)
196 {
197 int Comp = 2;
198 if (I->Pkg->CurrentVer != 0)
199 Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
200 if (Comp < 0)
201 fprintf(F,"> ");
202 if (Comp == 0)
203 fprintf(F,"= ");
204 if (Comp > 0)
205 fprintf(F,"< ");
206 fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
207 }
208 else
209 fprintf(F,"> - ");
210
211 // Show the filename/operation
212 if (I->Op == Item::Install)
213 {
214 // No errors here..
215 if (I->File[0] != '/')
216 fprintf(F,"**ERROR**\n");
217 else
218 fprintf(F,"%s\n",I->File.c_str());
219 }
220 if (I->Op == Item::Configure)
221 fprintf(F,"**CONFIGURE**\n");
222 if (I->Op == Item::Remove ||
223 I->Op == Item::Purge)
224 fprintf(F,"**REMOVE**\n");
225
226 if (ferror(F) != 0)
227 return false;
228 }
229 return true;
230}
231 /*}}}*/
db0c350f
AL
232// DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
233// ---------------------------------------------------------------------
234/* This looks for a list of scripts to run from the configuration file
235 each one is run and is fed on standard input a list of all .deb files
236 that are due to be installed. */
237bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
238{
239 Configuration::Item const *Opts = _config->Tree(Cnf);
240 if (Opts == 0 || Opts->Child == 0)
241 return true;
242 Opts = Opts->Child;
243
244 unsigned int Count = 1;
245 for (; Opts != 0; Opts = Opts->Next, Count++)
246 {
247 if (Opts->Value.empty() == true)
248 continue;
b2e465d6
AL
249
250 // Determine the protocol version
251 string OptSec = Opts->Value;
252 string::size_type Pos;
253 if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
254 Pos = OptSec.length();
b2e465d6
AL
255 OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
256
257 unsigned int Version = _config->FindI(OptSec+"::Version",1);
258
db0c350f
AL
259 // Create the pipes
260 int Pipes[2];
261 if (pipe(Pipes) != 0)
262 return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
263 SetCloseExec(Pipes[0],true);
264 SetCloseExec(Pipes[1],true);
265
266 // Purified Fork for running the script
267 pid_t Process = ExecFork();
268 if (Process == 0)
269 {
270 // Setup the FDs
271 dup2(Pipes[0],STDIN_FILENO);
272 SetCloseExec(STDOUT_FILENO,false);
273 SetCloseExec(STDIN_FILENO,false);
274 SetCloseExec(STDERR_FILENO,false);
90ecbd7d
AL
275
276 const char *Args[4];
db0c350f 277 Args[0] = "/bin/sh";
90ecbd7d
AL
278 Args[1] = "-c";
279 Args[2] = Opts->Value.c_str();
280 Args[3] = 0;
db0c350f
AL
281 execv(Args[0],(char **)Args);
282 _exit(100);
283 }
284 close(Pipes[0]);
b2e465d6
AL
285 FILE *F = fdopen(Pipes[1],"w");
286 if (F == 0)
287 return _error->Errno("fdopen","Faild to open new FD");
288
db0c350f 289 // Feed it the filenames.
b2e465d6
AL
290 bool Die = false;
291 if (Version <= 1)
db0c350f 292 {
b2e465d6 293 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
db0c350f 294 {
b2e465d6
AL
295 // Only deal with packages to be installed from .deb
296 if (I->Op != Item::Install)
297 continue;
298
299 // No errors here..
300 if (I->File[0] != '/')
301 continue;
302
303 /* Feed the filename of each package that is pending install
304 into the pipe. */
305 fprintf(F,"%s\n",I->File.c_str());
306 if (ferror(F) != 0)
307 {
308 Die = true;
309 break;
310 }
90ecbd7d 311 }
db0c350f 312 }
b2e465d6
AL
313 else
314 Die = !SendV2Pkgs(F);
315
316 fclose(F);
db0c350f
AL
317
318 // Clean up the sub process
319 if (ExecWait(Process,Opts->Value.c_str()) == false)
90ecbd7d 320 return _error->Error("Failure running script %s",Opts->Value.c_str());
db0c350f
AL
321 }
322
323 return true;
324}
03e39e59
AL
325 /*}}}*/
326// DPkgPM::Go - Run the sequence /*{{{*/
327// ---------------------------------------------------------------------
328/* This globs the operations and calls dpkg */
007dc9e0 329bool pkgDPkgPM::Go(int status_fd)
03e39e59 330{
6b8147b8
MZ
331 unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
332 unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
aff4e2f1 333
6dd55be7
AL
334 if (RunScripts("DPkg::Pre-Invoke") == false)
335 return false;
db0c350f
AL
336
337 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
338 return false;
339
03e39e59
AL
340 for (vector<Item>::iterator I = List.begin(); I != List.end();)
341 {
342 vector<Item>::iterator J = I;
343 for (; J != List.end() && J->Op == I->Op; J++);
30e1eab5 344
03e39e59 345 // Generate the argument list
aff4e2f1
AL
346 const char *Args[MaxArgs + 50];
347 if (J - I > (signed)MaxArgs)
348 J = I + MaxArgs;
03e39e59 349
30e1eab5
AL
350 unsigned int n = 0;
351 unsigned long Size = 0;
43b3b626 352 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
50914ffa 353 Args[n++] = Tmp.c_str();
30e1eab5 354 Size += strlen(Args[n-1]);
03e39e59 355
6dd55be7
AL
356 // Stick in any custom dpkg options
357 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
358 if (Opts != 0)
359 {
360 Opts = Opts->Child;
361 for (; Opts != 0; Opts = Opts->Next)
362 {
363 if (Opts->Value.empty() == true)
364 continue;
365 Args[n++] = Opts->Value.c_str();
366 Size += Opts->Value.length();
367 }
368 }
369
007dc9e0
MZ
370 // if we got a status_fd argument, we pass it to apt
371 char status_fd_buf[20];
372 if(status_fd > 0)
373 {
374 Args[n++] = "--status-fd";
375 Size += strlen(Args[n-1]);
376 snprintf(status_fd_buf,20,"%i",status_fd);
377 Args[n++] = status_fd_buf;
378 Size += strlen(Args[n-1]);
379 }
380
03e39e59
AL
381 switch (I->Op)
382 {
383 case Item::Remove:
384 Args[n++] = "--force-depends";
30e1eab5 385 Size += strlen(Args[n-1]);
03e39e59 386 Args[n++] = "--force-remove-essential";
30e1eab5 387 Size += strlen(Args[n-1]);
03e39e59 388 Args[n++] = "--remove";
30e1eab5 389 Size += strlen(Args[n-1]);
03e39e59
AL
390 break;
391
fc4b5c9f
AL
392 case Item::Purge:
393 Args[n++] = "--force-depends";
394 Size += strlen(Args[n-1]);
395 Args[n++] = "--force-remove-essential";
396 Size += strlen(Args[n-1]);
397 Args[n++] = "--purge";
398 Size += strlen(Args[n-1]);
399 break;
400
03e39e59
AL
401 case Item::Configure:
402 Args[n++] = "--configure";
30e1eab5 403 Size += strlen(Args[n-1]);
03e39e59
AL
404 break;
405
406 case Item::Install:
407 Args[n++] = "--unpack";
30e1eab5 408 Size += strlen(Args[n-1]);
03e39e59
AL
409 break;
410 }
411
412 // Write in the file or package names
413 if (I->Op == Item::Install)
30e1eab5 414 {
aff4e2f1 415 for (;I != J && Size < MaxArgBytes; I++)
30e1eab5 416 {
cf544e14
AL
417 if (I->File[0] != '/')
418 return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
03e39e59 419 Args[n++] = I->File.c_str();
30e1eab5
AL
420 Size += strlen(Args[n-1]);
421 }
422 }
03e39e59 423 else
30e1eab5 424 {
aff4e2f1 425 for (;I != J && Size < MaxArgBytes; I++)
30e1eab5 426 {
03e39e59 427 Args[n++] = I->Pkg.Name();
30e1eab5
AL
428 Size += strlen(Args[n-1]);
429 }
430 }
03e39e59 431 Args[n] = 0;
30e1eab5
AL
432 J = I;
433
434 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
435 {
436 for (unsigned int k = 0; k != n; k++)
437 clog << Args[k] << ' ';
438 clog << endl;
439 continue;
440 }
03e39e59 441
03e39e59
AL
442 cout << flush;
443 clog << flush;
444 cerr << flush;
445
446 /* Mask off sig int/quit. We do this because dpkg also does when
447 it forks scripts. What happens is that when you hit ctrl-c it sends
448 it to all processes in the group. Since dpkg ignores the signal
449 it doesn't die but we do! So we must also ignore it */
7f9a6360
AL
450 sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
451 sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
6dd55be7 452
03e39e59 453 // Fork dpkg
007dc9e0
MZ
454 pid_t Child;
455 if(status_fd > 0)
456 Child = ExecFork(status_fd);
457 else
458 Child = ExecFork();
6dd55be7 459
03e39e59
AL
460 // This is the child
461 if (Child == 0)
462 {
cf544e14 463 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
0dbb95d8 464 _exit(100);
03e39e59 465
421ff807 466 if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
8b5fe26c
AL
467 {
468 int Flags,dummy;
469 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
470 _exit(100);
471
472 // Discard everything in stdin before forking dpkg
473 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
474 _exit(100);
475
476 while (read(STDIN_FILENO,&dummy,1) == 1);
477
478 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
479 _exit(100);
480 }
03e39e59 481
03e39e59
AL
482 /* No Job Control Stop Env is a magic dpkg var that prevents it
483 from using sigstop */
101030ab 484 putenv("DPKG_NO_TSTP=yes");
d568ed2d 485 execvp(Args[0],(char **)Args);
03e39e59 486 cerr << "Could not exec dpkg!" << endl;
0dbb95d8 487 _exit(100);
03e39e59
AL
488 }
489
490 // Wait for dpkg
491 int Status = 0;
492 while (waitpid(Child,&Status,0) != Child)
493 {
494 if (errno == EINTR)
495 continue;
6dd55be7 496 RunScripts("DPkg::Post-Invoke");
7f9a6360
AL
497
498 // Restore sig int/quit
499 signal(SIGQUIT,old_SIGQUIT);
500 signal(SIGINT,old_SIGINT);
03e39e59
AL
501 return _error->Errno("waitpid","Couldn't wait for subprocess");
502 }
03e39e59
AL
503
504 // Restore sig int/quit
7f9a6360
AL
505 signal(SIGQUIT,old_SIGQUIT);
506 signal(SIGINT,old_SIGINT);
6dd55be7
AL
507
508 // Check for an error code.
509 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
510 {
511 RunScripts("DPkg::Post-Invoke");
f956efb4 512 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
b2e465d6
AL
513 return _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
514
f956efb4 515 if (WIFEXITED(Status) != 0)
0410b3d5 516 return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
f956efb4 517
0410b3d5 518 return _error->Error("Sub-process %s exited unexpectedly",Args[0]);
6dd55be7 519 }
03e39e59 520 }
6dd55be7
AL
521
522 if (RunScripts("DPkg::Post-Invoke") == false)
523 return false;
03e39e59
AL
524 return true;
525}
526 /*}}}*/
281daf46
AL
527// pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
528// ---------------------------------------------------------------------
529/* */
530void pkgDPkgPM::Reset()
531{
532 List.erase(List.begin(),List.end());
533}
534 /*}}}*/