]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
Needs Unpack fixes
[apt.git] / apt-pkg / deb / dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dpkgpm.cc,v 1.3 1998/11/23 07:03:11 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::Go - Run the sequence /*{{{*/
78 // ---------------------------------------------------------------------
79 /* This globs the operations and calls dpkg */
80 bool pkgDPkgPM::Go()
81 {
82 for (vector<Item>::iterator I = List.begin(); I != List.end();)
83 {
84 vector<Item>::iterator J = I;
85 for (; J != List.end() && J->Op == I->Op; J++);
86
87 // Generate the argument list
88 const char *Args[400];
89 if (J - I > 350)
90 J = I + 350;
91
92 unsigned int n = 0;
93 unsigned long Size = 0;
94 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
95 Size += strlen(Args[n-1]);
96
97 switch (I->Op)
98 {
99 case Item::Remove:
100 Args[n++] = "--force-depends";
101 Size += strlen(Args[n-1]);
102 Args[n++] = "--force-remove-essential";
103 Size += strlen(Args[n-1]);
104 Args[n++] = "--remove";
105 Size += strlen(Args[n-1]);
106 break;
107
108 case Item::Configure:
109 Args[n++] = "--configure";
110 Size += strlen(Args[n-1]);
111 break;
112
113 case Item::Install:
114 Args[n++] = "--unpack";
115 Size += strlen(Args[n-1]);
116 break;
117 }
118
119 // Write in the file or package names
120 if (I->Op == Item::Install)
121 {
122 for (;I != J && Size < 1024; I++)
123 {
124 Args[n++] = I->File.c_str();
125 Size += strlen(Args[n-1]);
126 }
127 }
128 else
129 {
130 for (;I != J && Size < 1024; I++)
131 {
132 Args[n++] = I->Pkg.Name();
133 Size += strlen(Args[n-1]);
134 }
135 }
136 Args[n] = 0;
137 J = I;
138
139 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
140 {
141 for (unsigned int k = 0; k != n; k++)
142 clog << Args[k] << ' ';
143 clog << endl;
144 continue;
145 }
146
147 cout << flush;
148 clog << flush;
149 cerr << flush;
150
151 /* Mask off sig int/quit. We do this because dpkg also does when
152 it forks scripts. What happens is that when you hit ctrl-c it sends
153 it to all processes in the group. Since dpkg ignores the signal
154 it doesn't die but we do! So we must also ignore it */
155 signal(SIGQUIT,SIG_IGN);
156 signal(SIGINT,SIG_IGN);
157
158 // Fork dpkg
159 pid_t Child = fork();
160 if (Child < 0)
161 return _error->Errno("fork","Could't fork");
162
163 // This is the child
164 if (Child == 0)
165 {
166 signal(SIGPIPE,SIG_DFL);
167 signal(SIGQUIT,SIG_DFL);
168 signal(SIGINT,SIG_DFL);
169 signal(SIGWINCH,SIG_DFL);
170 signal(SIGCONT,SIG_DFL);
171 signal(SIGTSTP,SIG_DFL);
172
173 if (chdir(_config->FindDir("Dir::Cache::Archives").c_str()) != 0)
174 exit(100);
175
176 // Close all of our FDs - just in case
177 for (int K = 3; K != 40; K++)
178 fcntl(K,F_SETFD,FD_CLOEXEC);
179
180 int Flags,dummy;
181 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
182 exit(100);
183
184 // Discard everything in stdin before forking dpkg
185 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
186 exit(100);
187
188 while (read(STDIN_FILENO,&dummy,1) == 1);
189
190 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
191 exit(100);
192
193 /* No Job Control Stop Env is a magic dpkg var that prevents it
194 from using sigstop */
195 setenv("DPKG_NO_TSTP","yes",1);
196 execvp("dpkg",(char **)Args);
197 cerr << "Could not exec dpkg!" << endl;
198 exit(100);
199 }
200
201 // Wait for dpkg
202 int Status = 0;
203 while (waitpid(Child,&Status,0) != Child)
204 {
205 if (errno == EINTR)
206 continue;
207 return _error->Errno("waitpid","Couldn't wait for subprocess");
208 }
209
210 // Check for an error code.
211 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
212 return _error->Error("Sub-process returned an error code");
213
214 // Restore sig int/quit
215 signal(SIGQUIT,SIG_DFL);
216 signal(SIGINT,SIG_DFL);
217 }
218 return true;
219 }
220 /*}}}*/