]>
Commit | Line | Data |
---|---|---|
03e39e59 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
54676e1a | 3 | // $Id: dpkgpm.cc,v 1.9 1999/04/20 05:02:09 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 | /* */ | |
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)); | |
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. */ | |
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 | |
54676e1a | 89 | pid_t Child = ExecFork(); |
6dd55be7 AL |
90 | |
91 | // This is the child | |
92 | if (Child == 0) | |
93 | { | |
6dd55be7 AL |
94 | if (chdir("/tmp/") != 0) |
95 | _exit(100); | |
96 | ||
6dd55be7 AL |
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--); | |
cf544e14 | 130 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); |
6dd55be7 AL |
131 | } |
132 | ||
133 | return _error->Error("Sub-process returned an error code"); | |
134 | } | |
135 | ||
03e39e59 AL |
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 | { | |
6dd55be7 AL |
144 | if (RunScripts("DPkg::Pre-Invoke") == false) |
145 | return false; | |
146 | ||
03e39e59 AL |
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++); | |
30e1eab5 | 151 | |
03e39e59 AL |
152 | // Generate the argument list |
153 | const char *Args[400]; | |
154 | if (J - I > 350) | |
155 | J = I + 350; | |
156 | ||
30e1eab5 AL |
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]); | |
03e39e59 | 161 | |
6dd55be7 AL |
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 | ||
03e39e59 AL |
176 | switch (I->Op) |
177 | { | |
178 | case Item::Remove: | |
179 | Args[n++] = "--force-depends"; | |
30e1eab5 | 180 | Size += strlen(Args[n-1]); |
03e39e59 | 181 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 182 | Size += strlen(Args[n-1]); |
03e39e59 | 183 | Args[n++] = "--remove"; |
30e1eab5 | 184 | Size += strlen(Args[n-1]); |
03e39e59 AL |
185 | break; |
186 | ||
187 | case Item::Configure: | |
188 | Args[n++] = "--configure"; | |
30e1eab5 | 189 | Size += strlen(Args[n-1]); |
03e39e59 AL |
190 | break; |
191 | ||
192 | case Item::Install: | |
193 | Args[n++] = "--unpack"; | |
30e1eab5 | 194 | Size += strlen(Args[n-1]); |
03e39e59 AL |
195 | break; |
196 | } | |
197 | ||
198 | // Write in the file or package names | |
199 | if (I->Op == Item::Install) | |
30e1eab5 AL |
200 | { |
201 | for (;I != J && Size < 1024; I++) | |
202 | { | |
cf544e14 AL |
203 | if (I->File[0] != '/') |
204 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 205 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
206 | Size += strlen(Args[n-1]); |
207 | } | |
208 | } | |
03e39e59 | 209 | else |
30e1eab5 AL |
210 | { |
211 | for (;I != J && Size < 1024; I++) | |
212 | { | |
03e39e59 | 213 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
214 | Size += strlen(Args[n-1]); |
215 | } | |
216 | } | |
03e39e59 | 217 | Args[n] = 0; |
30e1eab5 AL |
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 | } | |
03e39e59 | 227 | |
03e39e59 AL |
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); | |
6dd55be7 | 238 | |
03e39e59 | 239 | // Fork dpkg |
54676e1a | 240 | pid_t Child = ExecFork(); |
6dd55be7 | 241 | |
03e39e59 AL |
242 | // This is the child |
243 | if (Child == 0) | |
244 | { | |
cf544e14 | 245 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 246 | _exit(100); |
03e39e59 | 247 | |
03e39e59 AL |
248 | int Flags,dummy; |
249 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
0dbb95d8 | 250 | _exit(100); |
03e39e59 AL |
251 | |
252 | // Discard everything in stdin before forking dpkg | |
253 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
0dbb95d8 | 254 | _exit(100); |
03e39e59 AL |
255 | |
256 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
257 | ||
258 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
0dbb95d8 | 259 | _exit(100); |
03e39e59 AL |
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); | |
d568ed2d | 264 | execvp(Args[0],(char **)Args); |
03e39e59 | 265 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 266 | _exit(100); |
03e39e59 AL |
267 | } |
268 | ||
269 | // Wait for dpkg | |
270 | int Status = 0; | |
271 | while (waitpid(Child,&Status,0) != Child) | |
272 | { | |
273 | if (errno == EINTR) | |
274 | continue; | |
6dd55be7 | 275 | RunScripts("DPkg::Post-Invoke"); |
03e39e59 AL |
276 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
277 | } | |
03e39e59 AL |
278 | |
279 | // Restore sig int/quit | |
280 | signal(SIGQUIT,SIG_DFL); | |
281 | signal(SIGINT,SIG_DFL); | |
6dd55be7 AL |
282 | |
283 | // Check for an error code. | |
284 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
285 | { | |
286 | RunScripts("DPkg::Post-Invoke"); | |
f956efb4 AL |
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"); | |
6dd55be7 | 294 | } |
03e39e59 | 295 | } |
6dd55be7 AL |
296 | |
297 | if (RunScripts("DPkg::Post-Invoke") == false) | |
298 | return false; | |
03e39e59 AL |
299 | return true; |
300 | } | |
301 | /*}}}*/ |