示例#1
0
文件: spawn.c 项目: matyler/mtPaint
int spawn_expansion(char *cline, char *directory)
	// Replace %f with "current filename", then run via shell
{
	int res = -1;
	char *s1;
#ifdef WIN32
	char *argv[4] = { getenv("COMSPEC"), "/C", cline, NULL };
#else
	char *argv[4] = { "sh", "-c", cline, NULL };
#endif

	s1 = strstr( cline, "%f" );	// Has user included the current filename?
	if (s1)
	{
		s1 = insert_temp_file(cline, s1 - cline, 2);
		if (s1)
		{
			argv[2] = s1;
			res = spawn_process(argv, directory);
			free(s1);
		}
		else return -1;
	}
	else
	{
		res = spawn_process(argv, directory);
	}

		// Note that on Linux systems, returning 0 means that the shell was
		// launched OK, but gives no info on the success of the program being run by the shell.
		// To find out what happened you will need to use the output window.
		// MT 18-1-2007

	return res;
}
int main(int argc, char **argv)
{
  char command[1000], env[1000];
  int i, count;

  if(argc < 4)
    dgc_die("Error: not enough arguments.\n"
	    "Usage: %s central-count bin-path param-file\n", argv[0]);

  count = atoi(argv[1]);
  for(i = 0; i < count; i++) {
    fprintf(stderr, "%d ", i + 1);
    /* spawn central */
    sprintf(command, "%s/central -p%d >& /dev/null", argv[2], 1381 + i);
    spawn_process(command, NULL);
    usleep(100000);

    sprintf(command, "%s/param_server %s >& /dev/null", argv[2], argv[3]);
    sprintf(env, "CENTRALHOST=localhost:%d", 1381 + i);
    spawn_process(command, env);
    usleep(100000);
  }

  while(1)
    sleep(1);
  return 0;
}
示例#3
0
文件: init.c 项目: nori6001/FUZIX
static int clear_utmp(struct initpid *ip, uint16_t count, pid_t pid)
{
	uint16_t i;
	for (i = 0; i < count; i++) {
		if (ip->pid == pid) {
			uint8_t *p = initpid[i].id;
			/* Clear the utmp entry */
			ut.ut_pid = 1;
			ut.ut_type = INIT_PROCESS;
			*ut.ut_line = 0;
			ut.ut_id[0] = p[1];
			ut.ut_id[1] = p[2];
			*ut.ut_user = 0;
			pututline(&ut);
			/* Mark as done */
			initpid[i].pid = 0;
			/* Respawn the task if appropriate */
			if ((p[3] & (1 << runlevel)) && p[4] == INIT_RESPAWN)
				initpid[i].pid = spawn_process(p, 0);
			return 0;
		}
		ip++;
	}
	return -1;
}
示例#4
0
文件: cgi.c 项目: mmocean/shttpd
//CGI这个值得一看
int
run_cgi(struct conn *c, const char *prog)
{
	struct env_block	blk;
	char			dir[FILENAME_MAX], *p;
	int			ret, pair[2];

	prepare_environment(c, prog, &blk);

	/* CGI must be executed in its own directory */
	(void) my_snprintf(dir, sizeof(dir), "%s", prog);
	for (p = dir + strlen(dir) - 1; p > dir; p--)
		if (*p == '/') {
			*p++ = '\0';
			break;
		}
	
	if (my_socketpair(c, pair) != 0) {
		ret = -1;
	} else if (spawn_process(c, prog, blk.buf, blk.vars, pair[1], dir)) {
		ret = -1;
		(void) closesocket(pair[0]);
		(void) closesocket(pair[1]);
	} else {
		ret = 0;
		c->loc.chan.sock = pair[0];
	}

	return (ret);
}
static int
start_seaf_server ()
{
    if (!ctl->config_dir || !ctl->seafile_dir)
        return -1;

    seaf_message ("starting seaf-server ...\n");
    static char *logfile = NULL;
    if (logfile == NULL) {
        logfile = g_build_filename (ctl->logdir, "seafile.log", NULL);
    }

    char *argv[] = {
        "seaf-server",
        "-F", ctl->central_config_dir,
        "-c", ctl->config_dir,
        "-d", ctl->seafile_dir,
        "-l", logfile,
        "-P", ctl->pidfile[PID_SERVER],
        NULL};

    int pid = spawn_process (argv);
    if (pid <= 0) {
        seaf_warning ("Failed to spawn seaf-server\n");
        return -1;
    }

    return 0;
}
/*
=== multiprocesss_handler function ===
+++ Takes in a array of pointers to args and an iterator value.
>>> Iterator value defines current arg processed and decrements after
> 	it is dealt with. Handles which redirect/pipe function to call
>	if needed and lastly execs when leftmost arg is reached.
>	Closes all open file descriptors prior to returning.
*/
int multiprocess_handler(char** arg_ptrs, int itr){
	int fd_in = -1, fd_out = -1, fd_pipe = -1;
	
	while(itr >= 0){
		if(itr == 0){ //leftmost command.
				spawn_process(arg_ptrs, itr);
	
				if(fd_out != -1) close(fd_out); //cleanup.
				if(fd_pipe != -1) close(fd_pipe);
				if(fd_in != -1) close(fd_in); 
 	
			return 0;
		}
		itr--;
		switch (*arg_ptrs[itr]){
			case '>':
				arg_ptrs[itr] = '\0'; //Null >.
				fd_out = rd_out_handler(arg_ptrs[itr+1]);
				break;
			case '<': 
				arg_ptrs[itr] = '\0'; //Null <.
				fd_in = rd_in_handler(arg_ptrs[itr+1]);
				break;	
			case '|':
				arg_ptrs[itr] = '\0'; //Null |.
				fd_pipe = rd_pipe_handler(arg_ptrs, itr);
				break;	
		}
	}
	return 1; //should never get here unless stack overflow.
}
示例#7
0
文件: init.c 项目: aralbrec/FUZIX
/*
 *	Clear any dead processes
 */
