2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-07-06 11:04:46 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-07-06 11:04:46 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2006-07-06 11:04:46 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-07-06 11:04:46 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2006-07-06 11:04:46 +00:00
|
|
|
|
2006-06-05 04:30:16 +00:00
|
|
|
// switcher.C
|
|
|
|
//
|
|
|
|
// When run as
|
2008-09-03 00:21:25 +00:00
|
|
|
// switcher Full-Path Executable-Name X1 ... Xn
|
|
|
|
// runs program at Full-Path with args X1. ... Xn
|
2011-01-06 23:09:13 +00:00
|
|
|
// note that the executable name nust be specified twice:
|
2008-09-03 00:21:25 +00:00
|
|
|
// once as part of the Full_Path and again as just the name
|
2006-06-05 04:30:16 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2006-07-01 05:32:02 +00:00
|
|
|
#include <cerrno>
|
2011-01-06 23:09:13 +00:00
|
|
|
#include <pwd.h> // getpwuid
|
2007-10-02 09:31:20 +00:00
|
|
|
#include <grp.h>
|
2006-06-05 04:30:16 +00:00
|
|
|
|
2010-01-12 11:34:15 +00:00
|
|
|
#include "app_ipc.h"
|
2011-03-24 09:32:02 +00:00
|
|
|
#include "filesys.h"
|
2010-01-12 11:34:15 +00:00
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
using std::strcpy;
|
|
|
|
|
2011-11-03 19:19:36 +00:00
|
|
|
int main(int /*argc*/, char** argv) {
|
2010-01-12 11:34:15 +00:00
|
|
|
passwd *pw;
|
|
|
|
group *grp;
|
|
|
|
char user_name[256], group_name[256];
|
|
|
|
APP_INIT_DATA aid;
|
|
|
|
FILE *f;
|
|
|
|
int retval = -1;
|
|
|
|
char libpath[8192];
|
|
|
|
char newlibs[256];
|
|
|
|
char *projectDirName;
|
2007-10-02 09:31:20 +00:00
|
|
|
|
2007-10-02 23:29:28 +00:00
|
|
|
strcpy(user_name, "boinc_project");
|
|
|
|
strcpy(group_name, "boinc_project");
|
2006-06-17 11:51:48 +00:00
|
|
|
|
2006-07-06 11:04:46 +00:00
|
|
|
#if 0 // For debugging only
|
2011-01-06 23:09:13 +00:00
|
|
|
char current_dir[MAXPATHLEN];
|
2006-07-06 11:04:46 +00:00
|
|
|
|
|
|
|
getcwd( current_dir, sizeof(current_dir));
|
|
|
|
fprintf(stderr, "current directory = %s\n", current_dir);
|
2011-01-06 23:09:13 +00:00
|
|
|
|
2006-06-17 11:51:48 +00:00
|
|
|
for (int i=0; i<argc; i++) {
|
|
|
|
fprintf(stderr, "switcher arg %d: %s\n", i, argv[i]);
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
|
2007-10-02 09:31:20 +00:00
|
|
|
#if 0 // For debugging only
|
|
|
|
// Allow debugging without running as user or group boinc_project
|
|
|
|
pw = getpwuid(getuid());
|
2007-10-02 23:29:28 +00:00
|
|
|
if (pw) strcpy(user_name, pw->pw_name);
|
2007-10-02 09:31:20 +00:00
|
|
|
grp = getgrgid(getgid());
|
2007-10-02 23:29:28 +00:00
|
|
|
if (grp) strcpy(group_name, grp->gr_gid);
|
2007-10-02 09:31:20 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 23:09:13 +00:00
|
|
|
// We are running setuid root, so setgid() sets real group ID,
|
2007-10-02 09:31:20 +00:00
|
|
|
// effective group ID and saved set_group-ID for this process
|
|
|
|
grp = getgrnam(group_name);
|
|
|
|
if (grp) setgid(grp->gr_gid);
|
|
|
|
|
2011-01-06 23:09:13 +00:00
|
|
|
// We are running setuid root, so setuid() sets real user ID,
|
2007-10-02 09:31:20 +00:00
|
|
|
// effective user ID and saved set_user-ID for this process
|
|
|
|
pw = getpwnam(user_name);
|
|
|
|
if (pw) setuid(pw->pw_uid);
|
|
|
|
|
2010-01-12 11:34:15 +00:00
|
|
|
// For unknown reasons, the LD_LIBRARY_PATH and DYLD_LIBRARY_PATH
|
2011-01-06 23:09:13 +00:00
|
|
|
// environment variables are not passed in to switcher, though all
|
|
|
|
// other environment variables do get propagated. So we recreate
|
2010-01-12 11:34:15 +00:00
|
|
|
// LD_LIBRARY_PATH and DYLD_LIBRARY_PATH here.
|
|
|
|
f = fopen(INIT_DATA_FILE, "r");
|
|
|
|
if (f) {
|
|
|
|
retval = parse_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
// Get project name without leading path
|
|
|
|
projectDirName = strrchr(aid.project_dir, '/');
|
|
|
|
if (projectDirName) {
|
|
|
|
++projectDirName;
|
|
|
|
} else {
|
|
|
|
projectDirName = aid.project_dir;
|
2011-01-06 23:09:13 +00:00
|
|
|
}
|
2010-01-12 11:34:15 +00:00
|
|
|
sprintf(newlibs, "../../%s:.:../..", projectDirName);
|
|
|
|
#ifdef __APPLE__
|
|
|
|
strcat(newlibs, ":/usr/local/cuda/lib/");
|
|
|
|
#endif
|
|
|
|
char* p = getenv("LD_LIBRARY_PATH");
|
|
|
|
if (p) {
|
|
|
|
sprintf(libpath, "%s:%s", newlibs, p);
|
|
|
|
} else {
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(libpath, newlibs);
|
2010-01-12 11:34:15 +00:00
|
|
|
}
|
|
|
|
setenv("LD_LIBRARY_PATH", libpath, 1);
|
|
|
|
|
|
|
|
// On the Mac, do the same for DYLD_LIBRARY_PATH
|
|
|
|
//
|
|
|
|
#ifdef __APPLE__
|
|
|
|
p = getenv("DYLD_LIBRARY_PATH");
|
|
|
|
if (p) {
|
|
|
|
sprintf(libpath, "%s:%s", newlibs, p);
|
|
|
|
} else {
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(libpath, newlibs);
|
2010-01-12 11:34:15 +00:00
|
|
|
}
|
|
|
|
setenv("DYLD_LIBRARY_PATH", libpath, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-17 11:51:48 +00:00
|
|
|
execv(argv[1], argv+2);
|
2011-01-06 23:09:13 +00:00
|
|
|
|
2006-07-01 05:32:02 +00:00
|
|
|
// If we got here execv failed
|
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", argv[1], errno);
|
|
|
|
|
2006-06-05 04:30:16 +00:00
|
|
|
}
|