]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz 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");
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.*/
152 bool pkgDPkgPM::SendV2Pkgs(FILE *F
)
154 fprintf(F
,"VERSION 2\n");
156 /* Write out all of the configuration directives by walking the
157 configuration tree */
158 const Configuration::Item
*Top
= _config
->Tree(0);
161 if (Top
->Value
.empty() == false)
164 QuoteString(Top
->FullTag(),"=\"\n").c_str(),
165 QuoteString(Top
->Value
,"\n").c_str());
174 while (Top
!= 0 && Top
->Next
== 0)
181 // Write out the package actions in order.
182 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
184 pkgDepCache::StateCache
&S
= Cache
[I
->Pkg
];
186 fprintf(F
,"%s ",I
->Pkg
.Name());
188 if (I
->Pkg
->CurrentVer
== 0)
191 fprintf(F
,"%s ",I
->Pkg
.CurrentVer().VerStr());
193 // Show the compare operator
195 if (S
.InstallVer
!= 0)
198 if (I
->Pkg
->CurrentVer
!= 0)
199 Comp
= S
.InstVerIter(Cache
).CompareVer(I
->Pkg
.CurrentVer());
206 fprintf(F
,"%s ",S
.InstVerIter(Cache
).VerStr());
211 // Show the filename/operation
212 if (I
->Op
== Item::Install
)
215 if (I
->File
[0] != '/')
216 fprintf(F
,"**ERROR**\n");
218 fprintf(F
,"%s\n",I
->File
.c_str());
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");
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. */
237 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
239 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
240 if (Opts
== 0 || Opts
->Child
== 0)
244 unsigned int Count
= 1;
245 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
247 if (Opts
->Value
.empty() == true)
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();
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
);
318 // Clean up the sub process
319 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
320 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
326 // DPkgPM::Go - Run the sequence /*{{{*/
327 // ---------------------------------------------------------------------
328 /* This globs the operations and calls dpkg */
329 bool pkgDPkgPM::Go(int status_fd
)
331 unsigned int MaxArgs
= _config
->FindI("Dpkg::MaxArgs",8*1024);
332 unsigned int MaxArgBytes
= _config
->FindI("Dpkg::MaxArgBytes",32*1024);
334 if (RunScripts("DPkg::Pre-Invoke") == false)
337 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
340 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
342 vector
<Item
>::iterator J
= I
;
343 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
345 // Generate the argument list
346 const char *Args
[MaxArgs
+ 50];
347 if (J
- I
> (signed)MaxArgs
)
351 unsigned long Size
= 0;
352 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
353 Args
[n
++] = Tmp
.c_str();
354 Size
+= strlen(Args
[n
-1]);
356 // Stick in any custom dpkg options
357 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
361 for (; Opts
!= 0; Opts
= Opts
->Next
)
363 if (Opts
->Value
.empty() == true)
365 Args
[n
++] = Opts
->Value
.c_str();
366 Size
+= Opts
->Value
.length();
370 // if we got a status_fd argument, we pass it to apt
371 char status_fd_buf
[20];
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]);
384 Args
[n
++] = "--force-depends";
385 Size
+= strlen(Args
[n
-1]);
386 Args
[n
++] = "--force-remove-essential";
387 Size
+= strlen(Args
[n
-1]);
388 Args
[n
++] = "--remove";
389 Size
+= strlen(Args
[n
-1]);
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]);
401 case Item::Configure
:
402 Args
[n
++] = "--configure";
403 Size
+= strlen(Args
[n
-1]);
407 Args
[n
++] = "--unpack";
408 Size
+= strlen(Args
[n
-1]);
412 // Write in the file or package names
413 if (I
->Op
== Item::Install
)
415 for (;I
!= J
&& Size
< MaxArgBytes
; I
++)
417 if (I
->File
[0] != '/')
418 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
419 Args
[n
++] = I
->File
.c_str();
420 Size
+= strlen(Args
[n
-1]);
425 for (;I
!= J
&& Size
< MaxArgBytes
; I
++)
427 Args
[n
++] = I
->Pkg
.Name();
428 Size
+= strlen(Args
[n
-1]);
434 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
436 for (unsigned int k
= 0; k
!= n
; k
++)
437 clog
<< Args
[k
] << ' ';
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 */
450 sighandler_t old_SIGQUIT
= signal(SIGQUIT
,SIG_IGN
);
451 sighandler_t old_SIGINT
= signal(SIGINT
,SIG_IGN
);
456 Child
= ExecFork(status_fd
);
463 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
466 if (_config
->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO
))
469 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
472 // Discard everything in stdin before forking dpkg
473 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
476 while (read(STDIN_FILENO
,&dummy
,1) == 1);
478 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
482 /* No Job Control Stop Env is a magic dpkg var that prevents it
483 from using sigstop */
484 putenv("DPKG_NO_TSTP=yes");
485 execvp(Args
[0],(char **)Args
);
486 cerr
<< "Could not exec dpkg!" << endl
;
492 while (waitpid(Child
,&Status
,0) != Child
)
496 RunScripts("DPkg::Post-Invoke");
498 // Restore sig int/quit
499 signal(SIGQUIT
,old_SIGQUIT
);
500 signal(SIGINT
,old_SIGINT
);
501 return _error
->Errno("waitpid","Couldn't wait for subprocess");
504 // Restore sig int/quit
505 signal(SIGQUIT
,old_SIGQUIT
);
506 signal(SIGINT
,old_SIGINT
);
508 // Check for an error code.
509 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
511 RunScripts("DPkg::Post-Invoke");
512 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
513 return _error
->Error("Sub-process %s received a segmentation fault.",Args
[0]);
515 if (WIFEXITED(Status
) != 0)
516 return _error
->Error("Sub-process %s returned an error code (%u)",Args
[0],WEXITSTATUS(Status
));
518 return _error
->Error("Sub-process %s exited unexpectedly",Args
[0]);
522 if (RunScripts("DPkg::Post-Invoke") == false)
527 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
528 // ---------------------------------------------------------------------
530 void pkgDPkgPM::Reset()
532 List
.erase(List
.begin(),List
.end());