static void clear_zombies(int flags)
{
	int i;
	pid_t pid = waitpid(-1, NULL, flags);
	/* Interrupted ? */
	if (pid < 0)
		return;
	/* See if we care what died. If we do then also check that
	 * do not need to respawn it
	 */
	for (i = 0; i < initcount; i++) {
		if (initpid[i] == pid) {
			uint8_t *p = initptr[i];
			/* Clear the utmp entry */
			ut.ut_pid = 1;
			ut.ut_type = INIT_PROCESS;
			*ut.ut_line = 0;
			ut.ut_id[0] = p[1];
			ut.ut_id[1] = p[2];
			*ut.ut_user = 0;
			pututline(&ut);
			/* Mark as done */
			initpid[i] = 0;
			/* Respawn the task if appropriate */
			if ((p[3] & (1 << runlevel)) && p[4] == INIT_RESPAWN)
				initpid[i] = spawn_process(p, 0);
			break;
		}
	}
}
示例#8
0
static int
start_seaf_server ()
{
    if (!ctl->config_dir || !ctl->seafile_dir)
        return -1;

    seaf_message ("starting seaf-server ...\n");

    char *argv[] = {
        "seaf-server",
        "-c", ctl->config_dir,
        "-d", ctl->seafile_dir,
        "-P", ctl->pidfile[PID_SERVER],
        "-C",
        NULL};

    if (!ctl->cloud_mode) {
        argv[7] = NULL;
    }
    
    int pid = spawn_process (argv);
    if (pid <= 0) {
        seaf_warning ("Failed to spawn seaf-server\n");
        return -1;
    }

    return 0;
}
示例#9
0
EXPORT
mach_port_t run(char *command, char *args[]) {
    pid_t infoPid;

    infoPid = spawn_process(command, args);
    usleep(1 * 1000);
    return attach(infoPid);
}
示例#10
0
/*
 * start an isolation tester process for specified file (including
 * redirection), and return process ID
 */
