当前位置: 首页>>代码示例>>C++>>正文


C++ posix_spawn_file_actions_adddup2函数代码示例

本文整理汇总了C++中posix_spawn_file_actions_adddup2函数的典型用法代码示例。如果您正苦于以下问题:C++ posix_spawn_file_actions_adddup2函数的具体用法?C++ posix_spawn_file_actions_adddup2怎么用?C++ posix_spawn_file_actions_adddup2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了posix_spawn_file_actions_adddup2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: run

static int run(char* const commands[], pid_t* pid, bool include_stderr) {
    if(commands == NULL) { return 0; }
    if(pid == NULL) { die("Given NULL pid"); }

    int fds[2];
    if(pipe(fds)) {
        die("Failed to open pipe");
    }

    posix_spawn_file_actions_t action;
    posix_spawn_file_actions_init(&action);
    posix_spawn_file_actions_addclose(&action, fds[0]);
    posix_spawn_file_actions_adddup2(&action, fds[1], 1);
    posix_spawn_file_actions_addclose(&action, fds[1]);

    if(include_stderr) {
        posix_spawn_file_actions_adddup2(&action, 1, 2);
    }

    int status = posix_spawn(pid, commands[0],
                             &action,
                             NULL,
                             commands,
                             NULL);
    if(status != 0) {
        die("Failed to spawn process");
    }
    close(fds[1]);

    return fds[0];
}
开发者ID:i80and,项目名称:network,代码行数:31,代码来源:service_exec.c

示例2: main

int main(int argc, char* argv[])
{
	char** newargv;
	int status;
	argv0 = argv[0];
	argc--; argv++;

	if(!argc)
	{
		usage(1);
	}

	struct child {
		int in[2];
		int out[2];
	} child;

	if(pipe(child.in) < 0)
		perror("pipe");
	if(pipe(child.out) < 0)
		perror("pipe");

	pid_t pid;

	posix_spawn_file_actions_t action;
	posix_spawn_file_actions_init(&action);
	posix_spawn_file_actions_adddup2(&action, child.in[0], STDIN_FILENO);
	posix_spawn_file_actions_addclose(&action, child.in[0]);
	posix_spawn_file_actions_adddup2(&action, child.out[1], STDOUT_FILENO);
	posix_spawn_file_actions_addclose(&action, child.out[1]);

	if(posix_spawnp(&pid, argv[0], &action, NULL, argv, NULL) < 0)
	{
		// TODO: manage errors here.
	}

	int i;
	char c[2];
	c[1] = '\0';
	for(i = 0; i < 5; i++)
	{
		c[0] = '0' + i;
		write(child.in[1], "echo ", 5);
		write(child.in[1], c, 1);
		write(child.in[1], "\n", 1);
		c[0] = '\0';
		read(child.out[0], c, 1);
		printf("Output was: %s\n", c);
		read(child.out[0], c, 1); // read the newline
	}

	write(child.in[1], "exit\n", 5);

	waitpid(pid, &status, 0);

	printf("%s exited with status %d\n", argv[0], status);
	return 0;
}
开发者ID:Rant-and-Dev,项目名称:maestro,代码行数:58,代码来源:maestro-apply.c

示例3: setup_actions

/* Set up file actions for posix_spawn.
   *std__fd is FD_FORWARD, FD_CLOSE, FD_PIPE etc or a file descriptor #
   pipe[2] is the pipe created for FD_PIPE
   childfd is 0 (for stdin) 1 (for stdout) 2 (for stderr)
   input_to_child is 1 for stdin, 0 otherwise
   *hasactions is set on output if we added any file actions
   */
