# HG changeset patch # User lbatlle@npdl268.bpo.hp.com # Date 1174656856 -3600 # Node ID 9acd8ae3190c49419503af0354f370423ce8a699 # Parent 03339adb7014b420052efaac437406d7b2bffa1b First usable version! diff -r 03339adb7014 -r 9acd8ae3190c Makefile --- a/Makefile Fri Mar 23 08:37:45 2007 +0100 +++ b/Makefile Fri Mar 23 14:34:16 2007 +0100 @@ -1,3 +1,3 @@ -CFLAGS=-g +CFLAGS=-g -O0 test: main.o server.o server_start.o client.o msgdump.o jobs.o execute.o gcc -o test $^ diff -r 03339adb7014 -r 9acd8ae3190c client.c --- a/client.c Fri Mar 23 08:37:45 2007 +0100 +++ b/client.c Fri Mar 23 14:34:16 2007 +0100 @@ -1,8 +1,10 @@ #include +#include #include "msg.h" #include "main.h" static char *client_command = 0; +static void c_end_of_job(); void c_new_job(const char *command) { @@ -16,9 +18,12 @@ strcpy(m.u.command, command); - res = write(server_socket, &m, sizeof(m)); + res = send(server_socket, &m, sizeof(m), 0); if(res == -1) - perror("write"); + { + perror("c: send"); + exit(-1); + } } void c_wait_server_commands() @@ -28,12 +33,20 @@ while (1) { - res = read(server_socket, &m, sizeof(m)); + res = recv(server_socket, &m, sizeof(m), 0); if(res == -1) + { perror("read"); + exit(-1); + } if (res == 0) break; + if (res != sizeof(m)) + { + fprintf(stderr, "c: recv() message size wrong: %i instead of %i\n", + res, (int) sizeof(m)); + } assert(res == sizeof(m)); msgdump(&m); if (m.type == NEWJOB_OK) @@ -41,6 +54,7 @@ if (m.type == RUNJOB) { run_job(client_command); + c_end_of_job(); break; } } @@ -54,7 +68,7 @@ while (1) { - res = read(server_socket, &m, sizeof(m)); + res = recv(server_socket, &m, sizeof(m),0); if(res == -1) perror("read"); @@ -76,21 +90,21 @@ m.type = LIST; - res = write(server_socket, &m, sizeof(m)); + res = send(server_socket, &m, sizeof(m), 0); if(res == -1) - perror("write"); + perror("send"); } -void c_end_of_job() +static void c_end_of_job() { struct msg m; int res; m.type = ENDJOB; - res = write(server_socket, &m, sizeof(m)); + res = send(server_socket, &m, sizeof(m),0); if(res == -1) - perror("write"); + perror("send"); } int c_shutdown_server() @@ -99,6 +113,6 @@ int res; m.type = KILL; - res = write(server_socket, &m, sizeof(m)); + res = send(server_socket, &m, sizeof(m), 0); assert(res != -1); } diff -r 03339adb7014 -r 9acd8ae3190c execute.c --- a/execute.c Fri Mar 23 08:37:45 2007 +0100 +++ b/execute.c Fri Mar 23 14:34:16 2007 +0100 @@ -1,18 +1,28 @@ #include #include #include +#include +#include "msg.h" #include "main.h" +static void program_signal(); + static void run_parent() { int status; wait(&status); - printf("End of child\n"); }; static void run_child(const char *command) { + int p[2]; + /* Closing input */ + pipe(&p); + close(p[1]); /* closing the write handle */ + close(0); + + dup(p[0]); /* the pipe reading goes to stdin */ execlp("bash", "bash", "-c", command, NULL); } @@ -22,9 +32,13 @@ pid = fork(); + /* For the parent */ + /*program_signal(); Still not needed*/ + switch(pid) { case 0: + close(server_socket); run_child(command); break; case -1: @@ -36,3 +50,20 @@ break; } } + +static void sigchld_handler(int val) +{ +} + +static void program_signal() +{ + struct sigaction act; + + act.sa_handler = sigchld_handler; + /* Reset the mask */ + memset(&act.sa_mask,0,sizeof(act.sa_mask)); + act.sa_flags = SA_NOCLDSTOP; + act.sa_restorer = NULL; + + sigaction(SIGCHLD, &act, NULL); +} diff -r 03339adb7014 -r 9acd8ae3190c jobs.c --- a/jobs.c Fri Mar 23 08:37:45 2007 +0100 +++ b/jobs.c Fri Mar 23 14:34:16 2007 +0100 @@ -97,7 +97,6 @@ struct Job *p; struct Job *newnext; - printf("Remove job %i\n", jobid); if (firstjob->jobid == jobid) { struct Job *newfirst; @@ -131,7 +130,10 @@ return -1; if (firstjob != 0) + { + state = WAITING; return firstjob->jobid; + } return -1; } @@ -142,7 +144,6 @@ assert(state == WAITING); assert(firstjob != 0); - fprintf(stderr, "s: Job %i finished.\n", firstjob->jobid); newfirst = firstjob->next; free(firstjob); diff -r 03339adb7014 -r 9acd8ae3190c server.c --- a/server.c Fri Mar 23 08:37:45 2007 +0100 +++ b/server.c Fri Mar 23 14:34:16 2007 +0100 @@ -109,8 +109,6 @@ 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)) @@ -145,7 +143,6 @@ if(client_cs[index].hasjob) { - printf("s: removing job [%i] %i\n", index, client_cs[index].jobid); s_removejob(client_cs[index].jobid); } @@ -167,16 +164,14 @@ s = client_cs[index].socket; /* Read the message */ - res = read(s, &m, sizeof(m)); + res = recv(s, &m, sizeof(m),0); if (res == -1) { - sleep(60); - fprintf(stderr, "Error reading from client %i, socket %i", + fprintf(stderr, "Error reading from client %i, socket %i\n", index, s); perror("client read"); exit(-1); } - assert(res != -1); if (res == 0) { close(s); @@ -202,6 +197,7 @@ if (m.type == LIST) { s_list(client_cs[index].socket); + /* We must actively close, meaning End of Lines */ close(client_cs[index].socket); remove_connection(index); } @@ -226,9 +222,9 @@ m.type = RUNJOB; - res = write(s, &m, sizeof(m)); + res = send(s, &m, sizeof(m), 0); if(res == -1) - perror("write"); + perror("send"); } static void s_newjob_ok(int index) @@ -244,7 +240,7 @@ m.type = NEWJOB_OK; m.u.jobid = client_cs[index].jobid; - res = write(s, &m, sizeof(m)); + res = send(s, &m, sizeof(m), 0); if(res == -1) - perror("write"); + perror("send"); } diff -r 03339adb7014 -r 9acd8ae3190c server_start.c --- a/server_start.c Fri Mar 23 08:37:45 2007 +0100 +++ b/server_start.c Fri Mar 23 14:34:16 2007 +0100 @@ -4,6 +4,7 @@ #include #include #include +#include #include "main.h" @@ -63,7 +64,10 @@ /* If error other than "No one listens on the other end"... */ if (!(errno == ENOENT || errno == ECONNREFUSED)) - return 0; + { + perror("c: cannot connect to the server"); + exit(-1); + } if (errno == ECONNREFUSED) unlink("/tmp/prova.socket"); @@ -75,9 +79,11 @@ /* The second time didn't work. Abort. */ if (res == -1) - return 0; + { + fprintf(stderr, "The server didn't come up.\n"); + exit(-1); + } - printf("Good connection 2\n"); /* Good connection on the second time */ return 1; }