]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.24 2002/04/02 06:42:39 jgg Exp $
4 /* ######################################################################
6 DPKG Package Manager - Provide an interface to dpkg
8 ##################################################################### */
12 #pragma implementation "apt-pkg/dpkgpm.h"
14 #include <apt-pkg/dpkgpm.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/depcache.h>
18 #include <apt-pkg/strutl.h>
23 #include <sys/types.h>
33 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
36 pkgDPkgPM::pkgDPkgPM(pkgDepCache
*Cache
) : pkgPackageManager(Cache
)
40 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
41 // ---------------------------------------------------------------------
43 pkgDPkgPM::~pkgDPkgPM()
47 // DPkgPM::Install - Install a package /*{{{*/
48 // ---------------------------------------------------------------------
49 /* Add an install operation to the sequence list */
50 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
52 if (File
.empty() == true || Pkg
.end() == true)
53 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
55 List
.push_back(Item(Item::Install
,Pkg
,File
));
59 // DPkgPM::Configure - Configure a package /*{{{*/
60 // ---------------------------------------------------------------------
61 /* Add a configure operation to the sequence list */
62 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
64 if (Pkg
.end() == true)
67 List
.push_back(Item(Item::Configure
,Pkg
));
71 // DPkgPM::Remove - Remove a package /*{{{*/
72 // ---------------------------------------------------------------------
73 /* Add a remove operation to the sequence list */
74 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
76 if (Pkg
.end() == true)
80 List
.push_back(Item(Item::Purge
,Pkg
));
82 List
.push_back(Item(Item::Remove
,Pkg
));
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. */
90 bool pkgDPkgPM::RunScripts(const char *Cnf
)
92 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
93 if (Opts
== 0 || Opts
->Child
== 0)
97 // Fork for running the system calls
98 pid_t Child
= ExecFork();
103 if (chdir("/tmp/") != 0)
106 unsigned int Count
= 1;
107 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
109 if (Opts
->Value
.empty() == true)
112 if (system(Opts
->Value
.c_str()) != 0)
118 // Wait for the child
120 while (waitpid(Child
,&Status
,0) != Child
)
124 return _error
->Errno("waitpid","Couldn't wait for subprocess");
127 // Restore sig int/quit
128 signal(SIGQUIT
,SIG_DFL
);
129 signal(SIGINT
,SIG_DFL
);
131 // Check for an error code.
132 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
134 unsigned int Count
= WEXITSTATUS(Status
);
138 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
139 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
142 return _error
->Error("Sub-process returned an error code");
149 // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This is part of the helper script communication interface, it sends
152 very complete information down to the other end of the pipe.*/
153 bool pkgDPkgPM::SendV2Pkgs(FILE *F
)
155 fprintf(F
,"VERSION 2\n");
157 /* Write out all of the configuration directives by walking the
158 configuration tree */
159 const Configuration::Item
*Top
= _config
->Tree(0);
162 if (Top
->Value
.empty() == false)
165 QuoteString(Top
->FullTag(),"=\"\n").c_str(),
166 QuoteString(Top
->Value
,"\n").c_str());
175 while (Top
!= 0 && Top
->Next
== 0)
182 // Write out the package actions in order.
183 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
185 pkgDepCache::StateCache
&S
= Cache
[I
->Pkg
];
187 fprintf(F
,"%s ",I
->Pkg
.Name());
189 if (I
->Pkg
->CurrentVer
== 0)
192 fprintf(F
,"%s ",I
->Pkg
.CurrentVer().VerStr());
194 // Show the compare operator
196 if (S
.InstallVer
!= 0)
199 if (I
->Pkg
->CurrentVer
!= 0)
200 Comp
= S
.InstVerIter(Cache
).CompareVer(I
->Pkg
.CurrentVer());
207 fprintf(F
,"%s ",S
.InstVerIter(Cache
).VerStr());
212 // Show the filename/operation
213 if (I
->Op
== Item::Install
)
216 if (I
->File
[0] != '/')
217 fprintf(F
,"**ERROR**\n");
219 fprintf(F
,"%s\n",I
->File
.c_str());
221 if (I
->Op
== Item::Configure
)
222 fprintf(F
,"**CONFIGURE**\n");
223 if (I
->Op
== Item::Remove
||
224 I
->Op
== Item::Purge
)
225 fprintf(F
,"**REMOVE**\n");
233 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
234 // ---------------------------------------------------------------------
235 /* This looks for a list of scripts to run from the configuration file
236 each one is run and is fed on standard input a list of all .deb files
237 that are due to be installed. */
238 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
240 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
241 if (Opts
== 0 || Opts
->Child
== 0)
245 unsigned int Count
= 1;
246 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
248 if (Opts
->Value
.empty() == true)
251 // Determine the protocol version
252 string OptSec
= Opts
->Value
;
253 string::size_type Pos
;
254 if ((Pos
= OptSec
.find(' ')) == string::npos
|| Pos
== 0)
255 Pos
= OptSec
.length();
256 OptSec
= "DPkg::Tools::Options::" + string(Opts
->Value
.c_str(),Pos
);
258 unsigned int Version
= _config
->FindI(OptSec
+"::Version",1);
262 if (pipe(Pipes
) != 0)
263 return _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
264 SetCloseExec(Pipes
[0],true);
265 SetCloseExec(Pipes
[1],true);
267 // Purified Fork for running the script
268 pid_t Process
= ExecFork();
272 dup2(Pipes
[0],STDIN_FILENO
);
273 SetCloseExec(STDOUT_FILENO
,false);
274 SetCloseExec(STDIN_FILENO
,false);
275 SetCloseExec(STDERR_FILENO
,false);
280 Args
[2] = Opts
->Value
.c_str();
282 execv(Args
[0],(char **)Args
);
286 FILE *F
= fdopen(Pipes
[1],"w");
288 return _error
->Errno("fdopen","Faild to open new FD");
290 // Feed it the filenames.
294 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
296 // Only deal with packages to be installed from .deb
297 if (I
->Op
!= Item::Install
)
301 if (I
->File
[0] != '/')
304 /* Feed the filename of each package that is pending install
306 fprintf(F
,"%s\n",I
->File
.c_str());
315 Die
= !SendV2Pkgs(F
);
319 // Clean up the sub process
320 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
321 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
328 // DPkgPM::Go - Run the sequence /*{{{*/
329 // ---------------------------------------------------------------------
330 /* This globs the operations and calls dpkg */
333 if (RunScripts("DPkg::Pre-Invoke") == false)
336 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
339 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
341 vector
<Item
>::iterator J
= I
;
342 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
344 // Generate the argument list
345 const char *Args
[400];
350 unsigned long Size
= 0;
351 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
352 Args
[n
++] = Tmp
.c_str();
353 Size
+= strlen(Args
[n
-1]);
355 // Stick in any custom dpkg options
356 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
360 for (; Opts
!= 0; Opts
= Opts
->Next
)
362 if (Opts
->Value
.empty() == true)
364 Args
[n
++] = Opts
->Value
.c_str();
365 Size
+= Opts
->Value
.length();
372 Args
[n
++] = "--force-depends";
373 Size
+= strlen(Args
[n
-1]);
374 Args
[n
++] = "--force-remove-essential";
375 Size
+= strlen(Args
[n
-1]);
376 Args
[n
++] = "--remove";
377 Size
+= strlen(Args
[n
-1]);
381 Args
[n
++] = "--force-depends";
382 Size
+= strlen(Args
[n
-1]);
383 Args
[n
++] = "--force-remove-essential";
384 Size
+= strlen(Args
[n
-1]);
385 Args
[n
++] = "--purge";
386 Size
+= strlen(Args
[n
-1]);
389 case Item::Configure
:
390 Args
[n
++] = "--configure";
391 Size
+= strlen(Args
[n
-1]);
395 Args
[n
++] = "--unpack";
396 Size
+= strlen(Args
[n
-1]);
400 // Write in the file or package names
401 if (I
->Op
== Item::Install
)
403 for (;I
!= J
&& Size
< 1024; I
++)
405 if (I
->File
[0] != '/')
406 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
407 Args
[n
++] = I
->File
.c_str();
408 Size
+= strlen(Args
[n
-1]);
413 for (;I
!= J
&& Size
< 1024; I
++)
415 Args
[n
++] = I
->Pkg
.Name();
416 Size
+= strlen(Args
[n
-1]);
422 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
424 for (unsigned int k
= 0; k
!= n
; k
++)
425 clog
<< Args
[k
] << ' ';
434 /* Mask off sig int/quit. We do this because dpkg also does when
435 it forks scripts. What happens is that when you hit ctrl-c it sends
436 it to all processes in the group. Since dpkg ignores the signal
437 it doesn't die but we do! So we must also ignore it */
438 signal(SIGQUIT
,SIG_IGN
);
439 signal(SIGINT
,SIG_IGN
);
442 pid_t Child
= ExecFork();
447 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
450 if (_config
->FindB("DPkg::FlushSTDIN",true) == true)
453 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
456 // Discard everything in stdin before forking dpkg
457 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
460 while (read(STDIN_FILENO
,&dummy
,1) == 1);
462 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
466 /* No Job Control Stop Env is a magic dpkg var that prevents it
467 from using sigstop */
468 putenv("DPKG_NO_TSTP=yes");
469 execvp(Args
[0],(char **)Args
);
470 cerr
<< "Could not exec dpkg!" << endl
;
476 while (waitpid(Child
,&Status
,0) != Child
)
480 RunScripts("DPkg::Post-Invoke");
481 return _error
->Errno("waitpid","Couldn't wait for subprocess");
484 // Restore sig int/quit
485 signal(SIGQUIT
,SIG_DFL
);
486 signal(SIGINT
,SIG_DFL
);
488 // Check for an error code.
489 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
491 RunScripts("DPkg::Post-Invoke");
492 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
493 return _error
->Error("Sub-process %s received a segmentation fault.",Args
[0]);
495 if (WIFEXITED(Status
) != 0)
496 return _error
->Error("Sub-process %s returned an error code (%u)",Args
[0],WEXITSTATUS(Status
));
498 return _error
->Error("Sub-process %s exited unexpectedly",Args
[0]);
502 if (RunScripts("DPkg::Post-Invoke") == false)
507 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
508 // ---------------------------------------------------------------------
510 void pkgDPkgPM::Reset()
512 List
.erase(List
.begin(),List
.end());