fixed speeling
[ntk/apt.git] / apt-pkg / deb / dpkgpm.cc
CommitLineData
03e39e59
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
cf544e14 3// $Id: dpkgpm.cc,v 1.7 1999/01/31 08:55:53 jgg Exp $
03e39e59
AL
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/* */
30pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache)
31{
32}
33 /*}}}*/
34// DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
37pkgDPkgPM::~pkgDPkgPM()
38{
39}
40 /*}}}*/
41// DPkgPM::Install - Install a package /*{{{*/
42// ---------------------------------------------------------------------
43/* Add an install operation to the sequence list */
44bool 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 */
56bool 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 */
68bool pkgDPkgPM::Remove(PkgIterator Pkg)
69{
70 if (Pkg.end() == true)
71 return false;
72
73 List.push_back(Item(Item::Remove,Pkg));
6dd55be7
AL
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. */
81bool 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 = fork();
90 if (Child < 0)
91 return _error->Errno("fork","Could't fork");
92
93 // This is the child
94 if (Child == 0)
95 {
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);
102
103 if (chdir("/tmp/") != 0)
104 _exit(100);
105
106 // Close all of our FDs - just in case
107 for (int K = 3; K != 40; K++)
108 fcntl(K,F_SETFD,FD_CLOEXEC);
109
110 unsigned int Count = 1;
111 for (; Opts != 0; Opts = Opts->Next, Count++)
112 {
113 if (Opts->Value.empty() == true)
114 continue;
115
116 if (system(Opts->Value.c_str()) != 0)
117 _exit(100+Count);
118 }
119 _exit(0);
120 }
121
122 // Wait for the child
123 int Status = 0;
124 while (waitpid(Child,&Status,0) != Child)
125 {
126 if (errno == EINTR)
127 continue;
128 return _error->Errno("waitpid","Couldn't wait for subprocess");
129 }
130
131 // Restore sig int/quit
132 signal(SIGQUIT,SIG_DFL);
133 signal(SIGINT,SIG_DFL);
134
135 // Check for an error code.
136 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
137 {
138 unsigned int Count = WEXITSTATUS(Status);
139 if (Count > 100)
140 {
141 Count -= 100;
142 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
cf544e14 143 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
6dd55be7
AL
144 }
145
146 return _error->Error("Sub-process returned an error code");
147 }
148
03e39e59
AL
149 return true;
150}
151 /*}}}*/
152// DPkgPM::Go - Run the sequence /*{{{*/
153// ---------------------------------------------------------------------
154/* This globs the operations and calls dpkg */
155bool pkgDPkgPM::Go()
156{
6dd55be7
AL
157 if (RunScripts("DPkg::Pre-Invoke") == false)
158 return false;
159
03e39e59
AL
160 for (vector<Item>::iterator I = List.begin(); I != List.end();)
161 {
162 vector<Item>::iterator J = I;
163 for (; J != List.end() && J->Op == I->Op; J++);
30e1eab5 164
03e39e59
AL
165 // Generate the argument list
166 const char *Args[400];
167 if (J - I > 350)
168 J = I + 350;
169
30e1eab5
AL
170 unsigned int n = 0;
171 unsigned long Size = 0;
172 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
173 Size += strlen(Args[n-1]);
03e39e59 174
6dd55be7
AL
175 // Stick in any custom dpkg options
176 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
177 if (Opts != 0)
178 {
179 Opts = Opts->Child;
180 for (; Opts != 0; Opts = Opts->Next)
181 {
182 if (Opts->Value.empty() == true)
183 continue;
184 Args[n++] = Opts->Value.c_str();
185 Size += Opts->Value.length();
186 }
187 }
188
03e39e59
AL
189 switch (I->Op)
190 {
191 case Item::Remove:
192 Args[n++] = "--force-depends";
30e1eab5 193 Size += strlen(Args[n-1]);
03e39e59 194 Args[n++] = "--force-remove-essential";
30e1eab5 195 Size += strlen(Args[n-1]);
03e39e59 196 Args[n++] = "--remove";
30e1eab5 197 Size += strlen(Args[n-1]);
03e39e59
AL
198 break;
199
200 case Item::Configure:
201 Args[n++] = "--configure";
30e1eab5 202 Size += strlen(Args[n-1]);
03e39e59
AL
203 break;
204
205 case Item::Install:
206 Args[n++] = "--unpack";
30e1eab5 207 Size += strlen(Args[n-1]);
03e39e59
AL
208 break;
209 }
210
211 // Write in the file or package names
212 if (I->Op == Item::Install)
30e1eab5
AL
213 {
214 for (;I != J && Size < 1024; I++)
215 {
cf544e14
AL
216 if (I->File[0] != '/')
217 return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
03e39e59 218 Args[n++] = I->File.c_str();
30e1eab5
AL
219 Size += strlen(Args[n-1]);
220 }
221 }
03e39e59 222 else
30e1eab5
AL
223 {
224 for (;I != J && Size < 1024; I++)
225 {
03e39e59 226 Args[n++] = I->Pkg.Name();
30e1eab5
AL
227 Size += strlen(Args[n-1]);
228 }
229 }
03e39e59 230 Args[n] = 0;
30e1eab5
AL
231 J = I;
232
233 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
234 {
235 for (unsigned int k = 0; k != n; k++)
236 clog << Args[k] << ' ';
237 clog << endl;
238 continue;
239 }
03e39e59 240
03e39e59
AL
241 cout << flush;
242 clog << flush;
243 cerr << flush;
244
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);
6dd55be7 251
03e39e59
AL
252 // Fork dpkg
253 pid_t Child = fork();
254 if (Child < 0)
255 return _error->Errno("fork","Could't fork");
6dd55be7 256
03e39e59
AL
257 // This is the child
258 if (Child == 0)
259 {
d38b7b3d 260 signal(SIGPIPE,SIG_DFL);
03e39e59
AL
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);
6dd55be7 266
cf544e14 267 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
0dbb95d8 268 _exit(100);
03e39e59
AL
269
270 // Close all of our FDs - just in case
271 for (int K = 3; K != 40; K++)
272 fcntl(K,F_SETFD,FD_CLOEXEC);
273
274 int Flags,dummy;
275 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
0dbb95d8 276 _exit(100);
03e39e59
AL
277
278 // Discard everything in stdin before forking dpkg
279 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
0dbb95d8 280 _exit(100);
03e39e59
AL
281
282 while (read(STDIN_FILENO,&dummy,1) == 1);
283
284 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
0dbb95d8 285 _exit(100);
03e39e59
AL
286
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);
d568ed2d 290 execvp(Args[0],(char **)Args);
03e39e59 291 cerr << "Could not exec dpkg!" << endl;
0dbb95d8 292 _exit(100);
03e39e59
AL
293 }
294
295 // Wait for dpkg
296 int Status = 0;
297 while (waitpid(Child,&Status,0) != Child)
298 {
299 if (errno == EINTR)
300 continue;
6dd55be7 301 RunScripts("DPkg::Post-Invoke");
03e39e59
AL
302 return _error->Errno("waitpid","Couldn't wait for subprocess");
303 }
03e39e59
AL
304
305 // Restore sig int/quit
306 signal(SIGQUIT,SIG_DFL);
307 signal(SIGINT,SIG_DFL);
6dd55be7
AL
308
309 // Check for an error code.
310 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
311 {
312 RunScripts("DPkg::Post-Invoke");
313 return _error->Error("Sub-process returned an error code");
314 }
03e39e59 315 }
6dd55be7
AL
316
317 if (RunScripts("DPkg::Post-Invoke") == false)
318 return false;
03e39e59
AL
319 return true;
320}
321 /*}}}*/