Exemplo n.º 1
0
int command_restart()
{
  int retval = 0;

  retval = command_stop();
  if (retval != 0)
    return retval;

  return command_start();
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
  int retval = 0;

  /* We need root rights to initialize everything. */
  if (getuid() != 0) {
    fprintf(stderr, "This program has to be run as root. Exiting.\n");
    return 1;
  }

  piphoned_commandline_info_from_argv(argc, argv); /* sets up g_cli_options */

  setlogmask(LOG_UPTO(g_cli_options.loglevel));
  openlog("piphoned", LOG_CONS | LOG_ODELAY | LOG_PID, LOG_DAEMON);
  syslog(LOG_DEBUG, "Early startup phase entered.");

  piphoned_config_init(g_cli_options.config_file); /* sets g_piphoned_config_info */

  /* Library initialisation */
  wiringPiSetup(); /* Requires root */

  switch(g_cli_options.command) {
  case PIPHONED_COMMAND_START:
    retval = command_start();
    break;
  case PIPHONED_COMMAND_STOP:
    retval = command_stop();
    break;
  case PIPHONED_COMMAND_RESTART:
    retval = command_restart();
    break;
  default:
    fprintf(stderr, "Invalid command %d. This is a bug.\n", g_cli_options.command);
    return 1;
  }

  /* Library cleanup */

  piphoned_config_free();
  syslog(LOG_DEBUG, "Late termination phase ended.");
  closelog();

  return retval;
}
Exemplo n.º 3
0
static int
worker_cron (aeEventLoop * el, long long id, void *data)
{
  workerState *ws = (workerState *) data;
  long long ts = currtime_millis ();
  int ret;


  if (ws->reconn_after != 0LL && ts > ws->reconn_after)
    {
      if (ws->fd >= 0)
	{
	  free_connection (ws, 1);
	}
      else
	{
	  char ebuf[512];
	  (void) command_start (ws, ebuf, sizeof (ebuf));
	}
    }

  if (ws->tps > 0)
    {
      long long token_val;

      token_val = (ws->tps * SINGLE_TOKEN_VAL) / 1000 * WORKER_CRON_INTERVAL;
      ws->token_val += token_val;
      if (ws->token_val > ws->tps * SINGLE_TOKEN_VAL)
	{
	  ws->token_val = ws->tps * SINGLE_TOKEN_VAL;
	}
      if (ws->wprepare_at_token && ws->token_val >= SINGLE_TOKEN_VAL)
	{
	  ret = emit_command (ws);
	  assert (ret == 0);
	  ws->wprepare_at_token = 0;
	}
    }


  return WORKER_CRON_INTERVAL;
}
Exemplo n.º 4
0
/* Main function. */
int main(int argc, char *argv[]) {
	cli_t cli;
	char *hostname = DEFAULT_HOST;
	char history_file[4096];
	ybool_t interactive_mode = YTRUE;

	bzero(&cli, sizeof(cli_t));
	cli.autocheck = YTRUE;
	if (argc == 2 && argv[1][0] != '-')
		hostname = argv[1];
	if (argc == 3 && !strcmp(argv[2], "-"))
		interactive_mode = YFALSE;
	// init database connection
	if ((cli.finedb = finedb_create(hostname, 11138)) == NULL) {
		printf_color("red", "Memory error.");
		printf("\n");
		exit(1);
	}
	if (finedb_connect(cli.finedb) != FINEDB_OK) {
		printf_color("red", "Unable to connect to server '%s' on port '%d'.", argv[1], 11138);
		printf("\n");
		exit(2);
	}
	// interactive mode init
	if (interactive_mode) {
		char *home = NULL;

		if ((home = getenv("HOME")) != NULL) {
			FILE *hist;

			snprintf(history_file, sizeof(history_file), "%s/%s", home, HISTORY_FILE);
			if ((hist = fopen(history_file, "w+")) != NULL) {
				fclose(hist);
				linenoiseHistoryLoad(HISTORY_FILE);
			}
			linenoiseSetCompletionCallback(cli_completion);
		}
	}
	// main loop
	for (; ; ) {
		char buff[4096], *line = NULL, *pt, *cmd;

		if (!interactive_mode) {
			ssize_t bufsz, linesz = 0;

			while ((bufsz = read(0, buff, sizeof(buff))) > 0) {
				pt = (char*)malloc(linesz + bufsz + 1);
				memcpy(pt, line, linesz);
				memcpy((void*)((size_t)pt + linesz), buff, bufsz);
				linesz += bufsz;
				pt[linesz] = '\0';
				if (line)
					free(line);
				line = pt;
			}
		} else {
			snprintf(buff, sizeof(buff), "%s > ", (cli.dbname ? cli.dbname : "default"));
			if ((line = linenoise(buff)) == NULL)
				break;
		}
		pt = line;
		LTRIM(pt);
		cmd = pt;
		// add command line to history
		linenoiseHistoryAdd(cmd);
		linenoiseHistorySave(history_file);
		// extract the command
		while (*pt && !IS_SPACE(*pt))
			++pt;
		*pt++ = '\0';
		LTRIM(pt);
		/* command management */
		if (cmd[0] == '\0')
			goto reloop;
			//continue;
		// local commands, no need for a running connection
		if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit"))
			exit(0);
		if (!strcasecmp(cmd, "help") || cmd[0] == '?') {
			command_help();
			goto reloop;
			//continue;
		} else if (!strcasecmp(cmd, "sync")) {
			command_sync(&cli);
			goto reloop;
			//continue;
		} else if (!strcasecmp(cmd, "async")) {
			command_async(&cli);
			goto reloop;
			//continue;
		}
		// commands that need a running connection
		if (!strcasecmp(cmd, "use"))
			command_use(&cli, pt);
		else if (!strcasecmp(cmd, "get"))
			command_get(&cli, pt);
		else if (!strcasecmp(cmd, "del"))
			command_del(&cli, pt);
		else if (!strcasecmp(cmd, "put"))
			command_send_data(&cli, pt, YFALSE, YFALSE);
		else if (!strcasecmp(cmd, "add"))
			command_send_data(&cli, pt, YTRUE, YFALSE);
		else if (!strcasecmp(cmd, "update"))
			command_send_data(&cli, pt, YFALSE, YTRUE);
		else if (!strcasecmp(cmd, "inc"))
			command_inc(&cli, pt);
		else if (!strcasecmp(cmd, "dec"))
			command_dec(&cli, pt);
		else if (!strcasecmp(cmd, "start"))
			command_start(&cli);
		else if (!strcasecmp(cmd, "stop"))
			command_stop(&cli);
#if 0
		else if (!strcasecmp(cmd, "list"))
			command_list(&cli, pt);
#endif
		else if (!strcasecmp(cmd, "ping"))
			command_ping(&cli);
		else if (!strcasecmp(cmd, "autocheck"))
			command_autocheck(&cli, pt);
		else {
			printf_color("red", "Bad command.");
			printf("\n");
		}
reloop:
		free(line);
	}
	return (0);
}
Exemplo n.º 5
0
static int filter_apply(
	git_filter				*self,
	void					**payload, /* may be read and/or set */
	git_buf					*to,
	const git_buf			*from,
	const git_filter_source	*src)
{
	struct filter_filter *ffs = (struct filter_filter *)self;
	git_config *config;
	git_buf configKey = GIT_BUF_INIT;
	int isRequired = FALSE;
	int error;
	const char *cmd = NULL;
	git_buf cmdBuf = GIT_BUF_INIT;
	wchar_t *wide_cmd;
	COMMAND_HANDLE commandHandle = COMMAND_HANDLE_INIT;
	git_buf errBuf = GIT_BUF_INIT;
	DWORD exitCode;

	if (!*payload)
		return GIT_PASSTHROUGH;

	if (git_repository_config__weakptr(&config, git_filter_source_repo(src)))
		return -1;

	git_buf_join3(&configKey, '.', "filter", *payload, "required");
	if (git_buf_oom(&configKey)) {
		giterr_set_oom();
		return -1;
	}

	error = git_config_get_bool(&isRequired, config, configKey.ptr);
	git_buf_free(&configKey);
	if (error && error != GIT_ENOTFOUND)
		return -1;

	git_buf_join(&configKey, '.', "filter", *payload);
	if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) {
		git_buf_puts(&configKey, ".smudge");
	} else {
		git_buf_puts(&configKey, ".clean");
	}
	if (git_buf_oom(&configKey)) {
		giterr_set_oom();
		return -1;
	}

	error = git_config_get_string(&cmd, config, configKey.ptr);
	git_buf_free(&configKey);
	if (error && error != GIT_ENOTFOUND)
		return -1;

	if (error == GIT_ENOTFOUND) {
		if (isRequired)
			return -1;
		return GIT_PASSTHROUGH;
	}

	git_buf_puts(&cmdBuf, cmd);
	if (git_buf_oom(&cmdBuf)) {
		giterr_set_oom();
		return -1;
	}

	if (expandPerCentF(&cmdBuf, git_filter_source_path(src)))
		return -1;

	if (ffs->shexepath) {
		// build params for sh.exe
		git_buf shParams = GIT_BUF_INIT;
		git_buf_puts(&shParams, " -c \"");
		git_buf_text_puts_escaped(&shParams, cmdBuf.ptr, "\"\\", "\\");
		git_buf_puts(&shParams, "\"");
		if (git_buf_oom(&shParams)) {
			git_buf_free(&cmdBuf);
			giterr_set_oom();
			return -1;
		}
		git_buf_swap(&shParams, &cmdBuf);
		git_buf_free(&shParams);
	}

	if (git__utf8_to_16_alloc(&wide_cmd, cmdBuf.ptr) < 0)
	{
		git_buf_free(&cmdBuf);
		giterr_set_oom();
		return -1;
	}
	git_buf_free(&cmdBuf);

	if (ffs->shexepath) {
		// build cmd, i.e. shexepath + params
		size_t len = wcslen(ffs->shexepath) + wcslen(wide_cmd) + 1;
		wchar_t *tmp = git__calloc(len, sizeof(wchar_t));
		if (!tmp) {
			git__free(wide_cmd);
			giterr_set_oom();
			return -1;
		}
		wcscat_s(tmp, len, ffs->shexepath);
		wcscat_s(tmp, len, wide_cmd);
		git__free(wide_cmd);
		wide_cmd = tmp;
	}

	commandHandle.errBuf = &errBuf;
	if (command_start(wide_cmd, &commandHandle, ffs->pEnv)) {
		git__free(wide_cmd);
		if (isRequired)
			return -1;
		return GIT_PASSTHROUGH;
	}
	git__free(wide_cmd);

	if (commmand_start_stdout_reading_thread(&commandHandle, to)) {
		command_close(&commandHandle);
		return -1;
	}

	if (command_write_gitbuf(&commandHandle, from)) {
		DWORD exitCode = command_close(&commandHandle);
		if (exitCode)
			setProcessError(exitCode, &errBuf);
		git_buf_free(&errBuf);
		if (isRequired)
			return -1;
		return GIT_PASSTHROUGH;
	}
	command_close_stdin(&commandHandle);

	if (command_wait_stdout_reading_thread(&commandHandle)) {
		DWORD exitCode = command_close(&commandHandle);
		if (exitCode)
			setProcessError(exitCode, &errBuf);
		git_buf_free(&errBuf);
		if (isRequired)
			return -1;
		return GIT_PASSTHROUGH;
	}

	exitCode = command_close(&commandHandle);
	if (exitCode) {
		if (isRequired) {
			setProcessError(exitCode, &errBuf);
			git_buf_free(&errBuf);
			return -1;
		}
		git_buf_free(&errBuf);
		return GIT_PASSTHROUGH;
	}

	git_buf_free(&errBuf);

	return 0;
}
Exemplo n.º 6
0
static void
command_handler (aeEventLoop * el, int fd, void *data, int mask)
{
  int ret;
  workerState *ws = (workerState *) data;
  simpleBuf *sb = &ws->cin;
  char *cp;
  int is_quit = 0;
  int i;

  ret = sb_read (sb, fd);
  // master quits this fd instead
  assert (ret != -1);

  cp = sb->buf;
  while (!is_quit && sb->cp - cp >= sizeof (workerCmd *))
    {
      workerCmd *cmd = NULL;

      memcpy ((char *) &cmd, cp, sizeof (workerCmd *));
      switch (cmd->command)
	{
	case CMD_SIZE:
	  ws->size = cmd->ival;
	  cmd->ret = 0;
	  break;
	case CMD_TPS:
	  ws->tps = cmd->ival;
	  cmd->ret = 0;
	  break;
	case CMD_START:
	  cmd->ret = command_start (ws, cmd->ebuf, sizeof (cmd->ebuf));
	  break;
	case CMD_STOP:
	  free_connection (ws, 0);
	  cmd->ret = 0;
	  break;
	case CMD_STAT:
	  cmd->num_rqst = ws->num_rqst;
	  cmd->num_resp = ws->num_resp;
	  cmd->num_reconn = ws->num_reconn;
	  for (i = 0; i < MAX_HISTO; i++)
	    {
	      cmd->histo[i].count = ws->histo[i].count;
	      cmd->histo[i].sum = ws->histo[i].sum;
	    }
	  cmd->ret = 0;
	  break;
	case CMD_QUIT:
	  free_connection (ws, 0);
	  aeStop (ws->el);
	  cmd->ret = 0;
	  is_quit = 1;
	  break;
	default:
	  fprintf (stdout, "-ERR invalid command:%d\n", cmd->command);
	  abort ();
	}
      write (ws->arg->wfd, "k", 1);
      cp += sizeof (workerCmd *);
    }

  memmove (sb->buf, cp, sb->cp - cp);
  sb->cp = sb->buf + (sb->cp - cp);
}
Exemplo n.º 7
0
static int _git_ssh_setup_tunnel(
	ssh_subtransport *t,
	const char *url,
	const char *gitCmd,
	git_smart_subtransport_stream **stream)
{
	char *host = NULL, *port = NULL, *path = NULL, *user = NULL, *pass = NULL;
	size_t i;
	ssh_stream *s;
	wchar_t *ssh = t->sshtoolpath;
	wchar_t *wideParams = NULL;
	wchar_t *cmd = NULL;
	git_buf params = GIT_BUF_INIT;
	int isPutty;
	size_t length;

	*stream = NULL;
	if (ssh_stream_alloc(t, url, gitCmd, stream) < 0) {
		giterr_set_oom();
		return -1;
	}

	s = (ssh_stream *)*stream;

	for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
		const char *p = ssh_prefixes[i];

		if (!git__prefixcmp(url, p)) {
			if (extract_url_parts(&host, &port, &path, &user, &pass, url, NULL) < 0)
				goto on_error;

			goto post_extract;
		}
	}
	if (git_ssh_extract_url_parts(&host, &user, url) < 0)
		goto on_error;

