[launcherlib] Use fstream for file I/O

pull/1/head
Thomas Perl 11 years ago
parent b2fa57c0c4
commit 2af3c8ea95

@ -38,6 +38,8 @@
#include <stdexcept> #include <stdexcept>
#include <syslog.h> #include <syslog.h>
#include <fstream>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <grp.h> #include <grp.h>
@ -325,11 +327,10 @@ void Booster::renameProcess(int parentArgc, char** parentArgv,
} }
} }
#define BOOSTER_APP_PRIVILEGES_LIST "/usr/share/mapplauncherd/privileges" static bool isPrivileged(AppData *appData)
int isPrivileged(AppData *appData)
{ {
/* /*
Returns 1 if privileged, 0 if not privileged. Returns true if privileged, false if not privileged.
The privileges file has the following format: The privileges file has the following format:
/full/path/to/app,<permissions_list> /full/path/to/app,<permissions_list>
where the permissions_list is a string of characters where the permissions_list is a string of characters
@ -338,38 +339,35 @@ int isPrivileged(AppData *appData)
example: example:
/usr/bin/vcardconverter,p /usr/bin/vcardconverter,p
Currently, permission means both read+write permission. Currently, permission means both read+write permission.
Comment lines start with # and are ignored.
*/ */
ssize_t readVal = 0; const char *BOOSTER_APP_PRIVILEGES_LIST = "/usr/share/mapplauncherd/privileges";
size_t length = 0;
char *line = NULL;
FILE *stream = NULL;
stream = fopen(BOOSTER_APP_PRIVILEGES_LIST, "r"); std::ifstream infile(BOOSTER_APP_PRIVILEGES_LIST);
if (stream == NULL) if (infile) {
return 0; std::string line;
while (std::getline(infile, line)) {
if (line.find('#') == 0) {
// Comment line
continue;
}
size_t pos = line.find(',');
if (pos != std::string::npos) {
std::string filename = line.substr(0, pos);
std::string permissions = line.substr(pos+1);
while (readVal = getline(&line, &length, stream) != -1) { // TODO: Actually do something with "permissions"
if (strstr(line, appData->fileName().c_str()) != NULL) {
/* For now, we just check for the existence of any permissions. */ if (filename == appData->fileName()) {
char *statePtr = NULL; return true;
char *tok = strtok_r(line, ",", &statePtr);
if (tok != NULL) {
tok = strtok_r(NULL, ",", &statePtr);
if (tok != NULL) {
/* some permissions are defined for this application. */
free(line);
fclose(stream);
return 1;
} }
} }
} }
free(line);
line = NULL;
} }
fclose(stream); return false;
return 0;
} }
void Booster::setEnvironmentBeforeLaunch() void Booster::setEnvironmentBeforeLaunch()
@ -557,21 +555,15 @@ AppData* Booster::appData() const
void Booster::resetOomAdj() void Booster::resetOomAdj()
{ {
const char * PROC_OOM_ADJ_FILE = "/proc/self/oom_score_adj"; const char *PROC_OOM_ADJ_FILE = "/proc/self/oom_score_adj";
int fd = open(PROC_OOM_ADJ_FILE, O_WRONLY);
if (fd != -1)
{
if (write(fd, "0", sizeof(char)) == -1)
{
Logger::logError("Couldn't write to '%s': %s", PROC_OOM_ADJ_FILE,
strerror(errno));
}
close(fd); std::ofstream oom_adj(PROC_OOM_ADJ_FILE);
} if (oom_adj) {
else oom_adj << '0';
{ if (oom_adj.fail()) {
Logger::logError("Couldn't open '%s' for write: %s", PROC_OOM_ADJ_FILE, Logger::logError("Couldn't write to '%s'", PROC_OOM_ADJ_FILE);
strerror(errno)); }
} else {
Logger::logError("Couldn't open '%s' for writing", PROC_OOM_ADJ_FILE);
} }
} }

Loading…
Cancel
Save