Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-drm-fsl-dcu.git] / arch / um / os-Linux / mem.c
index c6432e7292411de1ea2006aabf97a13c3a7fa4eb..f1ea169db85e174cf2d6998043bce9d655eef0cd 100644 (file)
 
 #include <sys/param.h>
 
+/* Modified by which_tmpdir, which is called during early boot */
 static char *default_tmpdir = "/tmp";
+
+/*
+ *  Modified when creating the physical memory file and when checking
+ * the tmp filesystem for usability, both happening during early boot.
+ */
 static char *tempdir = NULL;
 
 static void __init find_tempdir(void)
@@ -29,7 +35,8 @@ static void __init find_tempdir(void)
        int i;
        char *dir = NULL;
 
-       if(tempdir != NULL) return;     /* We've already been called */
+       if(tempdir != NULL) /* We've already been called */
+               return;
        for(i = 0; dirs[i]; i++){
                dir = getenv(dirs[i]);
                if((dir != NULL) && (*dir != '\0'))
@@ -55,7 +62,7 @@ static void __init find_tempdir(void)
  */
 static int next(int fd, char *buf, int size, char c)
 {
-       int n;
+       int n, len;
        char *ptr;
 
        while((ptr = strchr(buf, c)) == NULL){
@@ -69,10 +76,21 @@ static int next(int fd, char *buf, int size, char c)
        }
 
        ptr++;
-       memmove(buf, ptr, strlen(ptr) + 1);
+       len = strlen(ptr);
+       memmove(buf, ptr, len + 1);
+
+       /* Refill the buffer so that if there's a partial string that we care
+        * about, it will be completed, and we can recognize it.
+        */
+       n = read(fd, &buf[len], size - len - 1);
+       if(n < 0)
+               return -errno;
+
+       buf[len + n] = '\0';
        return 1;
 }
 
+/* which_tmpdir is called only during early boot */
 static int checked_tmpdir = 0;
 
 /* Look for a tmpfs mounted at /dev/shm.  I couldn't find a cleaner
@@ -104,14 +122,14 @@ static void which_tmpdir(void)
        }
 
        while(1){
-               found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), ' ');
+               found = next(fd, buf, ARRAY_SIZE(buf), ' ');
                if(found != 1)
                        break;
 
                if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
                        goto found;
 
-               found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), '\n');
+               found = next(fd, buf, ARRAY_SIZE(buf), '\n');
                if(found != 1)
                        break;
        }
@@ -122,20 +140,24 @@ err:
        else if(found < 0)
                printf("read returned errno %d\n", -found);
 
+out:
+       close(fd);
+
        return;
 
 found:
-       found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), ' ');
+       found = next(fd, buf, ARRAY_SIZE(buf), ' ');
        if(found != 1)
                goto err;
 
        if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
                printf("not tmpfs\n");
-               return;
+               goto out;
        }
 
        printf("OK\n");
        default_tmpdir = "/dev/shm";
+       goto out;
 }
 
 /*
@@ -172,7 +194,7 @@ int make_tempfile(const char *template, char **out_tempname, int do_unlink)
        } else {
                free(tempname);
        }
-       return(fd);
+       return fd;
 out:
        free(tempname);
        return -1;
@@ -200,8 +222,11 @@ int create_tmp_file(unsigned long long len)
                exit(1);
        }
 
-        if (lseek64(fd, len, SEEK_SET) < 0) {
-               perror("os_seek_file");
+       /* Seek to len - 1 because writing a character there will
+        * increase the file size by one byte, to the desired length.
+        */
+       if (lseek64(fd, len - 1, SEEK_SET) < 0) {
+               perror("os_seek_file");
                exit(1);
        }
 
@@ -214,7 +239,7 @@ int create_tmp_file(unsigned long long len)
                exit(1);
        }
 
-       return(fd);
+       return fd;
 }
 
 int create_mem_file(unsigned long long len)
@@ -228,7 +253,7 @@ int create_mem_file(unsigned long long len)
                errno = -err;
                perror("exec_close");
        }
-       return(fd);
+       return fd;
 }