widget/LSBUtils.cpp
author Ryan VanderMeulen <ryanvm@gmail.com>
Mon, 21 Aug 2017 15:32:11 -0400
changeset 650678 86e3368b6058c3a298a6fd973de1313cfe706795
parent 396684 de07c5af9cc8975d82cbb5dde8cd2a64dc3d8b42
child 489668 cbb8fdf1daf98a15f7d57f6b08d273bdf96aa1a0
child 648192 c236dfe7dc98c387219d7cb45d0693560db708fd
child 691858 e4d89f103f98a2ae2302672e0739faefab60ae75
child 694109 98e2b4ee9775a2a2b5cc1bb8a390bfe901814f38
permissions -rw-r--r--
Bug 1389160 - Add missing FileUtils.h #include to fix bustage. r=rhelmer, a=bustage

/* -*- 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"

namespace mozilla {
namespace widget {
namespace lsb {

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"
  };

  std::vector<std::pair<int, int>> fdMap = {
    { pipefd[1], STDOUT_FILENO }
  };

  base::ProcessHandle process;
  base::LaunchApp(argv, fdMap, true, &process);
  close(pipefd[1]);
  if (!process) {
    NS_WARNING("Failed to spawn lsb_release!");
    close(pipefd[0]);
    return false;
  }

  FILE* stream = fdopen(pipefd[0], "r");
  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!");
    fclose(stream);
    close(pipefd[0]);
    return false;
  }
  fclose(stream);
  close(pipefd[0]);

  aDistributor.Assign(dist);
  aDescription.Assign(desc);
  aRelease.Assign(release);
  aCodename.Assign(codename);
  return true;
}

}  // namespace lsb
}  // namespace widget
}  // namespace mozilla