2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2007-10-03 06:16:58 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2007-10-03 06:16:58 +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.
|
2007-10-03 06:16:58 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2007-10-03 06:16:58 +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/>.
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
// gfx_switcher.C
|
|
|
|
//
|
|
|
|
// Used by screensaver to:
|
|
|
|
// - launch graphics application at given slot number as user & owner boinc_project
|
2009-04-15 08:28:51 +00:00
|
|
|
// - launch default graphics application as user & owner boinc_project
|
2009-04-02 10:25:16 +00:00
|
|
|
// - kill graphics application with given process ID as user & owner boinc_project
|
|
|
|
//
|
2007-10-03 06:16:58 +00:00
|
|
|
#include <unistd.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2007-10-03 06:16:58 +00:00
|
|
|
#include <cerrno>
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_PARAM_H
|
2007-10-03 06:16:58 +00:00
|
|
|
#include <sys/param.h> // for MAXPATHLEN
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
2007-10-03 06:16:58 +00:00
|
|
|
#include <pwd.h> // getpwuid
|
|
|
|
#include <grp.h>
|
2012-02-15 01:33:51 +00:00
|
|
|
#include <signal.h> // For kill()
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "common_defs.h"
|
|
|
|
|
|
|
|
#define CREATE_LOG 0
|
|
|
|
|
2007-10-25 09:34:16 +00:00
|
|
|
#if CREATE_LOG
|
2007-10-03 06:16:58 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
static void print_to_log_file(const char *format, ...);
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strip_cr(char *buf);
|
|
|
|
#endif
|
2007-10-25 09:34:16 +00:00
|
|
|
#endif // if CREATE_LOG
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
passwd *pw;
|
|
|
|
group *grp;
|
|
|
|
char user_name[256], group_name[256];
|
|
|
|
char gfx_app_path[MAXPATHLEN], resolved_path[MAXPATHLEN];
|
|
|
|
char *BOINCDatSlotsPath = "/Library/Application Support/BOINC Data/slots/";
|
|
|
|
int retval;
|
|
|
|
int pid;
|
|
|
|
|
2009-04-02 01:48:44 +00:00
|
|
|
if (argc < 2) return EINVAL;
|
|
|
|
|
2009-04-15 08:28:51 +00:00
|
|
|
strlcpy(user_name, "boinc_project", sizeof(user_name));
|
2009-04-02 01:48:44 +00:00
|
|
|
strlcpy(group_name, user_name, sizeof(group_name));
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
#if 0 // For debugging only
|
|
|
|
// Allow debugging without running as user or group boinc_project
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if (pw) strlcpy(user_name, pw->pw_name, sizeof(user_name));
|
|
|
|
grp = getgrgid(getgid());
|
|
|
|
if (grp) strlcpy(group_name, grp->gr_gid, sizeof(group_name));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We are running setuid root, so setgid() sets real group ID,
|
|
|
|
// effective group ID and saved set_group-ID for this process
|
|
|
|
grp = getgrnam(group_name);
|
|
|
|
if (grp) setgid(grp->gr_gid);
|
|
|
|
|
|
|
|
// We are running setuid root, so setuid() sets real user ID,
|
|
|
|
// effective user ID and saved set_user-ID for this process
|
|
|
|
pw = getpwnam(user_name);
|
|
|
|
if (pw) setuid(pw->pw_uid);
|
|
|
|
|
2007-10-12 05:10:52 +00:00
|
|
|
// NOTE: call print_to_log_file only after switching user and group
|
|
|
|
#if 0 // For debugging only
|
|
|
|
char current_dir[MAXPATHLEN];
|
|
|
|
|
|
|
|
getcwd( current_dir, sizeof(current_dir));
|
|
|
|
print_to_log_file( "current directory = %s", current_dir);
|
|
|
|
|
|
|
|
for (int i=0; i<argc; i++) {
|
2009-04-02 10:25:16 +00:00
|
|
|
print_to_log_file("switcher arg %d: %s", i, argv[i]);
|
2007-10-12 05:10:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-02 01:48:44 +00:00
|
|
|
if (strcmp(argv[1], "-default_gfx") == 0) {
|
|
|
|
strlcpy(resolved_path, "/Library/Application Support/BOINC Data/boincscr", sizeof(resolved_path));
|
|
|
|
argv[2] = resolved_path;
|
|
|
|
|
|
|
|
#if 0 // For debugging only
|
|
|
|
for (int i=2; i<argc; i++) {
|
2009-04-02 10:25:16 +00:00
|
|
|
print_to_log_file("calling execv with arg %d: %s", i-2, argv[i]);
|
2009-04-02 01:48:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For unknown reasons, the graphics application exits with
|
|
|
|
// "RegisterProcess failed (error = -50)" unless we pass its
|
|
|
|
// full path twice in the argument list to execv.
|
|
|
|
execv(resolved_path, argv+2);
|
|
|
|
// If we got here execv failed
|
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
|
|
|
return errno;
|
|
|
|
}
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
if (strcmp(argv[1], "-launch_gfx") == 0) {
|
|
|
|
strlcpy(gfx_app_path, BOINCDatSlotsPath, sizeof(gfx_app_path));
|
|
|
|
strlcat(gfx_app_path, argv[2], sizeof(gfx_app_path));
|
|
|
|
strlcat(gfx_app_path, "/", sizeof(gfx_app_path));
|
|
|
|
strlcat(gfx_app_path, GRAPHICS_APP_FILENAME, sizeof(gfx_app_path));
|
|
|
|
retval = boinc_resolve_filename(gfx_app_path, resolved_path, sizeof(resolved_path));
|
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
argv[2] = resolved_path;
|
|
|
|
|
|
|
|
#if 0 // For debugging only
|
|
|
|
for (int i=2; i<argc; i++) {
|
2009-04-02 10:25:16 +00:00
|
|
|
print_to_log_file("calling execv with arg %d: %s", i-2, argv[i]);
|
2007-10-03 06:16:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For unknown reasons, the graphics application exits with
|
|
|
|
// "RegisterProcess failed (error = -50)" unless we pass its
|
|
|
|
// full path twice in the argument list to execv.
|
|
|
|
execv(resolved_path, argv+2);
|
|
|
|
// If we got here execv failed
|
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2009-04-15 08:28:51 +00:00
|
|
|
if (strcmp(argv[1], "-kill_gfx") == 0) {
|
2007-10-03 06:16:58 +00:00
|
|
|
pid = atoi(argv[2]);
|
|
|
|
if (! pid) return EINVAL;
|
2009-04-02 10:25:16 +00:00
|
|
|
if ( kill(pid, SIGKILL)) {
|
|
|
|
#if 0 // For debugging only
|
|
|
|
print_to_log_file("kill(%d, SIGKILL) returned error %d", pid, errno);
|
|
|
|
#endif
|
|
|
|
return errno;
|
|
|
|
}
|
2007-10-03 06:16:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EINVAL; // Unknown command
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if CREATE_LOG
|
2007-10-25 09:34:16 +00:00
|
|
|
static void print_to_log_file(const char *format, ...) {
|
2007-10-03 06:16:58 +00:00
|
|
|
FILE *f;
|
|
|
|
va_list args;
|
|
|
|
char buf[256];
|
|
|
|
time_t t;
|
2007-10-12 05:10:52 +00:00
|
|
|
|
|
|
|
f = fopen("/Users/Shared/test_log.txt", "a");
|
2007-10-03 06:16:58 +00:00
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
// freopen(buf, "a", stdout);
|
|
|
|
// freopen(buf, "a", stderr);
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
strcpy(buf, asctime(localtime(&t)));
|
|
|
|
strip_cr(buf);
|
|
|
|
|
|
|
|
fputs(buf, f);
|
|
|
|
fputs(" ", f);
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(f, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
fputs("\n", f);
|
|
|
|
fflush(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strip_cr(char *buf)
|
|
|
|
{
|
|
|
|
char *theCR;
|
|
|
|
|
|
|
|
theCR = strrchr(buf, '\n');
|
|
|
|
if (theCR)
|
|
|
|
*theCR = '\0';
|
|
|
|
theCR = strrchr(buf, '\r');
|
|
|
|
if (theCR)
|
|
|
|
*theCR = '\0';
|
|
|
|
}
|
|
|
|
#endif // CREATE_LOG
|