OSDN Git Service

The range of the lock of the scheduler is corrected.
authorHideyuki Niwa <niwa.hideyuki@jp.fujitsu.com>
Wed, 26 Mar 2014 02:04:02 +0000 (11:04 +0900)
committerHideyuki Niwa <niwa.hideyuki@jp.fujitsu.com>
Wed, 26 Mar 2014 02:04:02 +0000 (11:04 +0900)
After the first job had been taken out, the unlock processing during the update of the job list was deleted.

lxcf/src/lxcf-sched.c

index f84a6fa..7a06801 100644 (file)
 #define        LQUEUE          "/var/tmp/lxcf/lqueue"
 
 #define        LXCF_LOG        "/var/log/lxcf/lxcf-messages"
-#define MAXQUEUE       100
 #define MAXBUF         4096
 
-char   buf[MAXBUF*MAXQUEUE];
+char   *buf;
+char   jobbuf[MAXBUF];
 char   cmdbuf[MAXBUF+5];
 
 void cmd_exec(char *cmd)
@@ -68,6 +68,7 @@ char* get_job_queue(char* filequeue)
        FILE *fdh;
        int  fh;
        int  i, j;
+       int  maxqueue, c;
 
        // get job from QUEUE   
        fdh = fopen(filequeue, "r");
@@ -80,9 +81,23 @@ char* get_job_queue(char* filequeue)
        fh = fileno(fdh);
        flock(fh, LOCK_EX);
 
+       // count jobfile lines
+       for (maxqueue = 0;;) {
+               if ((c = fgetc(fdh)) == '\n') {
+                       maxqueue++;
+               } else if (c == EOF) {
+                       break;
+               }
+       }
+       rewind(fdh);
+       
+       // The buffer where the job is put is allocated. 
+       buf = (char*)malloc(maxqueue*MAXBUF);
+       
        // get all QUEUE
-       for (i = 0; i < MAXQUEUE; i++) {
+       for (i = 0; i < maxqueue; i++) {
                if (fgets(&buf[i*MAXBUF], MAXBUF-1, fdh) == NULL) {
+                       buf[maxqueue*MAXBUF-1] = 0;
                        break;
                }
                buf[(i+1)*MAXBUF-1] = 0;
@@ -96,10 +111,6 @@ char* get_job_queue(char* filequeue)
                        exit(-1);
                }
                
-               // lock QUEUE
-               fh = fileno(fdh);
-               flock(fh, LOCK_EX);
-
                for (j = 1; j < i; j++) {
                        fprintf(fdh, "%s", &buf[j*MAXBUF]);
                }
@@ -108,14 +119,25 @@ char* get_job_queue(char* filequeue)
                flock(fh, LOCK_UN);
                fclose(fdh);
 
-               buf[strlen(buf)-1] = 0;
-               return buf;
+               // The first job is put in jobbuf. 
+               strncpy(jobbuf, buf, MAXBUF-1);
+               jobbuf[strlen(jobbuf)-1] = 0;
+               
+               // free buf
+               free(buf);
+               buf = NULL;
+               
+               return jobbuf;
        }
        
        // unlock QUEUE
        flock(fh, LOCK_UN);
        fclose(fdh);
 
+       // free buf
+       free(buf);
+
+       // Because the job is not found, NULL is returned. 
        return NULL;
 }