static qioerr setup_actions(
    posix_spawn_file_actions_t * actions,
    int* std__fd, int pipe[2], int childfd,
    int input_to_child,
    bool * hasactions)
{
  int pipe_parent_end;
  int pipe_child_end;
  int rc;

  assert(childfd == 0 || childfd == 1 || childfd == 2);

  // for stdin, the parent end is pipe[1], child end is pipe[0];
  // for everything else, the parent end is pipe[0], child end is pipe[1].
  if( input_to_child ) {
    pipe_parent_end = pipe[1];
    pipe_child_end = pipe[0];
  } else {
    pipe_parent_end = pipe[0];
    pipe_child_end = pipe[1];
  }

  if( *std__fd == QIO_FD_FORWARD ) {
    // Do nothing. Assume file descriptor childfd does not have close-on-exec.
  } else if( *std__fd == QIO_FD_PIPE || *std__fd == QIO_FD_BUFFERED_PIPE ) {
    // child can't write to the parent end of the pipe.
    rc = posix_spawn_file_actions_addclose(actions, pipe_parent_end);
    if( rc ) return qio_int_to_err(errno);

    // child needs to use its end of the pipe as fd childfd.
    rc = posix_spawn_file_actions_adddup2(actions, pipe_child_end, childfd);
    if( rc ) return qio_int_to_err(errno);

    // then close the pipe we dup'd since it is now known by another
    // name (e.g. fd 0).
    posix_spawn_file_actions_addclose(actions, pipe_child_end);
    *hasactions = true;
  } else if( *std__fd == QIO_FD_CLOSE ) {
    // close stdin.
    rc = posix_spawn_file_actions_addclose(actions, childfd);
    if( rc ) return qio_int_to_err(errno);
    *hasactions = true;
  } else if( *std__fd == QIO_FD_TO_STDOUT ) {
    // Do nothing.

  } else {
    // Use a given file descriptor for childfd (e.g. stdin).
    rc = posix_spawn_file_actions_adddup2(actions, *std__fd, childfd);
    if( rc ) return qio_int_to_err(errno);
    // then close the pipe we dup'd
    rc = posix_spawn_file_actions_addclose(actions, *std__fd);
    if( rc ) return qio_int_to_err(errno);
    *hasactions = true;
  }

  return 0;
}
开发者ID:chapel-lang,项目名称:chapel,代码行数:64,代码来源:qio_popen.c

示例4: do_bind_shell

void do_bind_shell(char* env, int port) {
  char* bundle_root = bundle_path();
  
  char* shell_path = NULL;
  asprintf(&shell_path, "%s/iosbinpack64/bin/bash", bundle_root);
  
  char* argv[] = {shell_path, NULL};
  char* envp[] = {env, NULL};
  
  struct sockaddr_in sa;
  sa.sin_len = 0;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  
  int sock = socket(PF_INET, SOCK_STREAM, 0);
  bind(sock, (struct sockaddr*)&sa, sizeof(sa));
  listen(sock, 1);
  
  printf("shell listening on port %d\n", port);
  
  for(;;) {
    int conn = accept(sock, 0, 0);
    
    posix_spawn_file_actions_t actions;
    
    posix_spawn_file_actions_init(&actions);
    posix_spawn_file_actions_adddup2(&actions, conn, 0);
    posix_spawn_file_actions_adddup2(&actions, conn, 1);
    posix_spawn_file_actions_adddup2(&actions, conn, 2);
    

    pid_t spawned_pid = 0;
    int spawn_err = posix_spawn(&spawned_pid, shell_path, &actions, NULL, argv, envp);
    
    if (spawn_err != 0){
      perror("shell spawn error");
    } else {
      printf("shell posix_spawn success!\n");
    }
    
    posix_spawn_file_actions_destroy(&actions);
    
    printf("our pid: %d\n", getpid());
    printf("spawned_pid: %d\n", spawned_pid);
    
    int wl = 0;
    while (waitpid(spawned_pid, &wl, 0) == -1 && errno == EINTR);
  }
  
  free(shell_path);
}
开发者ID:big538,项目名称:iOS-10.1.1-Project-0-Exploit-For-Jailbreak---F.C.E.-365-Fork-,代码行数:52,代码来源:drop_payload.c

示例5: child_spawn1_internal

pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to)
{
  posix_spawn_file_actions_t actions ;
  posix_spawnattr_t attr ;
  int e ;
  pid_t pid ;
  int haspath = !!env_get("PATH") ;
  if (coe(p[!(to & 1)]) < 0) { e = errno ; goto err ; }
  e = posix_spawnattr_init(&attr) ;
  if (e) goto err ;
  {
    sigset_t set ;
    sigemptyset(&set) ;
    e = posix_spawnattr_setsigmask(&attr, &set) ;
    if (e) goto errattr ;
    e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ;
    if (e) goto errattr ;
  }
  e = posix_spawn_file_actions_init(&actions) ;
  if (e) goto errattr ;
  e = posix_spawn_file_actions_adddup2(&actions, p[to & 1], to & 1) ;
  if (e) goto erractions ;
  e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ;
  if (e) goto erractions ;
  if (to & 2)
  {
    e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ;
    if (e) goto erractions ;
  }
  if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; }
  e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ;
  if (!haspath) unsetenv("PATH") ;
  posix_spawn_file_actions_destroy(&actions) ;
  posix_spawnattr_destroy(&attr) ;
  fd_close(p[to & 1]) ;
  if (e) goto errp ;
  return pid ;

 erractions:
  posix_spawn_file_actions_destroy(&actions) ;
 errattr:
  posix_spawnattr_destroy(&attr) ;
 err:
  fd_close(p[to & 1]) ;
 errp:
  fd_close(p[!(to & 1)]) ;
  errno = e ;
  return 0 ;
}
开发者ID:fvigotti,项目名称:skalibs,代码行数:49,代码来源:child_spawn1_internal.c

