author | viric@llimona |
Sun, 25 Mar 2007 04:55:18 +0200 | |
changeset 23 | 96fcebb68510 |
parent 22 | afdc8410633f |
child 28 | 107abb4ec98a |
permissions | -rw-r--r-- |
8 | 1 |
#include <unistd.h> |
2 |
#include <stdio.h> |
|
3 |
#include <stdlib.h> |
|
9 | 4 |
#include <signal.h> |
22 | 5 |
#include <assert.h> |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
6 |
#include <sys/types.h> |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
7 |
#include <sys/wait.h> |
8 | 8 |
|
9 | 9 |
#include "msg.h" |
8 | 10 |
#include "main.h" |
11 |
||
9 | 12 |
static void program_signal(); |
13 |
||
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
14 |
/* Returns errorlevel */ |
22 | 15 |
static int run_parent(int store_output, int fd_read_filename) |
8 | 16 |
{ |
17 |
int status; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
18 |
int errorlevel; |
22 | 19 |
char *ofname = 0; |
20 |
int namesize; |
|
21 |
int res; |
|
22 |
||
23 |
/* Read the filename */ |
|
24 |
/* This is linked with the write() in this same file, in run_child() */ |
|
25 |
if (store_output) { |
|
26 |
res = read(fd_read_filename, &namesize, sizeof(namesize)); |
|
27 |
if (res == -1) |
|
28 |
{ |
|
29 |
perror("read filename"); |
|
30 |
exit(-1); |
|
31 |
} |
|
32 |
assert(res == sizeof(namesize)); |
|
33 |
ofname = (char *) malloc(namesize); |
|
34 |
res = read(fd_read_filename, ofname, namesize); |
|
35 |
assert(res == namesize); |
|
36 |
} |
|
37 |
close(fd_read_filename); |
|
38 |
||
39 |
c_send_runjob_ok(store_output, ofname); |
|
40 |
free(ofname); |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
41 |
|
8 | 42 |
wait(&status); |
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
43 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
44 |
if (WIFEXITED(status)) |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
45 |
{ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
46 |
/* We force the proper cast */ |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
47 |
signed char tmp; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
48 |
tmp = WEXITSTATUS(status); |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
49 |
errorlevel = tmp; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
50 |
} else |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
51 |
return -1; |
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
52 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
53 |
return errorlevel; |
8 | 54 |
}; |
55 |
||
22 | 56 |
static void run_child(const char *command, int store_output, |
57 |
int fd_send_filename) |
|
8 | 58 |
{ |
9 | 59 |
int p[2]; |
22 | 60 |
char outfname[] = "/tmp/ts.XXXXXX"; |
61 |
int namesize; |
|
62 |
int outfd; |
|
63 |
||
64 |
if (store_output) |
|
65 |
{ |
|
66 |
int res; |
|
67 |
||
68 |
close(1); /* stdout */ |
|
69 |
close(2); /* stderr */ |
|
70 |
/* Prepare the filename */ |
|
71 |
outfd = mkstemp(outfname); /* stdout */ |
|
72 |
dup(outfd); /* stderr */ |
|
73 |
||
74 |
/* Send the filename */ |
|
75 |
namesize = sizeof(outfname); |
|
76 |
res = write(fd_send_filename, (char *)&namesize, sizeof(namesize)); |
|
77 |
write(fd_send_filename, outfname, sizeof(outfname)); |
|
78 |
} |
|
79 |
close(fd_send_filename); |
|
80 |
||
9 | 81 |
/* Closing input */ |
18 | 82 |
pipe(p); |
9 | 83 |
close(p[1]); /* closing the write handle */ |
84 |
close(0); |
|
22 | 85 |
dup(p[0]); /* the pipe reading goes to stdin */ |
9 | 86 |
|
8 | 87 |
execlp("bash", "bash", "-c", command, NULL); |
88 |
} |
|
89 |
||
22 | 90 |
int run_job(const char *command, int store_output) |
8 | 91 |
{ |
92 |
int pid; |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
93 |
int errorlevel; |
22 | 94 |
int p[2]; |
8 | 95 |
|
96 |
||
9 | 97 |
/* For the parent */ |
98 |
/*program_signal(); Still not needed*/ |
|
99 |
||
22 | 100 |
/* Prepare the output filename sending */ |
101 |
pipe(p); |
|
102 |
||
103 |
pid = fork(); |
|
104 |
||
8 | 105 |
switch(pid) |
106 |
{ |
|
107 |
case 0: |
|
9 | 108 |
close(server_socket); |
22 | 109 |
close(p[0]); |
110 |
run_child(command, store_output, p[1]); |
|
8 | 111 |
break; |
112 |
case -1: |
|
113 |
perror("Error in fork"); |
|
114 |
exit(-1); |
|
115 |
; |
|
116 |
default: |
|
22 | 117 |
close(p[1]); |
118 |
errorlevel = run_parent(store_output, p[0]); |
|
8 | 119 |
break; |
120 |
} |
|
19
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
121 |
|
5efc347cca8d
The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
18
diff
changeset
|
122 |
return errorlevel; |
8 | 123 |
} |
9 | 124 |
|
125 |
static void sigchld_handler(int val) |
|
126 |
{ |
|
127 |
} |
|
128 |
||
129 |
static void program_signal() |
|
130 |
{ |
|
131 |
struct sigaction act; |
|
132 |
||
133 |
act.sa_handler = sigchld_handler; |
|
134 |
/* Reset the mask */ |
|
135 |
memset(&act.sa_mask,0,sizeof(act.sa_mask)); |
|
136 |
act.sa_flags = SA_NOCLDSTOP; |
|
137 |
act.sa_restorer = NULL; |
|
138 |
||
139 |
sigaction(SIGCHLD, &act, NULL); |
|
140 |
} |