ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pjob/pjob.c
(Generate patch)

Comparing projects/pjob/pjob.c (file contents):
Revision 1.2 by pajs, Mon Jan 23 22:12:39 2006 UTC vs.
Revision 1.5 by pajs, Wed Jan 25 20:05:52 2006 UTC

# Line 25 | Line 25
25   #include <glib.h>
26   #include <glib/gprintf.h>
27   #include <poll.h>
28 + #include <sys/types.h>
29 + #include <sys/wait.h>
30  
31   #define DEFNUMTHREAD 10
32   #define DEFTIMEOUT 60
# Line 35 | Line 37 | struct _process_t{
37          gchar *jobname;
38          GTimer *timer;
39          GPid pid;
40 +        gint stat_loc;
41          gchar *file_stdout;
42          gchar *file_stderr;
43  
# Line 85 | Line 88 | void process_child(gpointer data, gpointer user_data){
88          struct pollfd fds[3];
89          gint fdssize=2;
90  
91 +        proc->timer = g_timer_new();
92 +        g_timer_start(proc->timer);
93 +
94          /* Setup files in output dir if requested to do so */
95          if(outdir != NULL){
96                  proc->file_stdout = g_strdup_printf("%s/%s-STDOUT", outdir, proc->jobname);
# Line 121 | Line 127 | void process_child(gpointer data, gpointer user_data){
127  
128          /* Exec the job */
129          if (infile == NULL){
130 <                if( ! g_spawn_async_with_pipes(NULL, execargv, NULL, 0, NULL, NULL, &(proc->pid), NULL, &(outpipes[0]), &(outpipes[1]), &err)){
130 >                if( ! g_spawn_async_with_pipes(NULL, execargv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &(proc->pid), NULL, &(outpipes[0]), &(outpipes[1]), &err)){
131                          g_printerr("Failed to execute job %s: %s\n", proc->jobname, err->message);
132                          return;
133                  }
134          }else{
135 <                if( ! g_spawn_async_with_pipes(NULL, execargv, NULL, 0, NULL, NULL, &(proc->pid), &(inpipes[1]), &(outpipes[0]), &(outpipes[1]), &err)){
135 >                if( ! g_spawn_async_with_pipes(NULL, execargv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &(proc->pid), &(inpipes[1]), &(outpipes[0]), &(outpipes[1]), &err)){
136                          g_printerr("Failed to execute job %s: %s\n", proc->jobname, err->message);
137                          return;
138                  }
# Line 162 | Line 168 | void process_child(gpointer data, gpointer user_data){
168                  poll(fds, fdssize, -1);
169                  /* For stdout and stderr see if there is any data, and read it */
170                  for(x=0; x<2; x++){
171 <                        if((fds[x].revents|POLLIN) == fds[x].revents){
171 >                        if((fds[x].revents & POLLIN) != 0){
172                                  /* We have data to read */
173                                  g_io_channel_read_line(sout[x], &readbuf, &rdatasize, NULL, NULL);
174                                  if(rdatasize > 0){
# Line 180 | Line 186 | void process_child(gpointer data, gpointer user_data){
186                          }
187                  }
188                  /* See if we need to pump more data down stdin */
189 <                if((fds[2].revents|POLLOUT) == fds[2].revents){
189 >                if((fds[2].revents & POLLOUT) != 0){
190                          /* We have data we can write */
191                          gchar *nextline;
192                          gint nextlinesize;
# Line 202 | Line 208 | void process_child(gpointer data, gpointer user_data){
208                  /* Even if we did get a hangup - lets make sure there is no more data to read first by looping again */
209                  if (readdata) continue;
210  
211 <                if(((fds[0].revents|POLLHUP) == fds[0].revents) && ((fds[1].revents|POLLHUP) == fds[1].revents)) break;
211 >                if(((fds[0].revents & POLLHUP) != 0) && ((fds[1].revents & POLLHUP) != 0)) break;
212          }      
213  
214 +        while((waitpid(proc->pid, &(proc->stat_loc), 0)) != proc->pid);
215 +
216 +        g_timer_stop(proc->timer);
217 +
218 +        /* If process exited cleanly */
219 +        if (WIFEXITED(proc->stat_loc)){
220 +                /* Get the exit code */
221 +                if (verbose) g_fprintf(stderr, "Job '%s' exited with code %d. Exec time: %.2f\n", proc->jobname, WEXITSTATUS(proc->stat_loc), g_timer_elapsed(proc->timer, 0));
222 +        }else{
223 +                /* Otherwise - find out what it died with */
224 +                /* TODO - this doesn't work quite right.. Mainly because its looking at the shell process, so the
225 +                 * child of the /bin/sh which does get a signal, this isn't passed up. Although, it handly tells
226 +                 * us if the /bin/sh gets a SEGV etc ;)
227 +                 */
228 +                g_fprintf(stderr, "Job %s exited with signal %d. Exec time: %.2f\n", proc->jobname, (WTERMSIG(proc->stat_loc)), g_timer_elapsed(proc->timer, 0));
229 +        }
230 +
231 +
232          g_io_channel_shutdown(sout[0], TRUE, NULL);
233          g_io_channel_shutdown(sout[1], TRUE, NULL);
234  
# Line 218 | Line 242 | void process_child(gpointer data, gpointer user_data){
242  
243          g_spawn_close_pid(proc->pid);
244  
221        if (verbose) g_fprintf(stderr, "Ending job '%s'\n", proc->jobname);
245  
246   }
247  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines