IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 35139 for branches


Ignore:
Timestamp:
Feb 9, 2013, 10:11:54 AM (13 years ago)
Author:
eugene
Message:

replace system call to launch kapa with fork/exec and test with waitpid in case kapa exits prematurely

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130207/Ohana/src/libkapa/src/KapaOpen.c

    r35138 r35139  
    88static int Nvalid = 0;
    99static int *VALID = NULL;
     10
     11char *_parse_nextword (char *string);
     12int KapaLaunchCommand (char *line);
    1013
    1114int KapaServerInit (KapaSockAddress *Address) {
     
    225228
    226229  if (kapa_name == NULL) {
    227     sprintf (line, "%s &", kapa_exec);
     230    sprintf (line, "%s", kapa_exec);
    228231  } else {
    229     sprintf (line, "%s -name '%s' &", kapa_exec, kapa_name);
    230   }
    231   int status = system (line);
    232   if (status == -1) {
     232    sprintf (line, "%s -name '%s'", kapa_exec, kapa_name);
     233  }
     234
     235  int pid = KapaLaunchCommand (line);
     236  if (!pid) {
    233237    fprintf (stderr, "failed to launch kapa\n");
    234     return 0;
    235   }
    236   if (status > 0) {
    237     fprintf (stderr, "error launching kapa\n");
    238     return 0;
     238    return (-1);
    239239  }
    240240
     
    246246    // if (errno != EAGAIN) break;
    247247    if (errno != ECONNREFUSED) break;
     248    // no connection yet. try again, but first check
     249    // if the kapa job has exited
     250    int waitstatus;
     251    int result = waitpid (pid, &waitstatus, WNOHANG);
     252    if (result == -1) {
     253      fprintf (stderr, "problem checking for kapa\n");
     254      return (-1);
     255    }
     256    if (result > 0) {
     257      fprintf (stderr, "kapa exited\n");
     258      return (-1);
     259    }
    248260    usleep (10000);
    249261    Ntry ++;
     
    282294    sprintf (temp, "%s -socket %s -name %s &", kapa_exec, socket_name, name);
    283295  }
    284   if (system (temp) == -1) {
     296
     297  int pid = KapaLaunchCommand (temp);
     298  if (!pid) {
    285299    fprintf (stderr, "failed to launch kapa\n");
    286     return 0;
     300    return (-1);
    287301  }
    288302
     
    296310    if (fd != -1) break;
    297311    if (errno != EAGAIN) break;
     312    // no connection yet. try again, but first check
     313    // if the kapa job has exited
     314    int waitstatus;
     315    int result = waitpid (pid, &waitstatus, WNOHANG);
     316    if (result == -1) {
     317      fprintf (stderr, "problem checking for kapa\n");
     318      return (-1);
     319    }
     320    if (result > 0) {
     321      fprintf (stderr, "kapa exited\n");
     322      return (-1);
     323    }
    298324    usleep (10000);
    299325    Ntry ++;
     
    327353  return (sock);
    328354}
     355
     356int KapaLaunchCommand (char *line) {
     357
     358  // use fork & exec to launch the command; return the PID for testing exit
     359  int i, done, Nalloc, Nargv;
     360  char **argv, *p, *q;
     361
     362  i = 0;
     363  Nalloc = 10;
     364  ALLOCATE (argv, char *, Nalloc);
     365
     366  // split out line into unique words
     367  p = line;
     368  done = FALSE;
     369  while (!done) {
     370    q = _parse_nextword (p);
     371    if (q && *q) {
     372      argv[i] = strncreate (p, q - p);
     373      stripwhite (argv[i]);
     374      p = q;
     375    } else {
     376      argv[i] = strcreate (p);
     377      stripwhite (argv[i]);
     378      done = TRUE;
     379    }
     380    i++;
     381    if (i == Nalloc - 1) {
     382      // need to keep one extra for the NULL pointer
     383      Nalloc += 10;
     384      REALLOCATE (argv, char *, Nalloc);
     385    }
     386  }
     387  Nargv = i;
     388  argv[Nargv] = NULL;
     389
     390  int pid = fork ();
     391  if (!pid) { /* must be child process */
     392    execvp (argv[0], argv);
     393    return (0);
     394  }
     395  for (i = 0; i < Nargv; i++) {
     396    if (argv[i]) free (argv[i]);
     397  }
     398  free (argv);
     399
     400  if (pid == -1) {
     401    return (0);
     402  }
     403
     404  return (pid);
     405}
Note: See TracChangeset for help on using the changeset viewer.