Exemple #1
0
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(""));
	}
}
int main(int argc, char **argv, char **envp) {
    aslclient aslc;
    pid_t child;
    int pstat;

    if(argc < 2 || strcmp(argv[1], "--help") == 0) {
        fprintf(stderr, "Usage: %s prog [args...]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    aslc = asl_open(BUNDLE_ID_PREFIX".startx", BUNDLE_ID_PREFIX, ASL_OPT_NO_DELAY);

    xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_INFO, STDOUT_FILENO);
    xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO);

#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
    assert(posix_spawnp(&child, argv[1], NULL, NULL, &argv[1], envp) == 0);
#else
    switch(child = fork()) {
        case -1:
            perror("fork");
            return errno;
        case 0:
            return execvp(argv[1], &argv[1]);
        default:
            break;
    }
#endif

    wait4(child, &pstat, 0, (struct rusage *)0);

    return pstat;
}
TEST(spawn, posix_spawnp) {
  ExecTestHelper eth;
  eth.SetArgs({"true", nullptr});
  pid_t pid;
  ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
  AssertChildExited(pid, 0);
}
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);
}
TEST(spawn, posix_spawnp_not_found) {
  ExecTestHelper eth;
  eth.SetArgs({"does-not-exist", nullptr});
  pid_t pid;
  ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), nullptr));
  AssertChildExited(pid, 127);
}
Exemple #6
0
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);
}
Exemple #7
0
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);
}
Exemple #8
0
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];
}
Exemple #9
0
B_WUR B_EXPORT_FUNC bool
b_run_loop_exec_basic(
    B_BORROW struct B_RunLoop *run_loop,
    B_BORROW char const *const *command_args,
    B_RunLoopProcessFunction *callback,
    B_RunLoopFunction *cancel_callback,
    B_BORROW void const *callback_data,
    size_t callback_data_size,
    B_OUT struct B_Error *e) {
  B_PRECONDITION(run_loop);
  B_PRECONDITION(command_args);
  B_PRECONDITION(command_args[0]);
  B_PRECONDITION(callback);
  B_PRECONDITION(cancel_callback);
  B_OUT_PARAMETER(e);

#if B_CONFIG_POSIX_SPAWN
  pid_t pid;
  int rc = posix_spawnp(
    &pid,
    command_args[0],
    NULL,
    NULL,
    // FIXME(strager): This cast looks like a bug.
    (char *const *) command_args,
    b_environ_());
  if (rc != 0) {
    *e = (struct B_Error) {.posix_error = rc};
    return false;
  }
Exemple #10
0
void
test_stop_after_delay(void *delay) {
#if HAVE_LEAKS
	int res;
	pid_t pid;
	char pidstr[10];
#endif

	if (delay != NULL) {
		Sleep((DWORD)(SIZE_T)delay * 1000);
	}

#if HAVE_LEAKS
	if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);

	/* leaks doesn't work against debug variant malloc */
	if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
	
	snprintf(pidstr, sizeof(pidstr), "%d", getpid());
	char* args[] = { "./leaks-wrapper", pidstr, NULL };
	res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
	if (res == 0 && pid > 0) {
		int status;
		waitpid(pid, &status, 0);
		test_long("Leaks", status, 0);
	} else {
		perror(args[0]);
	}
#endif
	_exit(EXIT_SUCCESS);
}
Exemple #11
0
/*
 * Static function implementations
 */
