]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.19 2001/02/22 06:26:27 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>
30 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
33 pkgDPkgPM::pkgDPkgPM(pkgDepCache
*Cache
) : pkgPackageManager(Cache
)
37 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
38 // ---------------------------------------------------------------------
40 pkgDPkgPM::~pkgDPkgPM()
44 // DPkgPM::Install - Install a package /*{{{*/
45 // ---------------------------------------------------------------------
46 /* Add an install operation to the sequence list */
47 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
49 if (File
.empty() == true || Pkg
.end() == true)
50 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
52 List
.push_back(Item(Item::Install
,Pkg
,File
));
56 // DPkgPM::Configure - Configure a package /*{{{*/
57 // ---------------------------------------------------------------------
58 /* Add a configure operation to the sequence list */
59 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
61 if (Pkg
.end() == true)
64 List
.push_back(Item(Item::Configure
,Pkg
));
68 // DPkgPM::Remove - Remove a package /*{{{*/
69 // ---------------------------------------------------------------------
70 /* Add a remove operation to the sequence list */
71 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
73 if (Pkg
.end() == true)
77 List
.push_back(Item(Item::Purge
,Pkg
));
79 List
.push_back(Item(Item::Remove
,Pkg
));
83 // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
84 // ---------------------------------------------------------------------
85 /* This looks for a list of script sto run from the configuration file,
86 each one is run with system from a forked child. */
87 bool pkgDPkgPM::RunScripts(const char *Cnf
)
89 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
90 if (Opts
== 0 || Opts
->Child
== 0)
94 // Fork for running the system calls
95 pid_t Child
= ExecFork();
100 if (chdir("/tmp/") != 0)
103 unsigned int Count
= 1;
104 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
106 if (Opts
->Value
.empty() == true)
109 if (system(Opts
->Value
.c_str()) != 0)
115 // Wait for the child
117 while (waitpid(Child
,&Status
,0) != Child
)
121 return _error
->Errno("waitpid","Couldn't wait for subprocess");
124 // Restore sig int/quit
125 signal(SIGQUIT
,SIG_DFL
);
126 signal(SIGINT
,SIG_DFL
);
128 // Check for an error code.
129 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
131 unsigned int Count
= WEXITSTATUS(Status
);
135 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
136 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
139 return _error
->Error("Sub-process returned an error code");
146 // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This is part of the helper script communication interface, it sends
149 very complete information down to the other end of the pipe.*/
150 bool pkgDPkgPM::SendV2Pkgs(FILE *F
)
152 fprintf(F
,"VERSION 2\n");
154 /* Write out all of the configuration directives by walking the
155 configuration tree */
156 const Configuration::Item
*Top
= _config
->Tree(0);
159 if (Top
->Value
.empty() == false)
162 QuoteString(Top
->FullTag(),"=\"\n").c_str(),
163 QuoteString(Top
->Value
,"\n").c_str());
172 while (Top
!= 0 && Top
->Next
== 0)
179 // Write out the package actions in order.
180 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
182 pkgDepCache::StateCache
&S
= Cache
[I
->Pkg
];
184 fprintf(F
,"%s ",I
->Pkg
.Name());
186 if (I
->Pkg
->CurrentVer
== 0)
189 fprintf(F
,"%s ",I
->Pkg
.CurrentVer().VerStr());
191 // Show the compare operator
193 if (S
.InstallVer
!= 0)
196 if (I
->Pkg
->CurrentVer
!= 0)
197 Comp
= S
.InstVerIter(Cache
).CompareVer(I
->Pkg
.CurrentVer());
204 fprintf(F
,"%s ",S
.InstVerIter(Cache
).VerStr());
209 // Show the filename/operation
210 if (I
->Op
== Item::Install
)
213 if (I
->File
[0] != '/')
214 fprintf(F
,"**ERROR**\n");
216 fprintf(F
,"%s\n",I
->File
.c_str());
218 if (I
->Op
== Item::Configure
)
219 fprintf(F
,"**CONFIGURE**\n");
220 if (I
->Op
== Item::Remove
||
221 I
->Op
== Item::Purge
)
222 fprintf(F
,"**REMOVE**\n");
230 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
231 // ---------------------------------------------------------------------
232 /* This looks for a list of scripts to run from the configuration file
233 each one is run and is fed on standard input a list of all .deb files
234 that are due to be installed. */
235 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
237 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
238 if (Opts
== 0 || Opts
->Child
== 0)
242 unsigned int Count
= 1;
243 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
245 if (Opts
->Value
.empty() == true)
248 // Determine the protocol version
249 string OptSec
= Opts
->Value
;
250 string::size_type Pos
;
251 if ((Pos
= OptSec
.find(' ')) == string::npos
|| Pos
== 0)
252 Pos
= OptSec
.length();
253 OptSec
= "DPkg::Tools::Options::" + string(Opts
->Value
.c_str(),Pos
);
255 unsigned int Version
= _config
->FindI(OptSec
+"::Version",1);
259 if (pipe(Pipes
) != 0)
260 return _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
261 SetCloseExec(Pipes
[0],true);
262 SetCloseExec(Pipes
[1],true);
264 // Purified Fork for running the script
265 pid_t Process
= ExecFork();
269 dup2(Pipes
[0],STDIN_FILENO
);
270 SetCloseExec(STDOUT_FILENO
,false);
271 SetCloseExec(STDIN_FILENO
,false);
272 SetCloseExec(STDERR_FILENO
,false);
277 Args
[2] = Opts
->Value
.c_str();
279 execv(Args
[0],(char **)Args
);
283 FILE *F
= fdopen(Pipes
[1],"w");
285 return _error
->Errno("fdopen","Faild to open new FD");
287 // Feed it the filenames.
291 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
293 // Only deal with packages to be installed from .deb
294 if (I
->Op
!= Item::Install
)
298 if (I
->File
[0] != '/')
301 /* Feed the filename of each package that is pending install
303 fprintf(F
,"%s\n",I
->File
.c_str());
312 Die
= !SendV2Pkgs(F
);
317 kill(Process
,SIGINT
);
318 ExecWait(Process
,Opts
->Value
.c_str(),true);
319 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
322 // Clean up the sub process
323 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
324 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
331 // DPkgPM::Go - Run the sequence /*{{{*/
332 // ---------------------------------------------------------------------
333 /* This globs the operations and calls dpkg */
336 if (RunScripts("DPkg::Pre-Invoke") == false)
339 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
342 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
344 vector
<Item
>::iterator J
= I
;
345 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
347 // Generate the argument list
348 const char *Args
[400];
353 unsigned long Size
= 0;
354 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
355 Size
+= strlen(Args
[n
-1]);
357 // Stick in any custom dpkg options
358 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
362 for (; Opts
!= 0; Opts
= Opts
->Next
)
364 if (Opts
->Value
.empty() == true)
366 Args
[n
++] = Opts
->Value
.c_str();
367 Size
+= Opts
->Value
.length();
374 Args
[n
++] = "--force-depends";
375 Size
+= strlen(Args
[n
-1]);
376 Args
[n
++] = "--force-remove-essential";
377 Size
+= strlen(Args
[n
-1]);
378 Args
[n
++] = "--remove";
379 Size
+= strlen(Args
[n
-1]);
383 Args
[n
++] = "--force-depends";
384 Size
+= strlen(Args
[n
-1]);
385 Args
[n
++] = "--force-remove-essential";
386 Size
+= strlen(Args
[n
-1]);
387 Args
[n
++] = "--purge";
388 Size
+= strlen(Args
[n
-1]);
391 case Item::Configure
:
392 Args
[n
++] = "--configure";
393 Size
+= strlen(Args
[n
-1]);
397 Args
[n
++] = "--unpack";
398 Size
+= strlen(Args
[n
-1]);
402 // Write in the file or package names
403 if (I
->Op
== Item::Install
)
405 for (;I
!= J
&& Size
< 1024; I
++)
407 if (I
->File
[0] != '/')
408 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
409 Args
[n
++] = I
->File
.c_str();
410 Size
+= strlen(Args
[n
-1]);
415 for (;I
!= J
&& Size
< 1024; I
++)
417 Args
[n
++] = I
->Pkg
.Name();
418 Size
+= strlen(Args
[n
-1]);
424 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
426 for (unsigned int k
= 0; k
!= n
; k
++)
427 clog
<< Args
[k
] << ' ';
436 /* Mask off sig int/quit. We do this because dpkg also does when
437 it forks scripts. What happens is that when you hit ctrl-c it sends
438 it to all processes in the group. Since dpkg ignores the signal
439 it doesn't die but we do! So we must also ignore it */
440 signal(SIGQUIT
,SIG_IGN
);
441 signal(SIGINT
,SIG_IGN
);
444 pid_t Child
= ExecFork();
449 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
452 if (_config
->FindB("DPkg::FlushSTDIN",true) == true)
455 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
458 // Discard everything in stdin before forking dpkg
459 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
462 while (read(STDIN_FILENO
,&dummy
,1) == 1);
464 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
468 /* No Job Control Stop Env is a magic dpkg var that prevents it
469 from using sigstop */
470 putenv("DPKG_NO_TSTP=yes");
471 execvp(Args
[0],(char **)Args
);
472 cerr
<< "Could not exec dpkg!" << endl
;
478 while (waitpid(Child
,&Status
,0) != Child
)
482 RunScripts("DPkg::Post-Invoke");
483 return _error
->Errno("waitpid","Couldn't wait for subprocess");
486 // Restore sig int/quit
487 signal(SIGQUIT
,SIG_DFL
);
488 signal(SIGINT
,SIG_DFL
);
490 // Check for an error code.
491 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
493 RunScripts("DPkg::Post-Invoke");
494 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
495 return _error
->Error("Sub-process %s received a segmentation fault.",Args
[0]);
497 if (WIFEXITED(Status
) != 0)
498 return _error
->Error("Sub-process %s returned an error code (%u)",Args
[0],WEXITSTATUS(Status
));
500 return _error
->Error("Sub-process %s exited unexpectedly",Args
[0]);
504 if (RunScripts("DPkg::Post-Invoke") == false)
509 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
510 // ---------------------------------------------------------------------
512 void pkgDPkgPM::Reset()
514 List
.erase(List
.begin(),List
.end());