Permalink
Browse files

Prevent deadlock when using fork

When forking of a new process in bionic, it is critical that it
does not allocate any memory according to the comment in
java_lang_ProcessManager.c:
"Note: We cannot malloc() or free() after this point!
A no-longer-running thread may be holding on to the heap lock, and
an attempt to malloc() or free() would result in deadlock."
However, as fork is using standard lib calls when tracing it a bit,
they might allocate memory, and thus causing the deadlock.
This is a rewrite so that the function cpuacct_add, that fork calls,
will use system calls instead of standard lib calls.

Signed-off-by: christian bejram <christian.bejram@stericsson.com>

Change-Id: Iff22ea6b424ce9f9bf0ac8e9c76593f689e0cc86
  • Loading branch information...
1 parent e4a21c8 commit 177ba8cb42ed6d232e7c8bcad5e6ee21fc51a0e8 @rabinv rabinv committed with Jean-Baptiste Queru Apr 8, 2011
Showing with 16 additions and 8 deletions.
  1. +16 −8 libc/bionic/cpuacct.c
View
@@ -30,31 +30,39 @@
#include <errno.h>
#include <sys/stat.h>
#include "cpuacct.h"
+#include <fcntl.h>
int cpuacct_add(uid_t uid)
{
int count;
- FILE *fp;
+ int fd;
char buf[80];
+ ssize_t n;
+ int ret = 0;
count = snprintf(buf, sizeof(buf), "/acct/uid/%d/tasks", uid);
- fp = fopen(buf, "w+");
- if (!fp) {
+ fd = open(buf, O_RDWR | O_CREAT, 0666);
+ if (fd == -1) {
/* Note: sizeof("tasks") returns 6, which includes the NULL char */
buf[count - sizeof("tasks")] = 0;
if (mkdir(buf, 0775) < 0)
return -errno;
/* Note: sizeof("tasks") returns 6, which includes the NULL char */
buf[count - sizeof("tasks")] = '/';
- fp = fopen(buf, "w+");
+ fd = open(buf, O_RDWR | O_CREAT, 0666);
}
- if (!fp)
+ if (fd == -1)
return -errno;
- fprintf(fp, "0");
- if (fclose(fp))
+ n = TEMP_FAILURE_RETRY(write(fd, "0", 1));
+ if (n < 0)
+ ret = -errno;
+ else if (n == 0)
+ ret = -EIO;
+
+ if (TEMP_FAILURE_RETRY(close(fd)) == -1)
return -errno;
- return 0;
+ return ret;
}

0 comments on commit 177ba8c

Please sign in to comment.