1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.18 2001/02/20 07:03:17 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();
255 OptSec
= "DPkg::Tools::Options::" + string(Opts
->Value
.c_str(),Pos
);
257 unsigned int Version
= _config
->FindI(OptSec
+"::Version",1);
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);
266 // Purified Fork for running the script
267 pid_t Process
= ExecFork();
271 dup2(Pipes
[0],STDIN_FILENO
);
272 SetCloseExec(STDOUT_FILENO
,false);
273 SetCloseExec(STDIN_FILENO
,false);
274 SetCloseExec(STDERR_FILENO
,false);
279 Args
[2] = Opts
->Value
.c_str();
281 execv(Args
[0],(char **)Args
);
285 FILE *F
= fdopen(Pipes
[1],"w");
287 return _error
->Errno("fdopen","Faild to open new FD");
289 // Feed it the filenames.
293 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
295 // Only deal with packages to be installed from .deb
296 if (I
->Op
!= Item::Install
)
300 if (I
->File
[0] != '/')
303 /* Feed the filename of each package that is pending install
305 fprintf(F
,"%s\n",I
->File
.c_str());
314 Die
= !SendV2Pkgs(F
);
319 kill(Process
,SIGINT
);
320 ExecWait(Process
,Opts
->Value
.c_str(),true);
321 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
324 // Clean up the sub process
325 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
326 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
333 // DPkgPM::Go - Run the sequence /*{{{*/
334 // ---------------------------------------------------------------------
335 /* This globs the operations and calls dpkg */
338 if (RunScripts("DPkg::Pre-Invoke") == false)
341 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
344 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
346 vector
<Item
>::iterator J
= I
;
347 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
349 // Generate the argument list
350 const char *Args
[400];
355 unsigned long Size
= 0;
356 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
357 Size
+= strlen(Args
[n
-1]);
359 // Stick in any custom dpkg options
360 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
364 for (; Opts
!= 0; Opts
= Opts
->Next
)
366 if (Opts
->Value
.empty() == true)
368 Args
[n
++] = Opts
->Value
.c_str();
369 Size
+= Opts
->Value
.length();
376 Args
[n
++] = "--force-depends";
377 Size
+= strlen(Args
[n
-1]);
378 Args
[n
++] = "--force-remove-essential";
379 Size
+= strlen(Args
[n
-1]);
380 Args
[n
++] = "--remove";
381 Size
+= strlen(Args
[n
-1]);
385 Args
[n
++] = "--force-depends";
386 Size
+= strlen(Args
[n
-1]);
387 Args
[n
++] = "--force-remove-essential";
388 Size
+= strlen(Args
[n
-1]);
389 Args
[n
++] = "--purge";
390 Size
+= strlen(Args
[n
-1]);
393 case Item::Configure
:
394 Args
[n
++] = "--configure";
395 Size
+= strlen(Args
[n
-1]);
399 Args
[n
++] = "--unpack";
400 Size
+= strlen(Args
[n
-1]);
404 // Write in the file or package names
405 if (I
->Op
== Item::Install
)
407 for (;I
!= J
&& Size
< 1024; I
++)
409 if (I
->File
[0] != '/')
410 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
411 Args
[n
++] = I
->File
.c_str();
412 Size
+= strlen(Args
[n
-1]);
417 for (;I
!= J
&& Size
< 1024; I
++)
419 Args
[n
++] = I
->Pkg
.Name();
420 Size
+= strlen(Args
[n
-1]);
426 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
428 for (unsigned int k
= 0; k
!= n
; k
++)
429 clog
<< Args
[k
] << ' ';
438 /* Mask off sig int/quit. We do this because dpkg also does when
439 it forks scripts. What happens is that when you hit ctrl-c it sends
440 it to all processes in the group. Since dpkg ignores the signal
441 it doesn't die but we do! So we must also ignore it */
442 signal(SIGQUIT
,SIG_IGN
);
443 signal(SIGINT
,SIG_IGN
);
446 pid_t Child
= ExecFork();
451 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
454 if (_config
->FindB("DPkg::FlushSTDIN",true) == true)
457 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
460 // Discard everything in stdin before forking dpkg
461 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
464 while (read(STDIN_FILENO
,&dummy
,1) == 1);
466 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
470 /* No Job Control Stop Env is a magic dpkg var that prevents it
471 from using sigstop */
472 putenv("DPKG_NO_TSTP=yes");
473 execvp(Args
[0],(char **)Args
);
474 cerr
<< "Could not exec dpkg!" << endl
;
480 while (waitpid(Child
,&Status
,0) != Child
)
484 RunScripts("DPkg::Post-Invoke");
485 return _error
->Errno("waitpid","Couldn't wait for subprocess");
488 // Restore sig int/quit
489 signal(SIGQUIT
,SIG_DFL
);
490 signal(SIGINT
,SIG_DFL
);
492 // Check for an error code.
493 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
495 RunScripts("DPkg::Post-Invoke");
496 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
497 return _error
->Error("Sub-process %s received a segmentation fault.",Args
[0]);
499 if (WIFEXITED(Status
) != 0)
500 return _error
->Error("Sub-process %s returned an error code (%u)",Args
[0],WEXITSTATUS(Status
));
502 return _error
->Error("Sub-process %s exited unexpectedly",Args
[0]);
506 if (RunScripts("DPkg::Post-Invoke") == false)
511 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
512 // ---------------------------------------------------------------------
514 void pkgDPkgPM::Reset()
516 List
.erase(List
.begin(),List
.end());