LSBUtils.cpp 1.9 KB

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