void xt::Process::Child(const char *program, char * const *options) { ::signal(SIGUSR2, SIG_IGN); /* Unblock SIGUSR2 */ sigset_t signalMask; sigemptyset(&signalMask); sigaddset(&signalMask, SIGUSR2); if (sigprocmask(SIG_UNBLOCK, &signalMask, NULL)) { std::stringstream ss; ss << "sigprocmask: " << strerror(errno); throw std::runtime_error(ss.str()); } if (!getenv("XBMC_WESTON_GTEST_CHILD_STDOUT")) { ::close(STDOUT_FILENO); ::close(STDERR_FILENO); } if (execvpe(program, options, environ) == -1) { std::stringstream ss; ss << "execvpe: " << strerror(errno); throw std::runtime_error(ss.str()); } }
//Execute with a redirect void execute_redirect_in(char ***argv, int background, char **env, char* path) { pid_t pid; int status; if ((pid = fork()) < 0) { printf("*** ERROR: forking child process failed\n"); exit(1); } else if (pid == 0) { errno = 0; FILE *fp; fp = fopen(path, "r"); dup2(fileno(fp), STDIN_FILENO); fclose(fp); if (execvpe(**argv, *argv, env) < 0) { printf("%s\n", strerror(errno)); exit(1); } } else { if (background == FALSE) while (wait(&status) != pid) ; else printf("[1] %i\n", pid); } return; }
//Basic execute function. Ripped straigh from the old source void execute(char ***argv, int background, char **env) { pid_t pid; int status; if ((pid = fork()) < 0) { printf("*** ERROR: forking child process failed\n"); exit(1); } else if (pid == 0) { errno = 0; if (execvpe(**argv, *argv, env) < 0) { printf("%s\n", strerror(errno)); exit(1); } } else { if (background == FALSE) while (wait(&status) != pid) ; else printf("[1] %i\n", pid); } return; }
static int run_script (const char *fname, char *args[]) { /* We want to test the `execvpe' function. To do this we restart the program with an additional parameter. */ int status; pid_t pid = fork (); if (pid == 0) { execvpe (fname, args, NULL); puts ("Cannot exec"); exit (EXIT_FAILURE); } else if (pid == (pid_t) -1) { puts ("Cannot fork"); return 1; } /* Wait for the child. */ if (waitpid (pid, &status, 0) != pid) { puts ("Wrong child"); return 1; } if (WTERMSIG (status) != 0) { puts ("Child terminated incorrectly"); return 1; } return 0; }
int __darwin_execvpe(const char *file, char *const argv[], char *const envp[]) { TRACE1(file); const char* path = findInPath(file); if (!path) { errno = DARWIN_ENOENT; return -1; } if (!MachO::isMachO(path)) { int rv = execv(path, argv); errnoOut(); return rv; } else { argv = prependLoaderPath(argv, path); int rv = execvpe(g_loader_path, argv, envp); errnoOut(); delete [] argv; return rv; } }
extern "C" CDECL int rust_run_program(const char* argv[], void* envp, const char* dir, int in_fd, int out_fd, int err_fd) { int pid = fork(); if (pid != 0) return pid; sigset_t sset; sigemptyset(&sset); sigprocmask(SIG_SETMASK, &sset, NULL); if (in_fd) dup2(in_fd, 0); if (out_fd) dup2(out_fd, 1); if (err_fd) dup2(err_fd, 2); /* Close all other fds. */ for (int fd = getdtablesize() - 1; fd >= 3; fd--) close(fd); if (dir) { chdir(dir); } #ifdef __APPLE__ if (envp) { *_NSGetEnviron() = (char **)envp; } execvp(argv[0], (char * const *)argv); #else if (!envp) { envp = environ; } execvpe(argv[0], (char * const *)argv, (char * const *)envp); #endif exit(1); }
int main(int argc, char* argv[]) { int a = fork(); char *env[] = {"PATH=C:\\TEST", NULL}; if(a > 0) { printf("\nParent ProcessID : %d\n",GetProcessId()); waitpid(a); printf("\nAfter Child Process has exited"); sleep(10); printf("\nParent : After Sleep"); } else { printf("\nChild ProcessID : %d\n",GetProcessId()); char *inp[] = {"bin/hello","a","b"}; execvpe(inp[0],inp,env); } return 0; }
static void on_fork(void *data) { unsigned i; static const char *daemon_possible_paths[] = { "/usr/libexec/bluetooth/bluetoothd", // fedora/yocto-style "/usr/lib/bluetooth/bluetoothd", // arch-style "/usr/sbin/bluetoothd" // debian-style }; const char *argv[] = { NULL, // waiting to be set "--nodetach", NULL }; static const char *envp[] = { "BLUETOOTH_SYSTEM_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket", NULL }; for (i = 0; i < SOL_UTIL_ARRAY_SIZE(daemon_possible_paths); i++) { argv[0] = daemon_possible_paths[i]; SOL_INF("attempting to exec %s", daemon_possible_paths[i]); execvpe(argv[0], (char *const *)argv, (char *const *)envp); } SOL_INF("bluetooth daemon executable not found, aborting"); sol_platform_linux_fork_run_exit(EXIT_FAILURE); }
int main(int argc, char **argv, char **envp) { if (argc < 3) { fprintf(stderr, "usage: %s PROGRAM ARGV0 [ARGS...]\n", argv[0]); return -1; } return execvpe(argv[1], argv + 2, envp); }
int my_execvpe(char* full, char* args[]) { struct stat access; if (stat(full, &access) == 0) if(execvpe(full, args, NULL) == -1) shell_error("execvpe"); return 0; }
static int exec_init(void *arg) { exec_data_t *data = (exec_data_t *)arg; setgid(data->omv_mock.mock_gid); setuid(data->omv_mock.omv_uid); execvpe("/bin/bash", argv, data->env); return 255; }
int spawnv_background(const char *cmd, char *const args[]) { int process = 0; if (process = fork()) { return process; } else { execvpe(cmd, args, environ); exit(1); } }
ProcHandle runProcess (char *const args[], char *workingDirectory, char **environment, int fdStdInput, int fdStdOutput, int fdStdError, int set_inthandler, long inthandler, int set_quithandler, long quithandler) { int pid; struct sigaction dfl; switch(pid = fork()) { case -1: return -1; case 0: { pPrPr_disableITimers(); if (workingDirectory) { if (chdir (workingDirectory) < 0) { // See #1593. The convention for the exit code when // exec() fails seems to be 127 (gleened from C's // system()), but there's no equivalent convention for // chdir(), so I'm picking 126 --SimonM. _exit(126); } } /* Set the SIGINT/SIGQUIT signal handlers in the child, if requested */ (void)sigemptyset(&dfl.sa_mask); dfl.sa_flags = 0; if (set_inthandler) { dfl.sa_handler = (void *)inthandler; (void)sigaction(SIGINT, &dfl, NULL); } if (set_quithandler) { dfl.sa_handler = (void *)quithandler; (void)sigaction(SIGQUIT, &dfl, NULL); } dup2 (fdStdInput, STDIN_FILENO); dup2 (fdStdOutput, STDOUT_FILENO); dup2 (fdStdError, STDERR_FILENO); if (environment) { execvpe(args[0], args, environment); } else { execvp(args[0], args); } } _exit(127); } return pid; }
int execs_common(const char *path, const char *args, char *const envp[], char *buf) { int argc=args_fsa(args,NULL,NULL); char *argv[argc+1]; args_fsa(args,argv,buf); if (path) return execve(path, argv, envp); else return execvpe(argv[0], argv, envp); }
int main(int argc, char **argv) { pid_t pid; if ((pid = fork()) == 0) { execvpe("/rootfs/bin/sh", NULL, NULL); } else { waitpid(pid, NULL, 0); } return 1; }
void call_execvpe() { char *const file = "execvpe.c"; char *const compiler = "cc"; char *const argv[] = {"/usr/bin/cc", "-c", file, 0}; char *const envp[] = {"THIS=THAT", 0}; expected_out(file); create_source(file); FORK(execvpe(compiler, argv, envp);) }
static int execute(int argc, char *argv[], char *const envp[], struct program_options *options) { pid_t pid; int status; /* Nothing to execute, just wait an amount of time */ if (!argc) return sleep(options->duration); pid = fork(); if (pid < 0) { perror("fork"); return -1; } if (pid == 0 && execvpe(argv[0], argv, envp)) { /* Forked child */ perror("execvpe"); exit(1); } if (pid) { struct sigaction s = { .sa_handler = sighandler, .sa_flags = SA_RESETHAND, }; sigaddset(&s.sa_mask, SIGALRM); sigaction(SIGALRM, &s, NULL); alarm(options->duration); again: if (waitpid(pid, &status, 0) < 0) { if (errno == EINTR && sigalrm) kill(pid, SIGTERM); goto again; } if (WIFEXITED(status)) { /* * Cancel the timer in case the program * finished before the timeout */ alarm(0); return 0; } if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM) return 0; } return -1; }
static int session (char **env) { char *argv[] = { "cockpit-bridge", NULL }; debug ("executing bridge: %s", argv[0]); if (env) execvpe (argv[0], argv, env); else execvp (argv[0], argv); warn ("can't exec %s", argv[0]); return 127; }
int spawnv(const char *cmd, char *const args[]) { int process = 0; process = fork(); if (process == 0) { execvpe(cmd, args, environ); exit(1); } else { int status; waitpid(process, &status, 0); return status; } }
_WCRTLINK int __F_NAME(execvp,_wexecvp)( const CHAR_TYPE *file, const CHAR_TYPE * const *argv ) { #ifdef __WIDECHAR__ if( _RWD_wenviron == NULL ) __create_wide_environment(); return( _wexecvpe( file, argv, (const CHAR_TYPE **)_RWD_wenviron ) ); #else #ifdef __RDOS__ return( execv( file, argv ) ); #else return( execvpe( file, argv, (const CHAR_TYPE **)_RWD_environ ) ); #endif #endif }
int main(void) { printf ("ExecHelper tests - pid %d\n\n", getpid()); extern char **environ; int ret; printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); printf("Attempting to execute /usr/bin/ristretto via a file descriptor... \n"); int execfd = open("/usr/bin/ristretto", O_RDONLY); char *fargv[] = {"/home/steve/Downloads", "~/Documents/Administratif/CNI", NULL}; ret = fexecve(execfd, fargv, environ); if (ret) printf("Failed: %s\n", strerror(errno)); else printf("Success!\n"); printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); printf("Attempting to execute /usr/bin/vlc with forbidden parameters... \n"); char *argv[] = {"/usr/bin/vlc", "/tmp/test.mp3", "/tmp/test-managed.mp3", "~/Documents/Administratif/Lol", "../../../../tmp/test-managed.mp3", NULL}; ret = execvpe("vlc", argv, environ); if (ret) printf("Failed: %s\n", strerror(errno)); else printf("Success!\n"); printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); printf("Attempting to execute /usr/bin/vlc with only allowed parameters... \n"); char *argv2[] = {"/usr/bin/vlc", "/tmp/test.mp3", "../../foo.mp3", "~/Documents/AuthorisedFiles/Test.mp3", NULL}; ret = execvpe("totem", argv2, environ); if (ret) printf("Failed: %s\n", strerror(errno)); else printf("Success!\n"); return 0; }
static pid_t spawn_with_redir(const char *const *argv, int child_stdin, int child_stdout) { fflush(0); pid_t child = fork(); if (child == -1) fatal_perror("fork"); if (child != 0) return child; /* to the parent */ /* We are the child. The parent has arranged for it to be safe for us to write to stderr under error conditions, but the cleanup handler should not do anything. */ controller_cleanups = false; up_script_cleanups = false; /* Child-side stdin and stdout redirections. */ if (child_stdin != 0) { if (close(0)) fatal_perror("close"); if (child_stdin < 0) { if (open("/dev/null", O_RDONLY) != 0) fatal_perror("open"); } else { if (dup(child_stdin) != 0) fatal_perror("dup"); } } if (child_stdout != 1) { if (close(1)) fatal_perror("close"); if (child_stdout < 1) { if (open("/dev/null", O_WRONLY) != 1) fatal_perror("open"); } else { if (dup(child_stdout) != 1) fatal_perror("dup"); } } become_only_root(); if (sigprocmask(SIG_SETMASK, &child_sigmask, 0)) fatal_perror("sigprocmask"); execvpe(argv[0], (char *const *)argv, (char *const *)child_env); fatal_perror("execvpe"); }
int main(int argc, char *argv[]) { int i, status; unsigned int n; char *cmd; char **args; char *envp[2]; int n_args = argc-2; char ncopia[17]; int **fds; char buffer[1024]; size_t len; if (argc < 3) err("usage: lancian n cmd arg1 arg2 ..."); envp[0] = (char *)ncopia; envp[1] = NULL; n = atoi(argv[1]); args = malloc((n_args+1)*sizeof(char *)); cmd = args[0] = argv[2]; args[n_args] = NULL; for (i = 1; i < n_args; ++i) args[i] = argv[i+2]; fds = malloc(n*sizeof(int *)); for (i = 0; i < n; ++i) { fds[i] = malloc(2*sizeof(int)); pipe(fds[i]); if (fork() == 0) { close(fds[i][0]); dup2(fds[i][1], 1); close(fds[i][1]); sprintf(ncopia, "NCOPIA=%d", i); if (execvpe(cmd, args, envp) == -1) sys_err("execvpe"); } } for (i = 0; i < n; ++i) { close(fds[i][1]); while ((len = read(fds[i][0], buffer, sizeof(buffer))) != 0) write(1, buffer, len); } printf("lancian: fine\n"); exit(0); }
int NAME(const char *path, const char *arg0, ...) { va_list ap, cap; int argc = 1, rv; const char **argv, **argp; const char *arg; char *const *envp; va_start(ap, arg0); va_copy(cap, ap); /* Count the number of arguments */ do { arg = va_arg(cap, const char *); argc++; } while (arg); va_end(cap); /* Allocate memory for the pointer array */ argp = argv = alloca(argc * sizeof(const char *)); if (!argv) { va_end(ap); return -1; } /* Copy the list into an array */ *argp++ = arg0; do { *argp++ = arg = va_arg(ap, const char *); } while (arg); #if EXEC_E /* execle() takes one more argument for the environment pointer */ envp = va_arg(ap, char *const *); #else envp = environ; #endif #if EXEC_P rv = execvpe(path, (char * const *)argv, envp); #else rv = execve(path, (char * const *)argv, envp); #endif va_end(ap); return rv; }
int main(int argc, char* argv[], char* envp[]) { int status, child; strcpy(proc_name, "parent"); init_shared(argv); fprintf(stderr, "Map address is: %p\n", g_shared); if (child = fork()) { parentWork(); waitpid(child, &status, 0); cleanup_shared(); fprintf(stderr, "Parent finished!\n"); } else { /* child executes shmem2 */ execvpe("./mult2Vecs", argv + 2, envp); } }
int execute(sigset_t *pset, char *prog, char *argv[], char *env[]) { child = fork(); if ( child > 1 ) { return child; } else if ( child < 0 ) { printf("fork failed\n"); exit(EXIT_FAILURE); } // We're now in the child so need to exec the requested program sigprocmask(SIG_SETMASK, pset, 0); setsid(); setpgid(0, 0); execvpe(prog, argv, env); printf("execvpe failed\n"); exit(255); }
int main(int argc, char **argv) { char *myenv[] = { "TESTSUCCESS=yes", NULL }; if (getenv("TESTSUCCESS")) { printf("TESTSUCCESS=%s\n", getenv("TESTSUCCESS")); } else { setenv("TESTSUCCESS", "no", 1); execvpe(argv[0], argv, myenv); } exit(EXIT_SUCCESS); }
static int ct_execv(void *a) { struct execv_args *ea = a; sigset_t mask; sigfillset(&mask); sigprocmask(SIG_UNBLOCK, &mask, NULL); /* This gets control in the container's new root (if any) */ if (ea->env) execvpe(ea->path, ea->argv, ea->env); else execvp(ea->path, ea->argv); return -1; }
static void fork_and_execvpe() { int pid = fork(); if (pid != 0) { if (bg_flag != '&') { waitpid(pid, NULL, 0); } else { printf("Added [%d] to BG", pid); } } else { execvpe(prog, execargs, NULL); exit(1); } }
int main (int argc, char* argv[]) { char cmdln[MAX_LEN]; char items[MAX_ITEMS][MAX_LEN]; int nr_items; int i; pid_t pid; int status; char * args[MAX_ITEMS]; while(1) { printf(">"); fgets(cmdln, MAX_LEN, stdin); nr_items = parseitems(cmdln, items); if( strcmp(items[0], "exit")==0 ) { printf("Goodbye!\n"); return 0; } if(nr_items==0) continue; for(i=0; i<MAX_ITEMS; i++) args[i]=NULL; for(i=0 ; i<nr_items; i++) args[i]=items[i]; if((pid=fork())==0) { if(execvpe(items[0], args, environ) <0) { printf("%s: Command not found.\n", items[0]); exit(0); } } else { if(waitpid(pid,&status, 0)<0) { printf("waitpid error\n"); } } } return 0; }