本文整理汇总了C++中posix_spawnp函数的典型用法代码示例。如果您正苦于以下问题:C++ posix_spawnp函数的具体用法?C++ posix_spawnp怎么用?C++ posix_spawnp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_spawnp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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(""));
}
}
示例2: main
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;
}
示例3: TEST
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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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];
}
示例7: b_run_loop_exec_basic
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;
}
示例8: test_stop_after_delay
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);
}
示例9: Launch_posixSpawnSuspended
/*
* 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;
}
示例10: SHVDir_system
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);
}
示例11: 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;
}
示例12: sta_ioctl
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;
}
示例13: do_test_setsid
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");
}
}
示例14: h2o_read_command
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;
}
示例15: cat_logfile
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);
}