post_extract:
	if (!ssh)
	{
		giterr_set(GITERR_SSH, "No GIT_SSH tool configured");
		goto on_error;
	}

	isPutty = wcstristr(ssh, L"plink");
	if (port) {
		if (isPutty)
			git_buf_printf(&params, " -P %s", port);
		else
			git_buf_printf(&params, " -p %s", port);
	}
	if (isPutty && !wcstristr(ssh, L"tortoiseplink")) {
		git_buf_puts(&params, " -batch");
	}
	if (user)
		git_buf_printf(&params, " %s@%s ", user, host);
	else
		git_buf_printf(&params, " %s ", host);
	if (gen_proto(&params, s->cmd, s->url))
		goto on_error;
	if (git_buf_oom(&params)) {
		giterr_set_oom();
		goto on_error;
	}

	if (git__utf8_to_16_alloc(&wideParams, params.ptr) < 0) {
		giterr_set_oom();
		goto on_error;
	}
	git_buf_free(&params);

	length = wcslen(ssh) + wcslen(wideParams) + 3;
	cmd = git__calloc(length, sizeof(wchar_t));
	if (!cmd) {
		giterr_set_oom();
		goto on_error;
	}

	wcscat_s(cmd, length, L"\"");
	wcscat_s(cmd, length, ssh);
	wcscat_s(cmd, length, L"\"");
	wcscat_s(cmd, length, wideParams);

	if (command_start(cmd, &s->commandHandle, t->pEnv, isPutty ? CREATE_NEW_CONSOLE : DETACHED_PROCESS))
		goto on_error;

	git__free(wideParams);
	git__free(cmd);
	t->current_stream = s;
	git__free(host);
	git__free(port);
	git__free(path);
	git__free(user);
	git__free(pass);

	return 0;

on_error:
	t->current_stream = NULL;

	if (*stream)
		ssh_stream_free(*stream);

	git_buf_free(&params);

	if (wideParams)
		git__free(wideParams);

	if (cmd)
		git__free(cmd);

	git__free(host);
	git__free(port);
	git__free(user);
	git__free(pass);

	return -1;
}