示例6: tf

static void *
tf (void *arg)
{
    xpthread_barrier_wait (&b);

    posix_spawn_file_actions_t a;
    if (posix_spawn_file_actions_init (&a) != 0)
    {
        puts ("error: spawn_file_actions_init failed");
        exit (1);
    }

    if (posix_spawn_file_actions_adddup2 (&a, pipefd[1], STDOUT_FILENO) != 0)
    {
        puts ("error: spawn_file_actions_adddup2 failed");
        exit (1);
    }

    if (posix_spawn_file_actions_addclose (&a, pipefd[0]) != 0)
    {
        puts ("error: spawn_file_actions_addclose");
        exit (1);
    }

    char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
                     NULL
                   };
    if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
    {
        puts ("error: spawn failed");
        exit (1);
    }

    return NULL;
}
开发者ID:kraj,项目名称:glibc,代码行数:35,代码来源:tst-exec5.c

示例7: posix_spawn_file_actions_adddup2

/*******************************************************************************
 * adddup2
 * 
 * behaviour as per POSIX
 * 
 * Returns:
 * 		EOK on success
 * 		EINVAL for any invalid parameter
 * 		ENOMEM if the action could not be added to the file actions object
*/
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fact_p, int fd, int new_fd)
{
	if (!valid_factp(fact_p) || (fd < 0)) {
		return EINVAL;
	} else {
		_posix_spawn_file_actions_t *_fact_p = GET_FACTP(fact_p);

		if (_fact_p == NULL) {
			if ((_fact_p = calloc(1, sizeof(*_fact_p))) == NULL) return ENOMEM;
			SET_FACTP(fact_p, _fact_p);
			return posix_spawn_file_actions_adddup2(fact_p, fd, new_fd);
		} else {
			unsigned num = _fact_p->num_entries + 1;

			if (num > 1) {		// not the first time
				_fact_p = realloc(_fact_p, FILE_ACTIONS_T_SIZE(num));
				if (_fact_p == NULL) return ENOMEM;
				SET_FACTP(fact_p, _fact_p);
			}
			_fact_p->action[_fact_p->num_entries].type = posix_file_action_type_DUP;
			_fact_p->action[_fact_p->num_entries]._type.dup.fd = fd;
			_fact_p->action[_fact_p->num_entries]._type.dup.new_fd = new_fd;
			++_fact_p->num_entries;
			return EOK;
		}
	}
}
开发者ID:vocho,项目名称:openqnx,代码行数:37,代码来源:posix_spawn_file_actions_adddup2.c

示例8: spawn_win32

static void spawn_win32(void) {
  char module_name[WATCHMAN_NAME_MAX];
  GetModuleFileName(NULL, module_name, sizeof(module_name));
  char *argv[MAX_DAEMON_ARGS] = {
    module_name,
    "--foreground",
    NULL
  };
  posix_spawn_file_actions_t actions;
  posix_spawnattr_t attr;
  pid_t pid;
  int i;

  for (i = 0; daemon_argv[i]; i++) {
    append_argv(argv, daemon_argv[i]);
  }

  posix_spawnattr_init(&attr);
  posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETPGROUP);
  posix_spawn_file_actions_init(&actions);
  posix_spawn_file_actions_addopen(&actions,
      STDIN_FILENO, "/dev/null", O_RDONLY, 0);
  posix_spawn_file_actions_addopen(&actions,
      STDOUT_FILENO, log_name, O_WRONLY|O_CREAT|O_APPEND, 0600);
  posix_spawn_file_actions_adddup2(&actions,
      STDOUT_FILENO, STDERR_FILENO);
  posix_spawnp(&pid, argv[0], &actions, &attr, argv, environ);
  posix_spawnattr_destroy(&attr);
  posix_spawn_file_actions_destroy(&actions);
}
开发者ID:Jerry-goodboy,项目名称:watchman,代码行数:30,代码来源:main.c

