1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debsystem.cc,v 1.4 2004/01/26 17:01:53 mdz Exp $
4 /* ######################################################################
6 System - Abstraction for running on different systems.
8 Basic general structure..
10 ##################################################################### */
12 // Include Files /*{{{*/
15 #include <apt-pkg/debsystem.h>
16 #include <apt-pkg/debversion.h>
17 #include <apt-pkg/debindexfile.h>
18 #include <apt-pkg/dpkgpm.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/cacheiterators.h>
35 #include <sys/types.h>
47 class APT_HIDDEN debSystemPrivate
{
49 debSystemPrivate() : LockFD(-1), LockCount(0), StatusFile(0)
52 // For locking support
56 debStatusIndex
*StatusFile
;
59 // System::debSystem - Constructor /*{{{*/
60 // ---------------------------------------------------------------------
62 debSystem::debSystem() : pkgSystem("Debian dpkg interface", &debVS
), d(new debSystemPrivate())
66 // System::~debSystem - Destructor /*{{{*/
67 // ---------------------------------------------------------------------
69 debSystem::~debSystem()
75 // System::Lock - Get the lock /*{{{*/
76 // ---------------------------------------------------------------------
77 /* This mirrors the operations dpkg does when it starts up. Note the
78 checking of the updates directory. */
79 bool debSystem::Lock()
81 // Disable file locking
82 if (_config
->FindB("Debug::NoLocking",false) == true || d
->LockCount
> 1)
88 // Create the lockfile
89 string AdminDir
= flNotFile(_config
->Find("Dir::State::status"));
90 d
->LockFD
= GetLock(AdminDir
+ "lock");
93 if (errno
== EACCES
|| errno
== EAGAIN
)
94 return _error
->Error(_("Unable to lock the administration directory (%s), "
95 "is another process using it?"),AdminDir
.c_str());
97 return _error
->Error(_("Unable to lock the administration directory (%s), "
98 "are you root?"),AdminDir
.c_str());
101 // See if we need to abort with a dirty journal
102 if (CheckUpdates() == true)
107 if (getenv("SUDO_USER") != NULL
)
108 cmd
= "sudo dpkg --configure -a";
110 cmd
= "dpkg --configure -a";
111 // TRANSLATORS: the %s contains the recovery command, usually
112 // dpkg --configure -a
113 return _error
->Error(_("dpkg was interrupted, you must manually "
114 "run '%s' to correct the problem. "), cmd
);
122 // System::UnLock - Drop a lock /*{{{*/
123 // ---------------------------------------------------------------------
125 bool debSystem::UnLock(bool NoErrors
)
127 if (d
->LockCount
== 0 && NoErrors
== true)
130 if (d
->LockCount
< 1)
131 return _error
->Error(_("Not locked"));
132 if (--d
->LockCount
== 0)
141 // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
142 // ---------------------------------------------------------------------
143 /* This does a check of the updates directory (dpkg journal) to see if it has
144 any entries in it. */
145 bool debSystem::CheckUpdates()
147 // Check for updates.. (dirty)
148 string File
= flNotFile(_config
->Find("Dir::State::status")) + "updates/";
149 DIR *DirP
= opendir(File
.c_str());
153 /* We ignore any files that are not all digits, this skips .,.. and
154 some tmp files dpkg will leave behind.. */
155 bool Damaged
= false;
156 for (struct dirent
*Ent
= readdir(DirP
); Ent
!= 0; Ent
= readdir(DirP
))
159 for (unsigned int I
= 0; Ent
->d_name
[I
] != 0; I
++)
161 // Check if its not a digit..
162 if (isdigit(Ent
->d_name
[I
]) == 0)
176 // System::CreatePM - Create the underlying package manager /*{{{*/
177 // ---------------------------------------------------------------------
179 pkgPackageManager
*debSystem::CreatePM(pkgDepCache
*Cache
) const
181 return new pkgDPkgPM(Cache
);
184 // System::Initialize - Setup the configuration space.. /*{{{*/
185 // ---------------------------------------------------------------------
186 /* These are the Debian specific configuration variables.. */
187 bool debSystem::Initialize(Configuration
&Cnf
)
189 /* These really should be jammed into a generic 'Local Database' engine
190 which is yet to be determined. The functions in pkgcachegen should
191 be the only users of these */
192 Cnf
.CndSet("Dir::State::extended_states", "extended_states");
193 Cnf
.CndSet("Dir::State::status","/var/lib/dpkg/status");
194 Cnf
.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
197 delete d
->StatusFile
;
204 // System::ArchiveSupported - Is a file format supported /*{{{*/
205 // ---------------------------------------------------------------------
206 /* The standard name for a deb is 'deb'.. There are no separate versions
207 of .deb to worry about.. */
208 APT_PURE
bool debSystem::ArchiveSupported(const char *Type
)
210 if (strcmp(Type
,"deb") == 0)
215 // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
216 // ---------------------------------------------------------------------
217 /* We check some files that are sure tell signs of this being a Debian
219 signed debSystem::Score(Configuration
const &Cnf
)
222 if (FileExists(Cnf
.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
224 if (FileExists(Cnf
.FindFile("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
226 if (FileExists("/etc/debian_version") == true)
231 // System::AddStatusFiles - Register the status files /*{{{*/
232 // ---------------------------------------------------------------------
234 bool debSystem::AddStatusFiles(std::vector
<pkgIndexFile
*> &List
)
236 if (d
->StatusFile
== 0)
237 d
->StatusFile
= new debStatusIndex(_config
->FindFile("Dir::State::status"));
238 List
.push_back(d
->StatusFile
);
242 // System::FindIndex - Get an index file for status files /*{{{*/
243 // ---------------------------------------------------------------------
245 bool debSystem::FindIndex(pkgCache::PkgFileIterator File
,
246 pkgIndexFile
*&Found
) const
248 if (d
->StatusFile
== 0)
250 if (d
->StatusFile
->FindInCache(*File
.Cache()) == File
)
252 Found
= d
->StatusFile
;
260 std::string
debSystem::GetDpkgExecutable() /*{{{*/
262 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
263 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
264 size_t dpkgChrootLen
= dpkgChrootDir
.length();
265 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
267 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
269 Tmp
= Tmp
.substr(dpkgChrootLen
);
274 std::vector
<std::string
> debSystem::GetDpkgBaseCommand() /*{{{*/
276 // Generate the base argument list for dpkg
277 std::vector
<std::string
> Args
= { GetDpkgExecutable() };
278 // Stick in any custom dpkg options
279 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
283 for (; Opts
!= 0; Opts
= Opts
->Next
)
285 if (Opts
->Value
.empty() == true)
287 Args
.push_back(Opts
->Value
);
293 void debSystem::DpkgChrootDirectory() /*{{{*/
295 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
296 if (chrootDir
== "/")
298 std::cerr
<< "Chrooting into " << chrootDir
<< std::endl
;
299 if (chroot(chrootDir
.c_str()) != 0)
305 pid_t
debSystem::ExecDpkg(std::vector
<std::string
> const &sArgs
, int * const inputFd
, int * const outputFd
, bool const DiscardOutput
)/*{{{*/
307 std::vector
<const char *> Args(sArgs
.size(), NULL
);
308 std::transform(sArgs
.begin(), sArgs
.end(), Args
.begin(), [](std::string
const &s
) { return s
.c_str(); });
309 Args
.push_back(NULL
);
311 int external
[2] = {-1, -1};
312 if (inputFd
!= nullptr || outputFd
!= nullptr)
313 if (pipe(external
) != 0)
315 _error
->WarningE("dpkg", "Can't create IPC pipe for dpkg call");
319 pid_t
const dpkg
= ExecFork();
321 int const nullfd
= open("/dev/null", O_RDONLY
);
322 if (inputFd
== nullptr)
323 dup2(nullfd
, STDIN_FILENO
);
327 dup2(external
[0], STDIN_FILENO
);
329 if (outputFd
== nullptr)
330 dup2(nullfd
, STDOUT_FILENO
);
334 dup2(external
[1], STDOUT_FILENO
);
336 if (DiscardOutput
== true)
337 dup2(nullfd
, STDERR_FILENO
);
338 debSystem::DpkgChrootDirectory();
339 execvp(Args
[0], (char**) &Args
[0]);
340 _error
->WarningE("dpkg", "Can't execute dpkg!");
343 if (outputFd
!= nullptr)
346 *outputFd
= external
[0];
348 else if (inputFd
!= nullptr)
351 *inputFd
= external
[1];
356 bool debSystem::SupportsMultiArch() /*{{{*/
358 std::vector
<std::string
> Args
= GetDpkgBaseCommand();
359 Args
.push_back("--assert-multi-arch");
360 pid_t
const dpkgAssertMultiArch
= ExecDpkg(Args
, nullptr, nullptr, true);
361 if (dpkgAssertMultiArch
> 0)
364 while (waitpid(dpkgAssertMultiArch
, &Status
, 0) != dpkgAssertMultiArch
)
368 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
371 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
377 std::vector
<std::string
> debSystem::SupportedArchitectures() /*{{{*/
379 std::vector
<std::string
> archs
;
381 string
const arch
= _config
->Find("APT::Architecture");
382 if (arch
.empty() == false)
383 archs
.push_back(std::move(arch
));
386 std::vector
<std::string
> sArgs
= GetDpkgBaseCommand();
387 sArgs
.push_back("--print-foreign-architectures");
389 pid_t
const dpkgMultiArch
= ExecDpkg(sArgs
, nullptr, &outputFd
, true);
390 if (dpkgMultiArch
== -1)
393 FILE *dpkg
= fdopen(outputFd
, "r");
397 while (getline(&buf
, &bufsize
, dpkg
) != -1)
400 char* arch
= strtok_r(buf
, " ", &tok_saveptr
);
401 while (arch
!= NULL
) {
402 for (; isspace_ascii(*arch
) != 0; ++arch
);
403 if (arch
[0] != '\0') {
404 char const* archend
= arch
;
405 for (; isspace_ascii(*archend
) == 0 && *archend
!= '\0'; ++archend
);
406 string
a(arch
, (archend
- arch
));
407 if (std::find(archs
.begin(), archs
.end(), a
) == archs
.end())
410 arch
= strtok_r(NULL
, " ", &tok_saveptr
);
416 ExecWait(dpkgMultiArch
, "dpkg --print-foreign-architectures", true);