diff -Naur udev-110.Orig/udevd.c udev-110.new/udevd.c --- udev-110.Orig/udevd.c 2007-05-01 08:33:39.000000000 -0400 +++ udev-110.new/udevd.c 2007-05-01 12:49:38.000000000 -0400 @@ -347,31 +347,21 @@ static int cpu_count(void) { - int f; - char buf[65536]; + FILE* f; + char buf[1024]; int len; const char *pos; int count = 0; - f = open("/proc/stat", O_RDONLY); - if (f == -1) + f = fopen("/proc/stat", "r"); + if (f == NULL) return -1; - len = read(f, buf, sizeof(buf)-1); - close(f); - if (len <= 0) - return -1; - buf[len] = '\0'; - - pos = strstr(buf, "cpu"); - if (pos == NULL) - return -1; - - while (pos != NULL) { - if (strncmp(pos, "cpu", 3) == 0 &&isdigit(pos[3])) + while (fgets(buf, 1024, f) != NULL) { + if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) count++; - pos = strstr(&pos[3], "cpu"); } + fclose(f); if (count == 0) return -1; @@ -380,30 +370,46 @@ static int running_processes(void) { - int f; - char buf[32768]; + FILE* f; + char buf[1024]; int len; - int running; + int running = -1; const char *pos; + int retries = 3; - f = open("/proc/stat", O_RDONLY); - if (f == -1) - return -1; - - len = read(f, buf, sizeof(buf)-1); - close(f); - if (len <= 0) - return -1; - buf[len] = '\0'; - - pos = strstr(buf, "procs_running "); - if (pos == NULL) - return -1; + /* + * The retries is probably overkill but the text display is + * changing rapidly.on large systems (>= 512p) and so, while + * I have not actually recorded an instance where the retry + * was used I would rather not have it fail. + * + * An alternative approach would be to note when we are on + * a fairly large system (8cpus and 16Gbytes say) at which + * point throttling is probably not really necessary. + */ + do { + f = fopen("/proc/stat", "r"); + if (f == NULL) + return -1; + + while (fgets(buf, 1024, f) != NULL) { + pos = strstr(buf, "procs_running "); + if (pos != NULL) { + int res; + res = sscanf(pos, "procs_running %u", + &running); + if (res == 1) { + fclose(f); + return running; + } + break; + } + } - if (sscanf(pos, "procs_running %u", &running) != 1) - return -1; + fclose(f); + } while (retries-- > 0); - return running; + return -1; } /* return the number of process es in our session, count only until limit */