Changeset 6376af1


Ignore:
Timestamp:
Jun 3, 2011, 11:07:50 PM (7 years ago)
Author:
David Benjamin <davidben@mit.edu>
Children:
fc625fb
Parents:
cc305b5
git-author:
David Benjamin <davidben@mit.edu> (06/03/11 22:58:53)
git-committer:
David Benjamin <davidben@mit.edu> (06/03/11 23:07:50)
Message:
Use glib spawn functions when launching a newmsgproc

The glib version is smaller and has the advantage of not leaking all our
file descriptors to the process. More importantly, it does not call
malloc between fork and exec which is not safe in the presence of
threads.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • functions.c

    r259e60a8 r6376af1  
    31833183    }
    31843184   
    3185     /* if it exited, fork & exec a new one */
     3185    /* if it exited, spawn a new one */
    31863186    if (owl_global_get_newmsgproc_pid(&g)==0) {
    3187       pid_t i;
    31883187      int myargc;
    3189       i=fork();
    3190       if (i) {
    3191         /* parent set the child's pid */
    3192         owl_global_set_newmsgproc_pid(&g, i);
    3193         owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
    3194       } else {
    3195         /* child exec's the program */
    3196         char **parsed;
    3197         parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
    3198         if (myargc < 0) {
    3199           owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
    3200         }
    3201         if (myargc <= 0) {
    3202           _exit(127);
    3203         }
    3204         owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
    3205        
    3206         execvp(parsed[0], parsed);
    3207        
    3208        
    3209         /* was there an error exec'ing? */
    3210         owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s",
    3211                               owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
    3212         _exit(127);
     3188      char **argv = owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
     3189      if (myargc < 0) {
     3190        owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?",
     3191                              owl_global_get_newmsgproc(&g));
     3192      } else if (myargc > 0) {
     3193        /* Spawn the child. */
     3194        pid_t pid;
     3195        GError *error = NULL;
     3196        owl_function_debugmsg("About to exec \"%s\" with %d arguments", argv[0], myargc);
     3197        if (g_spawn_async(NULL, argv, NULL,
     3198                          G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
     3199                          NULL, NULL, &pid, &error)) {
     3200          owl_global_set_newmsgproc_pid(&g, pid);
     3201          owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", pid);
     3202        } else {
     3203          owl_function_debugmsg("Cannot run newmsgproc '%s': %s",
     3204                                owl_global_get_newmsgproc(&g), error->message);
     3205          g_error_free(error);
     3206        }
    32133207      }
     3208      g_strfreev(argv);
    32143209    }
    32153210  }
Note: See TracChangeset for help on using the changeset viewer.