Prevent file descriptor from unclosed

This commit is contained in:
topjohnwu 2018-11-23 21:15:44 -05:00
parent 762b678d24
commit ec4723096f
3 changed files with 4 additions and 3 deletions

View File

@ -51,11 +51,12 @@ static int parse_ppid(int pid) {
char path[32]; char path[32];
int ppid; int ppid;
sprintf(path, "/proc/%d/stat", pid); sprintf(path, "/proc/%d/stat", pid);
FILE *stat = fopen(path, "r"); FILE *stat = xfopen(path, "re");
if (stat == nullptr) if (stat == nullptr)
return -1; return -1;
/* PID COMM STATE PPID ..... */ /* PID COMM STATE PPID ..... */
fscanf(stat, "%*d %*s %*c %d", &ppid); fscanf(stat, "%*d %*s %*c %d", &ppid);
fclose(stat);
return ppid; return ppid;
} }

View File

@ -211,7 +211,7 @@ int deleteprop(const char *name, bool persist) {
int load_prop_file(const char *filename, const bool trigger) { int load_prop_file(const char *filename, const bool trigger) {
if (init_resetprop()) return -1; if (init_resetprop()) return -1;
LOGD("resetprop: Load prop file [%s]\n", filename); LOGD("resetprop: Load prop file [%s]\n", filename);
FILE *fp = fopen(filename, "r"); FILE *fp = xfopen(filename, "re");
if (fp == nullptr) { if (fp == nullptr) {
LOGE("Cannot open [%s]\n", filename); LOGE("Cannot open [%s]\n", filename);
return 1; return 1;

View File

@ -395,7 +395,7 @@ int file_to_vector(const char *filename, Vector<CharArray> &arr) {
size_t len = 0; size_t len = 0;
ssize_t read; ssize_t read;
FILE *fp = xfopen(filename, "r"); FILE *fp = xfopen(filename, "re");
if (fp == nullptr) if (fp == nullptr)
return 1; return 1;