]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
998750b3b02011565bf3b0cd299397cd64bb056c
[apt.git] / apt-pkg / deb / dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dpkgpm.cc,v 1.10 1999/07/03 03:10:35 jgg Exp $
4 /* ######################################################################
5
6 DPKG Package Manager - Provide an interface to dpkg
7
8 ##################################################################### */
9 /*}}}*/
10 // Includes /*{{{*/
11 #ifdef __GNUG__
12 #pragma implementation "apt-pkg/dpkgpm.h"
13 #endif
14 #include <apt-pkg/dpkgpm.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <signal.h>
24 #include <errno.h>
25 /*}}}*/
26
27 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache)
31 {
32 }
33 /*}}}*/
34 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* */
37 pkgDPkgPM::~pkgDPkgPM()
38 {
39 }
40 /*}}}*/
41 // DPkgPM::Install - Install a package /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Add an install operation to the sequence list */
44 bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
45 {
46 if (File.empty() == true || Pkg.end() == true)
47 return _error->Error("Internal Error, No file name for %s",Pkg.Name());
48
49 List.push_back(Item(Item::Install,Pkg,File));
50 return true;
51 }
52 /*}}}*/
53 // DPkgPM::Configure - Configure a package /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Add a configure operation to the sequence list */
56 bool pkgDPkgPM::Configure(PkgIterator Pkg)
57 {
58 if (Pkg.end() == true)
59 return false;
60
61 List.push_back(Item(Item::Configure,Pkg));
62 return true;
63 }
64 /*}}}*/
65 // DPkgPM::Remove - Remove a package /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Add a remove operation to the sequence list */
68 bool pkgDPkgPM::Remove(PkgIterator Pkg)
69 {
70 if (Pkg.end() == true)
71 return false;
72
73 List.push_back(Item(Item::Remove,Pkg));
74 return true;
75 }
76 /*}}}*/
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)
82 {
83 Configuration::Item const *Opts = _config->Tree(Cnf);
84 if (Opts == 0 || Opts->Child == 0)
85 return true;
86 Opts = Opts->Child;
87
88 // Fork for running the system calls
89 pid_t Child = ExecFork();
90
91 // This is the child
92 if (Child == 0)
93 {
94 if (chdir("/tmp/") != 0)
95 _exit(100);
96
97 unsigned int Count = 1;
98 for (; Opts != 0; Opts = Opts->Next, Count++)
99 {
100 if (Opts->Value.empty() == true)
101 continue;
102
103 if (system(Opts->Value.c_str()) != 0)
104 _exit(100+Count);
105 }
106 _exit(0);
107 }
108
109 // Wait for the child
110 int Status = 0;
111 while (waitpid(Child,&Status,0) != Child)
112 {
113 if (errno == EINTR)
114 continue;
115 return _error->Errno("waitpid","Couldn't wait for subprocess");
116 }
117
118 // Restore sig int/quit
119 signal(SIGQUIT,SIG_DFL);
120 signal(SIGINT,SIG_DFL);
121
122 // Check for an error code.
123 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
124 {
125 unsigned int Count = WEXITSTATUS(Status);
126 if (Count > 100)
127 {
128 Count -= 100;
129 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
130 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
131 }
132
133 return _error->Error("Sub-process returned an error code");
134 }
135
136 return true;
137 }
138 /*}}}*/
139 // DPkgPM::Go - Run the sequence /*{{{*/
140 // ---------------------------------------------------------------------
141 /* This globs the operations and calls dpkg */
142 bool pkgDPkgPM::Go()
143 {
144 if (RunScripts("DPkg::Pre-Invoke") == false)
145 return false;
146
147 for (vector<Item>::iterator I = List.begin(); I != List.end();)
148 {
149 vector<Item>::iterator J = I;
150 for (; J != List.end() && J->Op == I->Op; J++);
151
152 // Generate the argument list
153 const char *Args[400];
154 if (J - I > 350)
155 J = I + 350;
156
157 unsigned int n = 0;
158 unsigned long Size = 0;
159 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
160 Size += strlen(Args[n-1]);
161
162 // Stick in any custom dpkg options
163 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
164 if (Opts != 0)
165 {
166 Opts = Opts->Child;
167 for (; Opts != 0; Opts = Opts->Next)
168 {
169 if (Opts->Value.empty() == true)
170 continue;
171 Args[n++] = Opts->Value.c_str();
172 Size += Opts->Value.length();
173 }
174 }
175
176 switch (I->Op)
177 {
178 case Item::Remove:
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]);
185 break;
186
187 case Item::Configure:
188 Args[n++] = "--configure";
189 Size += strlen(Args[n-1]);
190 break;
191
192 case Item::Install:
193 Args[n++] = "--unpack";
194 Size += strlen(Args[n-1]);
195 break;
196 }
197
198 // Write in the file or package names
199 if (I->Op == Item::Install)
200 {
201 for (;I != J && Size < 1024; I++)
202 {
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]);
207 }
208 }
209 else
210 {
211 for (;I != J && Size < 1024; I++)
212 {
213 Args[n++] = I->Pkg.Name();
214 Size += strlen(Args[n-1]);
215 }
216 }
217 Args[n] = 0;
218 J = I;
219
220 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
221 {
222 for (unsigned int k = 0; k != n; k++)
223 clog << Args[k] << ' ';
224 clog << endl;
225 continue;
226 }
227
228 cout << flush;
229 clog << flush;
230 cerr << flush;
231
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);
238
239 // Fork dpkg
240 pid_t Child = ExecFork();
241
242 // This is the child
243 if (Child == 0)
244 {
245 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
246 _exit(100);
247
248 int Flags,dummy;
249 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
250 _exit(100);
251
252 // Discard everything in stdin before forking dpkg
253 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
254 _exit(100);
255
256 while (read(STDIN_FILENO,&dummy,1) == 1);
257
258 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
259 _exit(100);
260
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;
266 _exit(100);
267 }
268
269 // Wait for dpkg
270 int Status = 0;
271 while (waitpid(Child,&Status,0) != Child)
272 {
273 if (errno == EINTR)
274 continue;
275 RunScripts("DPkg::Post-Invoke");
276 return _error->Errno("waitpid","Couldn't wait for subprocess");
277 }
278
279 // Restore sig int/quit
280 signal(SIGQUIT,SIG_DFL);
281 signal(SIGINT,SIG_DFL);
282
283 // Check for an error code.
284 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
285 {
286 RunScripts("DPkg::Post-Invoke");
287 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
288 return _error->Error("Sub-process recieved a segmentation fault.");
289
290 if (WIFEXITED(Status) != 0)
291 return _error->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status));
292
293 return _error->Error("Sub-process exited unexpectedly");
294 }
295 }
296
297 if (RunScripts("DPkg::Post-Invoke") == false)
298 return false;
299 return true;
300 }
301 /*}}}*/
302 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
303 // ---------------------------------------------------------------------
304 /* */
305 void pkgDPkgPM::Reset()
306 {
307 List.erase(List.begin(),List.end());
308 }
309 /*}}}*/