static PID_TYPE
isolation_start_test(const char *testname,
				_stringlist ** resultfiles,
				_stringlist ** expectfiles,
				_stringlist ** tags)
{
	PID_TYPE	pid;
	char		infile[MAXPGPATH];
	char		outfile[MAXPGPATH];
	char		expectfile[MAXPGPATH];
	char		psql_cmd[MAXPGPATH * 3];
	size_t		offset = 0;

	/*
	 * Look for files in the output dir first, consistent with a vpath search.
	 * This is mainly to create more reasonable error messages if the file is
	 * not found.  It also allows local test overrides when running pg_regress
	 * outside of the source tree.
	 */
	snprintf(infile, sizeof(infile), "%s/specs/%s.spec",
			 outputdir, testname);
	if (!file_exists(infile))
		snprintf(infile, sizeof(infile), "%s/specs/%s.spec",
				 inputdir, testname);

	snprintf(outfile, sizeof(outfile), "%s/results/%s.out",
			 outputdir, testname);

	snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
			 outputdir, testname);
	if (!file_exists(expectfile))
		snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
				 inputdir, testname);

	add_stringlist_item(resultfiles, outfile);
	add_stringlist_item(expectfiles, expectfile);

	if (launcher)
		offset += snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
						   "%s ", launcher);

	snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
			 SYSTEMQUOTE "./isolationtester \"dbname=%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
			 dblist->str,
			 infile,
			 outfile);

	pid = spawn_process(psql_cmd);

	if (pid == INVALID_PID)
	{
		fprintf(stderr, _("could not start process for test %s\n"),
				testname);
		exit_nicely(2);
	}

	return pid;
}
示例#11
0
int
open_web_browser(const char *url)
{
    GString *buf = g_string_new("xdg-open ");
    g_string_append (buf, url);
    spawn_process (buf->str);
    g_string_free (buf, TRUE);

    return 0;
}
示例#12
0
void 
start_worker_processes(int n, int type)
{
    int i;   
    fprintf(stderr, "start worker processes\n");

    for (i = 0; i < n; i++) {
        spawn_process(worker_process_cycle, (void *) (intptr_t) i, "worker process", type);
    }
}
示例#13
0
int tap_filter (struct pgEvent * evt)
{
  union pg_client_trigger *trig = evt->e.data.trigger;

  button *b;
  b = find_clicked_button(trig->content.u.mouse.x, trig->content.u.mouse.y);
  if (b)
    spawn_process(b->command);
  return 0;
}
示例#14
0
int start_web_server ()
{
    applet_message ("Starting web ...\n");

    if (spawn_process("seafile-web start") < 0) {
        applet_warning ("Failed to start seafile web\n");
        applet_exit(-1);
    }

    applet->web_status = WEB_STARTED;

    return 0;
}
示例#15
0
static int
start_seafdav() {
    static char *seafdav_log_file = NULL;
    if (seafdav_log_file == NULL)
        seafdav_log_file = g_build_filename (ctl->logdir,
                                             "seafdav.log",
                                             NULL);

    SeafDavConfig conf = ctl->seafdav_config;
    char port[16];
    snprintf (port, sizeof(port), "%d", conf.port);

    char *argv[] = {
        (char *)get_python_executable(),
        "-m", "wsgidav.server.run_server",
        "--log-file", seafdav_log_file,
        "--pid", ctl->pidfile[PID_SEAFDAV],
        "--port", port,
        "--host", conf.host,
        NULL
    };

    char *argv_fastcgi[] = {
        (char *)get_python_executable(),
        "-m", "wsgidav.server.run_server",
        "runfcgi",
        "--log-file", seafdav_log_file,
        "--pid", ctl->pidfile[PID_SEAFDAV],
        "--port", port,
        "--host", conf.host,
        NULL
    };

    char **args;
    if (ctl->seafdav_config.fastcgi) {
        args = argv_fastcgi;
    } else {
        args = argv;
    }

    int pid = spawn_process (args);

    if (pid <= 0) {
        seaf_warning ("Failed to spawn seafdav\n");
        return -1;
    }

    return 0;
}
示例#16
0
文件: fbe.c 项目: sjrct/Frosk64
kern_obj * exec_fbe(const uchar * buf, ulong buf_sz, kern_obj * par, uchar priv, uchar prior)
{
	uint csz, dsz, bsz;

	if (ATD(buf) != FBE_MARK) return NULL;

	csz = ATD(buf + 0x4);
	dsz = ATD(buf + 0x8);
	bsz = ATD(buf + 0xC);

	if (csz > buf_sz) csz = buf_sz;
	if (dsz > buf_sz - csz) dsz = buf_sz - csz;

	return spawn_process(buf + 0x10, csz, dsz, bsz, par, priv, prior);
}
示例#17
0
int spawn_subcontractor(const char *config_endpoint, char *contractor_endpoint)
{
  struct sigaction action;

  memset(&action, 0, sizeof(action));
  action.sa_sigaction = sigchld_handler;
  action.sa_flags = SA_SIGINFO;

  if (sigaction(SIGCHLD, &action, NULL))
  {
    perror("problem setting sigaction");
    return -1;
  }

  char **args = (char **) g_malloc(sizeof(char *) * 4);

  char *subcontractor;

  if ((config_get(NULL, CONFIG_TEST_HARNESS_SUBCONTRACTOR_TO_TEST_OPTION_NAME, &subcontractor) != 0) || (subcontractor == NULL))
  {
    g_free(args);
    debug_log("%s", CONFIG_TEST_HARNESS_SUBCONTRACTOR_TO_TEST_OPTION_NAME);
    error_log("Unable to get the name of the subcontractor to test!");
    return -1;
  }

  args[0] = subcontractor;
  args[1] = g_strdup(config_endpoint);
  args[2] = contractor_endpoint;
  args[3] = NULL;
  int ret = spawn_process(args, (sig_atomic_t *) & child_pid);

  g_free(args[1]);
  g_free(args[0]);
  g_free(args);

  if (ret != 0)
  {
    error_log("Could not spawn subcontractor");
    return -1;
  } else
  {
    debug_log("Subcontractor spawned");
  }

  return 0;
}
示例#18
0
文件: init.c 项目: aralbrec/FUZIX
/*
 *	Start everything that should be runnign at this run level. Take
 *	care not to re-start stuff that survives the transition
 */
static void do_for_runlevel(uint8_t newmask, int op)
{
	uint8_t *p = inittab;
	int n = 0;
	while (n < initcount) {
		initptr[n] = p;
		if (!(p[3] & newmask))
			goto next;
		if ((p[4] & INIT_OPMASK) == op) {
			/* Already running ? */
			if (op == INIT_RESPAWN && initpid[n])
				goto next;
			/* Spawn and maybe wait for a process */
			initpid[n] = spawn_process(p, (p[3] & INIT_WAIT));
		}
next:		p += *p;
		n++;
	}
}
示例#19
0
int
start_seafile_daemon ()
{
    GString *buf = g_string_new (NULL);

    applet_message ("Starting seafile ...\n");
    
    g_string_append_printf (buf, 
        "seaf-daemon -c \"%s\" -d \"%s\" -w \"%s\"",
        applet->config_dir, applet->seafile_dir, applet->seafile_worktree);
    
    if (spawn_process (buf->str) < 0) {
        applet_warning ("Failed to start seaf-daemon\n");
        applet_exit(-1);
    }

    g_string_free (buf, TRUE);
    return 0;
}
示例#20
0
int 
spawn_ccnet_daemon ()
{
    if (!applet->config_dir)
        return -1;

    int ret;
    char buf[1024];

    ret = snprintf (buf, sizeof(buf), "ccnet -c \"%s\" -D Peer,"
                    "Message,Connection,Other",
                    applet->config_dir);
    if (ret > 0) {
        if (spawn_process (buf) < 0) {
            applet_warning ("Failed to fork ccnet\n");
            return -1;
        }
        return 0;
    }
    return -1;
}
示例#21
0
void process_signal(void)
{
    if (SPAWN_PROCESS_ON_SIGNAL)
    {
        // Insert artificial delay so that a match for the agent right after
        // attempting to kill the daemon will not work. Trying to make it as
        // difficult as possible for the killing script! :-)
        sleep(1);

        spawn_process(SPAWN_PROCESS_ON_SIGNAL);
    }

    if (!REFUSE_TO_DIE)
    {
        if (PIDFILE)
        {
            unlink(PIDFILE);
        }
        exit(0);
    }
}
示例#22
0
文件: init.c 项目: nori6001/FUZIX
/*
 *	Start everything that should be runnign at this run level. Take
 *	care not to re-start stuff that survives the transition
 */
