]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
998750b3b02011565bf3b0cd299397cd64bb056c
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.10 1999/07/03 03:10:35 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
89 pid_t Child
= ExecFork();
94 if (chdir("/tmp/") != 0)
97 unsigned int Count
= 1;
98 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
100 if (Opts
->Value
.empty() == true)
103 if (system(Opts
->Value
.c_str()) != 0)
109 // Wait for the child
111 while (waitpid(Child
,&Status
,0) != Child
)
115 return _error
->Errno("waitpid","Couldn't wait for subprocess");
118 // Restore sig int/quit
119 signal(SIGQUIT
,SIG_DFL
);
120 signal(SIGINT
,SIG_DFL
);
122 // Check for an error code.
123 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
125 unsigned int Count
= WEXITSTATUS(Status
);
129 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
130 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
133 return _error
->Error("Sub-process returned an error code");
139 // DPkgPM::Go - Run the sequence /*{{{*/
140 // ---------------------------------------------------------------------
141 /* This globs the operations and calls dpkg */
144 if (RunScripts("DPkg::Pre-Invoke") == false)
147 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
149 vector
<Item
>::iterator J
= I
;
150 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
152 // Generate the argument list
153 const char *Args
[400];
158 unsigned long Size
= 0;
159 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
160 Size
+= strlen(Args
[n
-1]);
162 // Stick in any custom dpkg options
163 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
167 for (; Opts
!= 0; Opts
= Opts
->Next
)
169 if (Opts
->Value
.empty() == true)
171 Args
[n
++] = Opts
->Value
.c_str();
172 Size
+= Opts
->Value
.length();
179 Args
[n
++] = "--force-depends";
180 Size
+= strlen(Args
[n
-1]);
181 Args
[n
++] = "--force-remove-essential";
182 Size
+= strlen(Args
[n
-1]);
183 Args
[n
++] = "--remove";
184 Size
+= strlen(Args
[n
-1]);
187 case Item::Configure
:
188 Args
[n
++] = "--configure";
189 Size
+= strlen(Args
[n
-1]);
193 Args
[n
++] = "--unpack";
194 Size
+= strlen(Args
[n
-1]);
198 // Write in the file or package names
199 if (I
->Op
== Item::Install
)
201 for (;I
!= J
&& Size
< 1024; I
++)
203 if (I
->File
[0] != '/')
204 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
205 Args
[n
++] = I
->File
.c_str();
206 Size
+= strlen(Args
[n
-1]);
211 for (;I
!= J
&& Size
< 1024; I
++)
213 Args
[n
++] = I
->Pkg
.Name();
214 Size
+= strlen(Args
[n
-1]);
220 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
222 for (unsigned int k
= 0; k
!= n
; k
++)
223 clog
<< Args
[k
] << ' ';
232 /* Mask off sig int/quit. We do this because dpkg also does when
233 it forks scripts. What happens is that when you hit ctrl-c it sends
234 it to all processes in the group. Since dpkg ignores the signal
235 it doesn't die but we do! So we must also ignore it */
236 signal(SIGQUIT
,SIG_IGN
);
237 signal(SIGINT
,SIG_IGN
);
240 pid_t Child
= ExecFork();
245 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
249 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
252 // Discard everything in stdin before forking dpkg
253 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
256 while (read(STDIN_FILENO
,&dummy
,1) == 1);
258 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
261 /* No Job Control Stop Env is a magic dpkg var that prevents it
262 from using sigstop */
263 setenv("DPKG_NO_TSTP","yes",1);
264 execvp(Args
[0],(char **)Args
);
265 cerr
<< "Could not exec dpkg!" << endl
;
271 while (waitpid(Child
,&Status
,0) != Child
)
275 RunScripts("DPkg::Post-Invoke");
276 return _error
->Errno("waitpid","Couldn't wait for subprocess");
279 // Restore sig int/quit
280 signal(SIGQUIT
,SIG_DFL
);
281 signal(SIGINT
,SIG_DFL
);
283 // Check for an error code.
284 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
286 RunScripts("DPkg::Post-Invoke");
287 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
288 return _error
->Error("Sub-process recieved a segmentation fault.");
290 if (WIFEXITED(Status
) != 0)
291 return _error
->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status
));
293 return _error
->Error("Sub-process exited unexpectedly");
297 if (RunScripts("DPkg::Post-Invoke") == false)
302 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
303 // ---------------------------------------------------------------------
305 void pkgDPkgPM::Reset()
307 List
.erase(List
.begin(),List
.end());