IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 2, 2005, 9:40:50 AM (21 years ago)
Author:
eugene
Message:

psched bugfixes, moved IOBuffers and queues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/pantasks/LocalJob.c

    r4693 r4697  
    11# include "psched.h"
     2
     3/* local jobs are forked in the background
     4   we might need to limit the maximum number of local jobs.
     5   should we have a queue/stack of pending local jobs, much
     6   like controller? */
     7
     8/* update current state, drain stdout/stderr buffers */
     9int CheckLocalJob (Job *job) {
     10
     11  int Nread;
     12
     13  // XXX do something useful with exit status?
     14  CheckLocalJobStatus (job);
     15
     16  if ((job[0].state == JOB_EXIT) || (job[0].state == JOB_CRASH)) {
     17    EmptyIOBuffer (&job[0].stdout, 10, job[0].stdout_fd);
     18    EmptyIOBuffer (&job[0].stderr, 10, job[0].stderr_fd);
     19    close (job[0].stdout_fd);
     20    close (job[0].stderr_fd);
     21  } else {
     22    /* read stdout buffer */
     23    Nread = ReadtoIOBuffer (&job[0].stdout, job[0].stdout_fd);
     24    switch (Nread) {
     25      case -2:  /* error in read (programming error?  system level error?) */
     26        fprintf (stderr, "serious IO error\n");
     27        exit (2);
     28      case -1:  /* no data in pipe */
     29      case 0:   /* pipe is closed, change child state? **/
     30      default:  /* data in pipe */
     31        break;
     32    }
     33 
     34    /* read stderr buffer */
     35    Nread = ReadtoIOBuffer (&job[0].stderr, job[0].stderr_fd);
     36    switch (Nread) {
     37      case -2:  /* error in read (programming error?  system level error?) */
     38        fprintf (stderr, "serious IO error\n");
     39        exit (2);
     40      case -1:  /* no data in pipe */
     41      case 0:   /* pipe is closed, change child state? **/
     42      default:  /* data in pipe */
     43        break;
     44    }
     45  }
     46  return (TRUE);
     47}
     48
     49int CheckLocalJobStatus (Job *job) {
     50
     51  int result, waitstatus;
     52
     53  /* check local job status */
     54  result = waitpid (job[0].pid, &waitstatus, WNOHANG);
     55  switch (result) {
     56    case -1:  /* error with waitpid */
     57      switch (errno) {
     58        case ECHILD:
     59          fprintf (stderr, "unknown PID, not a child proc\n");
     60          fprintf (stderr, "did process already exit?  programming error?\n");
     61          job[0].state = JOB_NONE;
     62          job[0].exit_status = 0;
     63          return (FALSE);
     64        case EINVAL:
     65          fprintf (stderr, "error EINVAL (waitpid): programming error\n");
     66          exit (1);
     67        case EINTR:
     68          fprintf (stderr, "error EINTR (waitpid): programming error\n");
     69          exit (1);
     70        default:
     71          fprintf (stderr, "unknown error for waitpid (%d): programming error\n", errno);
     72          exit (1);
     73      }
     74      break;
     75     
     76    case 0:  /* process not exited */
     77      job[0].state = JOB_BUSY;
     78      job[0].exit_status = 0;
     79      return (TRUE);
     80
     81    default:
     82      if (result != job[0].pid) {
     83        fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, job[0].pid);
     84        exit (1);
     85      }
     86      if (WIFEXITED(waitstatus)) {
     87        job[0].state = JOB_EXIT;
     88        job[0].exit_status = WEXITSTATUS(waitstatus);
     89      }
     90      if (WIFSIGNALED(waitstatus)) {
     91        job[0].state = JOB_CRASH;
     92        job[0].exit_status = WTERMSIG(waitstatus);
     93      }
     94      if (WIFSTOPPED(waitstatus)) {
     95        fprintf (stderr, "waitpid returns 'stopped': programming error\n");
     96        exit (1);
     97      }
     98  }
     99  return (FALSE);
     100}
    2101
    3102/* this could be written a just a one-way pipe */
     
    58157}
    59158
    60 /* update current state, drain stdout/stderr buffers */
    61 int CheckLocalJob (Job *job) {
    62 
    63   int Nread;
    64 
    65   // XXX do something useful with exit status?
    66   CheckLocalJobStatus (job);
    67 
    68   if ((job[0].state == JOB_EXIT) || (job[0].state == JOB_CRASH)) {
    69     EmptyIOBuffer (&job[0].stdout, job[0].stdout_fd);
    70     EmptyIOBuffer (&job[0].stderr, job[0].stderr_fd);
    71   } else {
    72     /* read stdout buffer */
    73     Nread = ReadtoIOBuffer (&job[0].stdout, job[0].stdout_fd);
    74     switch (Nread) {
    75       case -2:  /* error in read (programming error?  system level error?) */
    76         fprintf (stderr, "serious IO error\n");
    77         exit (2);
    78       case -1:  /* no data in pipe */
    79       case 0:   /* pipe is closed, change child state? **/
    80       default:  /* data in pipe */
    81         break;
    82     }
    83  
    84     /* read stderr buffer */
    85     Nread = ReadtoIOBuffer (&job[0].stderr, job[0].stderr_fd);
    86     switch (Nread) {
    87       case -2:  /* error in read (programming error?  system level error?) */
    88         fprintf (stderr, "serious IO error\n");
    89         exit (2);
    90       case -1:  /* no data in pipe */
    91       case 0:   /* pipe is closed, change child state? **/
    92       default:  /* data in pipe */
    93         break;
    94     }
    95   }
    96   return (TRUE);
    97 }
    98 
    99 int CheckLocalJobStatus (Job *job) {
    100 
    101   int result, waitstatus;
    102 
    103   /*** if child has exited, does the pipe hang around until it is flushed? ***/
    104   /*** who closes the child stdout/stderr fd? */
    105 
    106   /* check local job status */
    107   result = waitpid (job[0].pid, &waitstatus, WNOHANG);
    108   switch (result) {
    109     case -1:  /* error with waitpid */
    110       switch (errno) {
    111         case ECHILD:
    112           fprintf (stderr, "unknown PID, not a child proc\n");
    113           fprintf (stderr, "did process already exit?  programming error?\n");
    114           job[0].state = JOB_NONE;
    115           job[0].exit_status = 0;
    116           return (FALSE);
    117         case EINVAL:
    118           fprintf (stderr, "error EINVAL (waitpid): programming error\n");
    119           exit (1);
    120         case EINTR:
    121           fprintf (stderr, "error EINTR (waitpid): programming error\n");
    122           exit (1);
    123         default:
    124           fprintf (stderr, "unknown error for waitpid (%d): programming error\n", errno);
    125           exit (1);
    126       }
    127       break;
    128      
    129     case 0:  /* process not exited */
    130       job[0].state = JOB_BUSY;
    131       job[0].exit_status = 0;
    132       return (TRUE);
    133 
    134     default:
    135       if (result != job[0].pid) {
    136         fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, job[0].pid);
    137         exit (1);
    138       }
    139       if (WIFEXITED(waitstatus)) {
    140         job[0].state = JOB_EXIT;
    141         job[0].exit_status = WEXITSTATUS(waitstatus);
    142       }
    143       if (WIFSIGNALED(waitstatus)) {
    144         job[0].state = JOB_CRASH;
    145         job[0].exit_status = WTERMSIG(waitstatus);
    146       }
    147       if (WIFSTOPPED(waitstatus)) {
    148         fprintf (stderr, "waitpid returns 'stopped': programming error\n");
    149         exit (1);
    150       }
    151   }
    152   return (FALSE);
    153 }
    154 
     159/* should this function close the fd's? */
    155160int KillLocalJob (Job *job) {
    156161
Note: See TracChangeset for help on using the changeset viewer.