Mac: add new mac_spawn.cpp to project to facilitate replacing system() calls with posix_spawn() calls as instructed by Apple, since system() is deprecated in mac OS 10.10.

This commit is contained in:
Charlie Fenton 2017-03-01 04:53:24 -08:00
parent 1065b6287a
commit 103eb0ce5e
2 changed files with 156 additions and 0 deletions

150
lib/mac/mac_spawn.cpp Normal file
View File

@ -0,0 +1,150 @@
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2017 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/>.
// system() is deprecated in Mac OS 10.10.
// Apple says to call posix_spawn instead.
#define VERBOSE_SPAWN 0 /* for debugging callPosixSpawn */
#include <Carbon/Carbon.h>
#include <spawn.h>
#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;
}
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;
}
p++;
}
argv[argc] = 0;
return argc;
}
int callPosixSpawn(const char *cmdline) {
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]);
}
fprintf(stderr, "***********\n");
#endif
errno = 0;
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;
}

View File

@ -74,6 +74,8 @@
DD2B6C8113149177005D6F3E /* procinfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD2B6C7E13149177005D6F3E /* procinfo.cpp */; };
DD2B6C8D131491B7005D6F3E /* procinfo_mac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DDB6934F0ABFE9C600689FD8 /* procinfo_mac.cpp */; };
DD2B6C8E131491B8005D6F3E /* procinfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD2B6C7E13149177005D6F3E /* procinfo.cpp */; };
DD2C5C3E1E66D83D00BF5511 /* mac_spawn.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD2C5C3D1E66D83D00BF5511 /* mac_spawn.cpp */; };
DD2C5C401E66F84300BF5511 /* mac_spawn.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD2C5C3D1E66D83D00BF5511 /* mac_spawn.cpp */; };
DD30CACF1C6B3C7F00AE1D14 /* project_init.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD30CACD1C6B3C7F00AE1D14 /* project_init.cpp */; };
DD30CAD01C6B3CCB00AE1D14 /* project_init.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD30CACD1C6B3C7F00AE1D14 /* project_init.cpp */; };
DD3157C316740D5300B6C909 /* gpu_intel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DD3157C016740C9100B6C909 /* gpu_intel.cpp */; };
@ -769,6 +771,7 @@
DD25F72715914F8C007845B5 /* gpu_detect.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gpu_detect.h; sourceTree = "<group>"; };
DD2B6C7E13149177005D6F3E /* procinfo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = procinfo.cpp; path = ../lib/procinfo.cpp; sourceTree = SOURCE_ROOT; };
DD2B6C7F13149177005D6F3E /* procinfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = procinfo.h; path = ../lib/procinfo.h; sourceTree = SOURCE_ROOT; };
DD2C5C3D1E66D83D00BF5511 /* mac_spawn.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = mac_spawn.cpp; path = ../lib/mac/mac_spawn.cpp; sourceTree = "<group>"; };
DD2D25CB07FAB41700151141 /* DlgSelectComputer.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = DlgSelectComputer.cpp; path = ../clientgui/DlgSelectComputer.cpp; sourceTree = SOURCE_ROOT; };
DD2D25CC07FAB41700151141 /* DlgSelectComputer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DlgSelectComputer.h; path = ../clientgui/DlgSelectComputer.h; sourceTree = SOURCE_ROOT; };
DD30446A0864332D00D73756 /* config.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = config.h; path = ../clientgui/mac/config.h; sourceTree = SOURCE_ROOT; };
@ -1707,6 +1710,7 @@
isa = PBXGroup;
children = (
DDA6BD030BD4551F008F7921 /* dyld_gdb.h */,
DD2C5C3D1E66D83D00BF5511 /* mac_spawn.cpp */,
DDBAA9B61DF190EB004C48FD /* mac_util.h */,
DDBAA9B41DF1902B004C48FD /* mac_util.mm */,
DDA6BD040BD4551F008F7921 /* mac_backtrace.cpp */,
@ -2805,6 +2809,7 @@
DD3E15210A774397007E0084 /* check_security.cpp in Sources */,
DD3E15230A774397007E0084 /* AdvancedFrame.cpp in Sources */,
DD3E15240A774397007E0084 /* BOINCBaseFrame.cpp in Sources */,
DD2C5C401E66F84300BF5511 /* mac_spawn.cpp in Sources */,
DD3E15260A774397007E0084 /* sg_DlgPreferences.cpp in Sources */,
DD3E152D0A774397007E0084 /* app_ipc.cpp in Sources */,
DD3E152E0A774397007E0084 /* proxy_info.cpp in Sources */,
@ -3177,6 +3182,7 @@
DD33C6F308B5BAF500768630 /* gui_http.cpp in Sources */,
DD73E36E08A0720500656EB1 /* gui_rpc_server_ops.cpp in Sources */,
DDD74DC807CF492E0065AC9D /* hostinfo.cpp in Sources */,
DD2C5C3E1E66D83D00BF5511 /* mac_spawn.cpp in Sources */,
DD33C70408B5BEDE00768630 /* http_curl.cpp in Sources */,
DDD74DCB07CF49310065AC9D /* md5.cpp in Sources */,
DDD74DCC07CF49320065AC9D /* md5_file.cpp in Sources */,