Merge "adb: set O_CLOEXEC on lots of file descriptors"

This commit is contained in:
Nick Kralevich 2014-07-22 02:45:39 +00:00 committed by Gerrit Code Review
commit ec331b1e40
5 changed files with 16 additions and 16 deletions

View file

@ -57,7 +57,7 @@ static void read_keys(const char *file, struct listnode *list)
char *sep; char *sep;
int ret; int ret;
f = fopen(file, "r"); f = fopen(file, "re");
if (!f) { if (!f) {
D("Can't open '%s'\n", file); D("Can't open '%s'\n", file);
return; return;
@ -126,7 +126,7 @@ int adb_auth_generate_token(void *token, size_t token_size)
FILE *f; FILE *f;
int ret; int ret;
f = fopen("/dev/urandom", "r"); f = fopen("/dev/urandom", "re");
if (!f) if (!f)
return 0; return 0;
@ -257,6 +257,7 @@ void adb_auth_init(void)
D("Failed to get adbd socket\n"); D("Failed to get adbd socket\n");
return; return;
} }
fcntl(fd, F_SETFD, FD_CLOEXEC);
ret = listen(fd, 4); ret = listen(fd, 4);
if (ret < 0) { if (ret < 0) {

View file

@ -178,18 +178,18 @@ static int handle_send_file(int s, char *path, uid_t uid,
unsigned int timestamp = 0; unsigned int timestamp = 0;
int fd; int fd;
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode); fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
if(fd < 0 && errno == ENOENT) { if(fd < 0 && errno == ENOENT) {
if(mkdirs(path) != 0) { if(mkdirs(path) != 0) {
if(fail_errno(s)) if(fail_errno(s))
return -1; return -1;
fd = -1; fd = -1;
} else { } else {
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode); fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
} }
} }
if(fd < 0 && errno == EEXIST) { if(fd < 0 && errno == EEXIST) {
fd = adb_open_mode(path, O_WRONLY, mode); fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
} }
if(fd < 0) { if(fd < 0) {
if(fail_errno(s)) if(fail_errno(s))
@ -383,7 +383,7 @@ static int do_recv(int s, const char *path, char *buffer)
syncmsg msg; syncmsg msg;
int fd, r; int fd, r;
fd = adb_open(path, O_RDONLY); fd = adb_open(path, O_RDONLY | O_CLOEXEC);
if(fd < 0) { if(fd < 0) {
if(fail_errno(s)) return -1; if(fail_errno(s)) return -1;
return 0; return 0;

View file

@ -39,7 +39,7 @@ static char *find_mount(const char *dir)
const char delims[] = "\n"; const char delims[] = "\n";
char buf[4096]; char buf[4096];
fd = unix_open("/proc/mounts", O_RDONLY); fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
return NULL; return NULL;
@ -83,7 +83,7 @@ static int remount_system()
if (!dev) if (!dev)
return -1; return -1;
fd = unix_open(dev, O_RDONLY); fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
return -1; return -1;

View file

@ -190,7 +190,7 @@ static void init_subproc_child()
setsid(); setsid();
// Set OOM score adjustment to prevent killing // Set OOM score adjustment to prevent killing
int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY); int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
if (fd >= 0) { if (fd >= 0) {
adb_write(fd, "0", 1); adb_write(fd, "0", 1);
adb_close(fd); adb_close(fd);
@ -209,12 +209,11 @@ static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg
char *devname; char *devname;
int ptm; int ptm;
ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY); ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
if(ptm < 0){ if(ptm < 0){
printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno)); printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
return -1; return -1;
} }
fcntl(ptm, F_SETFD, FD_CLOEXEC);
if(grantpt(ptm) || unlockpt(ptm) || if(grantpt(ptm) || unlockpt(ptm) ||
((devname = (char*) ptsname(ptm)) == 0)){ ((devname = (char*) ptsname(ptm)) == 0)){
@ -233,7 +232,7 @@ static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg
if (*pid == 0) { if (*pid == 0) {
init_subproc_child(); init_subproc_child();
int pts = unix_open(devname, O_RDWR); int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
if (pts < 0) { if (pts < 0) {
fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname); fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
exit(-1); exit(-1);
@ -417,7 +416,7 @@ int service_to_fd(const char *name)
#endif #endif
#if !ADB_HOST #if !ADB_HOST
} else if(!strncmp("dev:", name, 4)) { } else if(!strncmp("dev:", name, 4)) {
ret = unix_open(name + 4, O_RDWR); ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
} else if(!strncmp(name, "framebuffer:", 12)) { } else if(!strncmp(name, "framebuffer:", 12)) {
ret = create_service_thread(framebuffer_service, 0); ret = create_service_thread(framebuffer_service, 0);
} else if (!strncmp(name, "jdwp:", 5)) { } else if (!strncmp(name, "jdwp:", 5)) {

View file

@ -170,7 +170,7 @@ static void find_usb_device(const char *base,
} }
// DBGX("[ scanning %s ]\n", devname); // DBGX("[ scanning %s ]\n", devname);
if((fd = unix_open(devname, O_RDONLY)) < 0) { if((fd = unix_open(devname, O_RDONLY | O_CLOEXEC)) < 0) {
continue; continue;
} }
@ -597,10 +597,10 @@ static void register_device(const char *dev_name, const char *devpath,
usb->mark = 1; usb->mark = 1;
usb->reaper_thread = 0; usb->reaper_thread = 0;
usb->desc = unix_open(usb->fname, O_RDWR); usb->desc = unix_open(usb->fname, O_RDWR | O_CLOEXEC);
if(usb->desc < 0) { if(usb->desc < 0) {
/* if we fail, see if have read-only access */ /* if we fail, see if have read-only access */
usb->desc = unix_open(usb->fname, O_RDONLY); usb->desc = unix_open(usb->fname, O_RDONLY | O_CLOEXEC);
if(usb->desc < 0) goto fail; if(usb->desc < 0) goto fail;
usb->writeable = 0; usb->writeable = 0;
D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc); D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);