2009-09-17 10:42:13 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2009 University of California
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/* AddRemoveUser.cpp */
|
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
#define VERBOSE 0
|
2017-02-28 13:19:45 +00:00
|
|
|
#define VERBOSE_SPAWN 0 /* for debugging callPosixSpawn */
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
|
|
|
#include <unistd.h> // getlogin
|
|
|
|
#include <sys/types.h> // getpwname, getpwuid, getuid
|
2017-02-25 12:58:13 +00:00
|
|
|
#include <sys/time.h>
|
2009-09-17 10:42:13 +00:00
|
|
|
#include <pwd.h> // getpwname, getpwuid, getuid
|
|
|
|
#include <grp.h> // getgrnam
|
2017-03-07 14:21:03 +00:00
|
|
|
#include <sys/param.h> // for MAXPATHLEN
|
2017-02-25 12:58:13 +00:00
|
|
|
#include "mac_util.h"
|
2009-09-17 10:42:13 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
void printUsage(long brandID);
|
|
|
|
Boolean SetLoginItemOSAScript(long brandID, Boolean addLogInItem, char *userName);
|
|
|
|
pid_t FindProcessPID(char* name, pid_t thePID);
|
2009-09-17 10:42:13 +00:00
|
|
|
static char * PersistentFGets(char *buf, size_t buflen, FILE *f);
|
2017-02-25 12:58:13 +00:00
|
|
|
static double dtime(void);
|
|
|
|
static void SleepSeconds(double seconds);
|
2017-02-28 13:19:45 +00:00
|
|
|
long GetBrandID(void);
|
|
|
|
static int parse_posic_spawn_command_line(char* p, char** argv);
|
2017-03-01 12:45:38 +00:00
|
|
|
int callPosixSpawn(const char *cmd);
|
2017-02-28 13:19:45 +00:00
|
|
|
|
|
|
|
#define NUMBRANDS 4
|
|
|
|
static char *appName[NUMBRANDS];
|
|
|
|
static char *appPath[NUMBRANDS];
|
|
|
|
static char *brandName[NUMBRANDS];
|
2009-09-17 10:42:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-02-28 13:19:45 +00:00
|
|
|
long brandID = 0;
|
2009-09-17 10:42:13 +00:00
|
|
|
Boolean AddUsers = false;
|
|
|
|
Boolean SetSavers = false;
|
2010-02-05 04:34:43 +00:00
|
|
|
Boolean isBMGroupMember, isBPGroupMember;
|
2009-09-17 10:42:13 +00:00
|
|
|
Boolean saverIsSet = false;
|
|
|
|
passwd *pw;
|
|
|
|
uid_t saved_uid;
|
|
|
|
group grpBOINC_master, *grpBOINC_masterPtr;
|
2010-02-05 04:34:43 +00:00
|
|
|
group grpBOINC_project, *grpBOINC_projectPtr;
|
2009-09-17 10:42:13 +00:00
|
|
|
char bmBuf[32768];
|
2010-02-05 09:08:01 +00:00
|
|
|
char bpBuf[32768];
|
2009-09-17 10:42:13 +00:00
|
|
|
short index, i;
|
|
|
|
char *p;
|
|
|
|
char s[256];
|
|
|
|
FILE *f;
|
|
|
|
OSStatus err;
|
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
appName[0] = "BOINCManager";
|
|
|
|
appPath[0] = "/Applications/BOINCManager.app";
|
|
|
|
brandName[0] = "BOINC";
|
|
|
|
appName[1] = "GridRepublic Desktop";
|
|
|
|
appPath[1] = "/Applications/GridRepublic Desktop.app";
|
|
|
|
brandName[1] = "GridRepublic";
|
|
|
|
appName[2] = "Progress Thru Processors Desktop";
|
|
|
|
appPath[2] = "/Applications/Progress Thru Processors Desktop.app";
|
|
|
|
brandName[2] = "Progress Thru Processors";
|
|
|
|
appName[3] = "Charity Engine Desktop";
|
|
|
|
appPath[3] = "/Applications/Charity Engine Desktop.app";
|
|
|
|
brandName[3] = "Charity Engine";
|
|
|
|
|
|
|
|
brandID = GetBrandID();
|
2009-09-17 10:42:13 +00:00
|
|
|
|
2017-03-01 12:45:38 +00:00
|
|
|
#ifndef _DEBUG
|
|
|
|
if (getuid() != 0) {
|
|
|
|
printf("This program must be run as root\n");
|
|
|
|
printUsage(brandID);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
if (argc < 3) {
|
2017-02-28 13:19:45 +00:00
|
|
|
printUsage(brandID);
|
2009-09-17 10:42:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-a") == 0) {
|
|
|
|
AddUsers = true;
|
|
|
|
} else if (strcmp(argv[1], "-s") == 0) {
|
|
|
|
AddUsers = true;
|
|
|
|
SetSavers = true;
|
|
|
|
} else if (strcmp(argv[1], "-r") != 0) {
|
2017-02-28 13:19:45 +00:00
|
|
|
printUsage(brandID);
|
2009-09-17 10:42:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = getgrnam_r("boinc_master", &grpBOINC_master, bmBuf, sizeof(bmBuf), &grpBOINC_masterPtr);
|
|
|
|
if (err) { // Should never happen unless buffer too small
|
|
|
|
puts("getgrnam(\"boinc_master\") failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-05 09:08:01 +00:00
|
|
|
err = getgrnam_r("boinc_project", &grpBOINC_project, bpBuf, sizeof(bpBuf), &grpBOINC_projectPtr);
|
2010-02-05 04:34:43 +00:00
|
|
|
if (err) { // Should never happen unless buffer too small
|
|
|
|
puts("getgrnam(\"boinc_project\") failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
for (index=2; index<argc; index++) {
|
2010-04-17 23:55:00 +00:00
|
|
|
// getpwnam works with either the full / login name (pw->pw_gecos)
|
|
|
|
// or the short / Posix name (pw->pw_name)
|
2010-02-05 04:34:43 +00:00
|
|
|
pw = getpwnam(argv[index]);
|
|
|
|
if (pw == NULL) {
|
|
|
|
printf("User %s not found.\n", argv[index]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isBMGroupMember = false;
|
2009-09-17 10:42:13 +00:00
|
|
|
i = 0;
|
|
|
|
while ((p = grpBOINC_master.gr_mem[i]) != NULL) { // Step through all users in group boinc_master
|
2010-04-17 23:55:00 +00:00
|
|
|
if (strcmp(p, pw->pw_name) == 0) { // Only the short / Posix names are in the list
|
2009-09-17 10:42:13 +00:00
|
|
|
// User is a member of group boinc_master
|
2010-02-05 04:34:43 +00:00
|
|
|
isBMGroupMember = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
isBPGroupMember = false;
|
|
|
|
i = 0;
|
|
|
|
while ((p = grpBOINC_project.gr_mem[i]) != NULL) { // Step through all users in group boinc_project
|
2010-04-17 23:55:00 +00:00
|
|
|
if (strcmp(p, pw->pw_name) == 0) { // Only the short / Posix names are in the list
|
2010-02-05 04:34:43 +00:00
|
|
|
// User is a member of group boinc_master
|
|
|
|
isBPGroupMember = true;
|
2009-09-17 10:42:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if ((!isBMGroupMember) && AddUsers) {
|
2011-08-12 11:07:24 +00:00
|
|
|
sprintf(s, "dscl . -merge /groups/boinc_master GroupMembership %s", pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if ((!isBPGroupMember) && AddUsers) {
|
2011-08-12 11:07:24 +00:00
|
|
|
sprintf(s, "dscl . -merge /groups/boinc_project GroupMembership %s", pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2010-02-05 04:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isBMGroupMember && (!AddUsers)) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -delete /Groups/boinc_master GroupMembership %s", pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
2010-02-05 04:34:43 +00:00
|
|
|
if (isBPGroupMember && (!AddUsers)) {
|
2010-04-17 23:55:00 +00:00
|
|
|
sprintf(s, "dscl . -delete /Groups/boinc_project GroupMembership %s", pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
// Set or remove login item for this user
|
2017-02-28 13:19:45 +00:00
|
|
|
SetLoginItemOSAScript(brandID, AddUsers, pw->pw_name);
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
saved_uid = geteuid();
|
|
|
|
seteuid(pw->pw_uid); // Temporarily set effective uid to this user
|
|
|
|
|
2013-11-15 13:02:04 +00:00
|
|
|
if (compareOSVersionTo(10, 6) < 0) {
|
2009-09-17 10:42:13 +00:00
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost read com.apple.screensaver moduleName",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
} else {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost read com.apple.screensaver moduleDict -dict",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
f = popen(s, "r");
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
saverIsSet = false;
|
|
|
|
while (PersistentFGets(s, sizeof(s), f)) {
|
|
|
|
if (strstr(s, "BOINCSaver")) {
|
|
|
|
saverIsSet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!saverIsSet) && SetSavers) {
|
2013-11-15 13:02:04 +00:00
|
|
|
if (compareOSVersionTo(10, 6) < 0) {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleName BOINCSaver",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver modulePath \"/Library/Screen Savers/BOINCSaver.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
} else {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName BOINCSaver path \"/Library/Screen Savers/BOINCSaver.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saverIsSet && (!AddUsers)) {
|
2013-11-15 13:02:04 +00:00
|
|
|
if (compareOSVersionTo(10, 6) < 0) {
|
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleName Flurry",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver modulePath \"/System/Library/Screen Savers/Flurry.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
} else {
|
2017-02-28 13:19:45 +00:00
|
|
|
sprintf(s, "sudo -u %s defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName Flurry path \"/callPosixSpawn/Library/Screen Savers/Flurry.saver\"",
|
2010-04-17 23:55:00 +00:00
|
|
|
pw->pw_name);
|
2017-02-28 13:19:45 +00:00
|
|
|
callPosixSpawn(s);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seteuid(saved_uid); // Set effective uid back to privileged user
|
|
|
|
}
|
2011-08-12 00:09:13 +00:00
|
|
|
|
|
|
|
printf("WARNING: Changes may require a system restart to take effect.\n");
|
2009-09-17 10:42:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
void printUsage(long brandID) {
|
2009-09-17 10:42:13 +00:00
|
|
|
printf("Usage: sudo AddRemoveUser [-a | -s | -r] [user1 [user2 [user3...]]]\n");
|
2017-02-28 13:19:45 +00:00
|
|
|
printf(" -a: add users to those with permission to run %s.\n", appName[brandID]);
|
|
|
|
printf(" -s: same as -a plus set users' screensaver to %s.\n", brandName[brandID]);
|
|
|
|
printf(" -r: remove users' permission to run %s, and \n", appName[brandID]);
|
|
|
|
printf(" if their screensaver was set to %s change it to Flurry.\n", brandName[brandID]);
|
2009-09-17 10:42:13 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
enum {
|
|
|
|
kSystemEventsCreator = 'sevs'
|
|
|
|
};
|
2017-02-25 12:58:13 +00:00
|
|
|
CFStringRef kSystemEventsBundleID = CFSTR("com.apple.systemevents");
|
2017-02-28 13:19:45 +00:00
|
|
|
char *systemEventsAppName = "System Events";
|
2009-09-17 10:42:13 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
Boolean SetLoginItemOSAScript(long brandID, Boolean addLogInItem, char *userName)
|
2014-02-27 11:17:26 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
char cmd[2048];
|
|
|
|
char systemEventsPath[1024];
|
2017-02-28 13:19:45 +00:00
|
|
|
pid_t systemEventsPID;
|
|
|
|
OSErr err = 0, err2 = 0;
|
2014-02-27 11:17:26 +00:00
|
|
|
|
|
|
|
#if VERBOSE
|
|
|
|
fprintf(stderr, "Adjusting login items for user %s\n", userName);
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We must launch the System Events application for the target user
|
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
// Find SystemEvents process. If found, quit it in case
|
|
|
|
// it is running under a different user.
|
2014-02-27 11:17:26 +00:00
|
|
|
#if VERBOSE
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "Telling System Events to quit (at start of SetLoginItemOSAScript)\n");
|
|
|
|
fflush(stderr);
|
2014-02-27 11:17:26 +00:00
|
|
|
#endif
|
2017-02-28 13:19:45 +00:00
|
|
|
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
|
|
|
|
if (systemEventsPID != 0) {
|
|
|
|
err = kill(systemEventsPID, SIGKILL);
|
|
|
|
}
|
|
|
|
if (err != noErr) {
|
|
|
|
fprintf(stderr, "(systemEventsPID, SIGKILL) returned error %d \n", (int) err);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
// Wait for the process to be gone
|
|
|
|
for (i=0; i<50; ++i) { // 5 seconds max delay
|
|
|
|
SleepSeconds(0.1); // 1/10 second
|
|
|
|
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
|
|
|
|
if (systemEventsPID == 0) break;
|
|
|
|
}
|
|
|
|
if (i >= 50) {
|
|
|
|
fprintf(stderr, "Failed to make System Events quit\n");
|
|
|
|
fflush(stderr);
|
|
|
|
err = noErr;
|
|
|
|
goto cleanupSystemEvents;
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2017-02-28 13:19:45 +00:00
|
|
|
sleep(4);
|
2011-08-12 00:09:13 +00:00
|
|
|
|
2017-02-27 14:32:17 +00:00
|
|
|
err = GetPathToAppFromID(kSystemEventsCreator, kSystemEventsBundleID, systemEventsPath, sizeof(systemEventsPath));
|
2014-02-27 11:17:26 +00:00
|
|
|
if (err != noErr) {
|
2017-02-27 14:32:17 +00:00
|
|
|
fprintf(stderr, "GetPathToAppFromID(kSystemEventsCreator) returned error %d \n", (int) err);
|
2014-02-27 11:17:26 +00:00
|
|
|
fflush(stderr);
|
|
|
|
goto cleanupSystemEvents;
|
2017-02-25 12:58:13 +00:00
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
#if VERBOSE
|
2017-02-27 14:32:17 +00:00
|
|
|
fprintf(stderr, "SystemEvents is at %s\n", systemEventsPath);
|
|
|
|
fprintf(stderr, "Launching SystemEvents for user %s\n", userName);
|
|
|
|
fflush(stderr);
|
2014-02-27 11:17:26 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-27 14:32:17 +00:00
|
|
|
for (j=0; j<5; ++j) {
|
2017-02-28 13:19:45 +00:00
|
|
|
sprintf(cmd, "sudo -u \"%s\" -b \"%s/Contents/MacOS/System Events\" &", userName, systemEventsPath);
|
|
|
|
err = callPosixSpawn(cmd);
|
2017-02-27 14:32:17 +00:00
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Command: %s returned error %d (try %d of 5)\n", cmd, (int) err, j);
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2017-02-27 14:32:17 +00:00
|
|
|
// Wait for the process to start
|
|
|
|
for (i=0; i<50; ++i) { // 5 seconds max delay
|
|
|
|
SleepSeconds(0.1); // 1/10 second
|
2017-02-28 13:19:45 +00:00
|
|
|
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
|
|
|
|
if (systemEventsPID != 0) break;
|
2011-08-11 10:20:25 +00:00
|
|
|
}
|
2017-02-27 14:32:17 +00:00
|
|
|
if (i < 50) break; // Exit j loop on success
|
|
|
|
}
|
|
|
|
if (j >= 5) {
|
|
|
|
fprintf(stderr, "Failed to launch System Events for user %s\n", userName);
|
|
|
|
fflush(stderr);
|
|
|
|
err = noErr;
|
|
|
|
goto cleanupSystemEvents;
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
2017-02-27 14:32:17 +00:00
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
#if VERBOSE
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "Deleting any login items containing %s for user %s\n", appName[brandID], userName);
|
2014-02-27 11:17:26 +00:00
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2017-02-28 13:19:45 +00:00
|
|
|
sprintf(cmd, "sudo -u \"%s\" osascript -e 'tell application \"System Events\"' -e 'delete (every login item whose path contains \"%s\")' -e 'end tell'", userName, appName[brandID]);
|
|
|
|
err = callPosixSpawn(cmd);
|
2014-02-27 11:17:26 +00:00
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Command: %s\n", cmd);
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "Delete login item containing %s returned error %d\n", appName[brandID], err);
|
2014-02-27 11:17:26 +00:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2017-02-28 13:19:45 +00:00
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
if (addLogInItem == false) {
|
|
|
|
err = noErr;
|
|
|
|
goto cleanupSystemEvents;
|
|
|
|
}
|
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "Making new login item %s for user %s\n", appName[brandID], userName);
|
2014-02-27 11:17:26 +00:00
|
|
|
fflush(stderr);
|
2017-02-28 13:19:45 +00:00
|
|
|
sprintf(cmd, "sudo -u \"%s\" osascript -e 'tell application \"System Events\"' -e 'make new login item at end with properties {path:\"%s\", hidden:true, name:\"%s\"}' -e 'end tell'", userName, appPath[brandID], appName[brandID]);
|
|
|
|
err = callPosixSpawn(cmd);
|
2014-02-27 11:17:26 +00:00
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Command: %s\n", cmd);
|
2017-02-28 13:19:45 +00:00
|
|
|
printf("[Make login item for %s returned error %d\n", appPath[brandID], err);
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
|
|
|
fflush(stderr);
|
2011-08-12 00:09:13 +00:00
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
cleanupSystemEvents:
|
|
|
|
// Clean up in case this was our last user
|
|
|
|
#if VERBOSE
|
|
|
|
fprintf(stderr, "Telling System Events to quit (at end of SetLoginItemOSAScript)\n");
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2017-02-28 13:19:45 +00:00
|
|
|
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
|
|
|
|
err2 = noErr;
|
|
|
|
if (systemEventsPID != 0) {
|
|
|
|
err2 = kill(systemEventsPID, SIGKILL);
|
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
if (err2 != noErr) {
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "kill(systemEventsPID, SIGKILL) returned error %d \n", (int) err2);
|
2014-02-27 11:17:26 +00:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
// Wait for the process to be gone
|
|
|
|
for (i=0; i<50; ++i) { // 5 seconds max delay
|
2017-02-25 12:58:13 +00:00
|
|
|
SleepSeconds(0.1); // 1/10 second
|
2017-02-28 13:19:45 +00:00
|
|
|
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
|
|
|
|
if (systemEventsPID == 0) break;
|
2011-08-12 00:09:13 +00:00
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
if (i >= 50) {
|
|
|
|
fprintf(stderr, "Failed to make System Events quit\n");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(4);
|
|
|
|
|
|
|
|
return (err == noErr);
|
2009-09-17 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
pid_t FindProcessPID(char* name, pid_t thePID)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char buf[1024];
|
|
|
|
size_t n = 0;
|
|
|
|
pid_t aPID;
|
|
|
|
|
|
|
|
if (name != NULL) // Search ny name
|
|
|
|
n = strlen(name);
|
|
|
|
|
|
|
|
f = popen("ps -a -x -c -o command,pid", "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (PersistentFGets(buf, sizeof(buf), f))
|
|
|
|
{
|
|
|
|
if (name != NULL) { // Search by name
|
|
|
|
if (strncmp(buf, name, n) == 0)
|
|
|
|
{
|
|
|
|
aPID = atol(buf+16);
|
|
|
|
pclose(f);
|
|
|
|
return aPID;
|
|
|
|
}
|
|
|
|
} else { // Search by PID
|
|
|
|
aPID = atol(buf+16);
|
|
|
|
if (aPID == thePID) {
|
|
|
|
pclose(f);
|
|
|
|
return aPID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-17 10:42:13 +00:00
|
|
|
static char * PersistentFGets(char *buf, size_t buflen, FILE *f) {
|
|
|
|
char *p = buf;
|
|
|
|
size_t len = buflen;
|
|
|
|
size_t datalen = 0;
|
|
|
|
|
|
|
|
*buf = '\0';
|
|
|
|
while (datalen < (buflen - 1)) {
|
|
|
|
fgets(p, len, f);
|
|
|
|
if (feof(f)) break;
|
|
|
|
if (ferror(f) && (errno != EINTR)) break;
|
|
|
|
if (strchr(buf, '\n')) break;
|
|
|
|
datalen = strlen(buf);
|
|
|
|
p = buf + datalen;
|
|
|
|
len -= datalen;
|
|
|
|
}
|
|
|
|
return (buf[0] ? buf : NULL);
|
|
|
|
}
|
2013-11-15 13:02:04 +00:00
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-25 12:58:13 +00:00
|
|
|
// return time of day (seconds since 1970) as a double
|
|
|
|
//
|
|
|
|
static double dtime(void) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return tv.tv_sec + (tv.tv_usec/1.e6);
|
|
|
|
}
|
|
|
|
|
2014-02-27 11:17:26 +00:00
|
|
|
// Uses usleep to sleep for full duration even if a signal is received
|
2017-02-25 12:58:13 +00:00
|
|
|
static void SleepSeconds(double seconds) {
|
|
|
|
double end_time = dtime() + seconds - 0.01;
|
|
|
|
// sleep() and usleep() can be interrupted by SIGALRM,
|
|
|
|
// so we may need multiple calls
|
|
|
|
//
|
|
|
|
while (1) {
|
|
|
|
if (seconds >= 1) {
|
|
|
|
sleep((unsigned int) seconds);
|
|
|
|
} else {
|
|
|
|
usleep((int)fmod(seconds*1000000, 1000000));
|
|
|
|
}
|
|
|
|
seconds = end_time - dtime();
|
|
|
|
if (seconds <= 0) break;
|
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2013-11-15 13:02:04 +00:00
|
|
|
|
2017-02-25 12:58:13 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
long GetBrandID()
|
|
|
|
{
|
|
|
|
long iBrandId;
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
iBrandId = 0; // Default value
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
FILE *f = fopen("Contents/Resources/Branding", "r");
|
|
|
|
if (f) {
|
|
|
|
fscanf(f, "BrandId=%ld\n", &iBrandId);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
return iBrandId;
|
|
|
|
}
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
|
|
|
|
#define NOT_IN_TOKEN 0
|
|
|
|
#define IN_SINGLE_QUOTED_TOKEN 1
|
|
|
|
#define IN_DOUBLE_QUOTED_TOKEN 2
|
|
|
|
#define IN_UNQUOTED_TOKEN 3
|
|
|
|
|
|
|
|
static int parse_posic_spawn_command_line(char* p, char** argv) {
|
|
|
|
int state = NOT_IN_TOKEN;
|
|
|
|
int argc=0;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
switch(state) {
|
|
|
|
case NOT_IN_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
} else if (*p == '\'') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_SINGLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else if (*p == '\"') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_DOUBLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_UNQUOTED_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_SINGLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\'') {
|
|
|
|
if (*(p-1) == '\\') break;
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2017-02-28 13:19:45 +00:00
|
|
|
break;
|
|
|
|
case IN_DOUBLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\"') {
|
|
|
|
if (*(p-1) == '\\') break;
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_UNQUOTED_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2017-02-28 13:19:45 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
argv[argc] = 0;
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#include <spawn.h>
|
|
|
|
|
2017-03-01 12:45:38 +00:00
|
|
|
int callPosixSpawn(const char *cmdline) {
|
2017-02-28 13:19:45 +00:00
|
|
|
char command[1024];
|
|
|
|
char progName[1024];
|
|
|
|
char progPath[MAXPATHLEN];
|
|
|
|
char* argv[100];
|
|
|
|
int argc = 0;
|
|
|
|
char *p;
|
|
|
|
pid_t thePid = 0;
|
|
|
|
int result = 0;
|
|
|
|
int status = 0;
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
// Make a copy of cmdline because parse_posic_spawn_command_line modifies it
|
|
|
|
strlcpy(command, cmdline, sizeof(command));
|
|
|
|
argc = parse_posic_spawn_command_line(const_cast<char*>(command), argv);
|
|
|
|
strlcpy(progPath, argv[0], sizeof(progPath));
|
|
|
|
strlcpy(progName, argv[0], sizeof(progName));
|
|
|
|
p = strrchr(progName, '/');
|
|
|
|
if (p) {
|
|
|
|
argv[0] = p+1;
|
|
|
|
} else {
|
|
|
|
argv[0] = progName;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if VERBOSE_SPAWN
|
|
|
|
fprintf(stderr, "***********");
|
|
|
|
for (int i=0; i<argc; ++i) {
|
|
|
|
fprintf(stderr, "argv[%d]=%s", i, argv[i]);
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|
2017-02-28 13:19:45 +00:00
|
|
|
fprintf(stderr, "***********\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
errno = 0;
|
2014-02-27 11:17:26 +00:00
|
|
|
|
2017-02-28 13:19:45 +00:00
|
|
|
result = posix_spawnp(&thePid, progPath, NULL, NULL, argv, environ);
|
|
|
|
#if VERBOSE_SPAWN
|
|
|
|
fprintf(stderr, "callPosixSpawn command: %s", cmdline);
|
|
|
|
fprintf(stderr, "callPosixSpawn: posix_spawnp returned %d: %s", result, strerror(result));
|
|
|
|
#endif
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// CAF int val =
|
|
|
|
waitpid(thePid, &status, WUNTRACED);
|
|
|
|
// CAF if (val < 0) printf("first waitpid returned %d\n", val);
|
|
|
|
if (status != 0) {
|
|
|
|
#if VERBOSE_SPAWN
|
|
|
|
fprintf(stderr, "waitpid() returned status=%d", status);
|
|
|
|
#endif
|
|
|
|
result = status;
|
|
|
|
} else {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
result = WEXITSTATUS(status);
|
|
|
|
if (result == 1) {
|
|
|
|
#if VERBOSE_SPAWN
|
|
|
|
fprintf(stderr, "WEXITSTATUS(status) returned 1, errno=%d: %s", errno, strerror(errno));
|
|
|
|
#endif
|
|
|
|
result = errno;
|
|
|
|
}
|
|
|
|
#if VERBOSE_SPAWN
|
|
|
|
else if (result) {
|
|
|
|
fprintf(stderr, "WEXITSTATUS(status) returned %d", result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // end if (WIFEXITED(status)) else
|
|
|
|
} // end if waitpid returned 0 sstaus else
|
|
|
|
|
|
|
|
return result;
|
2014-02-27 11:17:26 +00:00
|
|
|
}
|