예제 #1
0
파일: join.c 프로젝트: reinerh/firejail
static void extract_command(int argc, char **argv, int index) {
	EUID_ASSERT();
	if (index >= argc)
		return;

	// doubledash followed by positional parameters
	if (strcmp(argv[index], "--") == 0) {
		arg_doubledash = 1;
		index++;
		if (index >= argc)
			return;
	}
		
	// first argv needs to be a valid command
	if (arg_doubledash == 0 && *argv[index] == '-') {
		fprintf(stderr, "Error: invalid option %s after --join\n", argv[index]);
		exit(1);
	}

	// build command
	build_cmdline(&cfg.command_line, &cfg.window_title, argc, argv, index);

	if (arg_debug)
		printf("Extracted command #%s#\n", cfg.command_line);
}
예제 #2
0
파일: run.c 프로젝트: Macaulay2/Singular
int WINAPI
WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
{
   int wait_for_child = FALSE;
   int compact_invocation = FALSE;
   DWORD ret_code = 0;


   char execname[FILENAME_MAX];
   char execpath[MAX_PATH];
   char* argv[MAX_ARGS+1]; /* leave extra slot for compact_invocation argv[0] */
   int argc;
   int i,j;
   char exec[MAX_PATH + FILENAME_MAX + 100];
   char cmdline2[MAX_ARGS * MAX_PATH];

   compact_invocation = get_exec_name_and_path(execname,execpath);

   if (compact_invocation)
   {
      argv[0] = execname;
      argc = parse_cmdline_to_arg_array(&(argv[1]),cmdline);
      argc++;
   }
   else
   {
      argc = parse_cmdline_to_arg_array(argv,cmdline);
      if (argc >= 1)
         strcpy(execname,argv[0]);
   }
   /* at this point, execpath is defined, as are argv[] and execname */
#ifdef DEBUG
   j = sprintf(buffer,"\nexecname : %s\nexecpath : %s\n",execname,execpath);
   for (i = 0; i < argc; i++)
      j += sprintf(buffer+j,"argv[%d]\t: %s\n",i,argv[i]);
   Trace((buffer));
#endif

   if (execname == NULL)
      error("you must supply a program name to run");

#if defined(__CYGWIN__)
   /* this insures that we search for symlinks before .exe's */
//   if (compact_invocation)
//      strip_exe(execname);
#endif

   process_execname(exec,execname,execpath);
   Trace(("exec\t%s\nexecname\t%s\nexecpath\t%s\n",
         exec,execname,execpath));

   wait_for_child = build_cmdline(cmdline2,exec,argc,argv);
   Trace((cmdline2));

   xemacs_special(exec);
   ret_code = start_child(cmdline2,wait_for_child);
   if (compact_invocation)
      for (i = 1; i < argc; i++) // argv[0] was not malloc'ed
         free(argv[i]);
   else
      for (i = 0; i < argc; i++)
         free(argv[i]);
   return (int) ret_code;
}
예제 #3
0
파일: runonce.c 프로젝트: carriercomm/NCD
static void func_new (NCDModuleInst *i)
{
    // allocate instance
    struct instance *o = malloc(sizeof(*o));
    if (!o) {
        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
        goto fail0;
    }
    NCDModuleInst_Backend_SetUser(i, o);
    
    // init arguments
    o->i = i;
    o->term_on_deinit = 0;
    
    // read arguments
    NCDValue *cmd_arg;
    NCDValue *opts_arg = NULL;
    if (!NCDValue_ListRead(i->args, 1, &cmd_arg) && !NCDValue_ListRead(i->args, 2, &cmd_arg, &opts_arg)) {
        ModuleLog(i, BLOG_ERROR, "wrong arity");
        goto fail1;
    }
    if (opts_arg && NCDValue_Type(opts_arg) != NCDVALUE_LIST) {
        ModuleLog(i, BLOG_ERROR, "wrong type");
        goto fail1;
    }
    
    int keep_stdout = 0;
    int keep_stderr = 0;
    int do_setsid = 0;
    
    // read options
    for (NCDValue *opt = (opts_arg ? NCDValue_ListFirst(opts_arg) : NULL); opt; opt = NCDValue_ListNext(opts_arg, opt)) {
        // read name
        if (NCDValue_Type(opt) != NCDVALUE_STRING) {
            ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
            goto fail1;
        }
        char *optname = NCDValue_StringValue(opt);
        
        if (!strcmp(optname, "term_on_deinit")) {
            o->term_on_deinit = 1;
        }
        else if (!strcmp(optname, "keep_stdout")) {
            keep_stdout = 1;
        }
        else if (!strcmp(optname, "keep_stderr")) {
            keep_stderr = 1;
        }
        else if (!strcmp(optname, "do_setsid")) {
            do_setsid = 1;
        }
        else {
            ModuleLog(o->i, BLOG_ERROR, "unknown option name");
            goto fail1;
        }
    }
    
    // build cmdline
    char *exec;
    CmdLine cl;
    if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
        goto fail1;
    }
    
    // build fd mapping
    int fds[3];
    int fds_map[2];
    int nfds = 0;
    if (keep_stdout) {
        fds[nfds] = 1;
        fds_map[nfds++] = 1;
    }
    if (keep_stderr) {
        fds[nfds] = 2;
        fds_map[nfds++] = 2;
    }
    fds[nfds] = -1;
    
    // build params
    struct BProcess_params params;
    params.username = NULL;
    params.fds = fds;
    params.fds_map = fds_map;
    params.do_setsid = do_setsid;
    
    // start process
    if (!BProcess_Init2(&o->process, o->i->params->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), params)) {
        ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
        CmdLine_Free(&cl);
        free(exec);
        goto fail1;
    }
    
    CmdLine_Free(&cl);
    free(exec);
    
    // set state
    o->state = STATE_RUNNING;
    
    return;
    
fail1:
    free(o);
fail0:
    NCDModuleInst_Backend_SetError(i);
    NCDModuleInst_Backend_Dead(i);
}