Close files, cleanup resourses

This commit is contained in:
topjohnwu 2017-05-02 04:55:55 +08:00
parent e16d604d0d
commit 6017ff2318
5 changed files with 17 additions and 12 deletions

View File

@ -62,6 +62,8 @@ static char *loopsetup(const char *img) {
return NULL; return NULL;
strcpy((char *) info.lo_file_name, img); strcpy((char *) info.lo_file_name, img);
ioctl(lfd, LOOP_SET_STATUS64, &info); ioctl(lfd, LOOP_SET_STATUS64, &info);
close(lfd);
close(ffd);
return strdup(device); return strdup(device);
} }
@ -241,6 +243,7 @@ static void clone_skeleton(struct node_entry *node, const char *real_path) {
insert_child(node, dummy); insert_child(node, dummy);
} }
} }
closedir(dir);
snprintf(buf, PATH_MAX, "/dev/magisk/dummy%s", real_path); snprintf(buf, PATH_MAX, "/dev/magisk/dummy%s", real_path);
xmkdir_p(buf, 0755); xmkdir_p(buf, 0755);

View File

@ -23,6 +23,7 @@
#include "magiskpolicy.h" #include "magiskpolicy.h"
pthread_t sepol_patch; pthread_t sepol_patch;
int null_fd;
static void *request_handler(void *args) { static void *request_handler(void *args) {
// Setup the default error handler for threads // Setup the default error handler for threads
@ -111,9 +112,10 @@ void start_daemon() {
xsetsid(); xsetsid();
setcon("u:r:su:s0"); setcon("u:r:su:s0");
umask(022); umask(022);
close(STDIN_FILENO); null_fd = xopen("/dev/null", O_RDWR);
close(STDOUT_FILENO); xdup2(null_fd, STDIN_FILENO);
close(STDERR_FILENO); xdup2(null_fd, STDOUT_FILENO);
xdup2(null_fd, STDERR_FILENO);
// Patch selinux with medium patch before we do anything // Patch selinux with medium patch before we do anything
load_policydb(SELINUX_POLICY); load_policydb(SELINUX_POLICY);

View File

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
#include "magisk.h" #include "magisk.h"
#include "utils.h" #include "utils.h"
@ -29,6 +30,7 @@ static void *logger_thread(void *args) {
while (fdgets(buffer, sizeof(buffer), fd)) { while (fdgets(buffer, sizeof(buffer), fd)) {
fprintf(logfile, "%s", buffer); fprintf(logfile, "%s", buffer);
} }
close(fd);
return NULL; return NULL;
} }

View File

@ -55,6 +55,7 @@ extern char *argv0; /* For changing process name */
extern char *applet[]; extern char *applet[];
extern int (*applet_main[]) (int, char *[]); extern int (*applet_main[]) (int, char *[]);
extern int null_fd;
// Multi-call entrypoints // Multi-call entrypoints
int magiskhide_main(int argc, char *argv[]); int magiskhide_main(int argc, char *argv[]);

View File

@ -45,17 +45,18 @@ unsigned get_radio_uid() {
} }
int check_data() { int check_data() {
int ret = 0;
char buffer[4096]; char buffer[4096];
FILE *fp = xfopen("/proc/mounts", "r"); FILE *fp = xfopen("/proc/mounts", "r");
while (fgets(buffer, sizeof(buffer), fp)) { while (fgets(buffer, sizeof(buffer), fp)) {
if (strstr(buffer, " /data ")) { if (strstr(buffer, " /data ")) {
if (strstr(buffer, "tmpfs")) if (strstr(buffer, "tmpfs") == NULL)
return 0; ret = 1;
else break;
return 1;
} }
} }
return 0; fclose(fp);
return ret;
} }
/* All the string should be freed manually!! */ /* All the string should be freed manually!! */
@ -238,10 +239,6 @@ int run_command(int *fd, const char *path, char *const argv[]) {
xdup2(sv[0], STDIN_FILENO); xdup2(sv[0], STDIN_FILENO);
xdup2(sv[0], STDOUT_FILENO); xdup2(sv[0], STDOUT_FILENO);
xdup2(sv[0], STDERR_FILENO); xdup2(sv[0], STDERR_FILENO);
} else {
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
} }
execv(path, argv); execv(path, argv);