blob: 752c43de15a0a6deb8b6b1522f87fa414d29234d [file] [log] [blame]
Mike Frysinger57538c02016-10-06 23:01:33 -04001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "debugd/src/swap_tool.h"
6
7#include <string>
8
9#include <base/strings/stringprintf.h>
10
11#include "debugd/src/process_with_output.h"
12
13namespace debugd {
14
15namespace {
16
17// This script holds the bulk of the real logic.
18const char kSwapHelperScript[] = "/usr/share/cros/init/swap.sh";
19
20std::string RunSwapHelper(const ProcessWithOutput::ArgList& arguments,
21 int* result) {
22 std::string stdout, stderr;
23 *result = ProcessWithOutput::RunProcessFromHelper(
24 kSwapHelperScript, arguments, nullptr, &stdout, &stderr);
25 return *result ? stderr : stdout;
26}
27
28} // namespace
29
Eric Carusoc93a15c2017-04-24 16:15:12 -070030std::string SwapTool::SwapEnable(uint32_t size, bool change_now) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040031 int result;
32 std::string output, buf;
33
34 buf = base::StringPrintf("%u", size);
35 output = RunSwapHelper({"enable", buf}, &result);
36 if (result != EXIT_SUCCESS)
37 return output;
38
39 if (change_now)
Eric Carusoc93a15c2017-04-24 16:15:12 -070040 output = SwapStartStop(true);
Mike Frysinger57538c02016-10-06 23:01:33 -040041
42 return output;
43}
44
Eric Carusoc93a15c2017-04-24 16:15:12 -070045std::string SwapTool::SwapDisable(bool change_now) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040046 int result;
47 std::string output;
48
49 output = RunSwapHelper({"disable", }, &result);
50 if (result != EXIT_SUCCESS)
51 return output;
52
53 if (change_now)
Eric Carusoc93a15c2017-04-24 16:15:12 -070054 output = SwapStartStop(false);
Mike Frysinger57538c02016-10-06 23:01:33 -040055
56 return output;
57}
58
Eric Carusoc93a15c2017-04-24 16:15:12 -070059std::string SwapTool::SwapStartStop(bool on) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040060 int result;
61 std::string output;
62
63 // We always turn off swap because the config might have changed.
64 // Also because the code doesn't like to turn on twice.
65 output = RunSwapHelper({"stop", }, &result);
66 if (result != EXIT_SUCCESS)
67 return output;
68
69 if (on)
70 output = RunSwapHelper({"start", }, &result);
71
72 return output;
73}
74
Eric Carusoc93a15c2017-04-24 16:15:12 -070075std::string SwapTool::SwapStatus() const {
Mike Frysinger57538c02016-10-06 23:01:33 -040076 int result;
77 return RunSwapHelper({"status", }, &result);
78}
79
Luigi Semenzatoe8b0cda2017-03-20 10:37:53 -070080
Eric Carusoc93a15c2017-04-24 16:15:12 -070081std::string SwapTool::SwapSetMargin(uint32_t margin) const {
Luigi Semenzatoe8b0cda2017-03-20 10:37:53 -070082 int result;
83 std::string output, buf;
84
85 buf = base::StringPrintf("%u", margin);
86 return RunSwapHelper({"set_margin", buf}, &result);
87}
88
Mike Frysinger57538c02016-10-06 23:01:33 -040089} // namespace debugd