static void do_for_runlevel(uint8_t newmask, int op)
{
	uint8_t *p = inittab;
	struct initpid *t = initpid;
	int n = 0;
	while (n < initcount) {
		t->id[0] = p[1];
		t->id[1] = p[2];
		if (!(p[3] & newmask))
			goto next;
		if ((p[4] & INIT_OPMASK) == op) {
			/* Already running ? */
			if (op == INIT_RESPAWN && t->pid)
				goto next;
			/* Spawn and maybe wait for a process */
			t->pid = spawn_process(p, (p[3] & INIT_WAIT));
		}
next:		p += *p;
		t++;
		n++;
	}
}
示例#23
0
static int
start_ccnet_server ()
{
    if (!ctl->config_dir)
        return -1;

    seaf_message ("starting ccnet-server ...\n");

    char *argv[] = {
        "ccnet-server",
        "-c", ctl->config_dir,
        "-d",
        "-P", ctl->pidfile[PID_CCNET],
        NULL};
    
    int pid = spawn_process (argv);
    if (pid <= 0) {
        seaf_warning ("Failed to spawn ccnet-server\n");
        return -1;
    }

    return 0;
}
示例#24
0
static int
start_seaf_monitor ()
{
    if (!ctl->config_dir || !ctl->seafile_dir)
        return -1;

    seaf_message ("starting seaf-mon ...\n");

    char *argv[] = {
        "seaf-mon",
        "-c", ctl->config_dir,
        "-d", ctl->seafile_dir,
        "-P", ctl->pidfile[PID_MONITOR],
        NULL};
    
    int pid = spawn_process (argv);
    if (pid <= 0) {
        seaf_warning ("Failed to spawn seaf-mon\n");
        return -1;
    }

    return 0;
}
示例#25
0
文件: spawn.c 项目: matyler/mtPaint
int show_html(char *browser, char *docs)
{
	char buf[PATHBUF + 2], buf2[PATHBUF];
	int i=-1;
#ifdef WIN32
	char *r;

	if (!docs || !docs[0])
	{
		/* Use default path relative to installdir */
		docs = buf + 1;
		i = GetModuleFileNameA(NULL, docs, PATHBUF);
		if (!i) return (-1);
		r = strrchr(docs, '\\');
		if (!r) return (-1);
		r[1] = 0;
		strnncat(docs, HANDBOOK_LOCATION_WIN, PATHBUF);
	}
#else /* Linux */
	char *argv[5];

	if (!docs || !docs[0])
	{
		docs = HANDBOOK_LOCATION2;
//FIXME
//		if (valid_file(docs) < 0) docs = HANDBOOK_LOCATION2;
	}
#endif
	else docs = gtkncpy(buf + 1, docs, PATHBUF);

	if ((valid_file(docs) < 0))
	{
		alert_box( _("Error"),
			_("I am unable to find the documentation.  Either you need to download the mtPaint Handbook from the web site and install it, or you need to set the correct location in the Preferences window."), NULL);
		return (-1);
	}

#ifdef WIN32
	if (browser && !browser[0]) browser = NULL;
	if (browser)
	{
		/* Quote the filename */
		i = strlen(docs);
		buf[0] = docs[i] = '"';
		docs[i + 1] = '\0';
		/* Rearrange parameters */
		docs = gtkncpy(buf2, browser, PATHBUF);
		browser = buf;
	}
	if ((unsigned int)ShellExecuteA(NULL, "open", docs, browser,
		NULL, SW_SHOW) <= 32) i = -1;
	else i = 0;
#else
	argv[1] = docs;
	argv[2] = NULL;
	/* Try to use default browser */
	if (!browser || !browser[0])
	{
		argv[0] = "xdg-open";
		i = spawn_process(argv, NULL);
		if (!i) return (0); // System has xdg-utils installed
		// No xdg-utils - try "BROWSER" variable then
		browser = getenv("BROWSER");
	}
	else browser = gtkncpy(buf2, browser, PATHBUF);

	if (!browser) browser = HANDBOOK_BROWSER;

	argv[0] = browser;
	i = spawn_process(argv, NULL);
#endif
	if (i) alert_box( _("Error"),
		_("There was a problem running the HTML browser.  You need to set the correct program name in the Preferences window."), NULL);
	return (i);
}
示例#26
0
void
do_autorestart() {
  char time_buffer[TIME_BUFFER_SIZE];
  time_t now;
  time_t *spam_respawn = NULL;
  int sri, num_sri;
  struct sigaction sa;

  if (spam_respawn_count > 1) {
    spam_respawn = (time_t *)malloc(sizeof(time_t) * spam_respawn_count);
  }

  /* Make our process its own process group. */
  setpgid(0, 0);

  /* Set up a signal handler to trap SIGTERM. */
  sa.sa_handler = sigterm_handler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGTERM, &sa, NULL) < 0) {
    perror("sigaction");
  }

  if (logfile_fd >= 0) {
    /* If we have a logfile, dup it onto stdout and stderr. */
    dup2(logfile_fd, STDOUT_FILENO);
    dup2(logfile_fd, STDERR_FILENO);
    close(logfile_fd);
  }

  /* Make sure stdin is closed. */
  close(STDIN_FILENO);

  now = time(NULL);
  strftime(time_buffer, TIME_BUFFER_SIZE, "%T on %A, %d %b %Y", localtime(&now));
  fprintf(stderr, "autorestart begun at %s.\n", time_buffer);

  sri = 1;
  num_sri = 1;
  if (spam_respawn_count > 1) {
    spam_respawn[1] = now;
  }
  
  while (spawn_process()) {
    now = time(NULL);

    if (respawn_script != NULL) {
      invoke_respawn_script(now);
    }
    
    if (respawn_delay_time) {
      sleep(respawn_delay_time);
    }

    /* Make sure we're not respawning too fast. */
    if (spam_respawn_count > 1) {
      sri = (sri + 1) % spam_respawn_count;
      spam_respawn[sri] = now;
      if (num_sri < spam_respawn_count) {
        num_sri++;
      } else {
        time_t last = spam_respawn[(sri + 1) % spam_respawn_count];
        if (now - last < spam_respawn_time && !spam_restart_delay_time) {
          fprintf(stderr, "respawning too fast, giving up.\n");
          break;
        } else {
          fprintf(stderr, "respawning too fast, will sleep for %d seconds.\n", spam_restart_delay_time);
          signal (SIGALRM, sigalarm_handler);
          alarm(spam_restart_delay_time);
          pause();
          signal (SIGALRM, SIG_IGN);
        }
      }
    }
    
    if (stop_always) {
      fprintf(stderr, "instructed to not autorestart, exiting.\n");
      break;
    }
      
    strftime(time_buffer, TIME_BUFFER_SIZE, "%T on %A, %d %b %Y", localtime(&now));
    fprintf(stderr, "respawning at %s.\n", time_buffer);
  }

  now = time(NULL);
  strftime(time_buffer, TIME_BUFFER_SIZE, "%T on %A, %d %b %Y", localtime(&now));
  fprintf(stderr, "autorestart terminated at %s.\n", time_buffer);
  exit(0);
}
示例#27
0
/*
 * start a psql test process for specified file (including redirection),
 * and return process ID
 */
