コード例 #1
0
ファイル: drip_proxy.c プロジェクト: Altonymous/drip
int main(int argc, char** argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: drip_proxy dir\n");
    exit(1);
  }
  jvm_dir = argv[1];

  char* tty_name = ttyname(0);
  struct termios prev;
  int exit_code = 0;

  int in  = 0;
  int out = 0;
  int err = 0;

  if (setjmp(env) == 0) { // try
    if (tty_name) {
      check("tcgetcattr", tcgetattr(0, &prev));
      struct termios raw = prev;
      cfmakeraw(&raw);
      check("tcsetattr raw", tcsetattr(0, TCSANOW, &raw));

      int pty = open_pty();
      char* pty_name = ptsname(pty);

      in = pty;
      check("symlink in", symlink(pty_name, path("in")));

      if (streql(tty_name, ttyname(1))) {
        out = pty;
        check("symlink out", symlink(pty_name, path("out")));
      }

      if (streql(tty_name, ttyname(2))) {
        err = pty;
        check("symlink err", symlink(pty_name, path("err")));
      }

      signal(SIGWINCH, winch);
      resize = 1;
    }

    if (!in)  in  = open_fifo("in",  O_WRONLY);
    if (!out) out = open_fifo("out", O_RDONLY);
    if (!err) err = open_fifo("err", O_RDONLY);

    proxy(in, out, err);
  } else { // catch
    exit_code = 1;
  }

  if (tty_name) {
    check("tcsetattr prev", tcsetattr(0, TCSANOW, &prev));
  }

  if (exit_code) perror(err_prefix);

  return exit_code;
}
コード例 #2
0
ファイル: rkpty.c プロジェクト: DavidMulder/heimdal
int
main(int argc, char **argv)
{
    int optidx = 0;
    pid_t pid;

    setprogname(argv[0]);

    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
	usage(1);

    if (help_flag)
	usage (0);

    if (version_flag) {
	fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
	return 0;
    }

    argv += optidx;
    argc -= optidx;

    if (argc < 2)
	usage(1);

    parse_configuration(argv[0]);

    argv += 1;

    open_pty();

    pid = fork();
    switch (pid) {
    case -1:
	err(1, "Failed to fork");
    case 0:

	if(setsid()<0)
	    err(1, "setsid");

	dup2(slave, STDIN_FILENO);
	dup2(slave, STDOUT_FILENO);
	dup2(slave, STDERR_FILENO);
	closefrom(STDERR_FILENO + 1);

	execvp(argv[0], argv); /* add NULL to end of array ? */
	err(1, "Failed to exec: %s", argv[0]);
    default:
	close(slave);
	{
	    struct sigaction sa;

	    sa.sa_handler = caught_signal;
	    sa.sa_flags = 0;
	    sigemptyset (&sa.sa_mask);

	    sigaction(SIGALRM, &sa, NULL);
	}

	return eval_parent(pid);
    }
}
コード例 #3
0
ファイル: serial_unix.cpp プロジェクト: dougmencken/macemu
int16 XSERDPort::open(uint16 config)
{
	// Don't open NULL name devices
	if (device_name == NULL)
		return openErr;

	// Init variables
	io_killed = false;
	quitting = false;

	// Open port, according to the syntax of the path
	if (device_name[0] == '|') {
		// Open a process via ptys
		if (!open_pty())
			goto open_error;
	}
	else if (!strcmp(device_name, "midi")) {
		// MIDI:  not yet implemented
		return openErr;
	}
	else {
		// Device special file
		fd = ::open(device_name, O_RDWR);
		if (fd < 0)
			goto open_error;

#if defined(__linux__)
		// Parallel port?
		struct stat st;
		if (fstat(fd, &st) == 0)
			if (S_ISCHR(st.st_mode))
				protocol = ((MAJOR(st.st_rdev) == LP_MAJOR) ? parallel : serial);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
		// Parallel port?
		struct stat st;
		if (fstat(fd, &st) == 0)
			if (S_ISCHR(st.st_mode))
				protocol = (((st.st_rdev >> 16) == 16) ? parallel : serial);
#endif
	}

	// Configure port for raw mode
	if (protocol == serial || protocol == pty) {
		if (tcgetattr(fd, &mode) < 0)
			goto open_error;
		cfmakeraw(&mode);
		mode.c_cflag |= HUPCL;
		mode.c_cc[VMIN] = 1;
		mode.c_cc[VTIME] = 0;
		tcsetattr(fd, TCSAFLUSH, &mode);
	}
	configure(config);

	// Start input/output threads
	input_thread_cancel = false;
	output_thread_cancel = false;
	if (sem_init(&input_signal, 0, 0) < 0)
		goto open_error;
	if (sem_init(&output_signal, 0, 0) < 0)
		goto open_error;
	input_thread_active = (pthread_create(&input_thread, &thread_attr, input_func, this) == 0);
	output_thread_active = (pthread_create(&output_thread, &thread_attr, output_func, this) == 0);
	if (!input_thread_active || !output_thread_active)
		goto open_error;
	return noErr;

open_error:
	if (input_thread_active) {
		input_thread_cancel = true;
#ifdef HAVE_PTHREAD_CANCEL
		pthread_cancel(input_thread);
#endif
		pthread_join(input_thread, NULL);
		sem_destroy(&input_signal);
		input_thread_active = false;
	}
	if (output_thread_active) {
		output_thread_cancel = true;
#ifdef HAVE_PTHREAD_CANCEL
		pthread_cancel(output_thread);
#endif
		pthread_join(output_thread, NULL);
		sem_destroy(&output_signal);
		output_thread_active = false;
	}
	if (fd > 0) {
		::close(fd);
		fd = -1;
	}
	return openErr;
}