Bug 1234176 - Introduce and use the WriteSysFile() helper function. r=dhylands

This commit is contained in:
Gabriele Svelto 2015-12-29 11:49:50 +01:00
parent a9d408188f
commit 02c6df1d54
3 changed files with 54 additions and 40 deletions

View File

@ -708,27 +708,6 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
namespace {
/**
* RAII class to help us remember to close file descriptors.
*/
bool WriteToFile(const char *filename, const char *toWrite)
{
int fd = open(filename, O_WRONLY);
ScopedClose autoClose(fd);
if (fd < 0) {
HAL_LOG("Unable to open file %s.", filename);
return false;
}
if (write(fd, toWrite, strlen(toWrite)) < 0) {
HAL_LOG("Unable to write to file %s.", filename);
return false;
}
return true;
}
// We can write to screenEnabledFilename to enable/disable the screen, but when
// we read, we always get "mem"! So we have to keep track ourselves whether
// the screen is on or not.
@ -849,7 +828,7 @@ UpdateCpuSleepState()
sInternalLockCpuMonitor->AssertCurrentThreadOwns();
bool allowed = sCpuSleepAllowed && !sInternalLockCpuCount;
WriteToFile(allowed ? wakeUnlockFilename : wakeLockFilename, "gecko");
WriteSysFile(allowed ? wakeUnlockFilename : wakeLockFilename, "gecko");
}
static void
@ -1257,7 +1236,7 @@ public:
mRegexes(nullptr)
{
// Enable timestamps in kernel's printk
WriteToFile("/sys/module/printk/parameters/time", "Y");
WriteSysFile("/sys/module/printk/parameters/time", "Y");
}
NS_DECL_ISUPPORTS
@ -1559,16 +1538,16 @@ EnsureCpuCGroupExists(const nsACString &aGroup)
nsAutoCString pathPrefix(kDevCpuCtl + aGroup + kSlash);
nsAutoCString cpuSharesPath(pathPrefix + NS_LITERAL_CSTRING("cpu.shares"));
if (cpuShares && !WriteToFile(cpuSharesPath.get(),
nsPrintfCString("%d", cpuShares).get())) {
if (cpuShares && !WriteSysFile(cpuSharesPath.get(),
nsPrintfCString("%d", cpuShares).get())) {
HAL_LOG("Could not set the cpu share for group %s", cpuSharesPath.get());
return false;
}
nsAutoCString notifyOnMigratePath(pathPrefix
+ NS_LITERAL_CSTRING("cpu.notify_on_migrate"));
if (!WriteToFile(notifyOnMigratePath.get(),
nsPrintfCString("%d", cpuNotifyOnMigrate).get())) {
if (!WriteSysFile(notifyOnMigratePath.get(),
nsPrintfCString("%d", cpuNotifyOnMigrate).get())) {
HAL_LOG("Could not set the cpu migration notification flag for group %s",
notifyOnMigratePath.get());
return false;
@ -1616,8 +1595,8 @@ EnsureMemCGroupExists(const nsACString &aGroup)
nsAutoCString pathPrefix(kMemCtl + aGroup + kSlash);
nsAutoCString memSwappinessPath(pathPrefix + NS_LITERAL_CSTRING("memory.swappiness"));
if (!WriteToFile(memSwappinessPath.get(),
nsPrintfCString("%d", memSwappiness).get())) {
if (!WriteSysFile(memSwappinessPath.get(),
nsPrintfCString("%d", memSwappiness).get())) {
HAL_LOG("Could not set the memory.swappiness for group %s", memSwappinessPath.get());
return false;
}
@ -1809,9 +1788,9 @@ EnsureKernelLowMemKillerParamsSet()
adjParams.Cut(adjParams.Length() - 1, 1);
minfreeParams.Cut(minfreeParams.Length() - 1, 1);
if (!adjParams.IsEmpty() && !minfreeParams.IsEmpty()) {
WriteToFile("/sys/module/lowmemorykiller/parameters/adj", adjParams.get());
WriteToFile("/sys/module/lowmemorykiller/parameters/minfree",
minfreeParams.get());
WriteSysFile("/sys/module/lowmemorykiller/parameters/adj", adjParams.get());
WriteSysFile("/sys/module/lowmemorykiller/parameters/minfree",
minfreeParams.get());
}
// Set the low-memory-notification threshold.
@ -1821,7 +1800,7 @@ EnsureKernelLowMemKillerParamsSet()
&lowMemNotifyThresholdKB))) {
// notify_trigger is in pages.
WriteToFile("/sys/module/lowmemorykiller/parameters/notify_trigger",
WriteSysFile("/sys/module/lowmemorykiller/parameters/notify_trigger",
nsPrintfCString("%ld", lowMemNotifyThresholdKB * 1024 / page_size).get());
}
@ -1856,11 +1835,11 @@ SetProcessPriority(int aPid, ProcessPriority aPriority, uint32_t aLRU)
// We try the newer interface first, and fall back to the older interface
// on failure.
if (!WriteToFile(nsPrintfCString("/proc/%d/oom_score_adj", aPid).get(),
nsPrintfCString("%d", oomScoreAdj).get()))
if (!WriteSysFile(nsPrintfCString("/proc/%d/oom_score_adj", aPid).get(),
nsPrintfCString("%d", oomScoreAdj).get()))
{
WriteToFile(nsPrintfCString("/proc/%d/oom_adj", aPid).get(),
nsPrintfCString("%d", OomAdjOfOomScoreAdj(oomScoreAdj)).get());
WriteSysFile(nsPrintfCString("/proc/%d/oom_adj", aPid).get(),
nsPrintfCString("%d", OomAdjOfOomScoreAdj(oomScoreAdj)).get());
}
HAL_LOG("Assigning pid %d to cgroup %s", aPid, pc->CGroup().get());

View File

@ -188,6 +188,35 @@ mozilla::ReadSysFile(
#endif /* ReadSysFile_PRESENT */
#ifdef WriteSysFile_PRESENT
bool
mozilla::WriteSysFile(
const char* aFilename,
const char* aBuf)
{
size_t aBufSize = strlen(aBuf);
int fd = MOZ_TEMP_FAILURE_RETRY(open(aFilename, O_WRONLY));
if (fd < 0) {
return false;
}
ScopedClose autoClose(fd);
ssize_t bytesWritten;
size_t offset = 0;
do {
bytesWritten = MOZ_TEMP_FAILURE_RETRY(write(fd, aBuf + offset,
aBufSize - offset));
if (bytesWritten == -1) {
return false;
}
offset += bytesWritten;
} while (bytesWritten > 0 && offset < aBufSize);
MOZ_ASSERT(offset == aBufSize);
return true;
}
#endif /* WriteSysFile_PRESENT */
void
mozilla::ReadAheadLib(nsIFile* aFile)
{

View File

@ -171,15 +171,19 @@ void ReadAhead(filedesc_t aFd, const size_t aOffset = 0,
}))
#endif
/* Define ReadSysFile() only on GONK to avoid unnecessary lubxul bloat.
Also define it in debug builds, so that unit tests for it can be written
and run in non-GONK builds. */
/* Define ReadSysFile() and WriteSysFile() only on GONK to avoid unnecessary
* libxul bloat. Also define it in debug builds, so that unit tests for it can
* be written and run in non-GONK builds. */
#if (defined(MOZ_WIDGET_GONK) || defined(DEBUG)) && defined(XP_UNIX)
#ifndef ReadSysFile_PRESENT
#define ReadSysFile_PRESENT
#endif /* ReadSysFile_PRESENT */
#ifndef WriteSysFile_PRESENT
#define WriteSysFile_PRESENT
#endif /* WriteSysFile_PRESENT */
/**
* Read the contents of a file.
* This function is intended for reading a single-lined text files from
@ -207,6 +211,8 @@ bool ReadSysFile(const char* aFilename, int* aVal);
*/
bool ReadSysFile(const char* aFilename, bool* aVal);
bool WriteSysFile(const char* aFilename, const char* aBuf);
#endif /* (MOZ_WIDGET_GONK || DEBUG) && XP_UNIX */
} // namespace mozilla