forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.cpp
740 lines (619 loc) · 23.8 KB
/
Installer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// 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/>.
/* Installer.cpp */
#define CREATE_LOG 0 /* for general debugging */
#define VERBOSE_TEST 0 /* for debugging callPosixSpawn */
#include <Carbon/Carbon.h>
#include <grp.h>
#include <unistd.h> // getlogin
#include <sys/types.h> // getpwname, getpwuid, getuid
#include <pwd.h> // getpwname, getpwuid, getuid
#include <sys/wait.h> // waitpid
#include <dirent.h>
#include <sys/param.h> // for MAXPATHLEN
#include <sys/stat.h>
#include "str_util.h"
#include "str_replace.h"
#include "mac_util.h"
#include "translate.h"
#define boinc_master_user_name "boinc_master"
#define boinc_master_group_name "boinc_master"
#define boinc_project_user_name "boinc_project"
#define boinc_project_group_name "boinc_project"
OSErr Initialize(void); /* function prototypes */
Boolean IsUserMemberOfGroup(const char *userName, const char *groupName);
Boolean IsRestartNeeded();
static void GetPreferredLanguages();
static void LoadPreferredLanguages();
static void ShowMessage(const char *format, ...);
static OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon);
int callPosixSpawn(const char *cmd);
void print_to_log_file(const char *format, ...);
void strip_cr(char *buf);
// We can't use translation because the translation catalogs
// have not yet been installed when this application is run.
#define MAX_LANGUAGES_TO_TRY 5
#define REPORT_ERROR(isError) if (isError) print_to_log_file("BOINC Installer error at line %d", __LINE__);
static char * Catalog_Name = (char *)"BOINC-Setup";
static char Catalogs_Dir[MAXPATHLEN];
static char loginName[256];
static char tempDirName[MAXPATHLEN];
Boolean gQuitFlag = false; /* global */
#if 0
CFStringRef valueRestartRequired = CFSTR("RequiredRestart");
CFStringRef valueLogoutRequired = CFSTR("RequiredLogout");
CFStringRef valueNoRestart = CFSTR("NoRestart");
#endif
int main(int argc, char *argv[])
{
char pkgPath[MAXPATHLEN];
char postInstallAppPath[MAXPATHLEN];
char temp[MAXPATHLEN];
char brand[64], s[256];
char *p;
OSStatus err = noErr;
Boolean restartNeeded = true;
FILE *restartNeededFile;
if (Initialize() != noErr) {
return 0;
}
strncpy(loginName, getenv("USER"), sizeof(loginName)-1);
if (loginName[0] == '\0') {
ShowMessage((char *)_("Could not get user login name"));
return 0;
}
snprintf(tempDirName, sizeof(tempDirName), "InstallBOINC-%s", loginName);
snprintf(temp, sizeof(temp), "/tmp/%s", tempDirName);
mkdir(temp, 0777);
chmod(temp, 0777); // Needed because mkdir sets permissions restricted by umask (022)
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/BOINC_Installer_Errors", tempDirName);
err = callPosixSpawn(temp);
snprintf(Catalogs_Dir, sizeof(Catalogs_Dir),
"/tmp/%s/BOINC_payload/Library/Application Support/BOINC Data/locale/",
tempDirName);
// Get the full path to Installer package inside this application's bundle
getPathToThisApp(pkgPath, sizeof(pkgPath));
strlcpy(temp, pkgPath, sizeof(temp));
strlcat(pkgPath, "/Contents/Resources/", sizeof(pkgPath));
strlcpy(postInstallAppPath, pkgPath, sizeof(postInstallAppPath));
strlcat(postInstallAppPath, "PostInstall.app", sizeof(postInstallAppPath));
// To allow for branding, assume name of installer package inside bundle corresponds to name of this application
p = strrchr(temp, '/'); // Point to name of this application (e.g., "BOINC Installer.app")
if (p == NULL)
p = temp - 1;
strlcpy(brand, p+1, sizeof(brand));
strlcat(pkgPath, p+1, sizeof(pkgPath));
p = strrchr(pkgPath, ' '); // Strip off last space character and everything following
if (p)
*p = '\0';
p = strstr(brand, " Installer.app"); // Strip off trailing " Installer.app"
if (p)
*p = '\0';
strlcat(pkgPath, ".pkg", sizeof(pkgPath));
// In the unlikely situation that /tmp has files from an earlier attempt to install
// BOINC by a different user, we won't have permission to delete or overwrite them,
// so include the current user's name as part of the paths to our temporary files.
// Expand the installer package
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/BOINC.pkg", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/expanded_BOINC.pkg", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/PostInstall.app", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
snprintf(temp, sizeof(temp), "rm -f /tmp/%s/BOINC_preferred_languages", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
snprintf(temp, sizeof(temp), "rm -f /tmp/%s/BOINC_restart_flag", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
sprintf(temp, "cp -fpR \"%s\" /tmp/%s/PostInstall.app", postInstallAppPath, tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
sprintf(temp, "pkgutil --expand \"%s\" /tmp/%s/expanded_BOINC.pkg", pkgPath, tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
if (err == noErr) {
GetPreferredLanguages();
}
if (compareOSVersionTo(10, 6) < 0) {
LoadPreferredLanguages();
BringAppToFront();
p = strrchr(brand, ' '); // Strip off last space character and everything following
if (p)
*p = '\0';
ShowMessage((char *)_("Sorry, this version of %s requires system 10.6 or higher."), brand);
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/BOINC_payload", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
return -1;
}
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/BOINC_payload", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
// Remove previous installer package receipt so we can run installer again
// (affects only older versions of OS X and fixes a bug in those versions)
// "rm -rf /Library/Receipts/GridRepublic.pkg"
sprintf(s, "rm -rf \"/Library/Receipts/%s.pkg\"", brand);
err = callPosixSpawn (s);
REPORT_ERROR(err);
restartNeeded = IsRestartNeeded();
// Write a temp file to tell our PostInstall.app whether restart is needed
snprintf(temp, sizeof(temp), "/tmp/%s/BOINC_restart_flag", tempDirName);
restartNeededFile = fopen(temp, "w");
if (restartNeededFile) {
fputs(restartNeeded ? "1\n" : "0\n", restartNeededFile);
fclose(restartNeededFile);
}
if (restartNeeded) {
if (err == noErr) {
// Change onConclusion="none" to onConclusion="RequireRestart"
snprintf(temp, sizeof(temp), "sed -i \".bak\" s/onConclusion=\"none\"/onConclusion=\"RequireRestart\"/g /tmp/%s/expanded_BOINC.pkg/Distribution", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
}
if (err == noErr) {
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/expanded_BOINC.pkg/Distribution.bak", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
// Flatten the installer package
sprintf(temp, "pkgutil --flatten /tmp/%s/expanded_BOINC.pkg /tmp/%s/%s.pkg", tempDirName, tempDirName, brand);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
}
if (err == noErr) {
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/expanded_BOINC.pkg", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
sprintf(temp, "open \"/tmp/%s/%s.pkg\"", tempDirName, brand);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
return err;
}
}
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/expanded_BOINC.pkg", tempDirName);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
sprintf(temp, "open \"%s\"", pkgPath);
err = callPosixSpawn(temp);
REPORT_ERROR(err);
return err;
}
Boolean IsUserMemberOfGroup(const char *userName, const char *groupName) {
group *grp;
short i = 0;
char *p;
grp = getgrnam(groupName);
if (!grp) {
printf("getgrnam(%s) failed\n", groupName);
fflush(stdout);
return false; // Group not found
}
while ((p = grp->gr_mem[i]) != NULL) { // Step through all users in group admin
if (strcmp(p, userName) == 0) {
return true;
}
++i;
}
return false;
}
Boolean IsRestartNeeded()
{
passwd *pw = NULL;
group *grp = NULL;
gid_t boinc_master_gid = 0, boinc_project_gid = 0;
uid_t boinc_master_uid = 0, boinc_project_uid = 0;
if (compareOSVersionTo(10, 9) >= 0) {
return false;
}
grp = getgrnam(boinc_master_group_name);
if (grp == NULL)
return true; // Group boinc_master does not exist
boinc_master_gid = grp->gr_gid;
grp = getgrnam(boinc_project_group_name);
if (grp == NULL)
return true; // Group boinc_project does not exist
boinc_project_gid = grp->gr_gid;
pw = getpwnam(boinc_master_user_name);
if (pw == NULL)
return true; // User boinc_master does not exist
boinc_master_uid = pw->pw_uid;
if (pw->pw_gid != boinc_master_gid)
return true; // User boinc_master does not have group boinc_master as its primary group
pw = getpwnam(boinc_project_user_name);
if (pw == NULL)
return true; // User boinc_project does not exist
boinc_project_uid = pw->pw_uid;
if (pw->pw_gid != boinc_project_gid)
return true; // User boinc_project does not have group boinc_project as its primary group
if (compareOSVersionTo(10, 5) >= 0) {
if (boinc_master_gid < 501)
return true; // We will change boinc_master_gid to a value > 501
if (boinc_project_gid < 501)
return true; // We will change boinc_project_gid to a value > 501
if (boinc_master_uid < 501)
return true; // We will change boinc_master_uid to a value > 501
if (boinc_project_uid < 501)
return true; // We will change boinc_project_uid to a value > 501
}
#ifdef SANDBOX
if (loginName[0]) {
if (IsUserMemberOfGroup(loginName, boinc_master_group_name)) {
return false; // Logged in user is already a member of group boinc_master
}
}
#endif // SANDBOX
return true;
}
OSErr Initialize() /* Initialize some managers */
{
// InitCursor();
return AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP((AEEventHandlerProcPtr)QuitAppleEventHandler), 0, false );
}
// Because language preferences are set on a per-user basis, we
// must get the preferred languages while set to the current
// user, before the Apple Installer switches us to root.
// So we get the preferred languages here and write them to a
// temporary file to be retrieved by our PostInstall app.
static void GetPreferredLanguages() {
DIR *dirp;
struct dirent *dp;
char temp[MAXPATHLEN];
char searchPath[MAXPATHLEN];
char savedWD[MAXPATHLEN];
struct stat sbuf;
CFMutableArrayRef supportedLanguages;
CFStringRef aLanguage;
char shortLanguage[32];
CFArrayRef preferredLanguages;
int i, j, k;
char * language;
char *uscore;
FILE *f;
getcwd(savedWD, sizeof(savedWD));
snprintf(temp, sizeof(temp), "rm -dfR /tmp/%s/BOINC_payload", tempDirName);
callPosixSpawn(temp);
snprintf(temp, sizeof(temp), "/tmp/%s/BOINC_payload", tempDirName);
mkdir(temp, 0777);
chmod(temp, 0777); // Needed because mkdir sets permissions restricted by umask (022)
chdir(temp);
snprintf(temp, sizeof(temp), "cpio -i -I /tmp/%s/expanded_BOINC.pkg/BOINC.pkg/Payload", tempDirName);
callPosixSpawn(temp);
chdir(savedWD);
// Create an array of all our supported languages
supportedLanguages = CFArrayCreateMutable(kCFAllocatorDefault, 100, &kCFTypeArrayCallBacks);
aLanguage = CFStringCreateWithCString(NULL, "en", kCFStringEncodingMacRoman);
CFArrayAppendValue(supportedLanguages, aLanguage);
CFRelease(aLanguage);
aLanguage = NULL;
dirp = opendir(Catalogs_Dir);
if (!dirp) {
REPORT_ERROR(true);
goto cleanup;
}
while (true) {
dp = readdir(dirp);
if (dp == NULL)
break; // End of list
if (dp->d_name[0] == '.')
continue; // Ignore names beginning with '.'
strlcpy(searchPath, Catalogs_Dir, sizeof(searchPath));
strlcat(searchPath, dp->d_name, sizeof(searchPath));
strlcat(searchPath, "/", sizeof(searchPath));
strlcat(searchPath, Catalog_Name, sizeof(searchPath));
strlcat(searchPath, ".mo", sizeof(searchPath));
if (stat(searchPath, &sbuf) != 0) continue;
// printf("Adding %s to supportedLanguages array\n", dp->d_name);
aLanguage = CFStringCreateWithCString(NULL, dp->d_name, kCFStringEncodingMacRoman);
CFArrayAppendValue(supportedLanguages, aLanguage);
CFRelease(aLanguage);
aLanguage = NULL;
// If it has a region code ("it_IT") also try without region code ("it")
// TODO: Find a more general solution
strlcpy(shortLanguage, dp->d_name, sizeof(shortLanguage));
uscore = strchr(shortLanguage, '_');
if (uscore) {
*uscore = '\0';
aLanguage = CFStringCreateWithCString(NULL, shortLanguage, kCFStringEncodingMacRoman);
CFArrayAppendValue(supportedLanguages, aLanguage);
CFRelease(aLanguage);
aLanguage = NULL;
}
}
closedir(dirp);
// Write a temp file to tell our PostInstall.app our preferred languages
snprintf(temp, sizeof(temp), "/tmp/%s/BOINC_preferred_languages", tempDirName);
f = fopen(temp, "w");
if (!f) {
REPORT_ERROR(true);
goto cleanup;
}
for (i=0; i<MAX_LANGUAGES_TO_TRY; ++i) {
preferredLanguages = CFBundleCopyLocalizationsForPreferences(supportedLanguages, NULL );
#if 0 // For testing
int c = CFArrayGetCount(preferredLanguages);
for (k=0; k<c; ++k) {
CFStringRef s = (CFStringRef)CFArrayGetValueAtIndex(preferredLanguages, k);
printf("Preferred language %u is %s\n", k, CFStringGetCStringPtr(s, kCFStringEncodingMacRoman));
}
#endif
for (j=0; j<CFArrayGetCount(preferredLanguages); ++j) {
aLanguage = (CFStringRef)CFArrayGetValueAtIndex(preferredLanguages, j);
language = (char *)CFStringGetCStringPtr(aLanguage, kCFStringEncodingMacRoman);
if (language == NULL) {
if (CFStringGetCString(aLanguage, shortLanguage, sizeof(shortLanguage), kCFStringEncodingMacRoman)) {
language = shortLanguage;
}
}
if (f && language) {
fprintf(f, "%s\n", language);
#if CREATE_LOG
print_to_log_file("Adding language: %s\n", language);
#endif
}
// Remove all copies of this language from our list of supported languages
// so we can get the next preferred language in order of priority
for (k=CFArrayGetCount(supportedLanguages)-1; k>=0; --k) {
if (CFStringCompare(aLanguage, (CFStringRef)CFArrayGetValueAtIndex(supportedLanguages, k), 0) == kCFCompareEqualTo) {
CFArrayRemoveValueAtIndex(supportedLanguages, k);
}
}
// Since the original strings are English, no
// further translation is needed for language en.
if (language) {
if (!strcmp(language, "en")) {
fclose(f);
CFRelease(preferredLanguages);
preferredLanguages = NULL;
goto cleanup;
}
}
}
CFRelease(preferredLanguages);
preferredLanguages = NULL;
}
if (f) {
fprintf(f, "en\n");
fclose(f);
}
cleanup:
CFArrayRemoveAllValues(supportedLanguages);
CFRelease(supportedLanguages);
supportedLanguages = NULL;
#if CREATE_LOG
print_to_log_file("Exiting GetPreferredLanguages");
#endif
}
static void LoadPreferredLanguages(){
FILE *f;
int i;
char *p;
char language[32];
char temp[MAXPATHLEN];
BOINCTranslationInit();
// GetPreferredLanguages() wrote a list of our preferred languages to a temp file
snprintf(temp, sizeof(temp), "/tmp/%s/BOINC_preferred_languages", tempDirName);
f = fopen(temp, "r");
if (!f) {
REPORT_ERROR(true);
return;
}
for (i=0; i<MAX_LANGUAGES_TO_TRY; ++i) {
fgets(language, sizeof(language), f);
if (feof(f)) break;
language[sizeof(language)-1] = '\0'; // Guarantee a null terminator
p = strchr(language, '\n');
if (p) *p = '\0'; // Replace newline with null terminator
if (language[0]) {
if (!BOINCTranslationAddCatalog(Catalogs_Dir, language, Catalog_Name)) {
REPORT_ERROR(true);
printf("could not load catalog for langage %s\n", language);
}
}
}
fclose(f);
}
static void ShowMessage(const char *format, ...) {
// CAUTION: vsprintf will produce undesirable results if the string
// contains a % character that is not a format specification!
// But CFString is OK!
va_list args;
char s[1024];
CFOptionFlags responseFlags;
CFURLRef myIconURLRef = NULL;
CFBundleRef myBundleRef;
myBundleRef = CFBundleGetMainBundle();
if (myBundleRef) {
myIconURLRef = CFBundleCopyResourceURL(myBundleRef, CFSTR("MacInstaller.icns"), NULL, NULL);
}
#if 1
va_start(args, format);
vsprintf(s, format, args);
va_end(args);
#else
strcpy(s, format);
#endif
// If defaultButton is nil or an empty string, a default localized
// button title ("OK" in English) is used.
CFStringRef myString = CFStringCreateWithCString(NULL, s, kCFStringEncodingUTF8);
BringAppToFront();
CFUserNotificationDisplayAlert(0.0, kCFUserNotificationPlainAlertLevel,
myIconURLRef, NULL, NULL, CFSTR(" "), myString,
NULL, NULL, NULL,
&responseFlags);
if (myIconURLRef) CFRelease(myIconURLRef);
if (myString) CFRelease(myString);
}
static OSErr QuitAppleEventHandler( const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon )
{
gQuitFlag = true;
return noErr;
}
#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;
}
#include <spawn.h>
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_posix_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_TEST
print_to_log_file("***********");
for (int i=0; i<argc; ++i) {
print_to_log_file("argv[%d]=%s", i, argv[i]);
}
print_to_log_file("***********\n");
#endif
errno = 0;
result = posix_spawnp(&thePid, progPath, NULL, NULL, argv, environ);
#if VERBOSE_TEST
print_to_log_file("callPosixSpawn command: %s", cmdline);
print_to_log_file("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_TEST
print_to_log_file("waitpid() returned status=%d", status);
#endif
result = status;
} else {
if (WIFEXITED(status)) {
result = WEXITSTATUS(status);
if (result == 1) {
#if VERBOSE_TEST
print_to_log_file("WEXITSTATUS(status) returned 1, errno=%d: %s", errno, strerror(errno));
#endif
result = errno;
}
#if VERBOSE_TEST
else if (result) {
print_to_log_file("WEXITSTATUS(status) returned %d", result);
}
#endif
} // end if (WIFEXITED(status)) else
} // end if waitpid returned 0 sstaus else
return result;
}
// For debugging
void print_to_log_file(const char *format, ...) {
FILE *f;
va_list args;
char buf[MAXPATHLEN];
time_t t;
// strlcpy(buf, getenv("HOME"), sizeof(buf));
// strlcat(buf, "/Documents/test_log.txt", sizeof(buf));
snprintf(buf, sizeof(buf), "/tmp/%s/BOINC_Installer_Errors", tempDirName);
f = fopen(buf, "a");
if (!f) return;
// freopen(buf, "a", stdout);
// freopen(buf, "a", stderr);
time(&t);
strlcpy(buf, asctime(localtime(&t)),sizeof(buf));
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);
}
void strip_cr(char *buf)
{
char *theCR;
theCR = strrchr(buf, '\n');
if (theCR)
*theCR = '\0';
theCR = strrchr(buf, '\r');
if (theCR)
*theCR = '\0';
}