pid_t Launch_posixSpawnSuspended(cpu_type_t cpuType, const char *path, char** argv) {
	pid_t retVal = -1;
	
	if (path == NULL || argv == NULL) {
		Log_invalidArgument("path: %p, argv: %p", path, argv);
		
	} else {
		posix_spawnattr_t attr = 0;
		int ret = posix_spawnattr_init(&attr);
		if (ret != 0) {
			Log_errorPosix(ret, "posix_spawnattr_init");
			
		} else {
			sigset_t no_signals = 0;
			sigset_t all_signals = 0;
			sigemptyset(&no_signals);
			sigfillset(&all_signals);
			posix_spawnattr_setsigmask(&attr, &no_signals);
			posix_spawnattr_setsigdefault(&attr, &all_signals);

			if (cpuType != CPU_TYPE_ANY) {
				size_t ocount = 0;
				// if specified choose the arch from the fat binary to run
				ret = posix_spawnattr_setbinpref_np(&attr, 1, &cpuType, &ocount);
				if (ret != 0) {
					Log_errorPosix(ret, "posix_spawnattr_setbinpref_np");
				}
			}
			
			if (ret == 0) {
				ret = posix_spawnattr_setflags(&attr, 
											     POSIX_SPAWN_START_SUSPENDED 
											   | _POSIX_SPAWN_DISABLE_ASLR 
											   | POSIX_SPAWN_SETSIGDEF 
											   | POSIX_SPAWN_SETSIGMASK);
				if (ret != 0) {
					Log_errorPosix(ret, "posix_spawnattr_setflags");
					
				} else {
					pid_t pid = -1;
					ret = posix_spawnp(&pid, 
									   path, 
									   NULL, 
									   &attr, 
									   (char * const*)argv, 
									   (char * const*)NULL);
					if (ret != 0) {
						Log_errorPosix(ret, "posix_spawnp");
						
					} else {
						retVal = pid;
					}
				}
			}
			posix_spawnattr_destroy(&attr);
		}
	}
	return retVal;
}
TEST(spawn, posix_spawn_environment) {
  ExecTestHelper eth;
  eth.SetArgs({"sh", "-c", "exit $posix_spawn_environment_test", nullptr});
  eth.SetEnv({"posix_spawn_environment_test=66", nullptr});
  pid_t pid;
  ASSERT_EQ(0, posix_spawnp(&pid, eth.GetArg0(), nullptr, nullptr, eth.GetArgs(), eth.GetEnv()));
  AssertChildExited(pid, 66);
}
Exemple #13
0
int SHVDir_system(const char* cmd)
{
pid_t pid;
int retVal;
const char* argv[] = { "sh", "-c", NULL, NULL }; argv[2] = cmd;
	posix_spawnp(&pid,argv[0],NULL,NULL,(char*const*)argv,environ);
	return waitpid(pid,&retVal,0);
}
Exemple #14
0
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;
}
Exemple #15
0
int sta_ioctl(struct sta_link_info *link_info, int cmd)
{
	int err = 0;
	pid_t pid = 0;
	int status = 0;
	int exit_flag = 0;
	char *spawn_env[] = {NULL};
	char *spawn_args[] = {"list_ap.sh", NULL, 
			link_info->interface, NULL};
	
	switch (cmd) {
		case CONNECT:
		case RESTART:
			err = connect_ap(link_info);
			break;
		case CHKSTATUS:
			spawn_args[1] = "-c";
			break;
		case DISCONNECT:
			spawn_args[1] = "-d";
			break;
		case RECONNECT:
			spawn_args[1] = "-r";
			break;
		default:
			break;
	}

	if(cmd == CONNECT || cmd == RESTART) { 
		if(-1 == err)
			return -1;
		else 
			return 0;
	}

	err = posix_spawnp(&pid, spawn_args[0], NULL, NULL, 
		spawn_args, spawn_env);
	if (0 != err) {
		FPRINTF_CA(stderr, "posix_spawnp() error=%d\n", err);
		return -1;
	}

	err = waitpid(pid, &status, 0);
	if (-1 == err) {
		perror("waitpid");
		return -1;
	}

	if (!strcmp(spawn_args[1], "-c")) {
		exit_flag = WEXITSTATUS(status);
		if (8 == exit_flag)
			return -1;
	}

	return 0;
}
static void
do_test_setsid (bool test_setsid)
{
  pid_t sid, child_sid;
  int res;

  /* Current session ID.  */
  sid = getsid(0);
  if (sid == (pid_t) -1)
    FAIL_EXIT1 ("getsid (0): %m");

  posix_spawnattr_t attrp;
  /* posix_spawnattr_init should not fail (it basically memset the
     attribute).  */
  posix_spawnattr_init (&attrp);
  if (test_setsid)
    {
      res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID);
      if (res != 0)
	{
	  errno = res;
	  FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
	}
    }

  /* Program to run.  */
  char *args[2] = { (char *) "true", NULL };
  pid_t child;

  res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
  /* posix_spawnattr_destroy is noop.  */
  posix_spawnattr_destroy (&attrp);

  if (res != 0)
    {
      errno = res;
      FAIL_EXIT1 ("posix_spawnp: %m");
    }

  /* Child should have a different session ID than parent.  */
  child_sid = getsid (child);

  if (child_sid == (pid_t) -1)
    FAIL_EXIT1 ("getsid (%i): %m", child);

  if (test_setsid)
    {
      if (child_sid == sid)
	FAIL_EXIT1 ("child session ID matched parent one");
    }
  else
    {
      if (child_sid != sid)
	FAIL_EXIT1 ("child session ID did not match parent one");
    }
}
Exemple #17
0
int h2o_read_command(const char *cmd, char **argv, h2o_buffer_t **resp, int *child_status)
{
    int respfds[2] = {-1, -1};
    posix_spawn_file_actions_t file_actions;
    pid_t pid = -1;
    int ret = -1;
    extern char **environ;

    h2o_buffer_init(resp, &h2o_socket_buffer_prototype);

    /* create pipe for reading the result */
    if (pipe(respfds) != 0)
        goto Exit;

    /* spawn */
    posix_spawn_file_actions_init(&file_actions);
    posix_spawn_file_actions_adddup2(&file_actions, respfds[1], 1);
    if ((errno = posix_spawnp(&pid, cmd, &file_actions, NULL, argv, environ)) != 0) {
        pid = -1;
        goto Exit;
    }
    close(respfds[1]);
    respfds[1] = -1;

    /* read the response from pipe */
    while (1) {
        h2o_iovec_t buf = h2o_buffer_reserve(resp, 8192);
        ssize_t r;
        while ((r = read(respfds[0], buf.base, buf.len)) == -1 && errno == EINTR)
            ;
        if (r <= 0)
            break;
        (*resp)->size += r;
    }

Exit:
    if (pid != -1) {
        /* wait for the child to complete */
        pid_t r;
        while ((r = waitpid(pid, child_status, 0)) == -1 && errno == EINTR)
            ;
        if (r == pid) {
            /* success */
            ret = 0;
        }
    }
    if (respfds[0] != -1)
        close(respfds[0]);
    if (respfds[1] != -1)
        close(respfds[1]);
    if (ret != 0)
        h2o_buffer_dispose(resp);

    return ret;
}
Exemple #18
0
void cat_logfile()
{
	static int cnt;
	char    *argv_child[] = {"cat", file_log, NULL};
	printf("%d={", cnt++);
	fflush(stdout);
	posix_spawnp(NULL, argv_child[0], NULL, NULL, argv_child, NULL);
	wait(NULL);
	printf("}\n");
	fflush(stdout);
}
Exemple #19
0
static pid_t start_program(char **command, sigset_t *mask)
{
	char **arg, *s;
	pid_t pid;

#ifdef HAVE_POSIX_SPAWN
	posix_spawnattr_t attr;

	if (posix_spawnattr_init(&attr)) {
		pr_err("failed to init spawn attributes: %m");
		return 0;
	}

	if (posix_spawnattr_setsigmask(&attr, mask) ||
	    posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ||
	    posix_spawnp(&pid, command[0], NULL, &attr, command, environ)) {
		pr_err("failed to spawn %s: %m", command[0]);
		posix_spawnattr_destroy(&attr);
		return 0;
	}

	posix_spawnattr_destroy(&attr);
#else
	pid = fork();

	if (pid < 0) {
		pr_err("fork() failed: %m");
		return 0;
	}

	if (!pid) {
		/* restore the signal mask */
		if (sigprocmask(SIG_SETMASK, mask, NULL) < 0) {
			pr_err("sigprocmask() failed: %m");
			exit(100);
		}

		execvp(command[0], (char **)command);

		pr_err("failed to execute %s: %m", command[0]);

		exit(101);
	}
#endif

	for (s = xstrdup(""), arg = command; *arg; arg++)
		string_appendf(&s, "%s ", *arg);

	pr_info("process %d started: %s", pid, s);

	free(s);

	return pid;
}
Exemple #20
0
void
test_proc(pid_t bad_pid)
{
	dispatch_source_t proc_s[PID_CNT], proc;
	int res;
	pid_t pid, monitor_pid;

	event_cnt = 0;
	// Creates a process and register multiple observers.  Send a signal,
	// exit the process, etc., and verify all observers were notified.

	posix_spawnattr_t attr;
	res = posix_spawnattr_init(&attr);
	assert(res == 0);
#if HAVE_DECL_POSIX_SPAWN_START_SUSPENDED
	res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
	assert(res == 0);
#endif

	char* args[] = {
		"/bin/sleep", "2", NULL
	};

	res = posix_spawnp(&pid, args[0], NULL, &attr, args, NULL);
	if (res < 0) {
		perror(args[0]);
		exit(127);
	}

	res = posix_spawnattr_destroy(&attr);
	assert(res == 0);

	dispatch_group_t group = dispatch_group_create();

	assert(pid > 0);
	monitor_pid = bad_pid ? bad_pid : pid; // rdar://problem/8090801

	int i;
	for (i = 0; i < PID_CNT; ++i) {
		dispatch_group_enter(group);
		proc = proc_s[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC,
				monitor_pid, DISPATCH_PROC_EXIT, dispatch_get_global_queue(0, 0));
		test_ptr_notnull("dispatch_source_proc_create", proc);
		dispatch_source_set_event_handler(proc, ^{
			long flags = dispatch_source_get_data(proc);
			test_long("DISPATCH_PROC_EXIT", flags, DISPATCH_PROC_EXIT);
			event_cnt++;
			dispatch_source_cancel(proc);
		});
		dispatch_source_set_cancel_handler(proc, ^{
			dispatch_group_leave(group);
		});
Exemple #21
0
// Spawn watchman via a site-specific spawn helper program.
// We'll pass along any daemon-appropriate arguments that
// we noticed during argument parsing.
static void spawn_site_specific(const char *spawner)
{
  char *argv[MAX_DAEMON_ARGS] = {
    (char*)spawner,
    NULL
  };
  posix_spawn_file_actions_t actions;
  posix_spawnattr_t attr;
  pid_t pid;
  int i;
  int res, err;

  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);
  res = posix_spawnp(&pid, argv[0], &actions, &attr, argv, environ);
  err = errno;

  posix_spawnattr_destroy(&attr);
  posix_spawn_file_actions_destroy(&actions);

  if (res) {
    w_log(W_LOG_FATAL, "Failed to spawn watchman via `%s': %s\n", spawner,
          strerror(err));
  }

  if (waitpid(pid, &res, 0) == -1) {
    w_log(W_LOG_FATAL, "Failed waiting for %s: %s\n", spawner, strerror(errno));
  }

  if (WIFEXITED(res) && WEXITSTATUS(res) == 0) {
    return;
  }

  if (WIFEXITED(res)) {
    w_log(W_LOG_FATAL, "%s: exited with status %d\n", spawner,
          WEXITSTATUS(res));
  } else if (WIFSIGNALED(res)) {
    w_log(W_LOG_FATAL, "%s: signaled with %d\n", spawner, WTERMSIG(res));
  }
  w_log(W_LOG_ERR, "%s: failed to start, exit status %d\n", spawner, res);
}
Exemple #22
0
/*
 * Spawn a sh(1) command. Writes the resulting process ID to the pid_t pointed
 * at by `pid'. Returns a file descriptor (int) suitable for writing data to
 * the spawned command (data written to file descriptor is seen as standard-in
 * by the spawned sh(1) command). Returns `-1' if unable to spawn command.
 *
 * If cmd contains a single "%s" sequence, replace it with label if non-NULL.
 */
int
shell_spawn_pipecmd(const char *cmd, const char *label, pid_t *pid)
{
	int error;
	int len;
	posix_spawn_file_actions_t action;
#if SHELL_SPAWN_DEBUG
	unsigned int i;
#endif
	int stdin_pipe[2] = { -1, -1 };

	/* Populate argument array */
	if (label != NULL && fmtcheck(cmd, "%s") == cmd)
		len = snprintf(cmdbuf, CMDBUFMAX, cmd, label);
	else
		len = snprintf(cmdbuf, CMDBUFMAX, "%s", cmd);
	if (len >= CMDBUFMAX) {
		warnx("%s:%d:%s: cmdbuf[%u] too small to hold cmd argument",
		    __FILE__, __LINE__, __func__, CMDBUFMAX);
		return (-1);
	}

	/* Open a pipe to communicate with [X]dialog(1) */
	if (pipe(stdin_pipe) < 0)
		err(EXIT_FAILURE, "%s: pipe(2)", __func__);

	/* Fork sh(1) process */
#if SHELL_SPAWN_DEBUG
	fprintf(stderr, "%s: spawning `", __func__);
	for (i = 0; shellcmd_argv[i] != NULL; i++) {
		if (i == 0)
			fprintf(stderr, "%s", shellcmd_argv[i]);
		else if (i == 2)
			fprintf(stderr, " '%s'", shellcmd_argv[i]);
		else
			fprintf(stderr, " %s", shellcmd_argv[i]);
	}
	fprintf(stderr, "'\n");
#endif
	posix_spawn_file_actions_init(&action);
	posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO);
	posix_spawn_file_actions_addclose(&action, stdin_pipe[1]);
	error = posix_spawnp(pid, shellcmd, &action,
	    (const posix_spawnattr_t *)NULL, shellcmd_argv, environ);
	if (error != 0)
		err(EXIT_FAILURE, "%s: posix_spawnp(3)", __func__);

	return stdin_pipe[1];
}
Exemple #23
0
cleanup(int status)
{
  // Mask signals, so if called from non-signal context, we
  // won't get a reentry (especially for SIGCHLD).
  unsigned i;
  sigset_t mask;
  sigemptyset (&mask);
  for (i = 0; i < sizeof(signals) / sizeof(*signals); ++i)
    sigaddset (&mask, signals[i]);
  sigprocmask(SIG_BLOCK, &mask, 0);

  if (staprun_pid > 0)
    {
      int rc, ret;
      kill(staprun_pid, SIGHUP);
      ret = waitpid(staprun_pid, &rc, 0);
      if (status == 0)
        {
          if (ret == staprun_pid)
            status = WIFEXITED(rc) ? WEXITSTATUS(rc) : 128 + WTERMSIG(rc);
          else
            status = 2;
        }
    }

  if (tmpdir[0])
    {
      pid_t pid = 0;
      const char* argv[] = {"rm", "-rf", "--", tmpdir, NULL};
      if (chdir("/")) {} // ignore failure, rm will probably work anyway
      if (!posix_spawnp(&pid, argv[0], NULL, NULL,
                        (char* const*)argv, environ))
        waitpid(pid, NULL, 0);
    }

  // Announce we're quitting if prefixing is on. Schemes that use the "data"
  // option (e.g. unix and libvirt) are not able to detect when stapsh exits and
  // thus need this announcement to close the connection.
  if (prefix_data)
    reply("quit\n");

  if (listening_port)
    {
      fclose(stapsh_in);
      fclose(stapsh_out);
    }

  exit(status);
}
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 ;
}
Exemple #25
0
void call_posix_spawnp() {
    char *const file = "posix_spawnp.c";
    char *const compiler = "cc";
    char *const argv[] = {compiler, "-c", file, 0};

    expected_out(file);
    create_source(file);

    pid_t child;
    if (0 != posix_spawnp(&child, "cc", 0, 0, argv, get_environ())) {
        perror("posix_spawnp");
        exit(EXIT_FAILURE);
    }
    wait_for(child);
}
Exemple #26
0
/* Almost same API as posix_spawn */
static int l_posix_spawnp(lua_State *L) {
	int r;
	pid_t pid;
	const char *file = luaL_checkstring(L, 1);
	posix_spawn_file_actions_t *file_actions = luaL_checkudata(L, 2, "posix_spawn_file_actions_t");
	posix_spawnattr_t *attr = luaL_checkudata(L, 3, "posix_spawnattr_t");
	const char **argv = luaL_checkarraystrings(L, 4);
	const char **envp = luaL_optarraystrings(L, 5, (const char**)environ);
	if (0 != (r = posix_spawnp(&pid, file, file_actions, attr, (char*const*)argv, (char*const*)envp))) {
		lua_pushnil(L);
		lua_pushstring(L, strerror(r));
		lua_pushinteger(L, r);
		return 3;
	}
	lua_pushinteger(L, pid);
	return 1;
}
int
main(int argc, char *argv[])
{
    dispatch_source_t tmp_ds;
    int res;
    pid_t pid;

    if (argc < 2) {
        fprintf(stderr, "usage: harness [...]\n");
        exit(1);
    }

    posix_spawnattr_t attr;
    res = posix_spawnattr_init(&attr);
    assert(res == 0);
    res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
    assert(res == 0);

    int i;
    char** newargv = calloc(argc, sizeof(void*));
    for (i = 1; i < argc; ++i) {
        newargv[i-1] = argv[i];
    }
    newargv[i-1] = NULL;

    res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
    if (res) {
        errno = res;
        perror(newargv[0]);
        exit(EXIT_FAILURE);
    }
    //fprintf(stderr, "pid = %d\n", pid);
    assert(pid > 0);

    dispatch_queue_t main_q = dispatch_get_main_queue();

    tmp_ds = dispatch_source_proc_create(pid, DISPATCH_PROC_EXIT, NULL, main_q,
    ^(dispatch_event_t ev __attribute__((unused))) {
        int status;
        int res2 = waitpid(pid, &status, 0);
        assert(res2 != -1);
        //int passed = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
        test_long("Process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
        exit(0);
    });
Exemple #28
0
/* Set current backlight brightness to the given value. */
static void setBacklightBrightness(float value) {

    char _value[16];
    char xbacklight[] = "xbacklight";
    char *argv[] = { xbacklight, "-set", _value, NULL };
    int status;
    pid_t pid;

    /* We're going to use the approach based on the explicit fork and exec
     * instead of the standard library system() call, because we need the
     * control to be returned to our own process immediately - we're not
     * interested in the return value of the child process very much. */

    sprintf(_value, "%f", value);
    if (posix_spawnp(&pid, xbacklight, NULL, NULL, argv, environ) == 0)
        waitpid(pid, &status, WNOHANG);

}
Exemple #29
0
void call_posix_spawnp()
{
    char * const argv[] =
    {
        "cc",
        "-c",
        "posix_spawnp.c",
        0
    };

    pid_t child;
    if (0 != posix_spawnp(&child, "cc", 0, 0, argv, getenviron()))
    {
        perror("posix_spawnp");
        exit(EXIT_FAILURE);
    }
    wait_for(child);
}
Exemple #30
0
/*
 * get security - set ap security type
 * @link_info: key info to associate ap
 * Returns: 0 if get security type succeed, -1 if fail
 */
int get_security(INOUT struct sta_link_info *link_info)
{
	int err = 0;
	pid_t pid = 0;
	int status = 0;
	int  exit_flag = 0;
	char *spawn_env[] = {NULL};
	char *spawn_args[] = {"list_ap.sh", "-s", 
		link_info->interface, link_info->ssid, NULL};

	err = posix_spawnp(&pid, spawn_args[0], NULL, NULL, 
		spawn_args, spawn_env);
	if (0 != err) {
		FPRINTF_CA(stderr, "posix_spawnp() error=%d\n", err);
		return -1;
	}

	err = waitpid(pid, &status, 0);
	if (-1 == err) {
		perror("waitpid");
		return -1;
	}
	exit_flag = WEXITSTATUS(status);

	switch (exit_flag) {
		case 1:
			strcpy(link_info->security, "NONE");
			break;
		case 2:
			strcpy(link_info->security, "WEP");
			break;
		case 3:
			strcpy(link_info->security, "WPAPSK");
			break;
		case 4:
			strcpy(link_info->security, "NULL");
			break;
		default:
			break;
	}
	return 0;
}