-
Notifications
You must be signed in to change notification settings - Fork 2
/
firmwaresyncd.c
853 lines (701 loc) · 23.3 KB
/
firmwaresyncd.c
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
* Copyright (c) 2007 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* firmwaresyncd.c
* bless
*
* Created by Shantonu Sen on 10/13/07.
* Copyright 2007 Apple Inc. All rights reserved.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <spawn.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <syslog.h>
#include <sysexits.h>
#include <copyfile.h>
#include <sys/sysctl.h>
#include <DiskArbitration/DiskArbitration.h>
#include <DiskArbitration/DiskArbitrationPrivate.h>
#include <IOKit/kext/kextmanager_types.h>
#include <IOKit/kext/kextmanager_mig.h>
#include <mach/mach.h>
#include <servers/bootstrap.h>
#include <sys/resource.h>
#include "bless.h"
#include "bless_private.h"
#define kFirmwareFileOSPath "/usr/standalone/i386/Firmware.scap"
#define kFirmwareFileEFIDir "/EFI/APPLE/EXTENSIONS"
#define kFirmwareFileEFIPath "/EFI/APPLE/EXTENSIONS/Firmware.scap"
#define kTimeDelay (4*60)
#define kTSCacheDir "/System/Library/Caches/com.apple.bootstamps"
extern char **environ;
void usage(void);
void catch_sigterm(int sig);
static bool gSIGTERM = false;
/* Should we even run? If not, exit out. If so, use the volume UUID for / */
bool should_run(void);
bool get_uuid(CFUUIDRef *uuid);
bool allocate_mach_ports(mach_port_t *kextdport, mach_port_t *vollock);
bool deallocate_mach_ports(mach_port_t kextdport, mach_port_t vollock);
bool lock_volume(CFUUIDRef uuid, mach_port_t kextdport, mach_port_t vollock);
bool unlock_volume(CFUUIDRef uuid, mach_port_t kextdport, mach_port_t vollock);
bool generate_timestamp_path(CFUUIDRef uuid, char *path);
bool check_if_uptodate(CFUUIDRef uuid);
bool update_esp(void);
bool update_timestamp(CFUUIDRef uuid);
bool run_tool(char *argv[], CFDataRef *output);
int main(int argc, char *argv[]) {
int ch;
int opt_d = 0;
CFUUIDRef uuid = NULL;
mach_port_t vollock = MACH_PORT_NULL, kextdport = MACH_PORT_NULL;
bool needunlock = false, immediately = false;
unsigned int sleepleft;
signal(SIGTERM, catch_sigterm);
while ((ch = getopt(argc, argv, "di")) != -1) {
switch (ch) {
case 'd':
opt_d = 1;
break;
case 'i':
immediately = true;
break;
case '?':
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
openlog(getprogname(), LOG_PID | (opt_d ? LOG_PERROR : 0), LOG_DAEMON);
setlogmask(opt_d ? LOG_UPTO(LOG_DEBUG) : LOG_UPTO(LOG_ERR));
// syslog(LOG_INFO, "This is informational");
// syslog(LOG_DEBUG, "This is debuggingational");
// In general we try exit on failures without complaining
if (!should_run()) {
goto done;
}
syslog(LOG_DEBUG, "Preflight passed");
if (!get_uuid(&uuid)) {
goto done;
}
if (!check_if_uptodate(uuid)) {
goto done;
}
sleepleft = (immediately ? 0 : kTimeDelay);
syslog(LOG_DEBUG, "Sleeping for %u seconds", sleepleft);
do {
if (gSIGTERM) {
syslog(LOG_DEBUG, "Caught SIGTERM and exiting");
goto done;
}
sleepleft = sleep(sleepleft);
} while (sleepleft > 0);
syslog(LOG_DEBUG, "Done sleeping");
if (!allocate_mach_ports(&kextdport, &vollock)) {
goto done;
}
// relock, in case the state changed while we were asleep
if (!lock_volume(uuid, kextdport, vollock)) {
goto done;
}
needunlock = true;
if (!check_if_uptodate(uuid)) {
goto done;
}
if (!update_esp()) {
goto done;
}
if (!update_timestamp(uuid)) {
goto done;
}
if (!unlock_volume(uuid, kextdport, vollock)) {
goto done;
}
needunlock = false;
done:
if (needunlock) {
unlock_volume(uuid, kextdport, vollock);
}
if (kextdport != MACH_PORT_NULL || vollock != MACH_PORT_NULL) {
deallocate_mach_ports(kextdport, vollock);
}
if (uuid) {
CFRelease(uuid);
}
closelog();
return 0;
}
void usage(void)
{
fprintf(stderr, "Usage: %s [-d]\n", getprogname());
exit(EX_USAGE);
}
void catch_sigterm(int sig)
{
gSIGTERM = true;
}
bool should_run(void)
{
bool result = false;
BLPreBootEnvType preBootType;
struct stat sb;
uint32_t safeboot = 0;
size_t safebootsize = sizeof(safeboot);
int ret;
ret = sysctlbyname("kern.safeboot", &safeboot, &safebootsize, NULL, 0);
if (ret) {
syslog(LOG_DEBUG, "Could not determine safeboot status: %s", strerror(errno));
return result;
}
syslog(LOG_DEBUG, "Safeboot status: %u", safeboot);
if (safeboot) {
return result;
}
if (0 != BLGetPreBootEnvironmentType(NULL, &preBootType)) {
syslog(LOG_DEBUG, "Could not determine preboot environment type");
return result;
}
if (preBootType != kBLPreBootEnvType_EFI) {
syslog(LOG_DEBUG, "Preboot environment type is not EFI");
return result;
}
if (0 != lstat(kFirmwareFileOSPath, &sb) || !S_ISREG(sb.st_mode)) {
syslog(LOG_DEBUG, "Font file %s is not accessible or not a regular file", kFirmwareFileOSPath);
return result;
}
result = true;
return result;
}
static void _DADiskAppearedCallback( DADiskRef disk, void * context );
static void _DADiskAppearedCallback( DADiskRef disk, void * context )
{
CFUUIDRef *uuid = (CFUUIDRef *)context;
CFUUIDRef dauuid = NULL;
CFDictionaryRef dadescription;
dadescription = DADiskCopyDescription(disk);
if (dadescription) {
dauuid = CFDictionaryGetValue(dadescription, kDADiskDescriptionVolumeUUIDKey);
if (dauuid) {
if (*uuid) {
CFRelease(*uuid);
}
*uuid = CFRetain(dauuid);
}
CFRelease(dadescription);
}
}
bool get_uuid(CFUUIDRef *uuid)
{
DASessionRef dasession;
CFURLRef rootpath;
CFMutableDictionaryRef matchdict;
bool result = false;
*uuid = NULL;
rootpath = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)"/", 1, true);
if (rootpath) {
dasession = DASessionCreate(kCFAllocatorDefault);
if (dasession) {
DASessionScheduleWithRunLoop(dasession, CFRunLoopGetCurrent(), CFSTR("FIRMWARESYNCD"));
matchdict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(matchdict, kDADiskDescriptionVolumePathKey, rootpath);
DARegisterDiskAppearedCallback(dasession, matchdict, _DADiskAppearedCallback, uuid);
CFRelease(matchdict);
while (!gSIGTERM && !*uuid) {
CFRunLoopRunInMode(CFSTR("FIRMWARESYNCD"), 1.0, true);
}
DASessionUnscheduleFromRunLoop(dasession, CFRunLoopGetCurrent(), CFSTR("FIRMWARESYNCD"));
CFRelease(dasession);
}
CFRelease(rootpath);
}
if (*uuid) {
result = true;
syslog(LOG_DEBUG, "Determined volume UUID for /");
} else {
syslog(LOG_DEBUG, "Could not determine volume UUID for /");
}
return result;
}
bool lock_volume(CFUUIDRef uuid, mach_port_t kextdport, mach_port_t vollock)
{
bool result = false;
kern_return_t kret;
uuid_t s_vol_uuid;
CFUUIDBytes uuidbytes;
int lckres = 0;
uuidbytes = CFUUIDGetUUIDBytes(uuid);
memcpy(&s_vol_uuid, &uuidbytes, sizeof(uuidbytes));
syslog(LOG_DEBUG, "Locking volume");
kret = kextmanager_lock_volume(kextdport, vollock, s_vol_uuid,
1 /* block */, &lckres);
if (kret || lckres) {
syslog(LOG_DEBUG, "Failed to obtain lock: %s/%s", mach_error_string(kret), strerror(lckres));
goto done;
}
result = true;
done:
return result;
}
bool unlock_volume(CFUUIDRef uuid, mach_port_t kextdport, mach_port_t vollock)
{
bool result = false;
kern_return_t kret;
uuid_t s_vol_uuid;
CFUUIDBytes uuidbytes;
uuidbytes = CFUUIDGetUUIDBytes(uuid);
memcpy(&s_vol_uuid, &uuidbytes, sizeof(uuidbytes));
syslog(LOG_DEBUG, "Unlocking volume");
kret = kextmanager_unlock_volume(kextdport, vollock, s_vol_uuid,
0);
if (kret) {
syslog(LOG_DEBUG, "Failed to unlock: %s", mach_error_string(kret));
goto done;
}
result = true;
done:
return result;
}
bool allocate_mach_ports(mach_port_t *kextdport, mach_port_t *vollock)
{
bool result = false;
kern_return_t kret;
mach_port_t sLockPort = MACH_PORT_NULL;
mach_port_t sKextdPort = MACH_PORT_NULL;
syslog(LOG_DEBUG, "Obtaining mach ports");
if (sKextdPort == MACH_PORT_NULL) {
kret = bootstrap_look_up(bootstrap_port, KEXTD_SERVER_NAME, &sKextdPort);
if (kret) {
syslog(LOG_DEBUG, "Failed to look up port for %s: %s", KEXTD_SERVER_NAME, mach_error_string(kret));
goto done;
}
}
if (sLockPort == MACH_PORT_NULL) {
kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &sLockPort);
if (kret) {
syslog(LOG_DEBUG, "Failed to look up port for %s: %s", KEXTD_SERVER_NAME, mach_error_string(kret));
goto done;
}
}
result = true;
*kextdport = sKextdPort;
*vollock = sLockPort;
done:
return result;
}
bool deallocate_mach_ports(mach_port_t kextdport, mach_port_t vollock)
{
bool result = false;
kern_return_t kret;
syslog(LOG_DEBUG, "Deallocating mach ports");
if (kextdport != MACH_PORT_NULL) {
kret = mach_port_mod_refs(mach_task_self(), kextdport, MACH_PORT_RIGHT_SEND, -1);
if (kret) {
syslog(LOG_DEBUG, "Failed to drop reference for %s: %s", KEXTD_SERVER_NAME, mach_error_string(kret));
goto done;
}
}
if (vollock != MACH_PORT_NULL) {
kret = mach_port_mod_refs(mach_task_self(), vollock, MACH_PORT_RIGHT_RECEIVE, -1);
if (kret) {
syslog(LOG_DEBUG, "Failed to drop reference for lock %s", mach_error_string(kret));
goto done;
}
}
result = true;
done:
return result;
}
bool generate_timestamp_path(CFUUIDRef uuid, char *path)
{
char timepath[MAXPATHLEN];
char colonpath[MAXPATHLEN], *cptr;
char uuidcomponent[NAME_MAX];
CFStringRef uuidstr;
bool result = false;
uuidstr = CFUUIDCreateString(kCFAllocatorDefault, uuid);
if (!CFStringGetCString(uuidstr, uuidcomponent, sizeof(uuidcomponent), kCFStringEncodingUTF8)) {
CFRelease(uuidstr);
goto done;
}
CFRelease(uuidstr);
strlcpy(colonpath, kFirmwareFileOSPath, sizeof(colonpath));
cptr = strchr(colonpath, '/');
while (cptr) {
*cptr++ = ':';
cptr = strchr(cptr, '/');
}
// we check for overflow on the last operation, since it should be cumulative
strlcpy(timepath, kTSCacheDir, sizeof(timepath));
strlcat(timepath, "/", sizeof(timepath));
strlcat(timepath, uuidcomponent, sizeof(timepath));
strlcat(timepath, "/", sizeof(timepath));
if (strlcat(timepath, colonpath, sizeof(timepath)) >= sizeof(timepath)) {
goto done;
}
strlcpy(path, timepath, MAXPATHLEN);
result = true;
done:
return result;
}
/* Check in /S/L/C if we have a cookie file newer than the font file */
bool check_if_uptodate(CFUUIDRef uuid)
{
bool result = false, needupdate = false;
struct stat sb, sb2;
int ret;
char timepath[MAXPATHLEN];
// ...
if (0 != lstat(kFirmwareFileOSPath, &sb)) {
syslog(LOG_DEBUG, "Could not access %s: %s", kFirmwareFileOSPath, strerror(errno));
goto done;
}
if (!S_ISREG(sb.st_mode)) {
syslog(LOG_DEBUG, "%s is not a regular file", kFirmwareFileOSPath);
goto done;
}
if (!generate_timestamp_path(uuid, timepath)) {
syslog(LOG_DEBUG, "Could not generate path (%s)", timepath);
goto done;
}
syslog(LOG_DEBUG, "Timestamp file is %s", timepath);
ret = lstat(timepath, &sb2);
if (ret) {
if (errno == ENOENT) {
syslog(LOG_DEBUG, "Timestamp does not exist");
needupdate = true;
} else {
ret = unlink(timepath);
if (ret) {
syslog(LOG_DEBUG, "Could not remove %s", timepath);
goto done;
}
needupdate = true;
}
} else {
struct timeval filetime, stamptime;
if (!S_ISREG(sb2.st_mode)) {
syslog(LOG_DEBUG, "%s is not a regular file", timepath);
goto done;
}
TIMESPEC_TO_TIMEVAL(&filetime, &sb.st_mtimespec);
TIMESPEC_TO_TIMEVAL(&stamptime, &sb2.st_mtimespec);
if (timercmp(&filetime, &stamptime, >)) {
syslog(LOG_DEBUG, "File newer than timestamp");
needupdate = true;
} else {
syslog(LOG_DEBUG, "File matches timestamp");
needupdate = false;
}
}
if (needupdate) {
result = true;
}
done:
return result;
}
bool update_esp(void)
{
bool result = false, needunmount = false, needrmdir = false;
struct statfs sb;
int ret;
CFDictionaryRef dict = NULL;
CFArrayRef array = NULL;
CFStringRef esp = NULL;
char *newargv[10];
int newargc;
char *slash;
char espname[MAXPATHLEN], espdev[MAXPATHLEN], mntpath[MAXPATHLEN], espfontpath[MAXPATHLEN];
CFDataRef output = NULL;
ret = statfs("/", &sb);
if (ret) {
syslog(LOG_DEBUG, "Failed to statfs /: %s", strerror(errno));
goto done;
}
if (0 != strncmp(sb.f_mntfromname, "/dev/", 5)) {
goto done;
}
ret = BLCreateBooterInformationDictionary(NULL, sb.f_mntfromname + 5, &dict);
if (ret) {
syslog(LOG_DEBUG, "Failed to obtain EFI System Partition information: %d", ret);
goto done;
}
array = CFDictionaryGetValue(dict, kBLSystemPartitionsKey);
if(array) {
if(CFArrayGetCount(array) > 0) {
esp = CFArrayGetValueAtIndex(array, 0);
if (!BLIsEFIRecoveryAccessibleDevice(NULL, esp)) {
syslog(LOG_DEBUG, "ESP %s is not accessible as a recovery device\n" , BLGetCStringDescription(esp));
goto done;
}
}
}
if(!esp) {
// needed ESP, but could not find it
syslog(LOG_DEBUG, "No appropriate ESP for %s\n" , "/");
goto done;
}
if (!CFStringGetCString(esp, espname, sizeof(espname), kCFStringEncodingUTF8)) {
goto done;
}
CFRelease(dict);
dict = NULL;
esp = NULL;
strlcpy(espdev, "/dev/", sizeof(espdev));
strlcat(espdev, espname, sizeof(espdev));
syslog(LOG_DEBUG, "ESP partition is %s", espdev);
syslog(LOG_DEBUG, "Verifying %s", espdev);
newargc = 0;
newargv[newargc++] = "/sbin/fsck_msdos";
newargv[newargc++] = "-fn";
newargv[newargc++] = espdev;
newargv[newargc++] = NULL;
if (!run_tool(newargv, &output)) {
if (output) {
syslog(LOG_ERR, "Command %s output: %.*s", newargv[0], (int)CFDataGetLength(output), CFDataGetBytePtr(output));
CFRelease(output);
}
goto done;
}
if (output) {
CFRelease(output);
}
strlcpy(mntpath, "/Volumes/firmwaresyncd.XXXXXX", sizeof(mntpath));
if(!mkdtemp(mntpath)) {
syslog(LOG_DEBUG, "Could not make temporary directory %s", mntpath);
goto done;
}
needrmdir = true;
syslog(LOG_DEBUG, "Temporary mount point is %s", mntpath);
newargc = 0;
newargv[newargc++] = "/sbin/mount";
newargv[newargc++] = "-t";
newargv[newargc++] = "msdos";
newargv[newargc++] = "-o";
newargv[newargc++] = "perm";
newargv[newargc++] = "-o";
newargv[newargc++] = "nobrowse";
newargv[newargc++] = espdev;
newargv[newargc++] = mntpath;
newargv[newargc++] = NULL;
if (!run_tool(newargv, &output)) {
if (output) {
syslog(LOG_ERR, "Command %s output: %.*s", newargv[0], (int)CFDataGetLength(output), CFDataGetBytePtr(output));
CFRelease(output);
}
goto done;
}
if (output) {
CFRelease(output);
}
needunmount = true;
strlcpy(espfontpath, mntpath, sizeof(espfontpath));
strlcat(espfontpath, kFirmwareFileEFIDir, sizeof(espfontpath));
// make parent directories if they are not present
slash = espfontpath + strlen(mntpath);
while (strsep(&slash, "/")) {
syslog(LOG_DEBUG, "Checking %s", espfontpath);
ret = mkdir(espfontpath, S_IRWXU);
if (ret && errno != EEXIST) {
syslog(LOG_DEBUG, "Failed to create %s: %s", espfontpath, strerror(errno));
break;
}
if (slash == NULL) {
break;
}
*(slash - 1) = '/';
}
strlcpy(espfontpath, mntpath, sizeof(espfontpath));
strlcat(espfontpath, kFirmwareFileEFIPath, sizeof(espfontpath));
ret = copyfile(kFirmwareFileOSPath, espfontpath, NULL, COPYFILE_DATA);
if (ret) {
syslog(LOG_DEBUG, "Could not copy %s to %s: %d", kFirmwareFileOSPath, espfontpath, ret);
goto done;
}
syslog(LOG_DEBUG, "Copied %s to %s", kFirmwareFileOSPath, espfontpath);
result = true;
done:
if (needunmount) {
newargc = 0;
newargv[newargc++] = "/sbin/umount";
newargv[newargc++] = mntpath;
newargv[newargc++] = NULL;
if (!run_tool(newargv, &output)) {
if (output) {
syslog(LOG_ERR, "Command %s output: %.*s", newargv[0], (int)CFDataGetLength(output), CFDataGetBytePtr(output));
CFRelease(output);
}
goto done2;
}
if (output) {
CFRelease(output);
}
}
done2:
if (needrmdir) {
syslog(LOG_DEBUG, "Removing %s\n", mntpath);
ret = rmdir(mntpath);
}
if (dict) {
CFRelease(dict);
}
return result;
}
bool update_timestamp(CFUUIDRef uuid)
{
bool result = false, closefd = false;;
char timepath[MAXPATHLEN];
int ret, tfd;
struct stat sb;
struct timeval times[2];
if (0 != lstat(kFirmwareFileOSPath, &sb)) {
syslog(LOG_DEBUG, "Could not access %s: %s", kFirmwareFileOSPath, strerror(errno));
goto done;
}
if (!S_ISREG(sb.st_mode)) {
syslog(LOG_DEBUG, "%s is not a regular file", kFirmwareFileOSPath);
goto done;
}
if (!generate_timestamp_path(uuid, timepath)) {
syslog(LOG_DEBUG, "Could not generate path (%s)", timepath);
goto done;
}
syslog(LOG_DEBUG, "Updating timestamp file %s", timepath);
ret = unlink(timepath);
if (ret && errno != ENOENT) {
syslog(LOG_DEBUG, "Could not remove %s: %s", timepath, strerror(errno));
goto done;
}
tfd = open(timepath, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (tfd < 0) {
syslog(LOG_DEBUG, "Could not create %s: %s", timepath, strerror(errno));
goto done;
}
closefd = true;
TIMESPEC_TO_TIMEVAL(×[0], &sb.st_atimespec);
TIMESPEC_TO_TIMEVAL(×[1], &sb.st_mtimespec);
ret = futimes(tfd, times);
if (ret) {
syslog(LOG_DEBUG, "Could not set time for %s: %s", timepath, strerror(errno));
goto done;
}
result = true;
done:
if (closefd) {
ret = close(tfd);
if (ret) {
syslog(LOG_DEBUG, "Could not close %s: %s", timepath, strerror(errno));
}
}
return result;
}
bool run_tool(char *argv[], CFDataRef *output)
{
bool result = false, destroyFileActions = false, closeFDs = false;
pid_t p, p2;
int ret;
CFMutableDataRef dataRef;
int fds[2];
posix_spawn_file_actions_t file_actions;
char buffer[100];
ssize_t readBytes;
dataRef = CFDataCreateMutable(kCFAllocatorDefault, 0);
ret = pipe(fds);
if (ret) {
syslog(LOG_DEBUG, "Could not call posix_spawn: %d", errno);
goto done;
}
closeFDs = true;
ret = posix_spawn_file_actions_init(&file_actions);
if (ret < 0) {
syslog(LOG_DEBUG, "Could not call posix_spawn_file_actions_init: %d", ret);
goto done;
}
destroyFileActions = true;
posix_spawn_file_actions_addclose(&file_actions, fds[0]);
if (fds[1] != STDOUT_FILENO) {
(void)posix_spawn_file_actions_adddup2(&file_actions, fds[1], STDOUT_FILENO);
if (fds[1] != STDERR_FILENO) {
(void)posix_spawn_file_actions_adddup2(&file_actions, fds[1], STDERR_FILENO);
}
(void)posix_spawn_file_actions_addclose(&file_actions, fds[1]);
}
syslog(LOG_DEBUG, "Calling %s\n", argv[0]);
ret = posix_spawn(&p, argv[0], &file_actions, NULL, argv, environ);
if (ret) {
syslog(LOG_DEBUG, "Could not call posix_spawn: %d", ret);
goto done;
}
// read 100 bytes at a time until we get EOF (child closed pipe);
close(fds[1]);
fds[1] = -1;
do {
readBytes = read(fds[0], buffer, sizeof(buffer));
syslog(LOG_DEBUG, "Read %ld from pipe", readBytes);
if (readBytes > 0) {
CFDataAppendBytes(dataRef, (UInt8 *)buffer, readBytes);
}
} while (readBytes > 0);
if (readBytes < 0) {
syslog(LOG_DEBUG, "read returned error: %d\n", errno );
goto done;
}
do {
p2 = waitpid(p, &ret, 0);
} while (p2 == -1 && errno == EINTR);
syslog(LOG_DEBUG, "Returned %d\n", ret);
if(p2 == -1) {
syslog(LOG_DEBUG, "%s failed to return: %d\n", argv[0], errno );
goto done;
}
if(ret) {
if (WIFEXITED(ret)) {
syslog(LOG_ERR, "%s exited with %d\n", argv[0], WEXITSTATUS(ret) );
} else {
syslog(LOG_ERR, "%s signaled with %d\n", argv[0], WTERMSIG(ret) );
}
goto done;
}
result = true;
done:
if (destroyFileActions) {
posix_spawn_file_actions_destroy(&file_actions);
}
if (closeFDs) {
close(fds[0]);
close(fds[1]);
}
*output = dataRef;
return result;
}