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