#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)
FILE *fdh;
int fh;
int i, j;
+ int maxqueue, c;
// get job from QUEUE
fdh = fopen(filequeue, "r");
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;
exit(-1);
}
- // lock QUEUE
- fh = fileno(fdh);
- flock(fh, LOCK_EX);
-
for (j = 1; j < i; j++) {
fprintf(fdh, "%s", &buf[j*MAXBUF]);
}
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;
}