static PID_TYPE
psql_start_test(const char *testname,
				_stringlist **resultfiles,
				_stringlist **expectfiles,
				_stringlist **tags)
{
	PID_TYPE	pid;
	char		infile[MAXPGPATH];
	char		outfile[MAXPGPATH];
	char		expectfile[MAXPGPATH];
	char		psql_cmd[MAXPGPATH * 3];
	size_t		offset = 0;
	char	   *appnameenv;

	/*
	 * Look for files in the output dir first, consistent with a vpath search.
	 * This is mainly to create more reasonable error messages if the file is
	 * not found.  It also allows local test overrides when running pg_regress
	 * outside of the source tree.
	 */
	snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
			 outputdir, testname);
	if (!file_exists(infile))
		snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
				 inputdir, testname);

	snprintf(outfile, sizeof(outfile), "%s/results/%s.out",
			 outputdir, testname);

	snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
			 outputdir, testname);
	if (!file_exists(expectfile))
		snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
				 inputdir, testname);

	add_stringlist_item(resultfiles, outfile);
	add_stringlist_item(expectfiles, expectfile);

	if (launcher)
		offset += snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
						   "%s ", launcher);

	appnameenv = psprintf("PGAPPNAME=pg_regress/%s", testname);
	putenv(appnameenv);

	snprintf(psql_cmd + offset, sizeof(psql_cmd) - offset,
			 "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1",
			 bindir ? bindir : "",
			 bindir ? "/" : "",
			 dblist->str,
			 infile,
			 outfile);

	pid = spawn_process(psql_cmd);

	if (pid == INVALID_PID)
	{
		fprintf(stderr, _("could not start process for test %s\n"),
				testname);
		exit(2);
	}

	unsetenv("PGAPPNAME");
	free(appnameenv);

	return pid;
}
示例#28
0
/*
 * start a psql test process for specified file (including redirection),
 * and return process ID
 */