示例9: pipeline

void pipeline(const char *const *argv, struct pipeline *pl)
{
  posix_spawn_file_actions_t file_acts;

  int pipefds[2];
  if (pipe(pipefds)) {
    die_errno(errno, "pipe");
  }

  die_errno(posix_spawn_file_actions_init(&file_acts),
            "posix_spawn_file_actions_init");
  die_errno(posix_spawn_file_actions_adddup2(&file_acts, pipefds[0], 0),
            "posix_spawn_file_actions_adddup2");
  die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[0]),
            "posix_spawn_file_actions_addclose");
  die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[1]),
            "posix_spawn_file_actions_addclose");

  die_errno(posix_spawnp(&pl->pid, argv[0], &file_acts, NULL,
                         (char * const *)argv, environ),
            "posix_spawnp: %s", argv[0]);

  die_errno(posix_spawn_file_actions_destroy(&file_acts),
            "posix_spawn_file_actions_destroy");

  if (close(pipefds[0])) {
    die_errno(errno, "close");
  }

  pl->infd = pipefds[1];
}
开发者ID:2600hz-archive,项目名称:rabbitmq-c,代码行数:31,代码来源:process.c

示例10: spawn_via_gimli

static void spawn_via_gimli(void)
{
  char *argv[MAX_DAEMON_ARGS] = {
    GIMLI_MONITOR_PATH,
#ifdef WATCHMAN_STATE_DIR
    "--trace-dir=" WATCHMAN_STATE_DIR "/traces",
#endif
    "--pidfile", pid_file,
    "watchman",
    "--foreground",
    NULL
  };
  posix_spawn_file_actions_t actions;
  posix_spawnattr_t attr;
  pid_t pid;
  int i;

  for (i = 0; daemon_argv[i]; i++) {
    append_argv(argv, daemon_argv[i]);
  }

  close_random_fds();

  posix_spawnattr_init(&attr);
  posix_spawn_file_actions_init(&actions);
  posix_spawn_file_actions_addopen(&actions,
      STDOUT_FILENO, log_name, O_WRONLY|O_CREAT|O_APPEND, 0600);
  posix_spawn_file_actions_adddup2(&actions,
      STDOUT_FILENO, STDERR_FILENO);
  posix_spawnp(&pid, argv[0], &actions, &attr, argv, environ);
  posix_spawnattr_destroy(&attr);
  posix_spawn_file_actions_destroy(&actions);
}
开发者ID:Jerry-goodboy,项目名称:watchman,代码行数:33,代码来源:main.c

示例11: posix_spawn_file_actions_adddup2

void ChildProcess::Options::dup2(int fd, int targetFd) {
  auto err = posix_spawn_file_actions_adddup2(&inner_->actions, fd, targetFd);
  if (err) {
    throw std::system_error(
        err, std::generic_category(), "posix_spawn_file_actions_adddup2");
  }
}
开发者ID:otrempe,项目名称:watchman,代码行数:7,代码来源:ChildProcess.cpp

示例12: launch_thread

