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
|
2024-05-03 08:12:27 +00:00
|
|
|
// Copyright (C) 2024 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:
|
2023-10-28 12:30:49 +00:00
|
|
|
// - launch graphics application at given slot number
|
|
|
|
// - launch default graphics application
|
|
|
|
// - kill graphics application with given process ID
|
2009-04-02 10:25:16 +00:00
|
|
|
//
|
2020-04-23 14:18:54 +00:00
|
|
|
|
|
|
|
// Special logic used only under OS 10.15 Catalina and later:
|
|
|
|
//
|
|
|
|
// BOINC screensaver plugin BOINCSaver.saver (BOINC Screensaver Coordinator)
|
2023-05-05 18:05:20 +00:00
|
|
|
// sends a run_graphics_app RPC to the BOINC client. The BOINC client then
|
2020-04-23 14:18:54 +00:00
|
|
|
// launches switcher, which submits a script to launchd as a LaunchAgent
|
|
|
|
// for the user that invoked the screensaver (the currently logged in user.)
|
|
|
|
//
|
2023-05-05 18:05:20 +00:00
|
|
|
// We must go through launchd to establish a connection to the windowserver
|
2020-04-23 14:18:54 +00:00
|
|
|
// in the currently logged in user's space for use by the project graphics
|
2023-05-05 18:05:20 +00:00
|
|
|
// app. This script then launches gfx_switcher, which uses fork and execv to
|
|
|
|
// launch the project graphics app. gfx_switcher writes the graphics app's
|
|
|
|
// process ID to shared memory, to be read by the Screensaver Coordinator.
|
2023-10-28 12:30:49 +00:00
|
|
|
// gfx_switcher waits for the graphics app to exit and then notifies the
|
|
|
|
// Screensaver Coordinator by writing 0 to the shared memory.
|
2020-04-23 14:18:54 +00:00
|
|
|
//
|
|
|
|
// This Rube Goldberg process is necessary due to limitations on screensavers
|
2023-10-28 12:30:49 +00:00
|
|
|
// introduced in OS 10.15 Catalina and OS 14.0 Sonoma.
|
2020-04-23 14:18:54 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <SystemConfiguration/SystemConfiguration.h>
|
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
|
2020-04-23 14:18:54 +00:00
|
|
|
#include <pwd.h> // getpwuid
|
2007-10-03 06:16:58 +00:00
|
|
|
#include <grp.h>
|
2020-04-23 14:18:54 +00:00
|
|
|
#include <signal.h> // For kill()
|
2019-11-08 11:40:22 +00:00
|
|
|
#include <pthread.h>
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "common_defs.h"
|
2019-11-08 11:40:22 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "mac_util.h"
|
2020-04-23 14:18:54 +00:00
|
|
|
#include "shmem.h"
|
2022-11-07 09:22:28 +00:00
|
|
|
#include "mac_spawn.h"
|
2007-10-03 06:16:58 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
#define VERBOSE 0
|
|
|
|
#define CREATE_LOG VERBOSE
|
2007-10-03 06:16:58 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
#if VERBOSE
|
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
|
2020-04-23 14:18:54 +00:00
|
|
|
#else
|
|
|
|
#define print_to_log_file(...)
|
|
|
|
#endif
|
2007-10-03 06:16:58 +00:00
|
|
|
|
2019-11-08 11:40:22 +00:00
|
|
|
void * MonitorScreenSaverEngine(void* param);
|
|
|
|
|
2024-05-03 08:12:27 +00:00
|
|
|
// struct ss_shmem_data must be kept in sync in these files:
|
|
|
|
// screensaver.cpp
|
|
|
|
// gfx_switcher.cpp
|
|
|
|
// gfx_cleanup.mm
|
|
|
|
// graphics2_unix.cpp
|
2022-11-10 13:30:28 +00:00
|
|
|
struct ss_shmem_data {
|
|
|
|
pid_t gfx_pid;
|
|
|
|
int gfx_slot;
|
2024-05-03 08:12:27 +00:00
|
|
|
int major_version;
|
|
|
|
int minor_version;
|
|
|
|
int release;
|
2022-11-10 13:30:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct ss_shmem_data* ss_shmem = NULL;
|
2019-11-08 11:40:22 +00:00
|
|
|
|
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];
|
2022-11-07 09:22:28 +00:00
|
|
|
char cmd[2048];
|
2020-04-23 14:18:54 +00:00
|
|
|
char gfx_app_path[MAXPATHLEN], resolved_path[MAXPATHLEN];
|
2007-10-03 06:16:58 +00:00
|
|
|
char *BOINCDatSlotsPath = "/Library/Application Support/BOINC Data/slots/";
|
2020-04-23 14:18:54 +00:00
|
|
|
int retval;
|
2007-10-03 06:16:58 +00:00
|
|
|
int pid;
|
2020-04-23 14:18:54 +00:00
|
|
|
int i;
|
|
|
|
const char *screensaverLoginUser = NULL;
|
2019-11-08 11:40:22 +00:00
|
|
|
pthread_t monitorScreenSaverEngineThread = 0;
|
2007-10-03 06:16:58 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
if (argc < 2) return EINVAL;
|
2009-04-02 01:48:44 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
CFStringRef cf_gUserName = SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL);
|
|
|
|
CFStringGetCString(cf_gUserName, user_name, sizeof(user_name), kCFStringEncodingUTF8);
|
2023-10-28 12:30:49 +00:00
|
|
|
// strlcpy(user_name, getenv("USER"), sizeof(user_name));
|
2020-04-23 14:18:54 +00:00
|
|
|
strlcpy(group_name, "boinc_project", sizeof(group_name));
|
|
|
|
|
|
|
|
// Under fast user switching, the BOINC client may be running under a
|
2023-05-05 18:05:20 +00:00
|
|
|
// different login than the screensaver
|
2020-04-23 14:18:54 +00:00
|
|
|
//
|
|
|
|
// If we need to join a different process group, it must be the last argument.
|
|
|
|
i = 0;
|
|
|
|
while(argv[i]) {
|
|
|
|
if (!strcmp(argv[i], "--ScreensaverLoginUser")) {
|
|
|
|
screensaverLoginUser = argv[i+1];
|
2022-11-07 09:22:28 +00:00
|
|
|
// strlcpy(user_name, screensaverLoginUser, sizeof(user_name));
|
2020-04-23 14:18:54 +00:00
|
|
|
argv[i] = 0; // Strip off the --ScreensaverLoginUser argument
|
|
|
|
argc -= 2;
|
|
|
|
#if VERBOSE // For debugging only
|
|
|
|
print_to_log_file("\n\ngfx_switcher: screensaverLoginUser = %s", screensaverLoginUser);
|
2019-11-08 11:40:22 +00:00
|
|
|
#endif
|
2020-04-23 14:18:54 +00:00
|
|
|
break;
|
2019-11-08 11:40:22 +00:00
|
|
|
}
|
2020-04-23 14:18:54 +00:00
|
|
|
++i;
|
2019-11-08 11:40:22 +00:00
|
|
|
}
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2022-11-08 13:48:36 +00:00
|
|
|
if (! screensaverLoginUser) {
|
|
|
|
screensaverLoginUser = user_name;
|
|
|
|
}
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
#if 0 // For debugging only
|
2022-11-07 09:22:28 +00:00
|
|
|
// Allow debugging without running as group boinc_project
|
2007-10-03 06:16:58 +00:00
|
|
|
grp = getgrgid(getgid());
|
2022-11-07 09:22:28 +00:00
|
|
|
if (grp) strlcpy(group_name, grp->gr_name, sizeof(group_name));
|
2020-04-23 14:18:54 +00:00
|
|
|
#endif
|
2022-11-07 09:22:28 +00:00
|
|
|
|
2023-05-05 18:05:20 +00:00
|
|
|
// We are running setuid root, so setgid() sets real group ID,
|
2007-10-03 06:16:58 +00:00
|
|
|
// effective group ID and saved set_group-ID for this process
|
|
|
|
grp = getgrnam(group_name);
|
|
|
|
if (grp) setgid(grp->gr_gid);
|
|
|
|
|
2022-11-07 09:22:28 +00:00
|
|
|
// As of MacOS 13.0 Ventura IOSurface cannot be used to share graphics
|
|
|
|
// between apps unless they are running as the same user, so we no
|
|
|
|
// longer run the graphics apps as user boinc_master.
|
2007-10-03 06:16:58 +00:00
|
|
|
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
|
2020-04-23 14:18:54 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2007-10-12 05:10:52 +00:00
|
|
|
char current_dir[MAXPATHLEN];
|
|
|
|
|
|
|
|
getcwd( current_dir, sizeof(current_dir));
|
2020-04-23 14:18:54 +00:00
|
|
|
print_to_log_file("current directory = %s", current_dir);
|
|
|
|
print_to_log_file("user_name is %s, euid=%d, uid=%d, egid=%d, gid=%d", user_name, geteuid(), getuid(), getegid(), getgid());
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
for (int i=0; i<argc; i++) {
|
|
|
|
print_to_log_file("gfx_switcher arg %d: %s", i, argv[i]);
|
2007-10-12 05:10:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
if (strcmp(argv[1], "-default_gfx") == 0) {
|
2009-04-02 01:48:44 +00:00
|
|
|
strlcpy(resolved_path, "/Library/Application Support/BOINC Data/boincscr", sizeof(resolved_path));
|
2020-04-23 14:18:54 +00:00
|
|
|
argv[2] = resolved_path;
|
|
|
|
|
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher using fork()");
|
2020-04-23 14:18:54 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
int pid = fork();
|
|
|
|
// As of MacOS 13.0 Ventura IOSurface cannot be used to share graphics
|
|
|
|
// between apps unless they are running as the same user, so we no
|
|
|
|
// longer run the graphics apps as user boinc_master. To replace the
|
|
|
|
// security that was provided by running as a different user, we use
|
|
|
|
// sandbox-exec() to launch the graphics apps. Note that sandbox-exec()
|
|
|
|
// is marked deprecated because it is an Apple private API so the syntax
|
|
|
|
// of the security specifications is subject to change without notice.
|
|
|
|
// But it is used widely in Apple's software, and the security profile
|
|
|
|
// elements we use are very unlikely to change.
|
|
|
|
//
|
|
|
|
if (pid == 0) {
|
2023-05-05 18:05:20 +00:00
|
|
|
// For unknown reasons, the graphics application exits with
|
|
|
|
// "RegisterProcess failed (error = -50)" unless we pass its
|
2022-11-08 13:48:36 +00:00
|
|
|
// full path twice in the argument list to execv.
|
|
|
|
strlcpy(cmd, "sandbox-exec -f \"", sizeof(cmd));
|
|
|
|
strlcat(cmd, argv[0], sizeof(cmd)); // path to this executable
|
|
|
|
char *slash = strrchr(cmd, '/');
|
|
|
|
if (slash) *slash = '\0'; // Directory containing this executable
|
|
|
|
strlcat(cmd, "/mac_restrict_access.sb\" \"", sizeof(cmd)); // path to sandboxing profile
|
|
|
|
strlcat(cmd, resolved_path, sizeof(cmd)); // path to graphics app
|
|
|
|
strlcat(cmd, "\"", sizeof(cmd)); // path to sandboxing profile
|
|
|
|
for (int i=3; i<argc; i++) {
|
|
|
|
strlcat(cmd, " ", sizeof(cmd));
|
|
|
|
strlcat(cmd, argv[i], sizeof(cmd)); // next argument to pass to graphics app
|
|
|
|
}
|
2020-08-06 11:48:19 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher calling callPosixSpawn with command:\n%s\n", cmd);
|
2020-08-06 11:48:19 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
retval = callPosixSpawn(cmd);
|
|
|
|
if (retval) {
|
2022-11-07 09:22:28 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher: Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
2022-11-07 09:22:28 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char shmem_name[MAXPATHLEN];
|
2020-08-06 11:48:19 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher: Child PID=%d", pid);
|
2020-08-06 11:48:19 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
snprintf(shmem_name, sizeof(shmem_name), "/tmp/boinc_ss_%s", screensaverLoginUser);
|
2022-11-10 13:30:28 +00:00
|
|
|
retval = attach_shmem_mmap(shmem_name, (void**)&ss_shmem);
|
|
|
|
if (ss_shmem != 0) {
|
|
|
|
ss_shmem->gfx_pid = pid;
|
|
|
|
ss_shmem->gfx_slot = -1; // Default GFX has no slot number
|
2022-11-08 13:48:36 +00:00
|
|
|
}
|
|
|
|
pthread_create(&monitorScreenSaverEngineThread, NULL, MonitorScreenSaverEngine, &pid);
|
|
|
|
waitpid(pid, 0, 0);
|
|
|
|
pthread_cancel(monitorScreenSaverEngineThread);
|
2022-11-10 13:30:28 +00:00
|
|
|
if (ss_shmem != 0) {
|
|
|
|
ss_shmem->gfx_pid = 0;
|
2024-05-03 08:12:27 +00:00
|
|
|
ss_shmem->major_version = 0;
|
|
|
|
ss_shmem->minor_version = 0;
|
|
|
|
ss_shmem->release = 0;
|
2019-11-08 11:40:22 +00:00
|
|
|
}
|
2022-11-08 13:48:36 +00:00
|
|
|
return 0;
|
2019-11-08 11:40:22 +00:00
|
|
|
}
|
2009-04-02 01:48:44 +00:00
|
|
|
}
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
if (strcmp(argv[1], "-launch_gfx") == 0) {
|
2007-10-03 06:16:58 +00:00
|
|
|
strlcpy(gfx_app_path, BOINCDatSlotsPath, sizeof(gfx_app_path));
|
2020-04-23 14:18:54 +00:00
|
|
|
strlcat(gfx_app_path, argv[2], sizeof(gfx_app_path));
|
2007-10-03 06:16:58 +00:00
|
|
|
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;
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher using fork()");;
|
2007-10-03 06:16:58 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
int pid = fork();
|
|
|
|
// As of MacOS 13.0 Ventura IOSurface cannot be used to share graphics
|
|
|
|
// between apps unless they are running as the same user, so we no
|
|
|
|
// longer run the graphics apps as user boinc_master. To replace the
|
|
|
|
// security that was provided by running as a different user, we use
|
|
|
|
// sandbox-exec() to launch the graphics apps. Note that sandbox-exec()
|
|
|
|
// is marked deprecated because it is an Apple private API so the syntax
|
|
|
|
// of the security specifications is subject to change without notice.
|
|
|
|
// But it is used widely in Apple's software, and the security profile
|
|
|
|
// elements we use are very unlikely to change.
|
|
|
|
if (pid == 0) {
|
2023-05-05 18:05:20 +00:00
|
|
|
// For unknown reasons, the graphics application exits with
|
|
|
|
// "RegisterProcess failed (error = -50)" unless we pass its
|
2019-11-08 11:40:22 +00:00
|
|
|
// full path twice in the argument list to execv.
|
2022-11-08 13:48:36 +00:00
|
|
|
strlcpy(cmd, "sandbox-exec -f \"", sizeof(cmd));
|
|
|
|
strlcat(cmd, argv[0], sizeof(cmd)); // path to this executable
|
|
|
|
char *slash = strrchr(cmd, '/');
|
|
|
|
if (slash) *slash = '\0'; // Directory containing this executable
|
|
|
|
strlcat(cmd, "/mac_restrict_access.sb\" \"", sizeof(cmd)); // path to sandboxing profile
|
|
|
|
strlcat(cmd, resolved_path, sizeof(cmd)); // path to graphics app
|
|
|
|
strlcat(cmd, "\"", sizeof(cmd));
|
|
|
|
for (int i=3; i<argc; i++) {
|
|
|
|
strlcat(cmd, " ", sizeof(cmd));
|
|
|
|
strlcat(cmd, argv[i], sizeof(cmd)); // next argument to pass to graphics app
|
|
|
|
}
|
2022-11-07 09:22:28 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher calling callPosixSpawn with command:\n%s\n", cmd);
|
2022-11-07 09:22:28 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
retval = callPosixSpawn(cmd);
|
|
|
|
if (retval) {
|
2022-11-07 09:22:28 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2022-11-08 13:48:36 +00:00
|
|
|
print_to_log_file("gfx_switcher: Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
2022-11-07 09:22:28 +00:00
|
|
|
#endif
|
2022-11-08 13:48:36 +00:00
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", resolved_path, errno);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char shmem_name[MAXPATHLEN];
|
|
|
|
snprintf(shmem_name, sizeof(shmem_name), "/tmp/boinc_ss_%s", screensaverLoginUser);
|
2022-11-10 13:30:28 +00:00
|
|
|
retval = attach_shmem_mmap(shmem_name, (void**)&ss_shmem);
|
|
|
|
if (ss_shmem != 0) {
|
|
|
|
ss_shmem->gfx_pid = pid;
|
|
|
|
ss_shmem->gfx_slot = atoi(argv[2]);
|
2022-11-08 13:48:36 +00:00
|
|
|
}
|
|
|
|
pthread_create(&monitorScreenSaverEngineThread, NULL, MonitorScreenSaverEngine, &pid);
|
|
|
|
waitpid(pid, 0, 0);
|
|
|
|
pthread_cancel(monitorScreenSaverEngineThread);
|
2022-11-10 13:30:28 +00:00
|
|
|
if (ss_shmem != 0) {
|
|
|
|
ss_shmem ->gfx_pid = 0;
|
2024-05-03 08:12:27 +00:00
|
|
|
ss_shmem->major_version = 0;
|
|
|
|
ss_shmem->minor_version = 0;
|
|
|
|
ss_shmem->release = 0;
|
2019-11-08 11:40:22 +00:00
|
|
|
}
|
2022-11-08 13:48:36 +00:00
|
|
|
return 0;
|
2020-04-23 14:18:54 +00:00
|
|
|
}
|
2007-10-03 06:16:58 +00:00
|
|
|
}
|
2020-04-23 14:18:54 +00:00
|
|
|
|
|
|
|
if (strcmp(argv[1], "-kill_gfx") == 0) {
|
|
|
|
pid = atoi(argv[2]);
|
2007-10-03 06:16:58 +00:00
|
|
|
if (! pid) return EINVAL;
|
2009-04-02 10:25:16 +00:00
|
|
|
if ( kill(pid, SIGKILL)) {
|
2020-04-23 14:18:54 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2009-04-02 10:25:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2007-10-03 06:16:58 +00:00
|
|
|
return EINVAL; // Unknown command
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:40:22 +00:00
|
|
|
// For extra safety, kill our graphics app if ScreenSaverEngine has exited
|
|
|
|
void * MonitorScreenSaverEngine(void* param) {
|
|
|
|
pid_t ScreenSaverEngine_Pid = 0;
|
|
|
|
pid_t graphics_Pid = *(pid_t*)param;
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2019-11-08 11:40:22 +00:00
|
|
|
while (true) {
|
|
|
|
boinc_sleep(1.0); // Test every second
|
|
|
|
ScreenSaverEngine_Pid = getPidIfRunning("com.apple.ScreenSaver.Engine");
|
2020-08-06 11:48:19 +00:00
|
|
|
#if VERBOSE // For debugging only
|
|
|
|
print_to_log_file("MonitorScreenSaverEngine: ScreenSaverEngine_Pid=%d", ScreenSaverEngine_Pid);
|
|
|
|
#endif
|
2019-11-08 11:40:22 +00:00
|
|
|
if (ScreenSaverEngine_Pid == 0) {
|
2023-10-28 12:30:49 +00:00
|
|
|
// legacyScreenSaver name under MacOS 14 Sonoma
|
|
|
|
ScreenSaverEngine_Pid = getPidIfRunning("com.apple.ScreenSaver.Engine.legacyScreenSaver");
|
|
|
|
}
|
|
|
|
if (ScreenSaverEngine_Pid == 0) {
|
2020-08-06 11:48:19 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
ScreenSaverEngine_Pid = getPidIfRunning("com.apple.ScreenSaver.Engine.legacyScreenSaver.x86_64");
|
|
|
|
#elif defined(__arm64__)
|
|
|
|
ScreenSaverEngine_Pid = getPidIfRunning("com.apple.ScreenSaver.Engine.legacyScreenSaver.arm64");
|
|
|
|
#endif
|
|
|
|
#if VERBOSE // For debugging only
|
|
|
|
print_to_log_file("MonitorScreenSaverEngine: ScreenSaverEngine_legacyScreenSaver_Pid=%d", ScreenSaverEngine_Pid);
|
|
|
|
#endif
|
|
|
|
}
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2020-08-06 11:48:19 +00:00
|
|
|
if (ScreenSaverEngine_Pid == 0) {
|
|
|
|
kill(graphics_Pid, SIGKILL);
|
2020-04-23 14:18:54 +00:00
|
|
|
#if VERBOSE // For debugging only
|
2020-08-06 11:48:19 +00:00
|
|
|
print_to_log_file("MonitorScreenSaverEngine calling kill(%d, SIGKILL", graphics_Pid);
|
2019-11-08 11:40:22 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-03 06:16:58 +00:00
|
|
|
|
|
|
|
#if CREATE_LOG
|
2019-11-08 11:40:22 +00:00
|
|
|
|
2020-04-23 14:18:54 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
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;
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2019-11-08 11:40:22 +00:00
|
|
|
f = fopen("/Users/Shared/test_log_gfx_switcher.txt", "a");
|
2007-10-03 06:16:58 +00:00
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
// freopen(buf, "a", stdout);
|
|
|
|
// freopen(buf, "a", stderr);
|
|
|
|
|
|
|
|
time(&t);
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(buf, asctime(localtime(&t)));
|
2007-10-03 06:16:58 +00:00
|
|
|
strip_cr(buf);
|
|
|
|
|
|
|
|
fputs(buf, f);
|
|
|
|
fputs(" ", f);
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(f, format, args);
|
|
|
|
va_end(args);
|
2023-05-05 18:05:20 +00:00
|
|
|
|
2007-10-03 06:16:58 +00:00
|
|
|
fputs("\n", f);
|
|
|
|
fflush(f);
|
|
|
|
fclose(f);
|
2019-11-08 11:40:22 +00:00
|
|
|
chmod("/Users/Shared/test_log_gfx_switcher.txt", 0666);
|
2007-10-03 06:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|