/src/mozilla-central/widget/LSBUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
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/. */ |
6 | | |
7 | | #include "LSBUtils.h" |
8 | | |
9 | | #include <unistd.h> |
10 | | #include "base/process_util.h" |
11 | | #include "mozilla/FileUtils.h" |
12 | | |
13 | | namespace mozilla { |
14 | | namespace widget { |
15 | | namespace lsb { |
16 | | |
17 | | static const char* gLsbReleasePath = "/usr/bin/lsb_release"; |
18 | | |
19 | | bool |
20 | | GetLSBRelease(nsACString& aDistributor, |
21 | | nsACString& aDescription, |
22 | | nsACString& aRelease, |
23 | | nsACString& aCodename) |
24 | 0 | { |
25 | 0 | if (access(gLsbReleasePath, R_OK) != 0) |
26 | 0 | return false; |
27 | 0 | |
28 | 0 | int pipefd[2]; |
29 | 0 | if (pipe(pipefd) == -1) { |
30 | 0 | NS_WARNING("pipe() failed!"); |
31 | 0 | return false; |
32 | 0 | } |
33 | 0 |
|
34 | 0 | std::vector<std::string> argv = { |
35 | 0 | gLsbReleasePath, "-idrc" |
36 | 0 | }; |
37 | 0 |
|
38 | 0 | base::LaunchOptions options; |
39 | 0 | options.fds_to_remap.push_back({ pipefd[1], STDOUT_FILENO }); |
40 | 0 | options.wait = true; |
41 | 0 |
|
42 | 0 | base::ProcessHandle process; |
43 | 0 | bool ok = base::LaunchApp(argv, options, &process); |
44 | 0 | close(pipefd[1]); |
45 | 0 | if (!ok) { |
46 | 0 | NS_WARNING("Failed to spawn lsb_release!"); |
47 | 0 | close(pipefd[0]); |
48 | 0 | return false; |
49 | 0 | } |
50 | 0 |
|
51 | 0 | ScopedCloseFile stream(fdopen(pipefd[0], "r")); |
52 | 0 | if (!stream) { |
53 | 0 | NS_WARNING("Could not wrap fd!"); |
54 | 0 | close(pipefd[0]); |
55 | 0 | return false; |
56 | 0 | } |
57 | 0 |
|
58 | 0 | char dist[256], desc[256], release[256], codename[256]; |
59 | 0 | if (fscanf(stream, "Distributor ID:\t%255[^\n]\n" |
60 | 0 | "Description:\t%255[^\n]\n" |
61 | 0 | "Release:\t%255[^\n]\n" |
62 | 0 | "Codename:\t%255[^\n]\n", |
63 | 0 | dist, desc, release, codename) != 4) |
64 | 0 | { |
65 | 0 | NS_WARNING("Failed to parse lsb_release!"); |
66 | 0 | return false; |
67 | 0 | } |
68 | 0 |
|
69 | 0 | aDistributor.Assign(dist); |
70 | 0 | aDescription.Assign(desc); |
71 | 0 | aRelease.Assign(release); |
72 | 0 | aCodename.Assign(codename); |
73 | 0 | return true; |
74 | 0 | } |
75 | | |
76 | | } // namespace lsb |
77 | | } // namespace widget |
78 | | } // namespace mozilla |