Line data Source code
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 :
12 : namespace mozilla {
13 : namespace widget {
14 : namespace lsb {
15 :
16 : static const char* gLsbReleasePath = "/usr/bin/lsb_release";
17 :
18 : bool
19 1 : GetLSBRelease(nsACString& aDistributor,
20 : nsACString& aDescription,
21 : nsACString& aRelease,
22 : nsACString& aCodename)
23 : {
24 1 : if (access(gLsbReleasePath, R_OK) != 0)
25 0 : return false;
26 :
27 : int pipefd[2];
28 1 : if (pipe(pipefd) == -1) {
29 0 : NS_WARNING("pipe() failed!");
30 0 : return false;
31 : }
32 :
33 : std::vector<std::string> argv = {
34 : gLsbReleasePath, "-idrc"
35 2 : };
36 :
37 : std::vector<std::pair<int, int>> fdMap = {
38 : { pipefd[1], STDOUT_FILENO }
39 2 : };
40 :
41 : base::ProcessHandle process;
42 1 : base::LaunchApp(argv, fdMap, true, &process);
43 1 : close(pipefd[1]);
44 1 : if (!process) {
45 0 : NS_WARNING("Failed to spawn lsb_release!");
46 0 : close(pipefd[0]);
47 0 : return false;
48 : }
49 :
50 1 : FILE* stream = fdopen(pipefd[0], "r");
51 1 : if (!stream) {
52 0 : NS_WARNING("Could not wrap fd!");
53 0 : close(pipefd[0]);
54 0 : return false;
55 : }
56 :
57 : char dist[256], desc[256], release[256], codename[256];
58 1 : if (fscanf(stream, "Distributor ID:\t%255[^\n]\n"
59 : "Description:\t%255[^\n]\n"
60 : "Release:\t%255[^\n]\n"
61 : "Codename:\t%255[^\n]\n",
62 : dist, desc, release, codename) != 4)
63 : {
64 0 : NS_WARNING("Failed to parse lsb_release!");
65 0 : fclose(stream);
66 0 : close(pipefd[0]);
67 0 : return false;
68 : }
69 1 : fclose(stream);
70 1 : close(pipefd[0]);
71 :
72 1 : aDistributor.Assign(dist);
73 1 : aDescription.Assign(desc);
74 1 : aRelease.Assign(release);
75 1 : aCodename.Assign(codename);
76 1 : return true;
77 : }
78 :
79 : } // namespace lsb
80 : } // namespace widget
81 : } // namespace mozilla
|