Some more code for execution.
authorviric@llimona
Fri, 23 Mar 2007 08:37:45 +0100
changeset 8 03339adb7014
parent 7 bbfd7918b5fc
child 9 9acd8ae3190c
Some more code for execution.
Makefile
README
buglist.bug
client.c
execute.c
jobs.c
main.c
main.h
server.c
setenv
--- a/Makefile	Thu Mar 22 19:59:41 2007 +0100
+++ b/Makefile	Fri Mar 23 08:37:45 2007 +0100
@@ -1,3 +1,3 @@
 CFLAGS=-g
-test: main.o server.o server_start.o client.o msgdump.o jobs.o
+test: main.o server.o server_start.o client.o msgdump.o jobs.o execute.o
 	gcc -o test $^
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README	Fri Mar 23 08:37:45 2007 +0100
@@ -0,0 +1,4 @@
+Developers
+------------------------
+Run ". setenv" before adding bugs to the database.
+Use 'bug' for the database.  http://freshmeat.net/cprojects/bug/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buglist.bug	Fri Mar 23 08:37:45 2007 +0100
@@ -0,0 +1,1 @@
+0
--- a/client.c	Thu Mar 22 19:59:41 2007 +0100
+++ b/client.c	Fri Mar 23 08:37:45 2007 +0100
@@ -2,6 +2,8 @@
 #include "msg.h"
 #include "main.h"
 
+static char *client_command = 0;
+
 void c_new_job(const char *command)
 {
     struct msg m;
@@ -9,6 +11,9 @@
 
     m.type = NEWJOB;
 
+    /* global */
+    client_command = command;
+
     strcpy(m.u.command, command);
 
     res = write(server_socket, &m, sizeof(m));
@@ -33,11 +38,13 @@
         msgdump(&m);
         if (m.type == NEWJOB_OK)
             ;
-        if (m.type == LIST_LINE)
+        if (m.type == RUNJOB)
         {
-            printf("%s", m.u.line);
+            run_job(client_command);
+            break;
         }
     }
+    close(server_socket);
 }
 
 void c_wait_server_lines()
@@ -62,7 +69,7 @@
     }
 }
 
-void c_list_jobs(const char *command)
+void c_list_jobs()
 {
     struct msg m;
     int res;
@@ -74,6 +81,18 @@
         perror("write");
 }
 
