static void compute_program_name_loc(const char* orig_argv0, const char** name, const char** loc) { char* argv0 = strdup(orig_argv0); char* lastslash = strrchr(argv0, '/'); if (lastslash == NULL) { *name = argv0; *loc = findProgramPath(orig_argv0); } else { *lastslash = '\0'; *name = lastslash+1; *loc = findProgramPath(orig_argv0); } chplBinaryName = *name; }
static void printStuff(const char* argv0) { bool shouldExit = false; bool printedSomething = false; if (fPrintVersion) { fprintf(stdout, "%s Version %s\n", sArgState.program_name, compileVersion); fPrintCopyright = true; printedSomething = true; shouldExit = true; } if (fPrintLicense) { fprintf(stdout, #include "LICENSE" ); fPrintCopyright = false; shouldExit = true; printedSomething = true; } if (fPrintCopyright) { fprintf(stdout, #include "COPYRIGHT" ); printedSomething = true; } if( fPrintChplHome ) { char* guess = findProgramPath(argv0); printf("%s\t%s\n", CHPL_HOME, guess); free(guess); printedSomething = true; } if (fPrintHelp || (!printedSomething && sArgState.nfile_arguments < 1)) { if (printedSomething) printf("\n"); usage(&sArgState, !fPrintHelp, fPrintEnvHelp, fPrintSettingsHelp); shouldExit = true; printedSomething = true; } if (printedSomething && sArgState.nfile_arguments < 1) { shouldExit = true; } if (shouldExit) { clean_exit(0); } }
static void execCommand(poptContext con) { const char ** argv; int pos = 0; const char * script = con->doExec->script; argv = malloc(sizeof(*argv) * (6 + con->numLeftovers + con->finalArgvCount)); if (!con->execAbsolute && strchr(script, '/')) return; if (!strchr(script, '/') && con->execPath) { char *s = alloca(strlen(con->execPath) + strlen(script) + 2); sprintf(s, "%s/%s", con->execPath, script); argv[pos] = s; } else { argv[pos] = script; } pos++; argv[pos] = findProgramPath(con->os->argv[0]); if (argv[pos]) pos++; argv[pos++] = ";"; memcpy(argv + pos, con->finalArgv, sizeof(*argv) * con->finalArgvCount); pos += con->finalArgvCount; if (con->numLeftovers) { argv[pos++] = "--"; memcpy(argv + pos, con->leftovers, sizeof(*argv) * con->numLeftovers); pos += con->numLeftovers; } argv[pos++] = NULL; #ifdef __hpux setresuid(getuid(), getuid(),-1); #else /* * XXX " ... on BSD systems setuid() should be preferred over setreuid()" * XXX sez' Timur Bakeyev <*****@*****.**> * XXX from Norbert Warmuth <*****@*****.**> */ #if defined(HAVE_SETUID) setuid(getuid()); #elif defined (HAVE_SETREUID) setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */ #else ; /* Can't drop privileges */ #endif #endif execvp(argv[0], (char *const *)argv); }
static void setupChplHome(const char* argv0) { const char* chpl_home = getenv("CHPL_HOME"); char* guess = NULL; // Get the executable path. guess = findProgramPath(argv0); if (guess) { // Determine CHPL_HOME based on the exe path. // Determined exe path, but don't have a env var set // Look for ../../../util/chplenv // Remove the /bin/some-platform/chpl part // from the path. if( guess[0] ) { int j = strlen(guess) - 5; // /bin and '\0' for( ; j >= 0; j-- ) { if( guess[j] == '/' && guess[j+1] == 'b' && guess[j+2] == 'i' && guess[j+3] == 'n' ) { guess[j] = '\0'; break; } } } if( isMaybeChplHome(guess) ) { // OK! } else { // Maybe we are in e.g. /usr/bin. free(guess); guess = NULL; } } if( chpl_home ) { if( strlen(chpl_home) > FILENAME_MAX ) USR_FATAL("$CHPL_HOME=%s path too long", chpl_home); if( guess == NULL ) { // Could not find exe path, but have a env var set strncpy(CHPL_HOME, chpl_home, FILENAME_MAX); } else { // We have env var and found exe path. // Check that they match and emit a warning if not. if( ! isSameFile(chpl_home, guess) ) { // Not the same. Emit warning. USR_WARN("$CHPL_HOME=%s mismatched with executable home=%s", chpl_home, guess); } // Since we have an enviro var, always use that. strncpy(CHPL_HOME, chpl_home, FILENAME_MAX); } } else { if( guess == NULL ) { // Could not find enviro var, and could not // guess at exe's path name. USR_FATAL("$CHPL_HOME must be set to run chpl"); } else { int rc; if( strlen(guess) > FILENAME_MAX ) USR_FATAL("chpl guessed home %s too long", guess); // Determined exe path, but don't have a env var set strncpy(CHPL_HOME, guess, FILENAME_MAX); // Also need to setenv in this case. rc = setenv("CHPL_HOME", guess, 0); if( rc ) USR_FATAL("Could not setenv CHPL_HOME"); } } // Check that the resulting path is a Chapel distribution. if( ! isMaybeChplHome(CHPL_HOME) ) { // Bad enviro var. USR_WARN("CHPL_HOME=%s is not a Chapel distribution", CHPL_HOME); } if( guess ) free(guess); parseCmdLineConfig("CHPL_HOME", astr("\"", CHPL_HOME, "\"")); }
/*@-bounds -boundswrite @*/ static int execCommand(poptContext con) /*@globals internalState @*/ /*@modifies internalState @*/ { poptItem item = con->doExec; const char ** argv; int argc = 0; int rc; if (item == NULL) /*XXX can't happen*/ return POPT_ERROR_NOARG; if (item->argv == NULL || item->argc < 1 || (!con->execAbsolute && strchr(item->argv[0], '/'))) return POPT_ERROR_NOARG; argv = (const char **)malloc( sizeof(*argv) * (6 + item->argc + con->numLeftovers + con->finalArgvCount)); if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */ if (!strchr(item->argv[0], '/') && con->execPath) { char *s = (char *)alloca( strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/")); sprintf(s, "%s/%s", con->execPath, item->argv[0]); argv[argc] = s; } else { argv[argc] = findProgramPath(item->argv[0]); } if (argv[argc++] == NULL) return POPT_ERROR_NOARG; if (item->argc > 1) { memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1)); argc += (item->argc - 1); } if (con->finalArgv != NULL && con->finalArgvCount > 0) { memcpy(argv + argc, con->finalArgv, sizeof(*argv) * con->finalArgvCount); argc += con->finalArgvCount; } if (con->leftovers != NULL && con->numLeftovers > 0) { #if 0 argv[argc++] = "--"; #endif memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers); argc += con->numLeftovers; } argv[argc] = NULL; #ifdef __hpux rc = setresuid(getuid(), getuid(),-1); if (rc) return POPT_ERROR_ERRNO; #else /* * XXX " ... on BSD systems setuid() should be preferred over setreuid()" * XXX sez' Timur Bakeyev <*****@*****.**> * XXX from Norbert Warmuth <*****@*****.**> */ #if defined(HAVE_SETUID) rc = setuid(getuid()); if (rc) return POPT_ERROR_ERRNO; #elif defined (HAVE_SETREUID) rc = setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */ if (rc) return POPT_ERROR_ERRNO; #else ; /* Can't drop privileges */ #endif #endif if (argv[0] == NULL) return POPT_ERROR_NOARG; #ifdef MYDEBUG if (_popt_debug) { const char ** avp; fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc); for (avp = argv; *avp; avp++) fprintf(stderr, " '%s'", *avp); fprintf(stderr, "\n"); } #endif rc = execvp(argv[0], (char *const *)argv); /* notreached */ if (rc) { return POPT_ERROR_ERRNO; } return 0; }