static PID_TYPE
psql_start_test(const char *testname,
				_stringlist ** resultfiles,
				_stringlist ** expectfiles,
				_stringlist ** tags)
{
	PID_TYPE	pid;
	char		infile[MAXPGPATH];
	char		outfile[MAXPGPATH];
	char		expectfile[MAXPGPATH] = "";
	char		psql_cmd[MAXPGPATH * 3];
	char		use_utility_mode = 0;
	char	   *lastslash;

	/* generalise later */
	if (strcmp(testname, "upg2") == 0)
		use_utility_mode = 1;

	/*
	 * Look for files in the output dir first, consistent with a vpath search.
	 * This is mainly to create more reasonable error messages if the file is
	 * not found.  It also allows local test overrides when running pg_regress
	 * outside of the source tree.
	 */
	snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
			 outputdir, testname);
	if (!file_exists(infile))
		snprintf(infile, sizeof(infile), "%s/sql/%s.sql",
				 inputdir, testname);

	snprintf(outfile, sizeof(outfile), "%s/results/%s.out",
			 outputdir, testname);

	/*
	 * If the test name contains slashes, create intermediary results
	 * directory.
	 */
	if ((lastslash = strrchr(outfile, '/')) != NULL)
	{
		char		resultdir[MAXPGPATH];

		memcpy(resultdir, outfile, lastslash - outfile);
		resultdir[lastslash - outfile] = '\0';
		if (mkdir(resultdir, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
		{
			if (errno == EEXIST)
			{
				/* exists already, that's OK */
			}
			else
			{
				fprintf(stderr, _("could not create directory \"%s\": %s\n"),
						resultdir, strerror(errno));
				exit_nicely(2);
			}
		}
	}
	
	if (optimizer_enabled)
	{
		snprintf(expectfile, sizeof(expectfile), "%s/expected/%s_optimizer.out",
				 outputdir, testname);
		if (!file_exists(expectfile))
		{
			snprintf(expectfile, sizeof(expectfile), "%s/expected/%s_optimizer.out",
					 inputdir, testname);
		}
	}

	// if optimizer is off or there is no orca-specific answer file, then
	// use the default answer file

	if (!file_exists(expectfile))
	{
		snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
				 outputdir, testname);

		if (!file_exists(expectfile))
		{
			snprintf(expectfile, sizeof(expectfile), "%s/expected/%s.out",
					 inputdir, testname);
		}
	}

	add_stringlist_item(resultfiles, outfile);
	add_stringlist_item(expectfiles, expectfile);

	snprintf(psql_cmd, sizeof(psql_cmd),
			 "%s " SYSTEMQUOTE "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
			 use_utility_mode ? "env PGOPTIONS='-c gp_session_role=utility'" : "",
			 psqldir ? psqldir : "",
			 psqldir ? "/" : "",
			 dblist->str,
			 infile,
			 outfile);

	pid = spawn_process(psql_cmd);

	if (pid == INVALID_PID)
	{
		fprintf(stderr, _("could not start process for test %s\n"),
				testname);
		exit_nicely(2);
	}

	return pid;
}
示例#29
0
文件: notes.c 项目: tylerberry/newts
int
main (int argc, char **argv)
{
  List nflist;

  short file_flag = FALSE;
  short quit_flag = FALSE;
  int result;

  newts_failed_malloc_hook = curses_malloc_die;

  srand ((unsigned) time (NULL));

  time (&orig_seqtime);
  time (&seqtime);

#ifdef __GLIBC__
  program_name = program_invocation_short_name;
#else
  program_name = base_name (argv[0]);
#endif

  /* Initialize i18n. */

#ifdef HAVE_SETLOCALE
  if (setlocale (LC_ALL, "") == NULL)
    {
      fprintf (stderr, _("%s: could not determine your locale\nCheck the "
                         "environment variables LANG, LC_ALL, etc.\n"),
               program_name);

      exit (EXIT_FAILURE);
    }
#endif

#if ENABLE_NLS
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
#endif

  /* Initial setup and global variable init. */

  init_blacklist ();
  list_init (&nflist,
             (void * (*) (void)) nfref_alloc,
             (void (*) (void *)) nfref_free,
             NULL);

  setup ();

  while (1)
    {
      int opt;
      short override_flag = FALSE;

      static struct option long_options[] =
        {
          {"alternate",       required_argument, 0, 'a'},
          {"black-threads",   no_argument,       0, 'b'},
          {"debug",           no_argument,       0, 'D'},
          {"seq-own-notes",   no_argument,       0, 'e'},
          {"file",            required_argument, 0, 'f'},
          {"no-signature",    no_argument,       0, 'g'},
          {"index",           no_argument,       0, 'i'},
          {"skip-own-notes",  no_argument,       0, 'k'},
          {"modern",          no_argument,       0, 'm'},
          {"no-sequencer",    no_argument,       0, 'n'},
          {"time",            required_argument, 0, 'o'},
          {"sequencer",       no_argument,       0, 's'},
          {"traditional",     no_argument,       0, 't'},
          {"user",            required_argument, 0, 'u'},
          {"white-basenotes", no_argument,       0, 'w'},
          {"extended",        no_argument,       0, 'x'},
          {"imsa",            no_argument,       0, 'y'},
          {"no-blacklist",    no_argument,       0, 'z'},
          {"help",            no_argument,       0, 'h'},
          {"version",         no_argument,       0, 0},
          {0, 0, 0, 0}
        };

      opt = getopt_long (argc, argv, "a:bDef:ghikmno:stu:wxz",
                         long_options, NULL);

      if (opt == -1)
        break;

      switch (opt)
        {
        case 0:
          {
            char *version_string;

            asprintf (&version_string, _("revision: %s"), newts_revision);
            printf (N_("notes - %s %s (%s)\n"), PACKAGE_NAME, VERSION,
                    version_string);

            free (version_string);

            list_destroy (&nflist);
            teardown ();

            if (fclose (stdout) == EOF)
              error (EXIT_FAILURE, errno, _("error writing output"));

            exit (EXIT_SUCCESS);
          }

        case 'a':
          {
            alt_sequencer = TRUE;

            if (sequencer == NONE)
              sequencer = SEQUENCER;

            newts_nrealloc (seqname, strlen (username) + strlen (optarg) + 2,
                            sizeof (char));
            strcpy (seqname, username);
            strcat (seqname, ":");
            strcat (seqname, optarg);

            if (alt_seqname)
              newts_free (alt_seqname);
            alt_seqname = newts_strdup (optarg);

            break;
          }

        case 'b':
          black_skip_seq = TRUE;
          break;

        case 'D':
          debug = TRUE;
          break;

        case 'f':
          if (parse_file (optarg, &nflist))
            file_flag = TRUE;
          break;

        case 'g':
          signature = FALSE;
          break;

        case 'i':
          sequencer = INDEX;
          break;

        case 'k':
          seq_own_notes = FALSE;
          override_flag = TRUE;
          break;

        case 'm':
          seq_own_notes = TRUE;
          break;

        case 'n':
          sequencer = NONE;
          break;

        case 'o':
          {
            struct timespec parsed_time, now;
            now.tv_sec = orig_seqtime;

            if (parse_datetime (&parsed_time, optarg, &now))
              {
                if (alt_time)
                  {
                    fprintf (stderr, _("%s: cannot specify multiple alternate times\n"),
                             program_name);
                    fprintf (stderr, _("See 'info newts' for more information.\n"));

                    list_destroy (&nflist);
                    teardown ();

                    exit (EXIT_FAILURE);
                  }
                else if (parsed_time.tv_sec <= orig_seqtime)
                  {
                    orig_seqtime = seqtime = parsed_time.tv_sec;
                    if (sequencer == NONE)
                      sequencer = SEQUENCER;
                    alt_time = TRUE;
                  }
                else
                  {
                    fprintf (stderr, _("%s: parsed time '%s' in the future\n"),
                             program_name, optarg);
                    fprintf (stderr, _("See 'info newts' for more information.\n"));

                    list_destroy (&nflist);
                    teardown ();

                    exit (EXIT_FAILURE);
                  }
              }
            else
              {
                fprintf (stderr, _("%s: error parsing time '%s'\n"),
                         program_name, optarg);

                list_destroy (&nflist);
                teardown ();

                exit (EXIT_FAILURE);
              }

            break;
          }

        case 's':
          sequencer = SEQUENCER;
          break;

        case 't':
        case 'y':   /* Pseudo-option for --imsa */
          traditional = TRUE;
          if (!override_flag)
            seq_own_notes = TRUE;
          break;

        case 'u':
          {
            struct passwd *pw;

            if (getuid () != 0)
              {
                fprintf (stderr, _("%s: only root is allowed to use '--user'\n"),
                         program_name);

                list_destroy (&nflist);
                teardown ();

                exit (EXIT_FAILURE);
              }

            pw = getpwnam (optarg);
            if (pw)
              {
                seteuid (pw->pw_uid);

                newts_free (username);
                username = newts_strdup (pw->pw_name);

                if (alt_sequencer)
                  {
                    newts_nrealloc (seqname,
                                    strlen (username) + strlen (alt_seqname) + 2,
                                    sizeof (char));
                    strcpy (seqname, username);
                    strcat (seqname, ":");
                    strcat (seqname, alt_seqname);
                  }
                else
                  {
                    newts_free (seqname);
                    seqname = newts_strdup (username);
                  }
              }

            else
              {
                fprintf (stderr, _("%s: no such user: '******'\n"), program_name,
                         optarg);

                list_destroy (&nflist);
                teardown ();

                exit (EXIT_FAILURE);
              }
          }
          break;

        case 'w':
          white_basenotes = TRUE;
          break;

        case 'x':
          sequencer = EXTENDED;
          break;

        case 'z':
          no_blacklist = TRUE;
          break;

        case 'h':
          printf (_("Usage: %s [OPTION]... NOTESFILE...\n"
                    "Run the UIUC-compatible notesfile client.\n\n"),
                  program_name);

          printf (_("If an argument to a long option is mandatory, it is also mandatory "
                    "for the\ncorresponding short option.\n\n"));

          printf (_("General options:\n"
                    "  -f, --file=FILE         Read list of notesfiles to view from specified file\n"
                    "  -g, --no-signature      Turn off automatic signature inclusion\n"
                    "  -h, --help              Display this help and exit\n"
                    "  -u, --user=USER         As root, run notes as the specified user\n"
                    "      --debug             Print debugging messages to stderr\n"
                    "      --version           Display version information and exit\n\n"));

          printf (_("Display options:\n"
                    "  -m, --modern            Use modern, consistent display style (default)\n"
                    "  -t, --traditional       Use traditional UIUC-style display\n"
                    "      --imsa              Use IMSA-style display (same as -t)\n\n"));

          printf (_("Blacklist options:\n"
                    "  -b, --black-threads     Skip threads with blacklisted basenotes while seqing\n"
                    "  -w, --white-basenotes   Do not apply blacklist to basenotes\n"
                    "  -z, --no-blacklist      Do not use the blacklist\n\n"));

          printf (_("Sequencer options:\n"
                    "  -a, --alternate=SEQ     Use alternate sequencer SEQ\n"
                    "  -e, --seq-own-notes     Make the sequencer view your notes\n"
                    "  -i, --index             Use the index sequencer\n"
                    "  -k, --skip-own-notes    Make the sequencer ignore your notes (default)\n"
                    "  -n, --no-sequencer      Do not use the sequencer (default)\n"
                    "  -o, --time=TIME         Use this date and time for the sequencer\n"
                    "  -s, --sequencer         Use the sequencer to read notes\n"
                    "  -x, --extended          Use the extended sequencer\n\n"));

          printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);

          list_destroy (&nflist);
          teardown ();

          if (fclose (stdout) == EOF)
            error (EXIT_FAILURE, errno, _("error writing output"));

          exit (EXIT_SUCCESS);

        case '?':
          fprintf (stderr, _("Try '%s --help' for more information.\n"),
                   program_name);

          list_destroy (&nflist);
          teardown ();

          exit (EXIT_FAILURE);
        }
    }

  if (optind == argc && !file_flag)
    {
      if (sequencer == NONE)
        {
          struct stat statbuf;

          fprintf (stderr, _("%s: too few arguments\n"), program_name);
          fprintf (stderr, _("Try '%s --help' for more information.\n"),
                   program_name);

          /* FIXME: should be a call to the notes system. */

          if (stat ("/etc/avail.notes", &statbuf) == 0)
            {
              fprintf (stderr,
                       _("Hit <RET> for a list of notesfiles on this system.\n"));
              getchar ();

              spawn_process (NULL, pager, "/etc/avail.notes", NULL);
            }

          list_destroy (&nflist);
          teardown ();

          exit (EXIT_FAILURE);
        }
      else
        {
          char *copy, *list, *token, *nfseq;

          nfseq = getenv ("NFSEQ");
          if (nfseq == NULL)
            {
              fprintf (stderr, _("%s: NFSEQ environment variable not set\n"),
                       program_name);
              fprintf (stderr, _("See 'info newts' for more information.\n"));

              list_destroy (&nflist);
              teardown ();

              exit (EXIT_FAILURE);
            }

          copy = newts_strdup (nfseq);

          token = strtok_r (copy, ", ", &list);
          while (token != NULL)
            {
              parse_nf (token, &nflist);
              token = strtok_r (NULL, ", ", &list);
            }

          newts_free (copy);
        }
    }
  else
    {
      while (optind < argc)
        parse_nf (argv[optind++], &nflist);
    }

  handle_signals ();

  /* For each notesfile, start up the master routine and go. */

  {
    ListNode *node = list_head (&nflist);

    while (node && !quit_flag)
      {
        newts_nfref *ref = (newts_nfref *) list_data (node);

        result = master (ref);

        node = list_next (node);

        if (result == QUITSEQ || result == QUITNOSEQ)
          quit_flag = TRUE;
      }
  }

  ignore_signals ();
  exit_curses ();

  if (*messages != '\0')
    printf ("%s", messages);

  teardown ();

  if (fclose (stdout) == EOF)
    error (EXIT_FAILURE, errno, _("error writing output"));

  exit (EXIT_SUCCESS);
}
示例#30
0
static PID_TYPE
ecpg_start_test(const char *testname,
				_stringlist ** resultfiles,
				_stringlist ** expectfiles,
				_stringlist ** tags)
{
	PID_TYPE	pid;
	char		inprg[MAXPGPATH];
	char		insource[MAXPGPATH];
	char	   *outfile_stdout,
				expectfile_stdout[MAXPGPATH];
	char	   *outfile_stderr,
				expectfile_stderr[MAXPGPATH];
	char	   *outfile_source,
				expectfile_source[MAXPGPATH];
	char		cmd[MAXPGPATH * 3];
	char	   *testname_dash;

	snprintf(inprg, sizeof(inprg), "%s/%s", inputdir, testname);

	testname_dash = strdup(testname);
	replace_string(testname_dash, "/", "-");
	snprintf(expectfile_stdout, sizeof(expectfile_stdout),
			 "%s/expected/%s.stdout",
			 outputdir, testname_dash);
	snprintf(expectfile_stderr, sizeof(expectfile_stderr),
			 "%s/expected/%s.stderr",
			 outputdir, testname_dash);
	snprintf(expectfile_source, sizeof(expectfile_source),
			 "%s/expected/%s.c",
			 outputdir, testname_dash);

	/*
	 * We can use replace_string() here because the replacement string does
	 * not occupy more space than the replaced one.
	 */
	outfile_stdout = strdup(expectfile_stdout);
	replace_string(outfile_stdout, "/expected/", "/results/");
	outfile_stderr = strdup(expectfile_stderr);
	replace_string(outfile_stderr, "/expected/", "/results/");
	outfile_source = strdup(expectfile_source);
	replace_string(outfile_source, "/expected/", "/results/");

	add_stringlist_item(resultfiles, outfile_stdout);
	add_stringlist_item(expectfiles, expectfile_stdout);
	add_stringlist_item(tags, "stdout");

	add_stringlist_item(resultfiles, outfile_stderr);
	add_stringlist_item(expectfiles, expectfile_stderr);
	add_stringlist_item(tags, "stderr");

	add_stringlist_item(resultfiles, outfile_source);
	add_stringlist_item(expectfiles, expectfile_source);
	add_stringlist_item(tags, "source");

	snprintf(insource, sizeof(insource), "%s.c", testname);
	ecpg_filter(insource, outfile_source);

	snprintf(inprg, sizeof(inprg), "%s/%s", inputdir, testname);

	snprintf(cmd, sizeof(cmd),
			 SYSTEMQUOTE "\"%s\" >\"%s\" 2>\"%s\"" SYSTEMQUOTE,
			 inprg,
			 outfile_stdout,
			 outfile_stderr);

	pid = spawn_process(cmd);

	if (pid == INVALID_PID)
	{
		fprintf(stderr, _("could not start process for test %s\n"),
				testname);
		exit_nicely(2);
	}

	free(outfile_stdout);
	free(outfile_stderr);
	free(outfile_source);

	return pid;
}