]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
f8c59d844b2223399c88089e24728e4b37671a52
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.8 1999/03/05 19:36:49 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>
21 #include <sys/types.h>
27 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
30 pkgDPkgPM::pkgDPkgPM(pkgDepCache
&Cache
) : pkgPackageManager(Cache
)
34 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35 // ---------------------------------------------------------------------
37 pkgDPkgPM::~pkgDPkgPM()
41 // DPkgPM::Install - Install a package /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Add an install operation to the sequence list */
44 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
46 if (File
.empty() == true || Pkg
.end() == true)
47 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
49 List
.push_back(Item(Item::Install
,Pkg
,File
));
53 // DPkgPM::Configure - Configure a package /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Add a configure operation to the sequence list */
56 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
58 if (Pkg
.end() == true)
61 List
.push_back(Item(Item::Configure
,Pkg
));
65 // DPkgPM::Remove - Remove a package /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Add a remove operation to the sequence list */
68 bool pkgDPkgPM::Remove(PkgIterator Pkg
)
70 if (Pkg
.end() == true)
73 List
.push_back(Item(Item::Remove
,Pkg
));
77 // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
78 // ---------------------------------------------------------------------
79 /* This looks for a list of script sto run from the configuration file,
80 each one is run with system from a forked child. */
81 bool pkgDPkgPM::RunScripts(const char *Cnf
)
83 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
84 if (Opts
== 0 || Opts
->Child
== 0)
88 // Fork for running the system calls
91 return _error
->Errno("fork","Could't fork");
96 signal(SIGPIPE
,SIG_DFL
);
97 signal(SIGQUIT
,SIG_DFL
);
98 signal(SIGINT
,SIG_DFL
);
99 signal(SIGWINCH
,SIG_DFL
);
100 signal(SIGCONT
,SIG_DFL
);
101 signal(SIGTSTP
,SIG_DFL
);
103 if (chdir("/tmp/") != 0)
106 // Close all of our FDs - just in case
107 for (int K
= 3; K
!= 40; K
++)
108 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
110 unsigned int Count
= 1;
111 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
113 if (Opts
->Value
.empty() == true)
116 if (system(Opts
->Value
.c_str()) != 0)
122 // Wait for the child
124 while (waitpid(Child
,&Status
,0) != Child
)
128 return _error
->Errno("waitpid","Couldn't wait for subprocess");
131 // Restore sig int/quit
132 signal(SIGQUIT
,SIG_DFL
);
133 signal(SIGINT
,SIG_DFL
);
135 // Check for an error code.
136 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
138 unsigned int Count
= WEXITSTATUS(Status
);
142 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
143 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
146 return _error
->Error("Sub-process returned an error code");
152 // DPkgPM::Go - Run the sequence /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This globs the operations and calls dpkg */
157 if (RunScripts("DPkg::Pre-Invoke") == false)
160 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
162 vector
<Item
>::iterator J
= I
;
163 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
165 // Generate the argument list
166 const char *Args
[400];
171 unsigned long Size
= 0;
172 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
173 Size
+= strlen(Args
[n
-1]);
175 // Stick in any custom dpkg options
176 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
180 for (; Opts
!= 0; Opts
= Opts
->Next
)
182 if (Opts
->Value
.empty() == true)
184 Args
[n
++] = Opts
->Value
.c_str();
185 Size
+= Opts
->Value
.length();
192 Args
[n
++] = "--force-depends";
193 Size
+= strlen(Args
[n
-1]);
194 Args
[n
++] = "--force-remove-essential";
195 Size
+= strlen(Args
[n
-1]);
196 Args
[n
++] = "--remove";
197 Size
+= strlen(Args
[n
-1]);
200 case Item::Configure
:
201 Args
[n
++] = "--configure";
202 Size
+= strlen(Args
[n
-1]);
206 Args
[n
++] = "--unpack";
207 Size
+= strlen(Args
[n
-1]);
211 // Write in the file or package names
212 if (I
->Op
== Item::Install
)
214 for (;I
!= J
&& Size
< 1024; I
++)
216 if (I
->File
[0] != '/')
217 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
218 Args
[n
++] = I
->File
.c_str();
219 Size
+= strlen(Args
[n
-1]);
224 for (;I
!= J
&& Size
< 1024; I
++)
226 Args
[n
++] = I
->Pkg
.Name();
227 Size
+= strlen(Args
[n
-1]);
233 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
235 for (unsigned int k
= 0; k
!= n
; k
++)
236 clog
<< Args
[k
] << ' ';
245 /* Mask off sig int/quit. We do this because dpkg also does when
246 it forks scripts. What happens is that when you hit ctrl-c it sends
247 it to all processes in the group. Since dpkg ignores the signal
248 it doesn't die but we do! So we must also ignore it */
249 signal(SIGQUIT
,SIG_IGN
);
250 signal(SIGINT
,SIG_IGN
);
253 pid_t Child
= fork();
255 return _error
->Errno("fork","Could't fork");
260 signal(SIGPIPE
,SIG_DFL
);
261 signal(SIGQUIT
,SIG_DFL
);
262 signal(SIGINT
,SIG_DFL
);
263 signal(SIGWINCH
,SIG_DFL
);
264 signal(SIGCONT
,SIG_DFL
);
265 signal(SIGTSTP
,SIG_DFL
);
267 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
270 // Close all of our FDs - just in case
271 for (int K
= 3; K
!= 40; K
++)
272 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
275 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
278 // Discard everything in stdin before forking dpkg
279 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
282 while (read(STDIN_FILENO
,&dummy
,1) == 1);
284 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
287 /* No Job Control Stop Env is a magic dpkg var that prevents it
288 from using sigstop */
289 setenv("DPKG_NO_TSTP","yes",1);
290 execvp(Args
[0],(char **)Args
);
291 cerr
<< "Could not exec dpkg!" << endl
;
297 while (waitpid(Child
,&Status
,0) != Child
)
301 RunScripts("DPkg::Post-Invoke");
302 return _error
->Errno("waitpid","Couldn't wait for subprocess");
305 // Restore sig int/quit
306 signal(SIGQUIT
,SIG_DFL
);
307 signal(SIGINT
,SIG_DFL
);
309 // Check for an error code.
310 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
312 RunScripts("DPkg::Post-Invoke");
313 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
314 return _error
->Error("Sub-process recieved a segmentation fault.");
316 if (WIFEXITED(Status
) != 0)
317 return _error
->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status
));
319 return _error
->Error("Sub-process exited unexpectedly");
323 if (RunScripts("DPkg::Post-Invoke") == false)