2016-07-26 18:23:47 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "LSBUtils.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "base/process_util.h"
|
2018-07-13 06:13:04 +00:00
|
|
|
#include "mozilla/FileUtils.h"
|
2023-04-13 08:35:24 +00:00
|
|
|
#include "mozilla/Result.h"
|
|
|
|
#include "mozilla/ResultVariant.h"
|
|
|
|
#include "mozilla/ipc/LaunchError.h"
|
2016-07-26 18:23:47 +00:00
|
|
|
|
2020-01-18 13:48:34 +00:00
|
|
|
namespace mozilla::widget::lsb {
|
2016-07-26 18:23:47 +00:00
|
|
|
|
|
|
|
static const char* gLsbReleasePath = "/usr/bin/lsb_release";
|
|
|
|
|
|
|
|
bool GetLSBRelease(nsACString& aDistributor, nsACString& aDescription,
|
|
|
|
nsACString& aRelease, nsACString& aCodename) {
|
|
|
|
if (access(gLsbReleasePath, R_OK) != 0) return false;
|
|
|
|
|
|
|
|
int pipefd[2];
|
|
|
|
if (pipe(pipefd) == -1) {
|
|
|
|
NS_WARNING("pipe() failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> argv = {gLsbReleasePath, "-idrc"};
|
|
|
|
|
2017-09-09 02:35:06 +00:00
|
|
|
base::LaunchOptions options;
|
|
|
|
options.fds_to_remap.push_back({pipefd[1], STDOUT_FILENO});
|
|
|
|
options.wait = true;
|
2016-07-26 18:23:47 +00:00
|
|
|
|
|
|
|
base::ProcessHandle process;
|
2023-04-13 08:35:24 +00:00
|
|
|
Result<Ok, ipc::LaunchError> err = base::LaunchApp(argv, options, &process);
|
2016-07-26 18:23:47 +00:00
|
|
|
close(pipefd[1]);
|
2023-04-13 08:35:24 +00:00
|
|
|
if (err.isErr()) {
|
2016-07-26 18:23:47 +00:00
|
|
|
NS_WARNING("Failed to spawn lsb_release!");
|
|
|
|
close(pipefd[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-13 06:13:04 +00:00
|
|
|
ScopedCloseFile stream(fdopen(pipefd[0], "r"));
|
2016-07-26 18:23:47 +00:00
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Could not wrap fd!");
|
|
|
|
close(pipefd[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char dist[256], desc[256], release[256], codename[256];
|
|
|
|
if (fscanf(stream,
|
|
|
|
"Distributor ID:\t%255[^\n]\n"
|
|
|
|
"Description:\t%255[^\n]\n"
|
|
|
|
"Release:\t%255[^\n]\n"
|
|
|
|
"Codename:\t%255[^\n]\n",
|
|
|
|
dist, desc, release, codename) != 4) {
|
|
|
|
NS_WARNING("Failed to parse lsb_release!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDistributor.Assign(dist);
|
|
|
|
aDescription.Assign(desc);
|
|
|
|
aRelease.Assign(release);
|
|
|
|
aCodename.Assign(codename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:48:34 +00:00
|
|
|
} // namespace mozilla::widget::lsb
|