--- 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 $^
--- 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 <assert.h>
+#include <stdio.h>
#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);
}
--- 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 <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <signal.h>
+#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);
+}
--- 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);
--- 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");
}
--- 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 <sys/un.h>
#include <errno.h>
#include <stdlib.h>
+#include <stdio.h>
#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;
}