+void c_end_of_job()
+{
+    struct msg m;
+    int res;
+
+    m.type = ENDJOB;
+
+    res = write(server_socket, &m, sizeof(m));
+    if(res == -1)
+        perror("write");
+}
+
 int c_shutdown_server()
 {
     struct msg m;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/execute.c	Fri Mar 23 08:37:45 2007 +0100
@@ -0,0 +1,38 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "main.h"
+
+static void run_parent()
+{
+    int status;
+    wait(&status);
+    printf("End of child\n");
+};
+
+static void run_child(const char *command)
+{
+    execlp("bash", "bash", "-c", command, NULL);
+}
+
+void run_job(const char *command)
+{
+    int pid;
+
+    pid = fork();
+
+    switch(pid)
+    {
+        case 0:
+            run_child(command);
+            break;
+        case -1:
+            perror("Error in fork");
+            exit(-1);
+            ;
+        default:
+            run_parent();
+            break;
+    }
+}
--- a/jobs.c	Thu Mar 22 19:59:41 2007 +0100
+++ b/jobs.c	Fri Mar 23 08:37:45 2007 +0100
@@ -1,6 +1,14 @@
+#include <stdlib.h>
+#include <stdio.h>
 #include <assert.h>
 #include "msg.h"
 
+static enum
+{
+    FREE,
+    WAITING
+} state = FREE;
+
 struct Job
 {
     int jobid;
@@ -115,3 +123,30 @@
     free(p->next);
     p->next = newnext;
 }
+
+/* -1 if no one should be run. */
+int next_run_job()
+{
+    if (state == WAITING)
+        return -1;
+
+    if (firstjob != 0)
+        return firstjob->jobid;
+
+    return -1;
+}
+
+void job_finished()
+{
+    struct Job *newfirst;
+
+    assert(state == WAITING);
+    assert(firstjob != 0);
+    fprintf(stderr, "s: Job %i finished.\n", firstjob->jobid);
+
+    newfirst = firstjob->next;
+    free(firstjob);
+    firstjob = newfirst;
+
+    state = FREE;
+}
--- a/main.c	Thu Mar 22 19:59:41 2007 +0100
+++ b/main.c	Fri Mar 23 08:37:45 2007 +0100
@@ -61,7 +61,7 @@
 
     if (list_jobs != 0)
     {
-        c_list_jobs(new_command);
+        c_list_jobs();
         c_wait_server_lines();
     }
     
--- a/main.h	Thu Mar 22 19:59:41 2007 +0100
+++ b/main.h	Fri Mar 23 08:37:45 2007 +0100
@@ -5,7 +5,7 @@
 /* client.c */
 void c_new_job(const char *command);
 void c_wait_server_commands();
-void c_list_jobs(const char *command);
+void c_list_jobs();
 int c_shutdown_server();
 void c_wait_server_lines();
 
@@ -13,6 +13,8 @@
 void s_list(int s);
 int s_newjob(struct msg *m);
 void s_removejob(int jobid);
+void job_finished();
+int next_run_job();
 
 /* msgdump.c */
 void msgdump(const struct msg *m);
@@ -25,3 +27,6 @@
 void wait_server_up();
 void fork_server();
 int ensure_server_up();
+
+/* execute.c */
+void run_job(const char *command);
--- a/server.c	Thu Mar 22 19:59:41 2007 +0100
+++ b/server.c	Fri Mar 23 08:37:45 2007 +0100
@@ -10,6 +10,7 @@
 #include "msg.h"
 #include "main.h"
 
+
 const char path[] = "/tmp/prova.socket";
 
 enum
@@ -28,7 +29,8 @@
 static enum Break
     client_read(int index);
 static void end_server(int ls);
-static s_newjob_ok(int index);
+static void s_newjob_ok(int index);
+static void s_runjob(int index);
 
 struct Client_conn
 {
@@ -72,12 +74,22 @@
     server_loop(ls);
 }
 
+static int get_conn_of_jobid(int jobid)
+{
+    int i;
+    for(i=0; i< nconnections; ++i)
+        if (client_cs[i].hasjob && client_cs[i].jobid == jobid)
+            return i;
+    return -1;
+}
+
 static void server_loop(int ls)
 {
     fd_set readset;
     int i;
     int maxfd;
     int keep_loop = 1;
+    int newjob;
 
     while (keep_loop)
     {
@@ -97,6 +109,8 @@
             cs = accept(ls, NULL, NULL);
             assert(cs != -1);
             client_cs[nconnections++].socket = cs;
+            fprintf(stderr, "New connection, socket %i at pos %i.\n", cs,
+                    nconnections-1);
         }
         for(i=0; i< nconnections; ++i)
             if (FD_ISSET(client_cs[i].socket, &readset))
@@ -107,6 +121,13 @@
                 if (b == BREAK)
                     keep_loop = 0;
             }
+        newjob = next_run_job();
+        if (newjob != -1)
+        {
+            int conn;
+            conn = get_conn_of_jobid(newjob);
+            s_runjob(conn);
+        }
     }
 
     end_server(ls);
@@ -147,6 +168,14 @@
 
     /* Read the message */
     res = read(s, &m, sizeof(m));
+    if (res == -1)
+    {
+        sleep(60);
+        fprintf(stderr, "Error reading from client %i, socket %i",
+                index, s);
+        perror("client read");
+        exit(-1);
+    }
     assert(res != -1);
     if (res == 0)
     {
@@ -177,10 +206,32 @@
         remove_connection(index);
     }
 
+    if (m.type == ENDJOB)
+    {
+        job_finished();
+    }
+
     return NOBREAK; /* normal */
 }
 
-static s_newjob_ok(int index)
+static void s_runjob(int index)
+{
+    int s;
+    struct msg m;
+    int res;
+    
+    assert(client_cs[index].hasjob);
+
+    s = client_cs[index].socket;
+
+    m.type = RUNJOB;
+
+    res = write(s, &m, sizeof(m));
+    if(res == -1)
+        perror("write");
+}
+
+static void s_newjob_ok(int index)
 {
     int s;
     struct msg m;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setenv	Fri Mar 23 08:37:45 2007 +0100
@@ -0,0 +1,1 @@
+export BUG_PROJECT=./buglist.bug