int execle(const char *name, const char *arg, ...) { va_list ap; char **argv, **envp; int n; va_start(ap, arg); n = 1; while (va_arg(ap, char *) != NULL) n++; va_end(ap); argv = alloca((n + 1) * sizeof(*argv)); if (argv == NULL) { errno = ENOMEM; return (-1); } va_start(ap, arg); n = 1; argv[0] = (char *)arg; while ((argv[n] = va_arg(ap, char *)) != NULL) n++; envp = va_arg(ap, char **); va_end(ap); return (_execve(name, argv, envp)); }
int system(const char *str) { int pid, exitstatus, waitval; int i; if ((pid = _fork()) < 0) return str ? -1 : 0; if (pid == 0) { for (i = 3; i <= 20; i++) _close(i); if (!str) str = "cd ."; /* just testing for a shell */ exec_tab[2] = str; /* fill in command */ _execve("/bin/sh", exec_tab, *_penviron); /* get here if execve fails ... */ _exit(FAIL); /* see manual page */ } while ((waitval = _wait(&exitstatus)) != pid) { if (waitval == -1) break; } if (waitval == -1) { /* no child ??? or maybe interrupted ??? */ exitstatus = -1; } if (!str) { if (exitstatus == FAIL << 8) /* execve() failed */ exitstatus = 0; else exitstatus = 1; /* /bin/sh exists */ } return exitstatus; }
/* We put this here so that we can blow the start.c code away on exec eventually, but still manage to get the panic() to happen if it fails */ void exec_or_die(void) { kputs("Starting /init\n"); platform_discard(); _execve(); panic(PANIC_NOINIT); /* BIG Trouble if we Get Here!! */ }
static int execve32_wrapper (int (*_execve) (const char *path, char *const argv[], char *const envp[]), char *path, char *const argv[], char *const envp[]) { char *custom_loader = NULL; char **new_argv; int i, num_elements, result; custom_loader = redirect_path ("/lib/ld-linux.so.2"); if (strcmp (custom_loader, "/lib/ld-linux.so.2") == 0) { free (custom_loader); return 0; } // envp is already adjusted for our needs. But we need to shift argv for (num_elements = 0; argv && argv[num_elements]; num_elements++) { // this space intentionally left blank } new_argv = malloc (sizeof (char *) * (num_elements + 2)); new_argv[0] = path; for (i = 0; i < num_elements; i++) { new_argv[i + 1] = argv[i]; } new_argv[num_elements + 1] = 0; // Now actually run execve with our loader and adjusted argv result = _execve (custom_loader, new_argv, envp); // Cleanup on error free (new_argv); free (custom_loader); return result; }
void DynamicResponseState::execveCgiProgram(){ char *emptylist[]={NULL}; if(_fork()==0){ setenv("QUERY_STRING",cgiArgs.c_str(),1); _dup2(getFileDescriptor(),STDOUT_FILENO); _execve(getFileName().c_str(),emptylist,environ); } _wait(NULL); }
static int execve_wrapper (const char *func, const char *path, char *const argv[], char *const envp[]) { int (*_execve) (const char *path, char *const argv[], char *const envp[]); char *new_path = NULL; char **new_envp = NULL; int i, result; _execve = (int (*)(const char *path, char *const argv[], char *const envp[])) dlsym (RTLD_NEXT, func); new_path = redirect_path (path); // Make sure we inject our original preload values, can't trust this // program to pass them along in envp for us. new_envp = execve_copy_envp (envp); result = _execve (new_path, argv, new_envp); if (result == -1 && errno == ENOENT) { // OK, get prepared for gross hacks here. In order to run 32-bit ELF // executables -- which will hardcode /lib/ld-linux.so.2 as their ld.so // loader, we must redirect that check to our own version of ld-linux.so.2. // But that lookup is done behind the scenes by execve, so we can't // intercept it like normal. Instead, we'll prefix the command by the // ld.so loader which will only work if the architecture matches. So if // we failed to run it normally above because the loader couldn't find // something, try with our own 32-bit loader. int (*_access) (const char *pathname, int mode); _access = (int (*)(const char *pathname, int mode)) dlsym (RTLD_NEXT, "access"); if (_access (new_path, F_OK) == 0) { // Only actually try this if the path actually did exist. That // means the ENOENT must have been a missing linked library or the // wrong ld.so loader. Lets assume the latter and try to run as // a 32-bit executable. result = execve32_wrapper (_execve, new_path, argv, new_envp); } } free (new_path); for (i = 0; new_envp[i]; i++) { free (new_envp[i]); } free (new_envp); return result; }
int system(char *cmd) { int pid, rc; char *argv[4]; if ((pid = _fork()) == -1) { return -1; } else if (pid) { _wait(&rc); return cmd? rc: !rc; } else { argv[0] = "/bin/sh"; argv[1] = "-c"; argv[2] = cmd? cmd: "exit"; argv[3] = NULL; _execve(argv[0], argv, environ); exit(cmd? -1: 0); } }
static void _exec_child (const char *filename, const char *cmdline) { _execve(filename, cmdline); _exit(-1); }
int execve (const char *path, char * const argv[], char * const envp[]) { return _execve (path, argv, envp); }
int execv(const char *name, char * const *argv) { _execve(name, argv, environ); return (-1); }
/* We put this here so that we can blow the start.c code away on exec eventually, but still manage to get the panic() to happen if it fails */ void exec_or_die(void) { kputs("Starting /init\n"); _execve(); panic("no /init"); /* BIG Trouble if we Get Here!! */ }
int Main( int argc, char argv[MAXARG][MAXARGLEN] ) { int k; int n; char* p; int status; char* argp[MAXARG+1]; char buffer[8192]; char rootdir[MAX_PATH]; char our_cmd[MAX_PATH]; char console[MAX_PATH]; char unix_rootdir[MAX_PATH]; // Get the Staden Package directory if( !::GetModuleFileName(0,rootdir,MAX_PATH) ) { ::MessageBox(0,"Unable to get Staden Package directory!", "SPRUN.EXE Message", MB_OK ); return -3; } _strlwr( rootdir ); p = strstr( rootdir, "\\sprun.exe" ); *p = 0; // Create a copy of root directory in unix format strcpy( unix_rootdir, rootdir ); p = unix_rootdir; while( *p ) { if( *p == '\\' ) *p = '/'; p++; } #ifdef RUNTIME_FONT_INSTALL /* Install the pregap font if it isn't already installed */ if( IsFontInstalled("Pregap") == false ) { //MessageBox(0,"Installing pregap.ttf","Debug",MB_OK); strcpy( buffer, rootdir ); strcat( buffer, "\\windows-bin\\pregap.ttf" ); ::GetShortPathName( buffer, buffer, MAX_PATH ); _strlwr( buffer ); if( !AddFontResource(buffer) ) ::MessageBox(0,"Warning: Failed to install font pregap.ttf.","SPRUN.EXE Message", MB_OK ); else ::SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 ); } #endif // Add Staden Package environment variables to current environment sprintf( buffer, "TK_LIBRARY=%s/lib/tk", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "TCL_LIBRARY=%s/lib/tcl", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADLIB=%s/lib", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADTABL=%s/tables", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADENROOT=%s", unix_rootdir ); _putenv( buffer ); _putenv( "MACHINE=windows" ); // Prepend our paths to PATH environment variable. Normally this is done // automatically by the App Path registry entry for sprun.exe, however if // we want to run more a remote copy of gap4 then the App Path will be // invalid, so we need to do it here again just to be sure we pick up the // appropriate dlls. sprintf( buffer, "PATH=%s\\windows-bin;%s\\lib\\windows-binaries;", rootdir, rootdir ); p = getenv( "PATH" ); assert(p); strcat( buffer, p ); _putenv( buffer ); // Set default command to be winstash, don't use quotes otherwise execve // won't work properly, but quotes are required in argp[0]! strcpy( our_cmd, rootdir ); strcat( our_cmd, "\\windows-bin\\wish.exe" ); // Get command console p = getenv( "COMSPEC" ); if(!p) { // Assume windows NT ::GetWindowsDirectory( console, MAX_PATH ); strcat( console, "\\system32\\cmd.exe" ); } else { // Console is specified by environment strcpy( console, p ); } // Construct suitable argp array for execve k = 1; n = 1; while( n<argc ) { // -c option runs the console with the staden environment intact // on windows NT platforms only. Command.com on win9x is crippled // in that it ignores the environment given to execve() if( (argv[n][0]=='-') && (argv[n][1]=='c') && !argv[n][2] ) strcpy( our_cmd, console ); else { argp[n] = &argv[n][0]; k++; } n++; } argp[k] = 0; assert(k<(MAXARG+1)); // Assemble argv[0], we must put quotes around this strcpy( buffer, "\"" ); strcat( buffer, our_cmd ); strcat( buffer, "\"" ); argp[0] = buffer; // Execute the program status = _execve( our_cmd, argp, _environ ); if( status == -1 ) { sprintf( buffer, "Sorry: Unable to execute %s - (%d).", our_cmd, errno ); ::MessageBox(0,buffer,"SPRUN.EXE Message", MB_OK ); } return 0; }
static pid_t runve(int mode, const char* path, char* const* argv, char* const* envv) { register char* s; register char** p; register char** v; void* m1; void* m2; pid_t pid; int oerrno; int ux; int n; #if defined(_P_DETACH) && defined(_P_NOWAIT) int pgrp; #endif #if CONVERT char* d; char* t; int m; #endif struct stat st; char buf[PATH_MAX]; char tmp[PATH_MAX]; #if DEBUG static int trace; #endif #if defined(_P_DETACH) && defined(_P_NOWAIT) if (mode == _P_DETACH) { /* * 2004-02-29 cygwin _P_DETACH is useless: * spawn*() returns 0 instead of the spawned pid * spawned { pgid sid } are the same as the parent */ mode = _P_NOWAIT; pgrp = 1; } else pgrp = 0; #endif if (!envv) envv = (char* const*)environ; m1 = m2 = 0; oerrno = errno; #if DEBUG if (!trace) trace = (s = getenv("_AST_exec_trace")) ? *s : 'n'; #endif if (execrate(path, buf, sizeof(buf), 0)) { if (!_stat(buf, &st)) path = (const char*)buf; else errno = oerrno; } if (path != (const char*)buf && _stat(path, &st)) return -1; if (!S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) { errno = EACCES; return -1; } if (magic(path, &ux)) { #if _CYGWIN_fork_works errno = ENOEXEC; return -1; #else ux = 1; p = (char**)argv; while (*p++); if (!(v = (char**)malloc((p - (char**)argv + 2) * sizeof(char*)))) { errno = EAGAIN; return -1; } m1 = v; p = v; *p++ = (char*)path; *p++ = (char*)path; path = (const char*)pathshell(); if (*argv) argv++; while (*p++ = (char*)*argv++); argv = (char* const*)v; #endif } /* * the win32 dll search order is * (1) the directory of path * (2) . * (3) /c/(WINNT|WINDOWS)/system32 /c/(WINNT|WINDOWS) * (4) the directories on $PATH * there are no cygwin dlls in (3), so if (1) and (2) fail * to produce the required dlls its up to (4) * * the standard allows PATH to be anything once the path * to an executable is determined; this code ensures that PATH * contains /bin so that at least the cygwin dll, required * by all cygwin executables, will be found */ if (p = (char**)envv) { n = 1; while (s = *p++) if (strneq(s, "PATH=", 5)) { s += 5; do { s = pathcat(tmp, s, ':', NiL, ""); if (streq(tmp, "/usr/bin/") || streq(tmp, "/bin/")) { n = 0; break; } } while (s); if (n) { n = 0; snprintf(tmp, sizeof(tmp), "%s:/bin", *(p - 1)); *(p - 1) = tmp; } break; } if (n) { n = p - (char**)envv + 1; p = (char**)envv; if (v = (char**)malloc(n * sizeof(char*))) { m2 = v; envv = (char* const*)v; *v++ = strcpy(tmp, "PATH=/bin"); while (*v++ = *p++); } } #if CONVERT if (!ux && (d = getenv(convertvars[0]))) for (p = (char**)envv; s = *p; p++) if ((n = convert(d, s)) && (m = cygwin_posix_to_win32_path_list_buf_size(s + n)) > 0) { if (!(t = malloc(n + m + 1))) break; *p = t; memcpy(t, s, n); cygwin_posix_to_win32_path_list(s + n, t + n); } #endif } #if DEBUG if (trace == 'a' || trace == 'e') { sfprintf(sfstderr, "%s %s [", mode == _P_OVERLAY ? "_execve" : "_spawnve", path); for (n = 0; argv[n]; n++) sfprintf(sfstderr, " '%s'", argv[n]); if (trace == 'e') { sfprintf(sfstderr, " ] ["); for (n = 0; envv[n]; n++) sfprintf(sfstderr, " '%s'", envv[n]); } sfprintf(sfstderr, " ]\n"); sfsync(sfstderr); } #endif #if _lib_spawn_mode if (mode != _P_OVERLAY) { pid = _spawnve(mode, path, argv, envv); #if defined(_P_DETACH) && defined(_P_NOWAIT) if (pid > 0 && pgrp) setpgid(pid, 0); #endif } else #endif { #if defined(_P_DETACH) && defined(_P_NOWAIT) if (pgrp) setpgid(0, 0); #endif pid = _execve(path, argv, envv); } if (m1) free(m1); if (m2) free(m2); return pid; }
int _execve(char *name, char **argv, char **env) { return _execve(name, argv, env); }
pid_t ProcessStart( process_t p ) { char * const env[] = { "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",NULL } ; if( pipe( p->fd_0 ) == -1 ){ return -1 ; } if( pipe( p->fd_1 ) == -1 ){ return -1 ; } if( pipe( p->fd_2 ) == -1 ){ return -1 ; } p->pid = fork() ; if( p->pid == -1 ){ return -1 ; } if( p->pid == 0 ){ if( p->str.user_id != ( uid_t )-1 ){ /* * drop privileges permanently */ _ignore_result( seteuid( 0 ) ) ; _ignore_result( setgid( p->str.user_id ) ) ; _ignore_result( setgroups( 1,&p->str.user_id ) ) ; _ignore_result( setegid( p->str.user_id ) ) ; _ignore_result( setuid( p->str.user_id ) ) ; } dup2( p->fd_0[ 1 ],0 ) ; dup2( p->fd_1[ 1 ],1 ) ; dup2( p->fd_2[ 1 ],2 ) ; close( p->fd_1[ 0 ] ) ; close( p->fd_0[ 0 ] ) ; close( p->fd_2[ 0 ] ) ; if( p->str.priority != 0 ){ setpriority( PRIO_PROCESS,0,p->str.priority ) ; } if( p->str.env == NULL || p->str.env[ 0 ] == NULL ){ if( environ[ 0 ] == NULL ){ _execve( p,env ) ; }else{ _execve( p,environ ) ; } }else{ _execve( p,p->str.env ) ; } /* * execv has failed :-( */ _Exit( 1 ) ; /* * child process block ends here */ } /* * parent process continues from here */ close( p->fd_0[ 0 ] ) ; close( p->fd_1[ 1 ] ) ; close( p->fd_2[ 1 ] ) ; p->state = ProcessIsStillRunning ; if( p->str.timeout != -1 ){ __ProcessStartTimer( p ) ; } return p->pid ; }
int Main( int argc, char argv[MAXARG][MAXARGLEN] ) { int k; int n; char* p; int status; char* argp[MAXARG+1]; char buffer[8192]; char rootdir[MAX_PATH]; char our_cmd[MAX_PATH]; char console[MAX_PATH]; char unix_rootdir[MAX_PATH]; char basename[MAX_PATH]; char arg1[MAX_PATH]; // Get the location of the executable name if( !::GetModuleFileName(0,rootdir,MAX_PATH) ) { ::MessageBox(0,"Unable to get Staden Package directory!", "SPRUN.EXE Message", MB_OK ); return -3; } _strlwr( rootdir ); // lowercase // Lop off the .exe and the bin to get the Staden Package // root directory. n = strlen(rootdir)-1; while (n && rootdir[n] != '\\' && rootdir[n] != '/') n--; strcpy(basename, &rootdir[n+1]); if (p = strstr(basename, ".exe")) *p = 0; if (n) n--; while (n && rootdir[n] != '\\' & rootdir[n] != '/') n--; rootdir[n] = 0; // Create a copy of root directory in unix format (fwd slashes) strcpy( unix_rootdir, rootdir ); p = unix_rootdir; while( *p ) { if( *p == '\\' ) *p = '/'; p++; } // Add Staden Package environment variables to current environment sprintf( buffer, "TK_LIBRARY=%s/lib/tk8.4", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "TCL_LIBRARY=%s/lib/tcl8.4", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADLIB=%s/lib/staden", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADTCL=%s/share/staden/tcl", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADTABL=%s/share/staden/etc", unix_rootdir ); _putenv( buffer ); sprintf( buffer, "STADENROOT=%s", unix_rootdir ); _putenv( buffer ); // _putenv( "MACHINE=windows" ); // Prepend our paths to PATH environment variable. Normally this is done // automatically by the App Path registry entry for sprun.exe, however if // we want to run more a remote copy of gap4 then the App Path will be // invalid, so we need to do it here again just to be sure we pick up the // appropriate dlls. sprintf( buffer, "PATH=%s\\bin;%s/lib/staden;%s/lib;", rootdir, rootdir, rootdir ); p = getenv( "PATH" ); assert(p); strcat( buffer, p ); _putenv( buffer ); // Set default command to be winstash, don't use quotes otherwise execve // won't work properly, but quotes are required in argp[0]! strcpy( our_cmd, rootdir ); strcat( our_cmd, "\\bin\\wish86.exe" ); // Get command console p = getenv( "COMSPEC" ); if(!p) { // Assume windows NT ::GetWindowsDirectory( console, MAX_PATH ); strcat( console, "\\system32\\cmd.exe" ); } else { // Console is specified by environment strcpy( console, p ); } // Construct suitable argp array for execve k = 1; n = 1; // -console option runs the console with the staden environment intact // on windows NT platforms only. Command.com on win9x is crippled // in that it ignores the environment given to execve() if (n < argc && strcmp(argv[n], "-console") == 0) { strcpy(our_cmd, console); n++; } if (strcmp(basename, "sprun") == 0) { // Just wish by itself argp[1] = 0; } else { // Otherwise start with a tcl file based on argv[0] and append // all other arguments sprintf(arg1, "\"%s\\share\\staden\\tcl\\%s\\%s.tcl\"", rootdir, basename, basename); argp[k++] = arg1; while( n<argc ) argp[k++] = &argv[n++][0]; argp[k] = 0; assert(k<(MAXARG+1)); } // Assemble argv[0], we must put quotes around this strcpy( buffer, "\"" ); strcat( buffer, our_cmd ); strcat( buffer, "\"" ); argp[0] = buffer; for (n = 0; argp[n]; n++) { printf("'%s' ", argp[n]); } printf("\n"); // Execute the program status = _execve( our_cmd, argp, _environ ); if( status == -1 ) { sprintf( buffer, "Sorry: Unable to execute %s - (%d).", our_cmd, errno ); ::MessageBox(0,buffer,"SPRUN.EXE Message", MB_OK ); } return 0; }