Bug 1852149 Part 1 - Add margin-rule pref r=firefox-style-system-reviewers,emilio
[gecko.git] / widget / LSBUtils.cpp
blob9ff52145131c4d04f3320218494a81c8298fbe26
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LSBUtils.h"
9 #include <unistd.h>
10 #include "base/process_util.h"
11 #include "mozilla/FileUtils.h"
12 #include "mozilla/Result.h"
13 #include "mozilla/ResultVariant.h"
14 #include "mozilla/ipc/LaunchError.h"
16 namespace mozilla::widget::lsb {
18 static const char* gLsbReleasePath = "/usr/bin/lsb_release";
20 bool GetLSBRelease(nsACString& aDistributor, nsACString& aDescription,
21 nsACString& aRelease, nsACString& aCodename) {
22 if (access(gLsbReleasePath, R_OK) != 0) return false;
24 int pipefd[2];
25 if (pipe(pipefd) == -1) {
26 NS_WARNING("pipe() failed!");
27 return false;
30 std::vector<std::string> argv = {gLsbReleasePath, "-idrc"};
32 base::LaunchOptions options;
33 options.fds_to_remap.push_back({pipefd[1], STDOUT_FILENO});
34 options.wait = true;
36 base::ProcessHandle process;
37 Result<Ok, ipc::LaunchError> err =
38 base::LaunchApp(argv, std::move(options), &process);
39 close(pipefd[1]);
40 if (err.isErr()) {
41 NS_WARNING("Failed to spawn lsb_release!");
42 close(pipefd[0]);
43 return false;
46 ScopedCloseFile stream(fdopen(pipefd[0], "r"));
47 if (!stream) {
48 NS_WARNING("Could not wrap fd!");
49 close(pipefd[0]);
50 return false;
53 char dist[256], desc[256], release[256], codename[256];
54 if (fscanf(stream,
55 "Distributor ID:\t%255[^\n]\n"
56 "Description:\t%255[^\n]\n"
57 "Release:\t%255[^\n]\n"
58 "Codename:\t%255[^\n]\n",
59 dist, desc, release, codename) != 4) {
60 NS_WARNING("Failed to parse lsb_release!");
61 return false;
64 aDistributor.Assign(dist);
65 aDescription.Assign(desc);
66 aRelease.Assign(release);
67 aCodename.Assign(codename);
68 return true;
71 } // namespace mozilla::widget::lsb