void launch_thread(jobtype ptype, pkgstate* state, pkg_exec* item, pkgdata* data) {
	char* arr[2];
	create_script(ptype, state, item, data);
	log_timestamp(1);
	log_putspace(1);
	if(ptype == JT_DOWNLOAD) {
		log_puts(1, SPL("downloading "));
	} else 
		log_puts(1, SPL("building "));

	log_put(1, VARIS(item->name), VARISL("("), VARIS(item->scripts.filename), VARISL(") -> "), VARIS(item->scripts.stdoutfn), NULL);

	arr[0] = item->scripts.filename->ptr;
	arr[1] = NULL;
	
	posix_spawn_file_actions_init(&item->fa);
	posix_spawn_file_actions_addclose(&item->fa, 0);
	posix_spawn_file_actions_addclose(&item->fa, 1);
	posix_spawn_file_actions_addclose(&item->fa, 2);
	posix_spawn_file_actions_addopen(&item->fa, 0, "/dev/null", O_RDONLY, 0);
	posix_spawn_file_actions_addopen(&item->fa, 1, item->scripts.stdoutfn->ptr, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
	posix_spawn_file_actions_adddup2(&item->fa, 1, 2);
	int ret = posix_spawnp(&item->pid, arr[0], &item->fa, NULL, arr, environ);
	if(ret == -1) {
		log_perror("posix_spawn");
		die(SPL(""));
	}
}
开发者ID:yasar11732,项目名称:butch,代码行数:28,代码来源:butch.c

示例13: TEST

TEST(spawn, posix_spawn_file_actions) {
  int fds[2];
  ASSERT_NE(-1, pipe(fds));

  posix_spawn_file_actions_t fa;
  ASSERT_EQ(0, posix_spawn_file_actions_init(&fa));

  ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[0]));
  ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, fds[1], 1));
  ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1]));
  // Check that close(2) failures are ignored by closing the same fd again.
  ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, fds[1]));
  ASSERT_EQ(0, posix_spawn_file_actions_addopen(&fa, 56, "/proc/version", O_RDONLY, 0));

  ExecTestHelper eth;
  eth.SetArgs({"ls", "-l", "/proc/self/fd", nullptr});
  pid_t pid;
  ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), &fa, nullptr, eth.GetArgs(), eth.GetEnv()));
  ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa));

  ASSERT_EQ(0, close(fds[1]));
  std::string content;
  ASSERT_TRUE(android::base::ReadFdToString(fds[0], &content));
  ASSERT_EQ(0, close(fds[0]));

  AssertChildExited(pid, 0);

  // We'll know the dup2 worked if we see any ls(1) output in our pipe.
  // The open we can check manually...
  bool open_to_fd_56_worked = false;
  for (const auto& line : android::base::Split(content, "\n")) {
    if (line.find(" 56 -> /proc/version") != std::string::npos) open_to_fd_56_worked = true;
  }
  ASSERT_TRUE(open_to_fd_56_worked);
}
开发者ID:android,项目名称:platform_bionic,代码行数:35,代码来源:spawn_test.cpp

示例14: posix_spawn_pipe_np

/*
 * Spawn a process to run "sh -c <cmd>".  Return the child's pid (in
 * *pidp), and a file descriptor (in *fdp) for reading or writing to the
 * child process, depending on the 'write' argument.
 * Return 0 on success; otherwise return an error code.
 */
int
posix_spawn_pipe_np(pid_t *pidp, int *fdp,
    const char *cmd, boolean_t write,
    posix_spawn_file_actions_t *fact, posix_spawnattr_t *attr)
{
	int	p[2];
	int	myside, yourside, stdio;
	const char *shpath = _PATH_BSHELL;
	const char *argvec[4] = { "sh", "-c", cmd, NULL };
	int	error;

	if (pipe(p) < 0)
		return (errno);

	if (access(shpath, X_OK))	/* XPG4 Requirement: */
		shpath = "";		/* force child to fail immediately */

	if (write) {
		/*
		 * Data is read from p[0] and written to p[1].
		 * 'stdio' is the fd in the child process that should be
		 * connected to the pipe.
		 */
		myside = p[1];
		yourside = p[0];
		stdio = STDIN_FILENO;
	} else {
		myside = p[0];
		yourside = p[1];
		stdio = STDOUT_FILENO;
	}

	error = posix_spawn_file_actions_addclose(fact, myside);
	if (yourside != stdio) {
		if (error == 0) {
			error = posix_spawn_file_actions_adddup2(fact,
			    yourside, stdio);
		}
		if (error == 0) {
			error = posix_spawn_file_actions_addclose(fact,
			    yourside);
		}
	}

	if (error)
		return (error);
	error = posix_spawn(pidp, shpath, fact, attr,
	    (char *const *)argvec, (char *const *)_environ);
	(void) close(yourside);
	if (error) {
		(void) close(myside);
		return (error);
	}

	*fdp = myside;
	return (0);
}
开发者ID:NanXiao,项目名称:illumos-joyent,代码行数:63,代码来源:spawn.c

示例15: spawn_param_redirect

void spawn_param_redirect(struct spawn_params *p, const char *stdname, int fd)
{
  int d;
  switch (stdname[3]) {
  case 'i': d = STDIN_FILENO; break;
  case 'o': d = STDOUT_FILENO; break;
  case 'e': d = STDERR_FILENO; break;
  }
  posix_spawn_file_actions_adddup2(&p->redirect, fd, d);
}
开发者ID:o-lim,项目名称:lua-ex,代码行数:10,代码来源:spawn.c


注:本文中的posix_spawn_file_actions_adddup2函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。