-
Notifications
You must be signed in to change notification settings - Fork 49
/
target.c
1298 lines (1068 loc) · 28.6 KB
/
target.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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Advance project.
*
* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Andrea Mazzoleni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Andrea Mazzoleni
* gives permission to link the code of this program with
* the MAME library (or with modified versions of MAME that use the
* same license as MAME), and distribute linked combinations including
* the two. You must obey the GNU General Public License in all
* respects for all of the code used other than MAME. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for EIP in ucontext.h */
#endif
#include "portable.h"
#include "target.h"
#include "log.h"
#include "file.h"
#include "snstring.h"
#include "oslinux.h"
#if HAVE_SCHED_H
#include <sched.h>
#endif
#if HAVE_TERMIOS_H
#include <termios.h>
#endif
#if GWINSZ_IN_SYS_IOCTL
#include <sys/ioctl.h>
#endif
#if HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if HAVE_SYS_IO_H
#include <sys/io.h>
#endif
#if HAVE_UCONTEXT_H
#include <ucontext.h>
#endif
#if HAVE_IOPL
#if HAVE_INOUT
#define USE_DIRECT_PORT
#endif
#endif
#if HAVE_BACKTRACE
#if HAVE_BACKTRACE_SYMBOLS
#define USE_BACKTRACE
#endif
#endif
#ifdef __i386__
#if HAVE_UCONTEXT_H
#define USE_BACKTRACE_REG
#endif
#endif
#ifdef USE_VC
#include "interface/vmcs_host/vc_tvservice.h"
#endif
struct target_context {
unsigned usleep_granularity; /**< Minimun sleep time in microseconds. */
unsigned col; /**< Number of columns. 0 if not detectable. */
unsigned row; /**< Number of rows. 0 if not detectable. */
unsigned size_x; /**< Screen size. 0 if not detectable. */
unsigned size_y; /**< Screen size. 0 if not detectable. */
unsigned aspect_x; /**< Screen aspect. 0 if not detectable. */
unsigned aspect_y; /**< Screen aspect. 0 if not detectable. */
#ifdef USE_DIRECT_PORT
adv_bool io_perm_flag; /**< IO Permission granted. */
adv_bool io_perm_iopl_flag; /**< IO iopl called. */
#endif
adv_bool io_dev_port_flag; /**< /dev/port granted. */
#ifdef USE_VC
VCHI_INSTANCE_T vchi_instance; /**< VideoCore instance. */
VCHI_CONNECTION_T* vchi_connection; /**< VideoCore connection. */
unsigned vc_callback_counter;
unsigned vc_callback_reason;
#endif
};
static struct target_context TARGET;
/***************************************************************************/
/* Init */
#ifdef USE_VC
pthread_mutex_t vc_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t vc_callback_cond = PTHREAD_COND_INITIALIZER;
static void vc_callback(void* arg, uint32_t reason, uint32_t param1, uint32_t param2)
{
unsigned counter;
(void)arg;
pthread_mutex_lock(&vc_callback_mutex);
counter = ++TARGET.vc_callback_counter;
TARGET.vc_callback_reason = reason;
pthread_cond_signal(&vc_callback_cond);
pthread_mutex_unlock(&vc_callback_mutex);
switch (reason) {
case VC_HDMI_UNPLUGGED: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_UNPLUGGED", param1, param2)); break;
case VC_HDMI_ATTACHED: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_ATTACHED", param1, param2)); break;
case VC_HDMI_DVI: log_std(("linux:vc: event %u: %s,group:%u,mode:%u\n", counter, "HDMI_DVI", param1, param2)); break;
case VC_HDMI_HDMI: log_std(("linux:vc: event %u: %s,group:%u,mode:%u\n", counter, "HDMI_HDMI", param1, param2)); break;
case VC_HDMI_HDCP_UNAUTH: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_HDCP_UNAUTH", param1, param2)); break;
case VC_HDMI_HDCP_AUTH: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_HDCP_AUTH", param1, param2)); break;
case VC_HDMI_HDCP_KEY_DOWNLOAD: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_HDCP_KEY_DOWNLOAD", param1, param2)); break;
case VC_HDMI_HDCP_SRM_DOWNLOAD: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "HDMI_HDCP_SRM_DOWNLOAD", param1, param2)); break;
case VC_SDTV_UNPLUGGED: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "SDTV_UNPLUGGED", param1, param2)); break;
case VC_SDTV_ATTACHED: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "SDTV_ATTACHED", param1, param2)); break;
case VC_SDTV_NTSC: log_std(("linux:vc: event %u: %s,mode:%u,aspect:%u\n", counter, "SDTV_NTSC", param1, param2)); break;
case VC_SDTV_PAL: log_std(("linux:vc: event %u: %s,mode:%u,aspect:%u\n", counter, "SDTV_PAL", param1, param2)); break;
case VC_SDTV_CP_INACTIVE: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "SDTV_CP_INACTIVE", param1, param2)); break;
case VC_SDTV_CP_ACTIVE: log_std(("linux:vc: event %u: %s,%u,%u\n", counter, "SDTV_CP_ACTIVE", param1, param2)); break;
default: log_std(("linux:vc: event %u: %u,%u,%u\n", counter, reason, param1, param2)); break;
}
}
#endif
adv_error target_init(void)
{
#ifdef USE_VC
int ret;
#endif
TARGET.usleep_granularity = 0;
TARGET.col = 0;
TARGET.row = 0;
#ifdef TIOCGWINSZ
{
struct winsize wind_struct;
if (ioctl(1, TIOCGWINSZ, &wind_struct) == 0) {
TARGET.col = wind_struct.ws_col;
TARGET.row = wind_struct.ws_row;
}
}
#endif
#ifdef USE_DIRECT_PORT
TARGET.io_perm_iopl_flag = 0;
TARGET.io_perm_flag = 0;
#endif
TARGET.io_dev_port_flag = 1;
#ifdef USE_VC
vcos_init();
ret = vchi_initialise(&TARGET.vchi_instance);
if (ret != 0) {
target_err("Failed to call VideoCore vchi_initialise()\n");
return -1;
}
ret = vchi_connect(0, 0, TARGET.vchi_instance);
if (ret != 0) {
target_err("Failed to call VideoCore vchi_connect()\n");
return -1;
}
ret = vc_vchi_tv_init(TARGET.vchi_instance, &TARGET.vchi_connection, 1);
if (ret != 0) {
target_err("Failed to call VideoCore vc_vchi_tv_init()\n");
return -1;
}
vc_tv_register_callback(&vc_callback, 0);
#endif
return 0;
}
void target_done(void)
{
#ifdef USE_VC
/*
* These calls seems to hang in some firmware versions
* Anyway they are not required as th system is able to deinitialize itself
*
* See: https://github.com/raspberrypi/userland/issues/197
*/
#if 0
vc_vchi_tv_stop();
vchi_disconnect(TARGET.vchi_instance);
vcos_deinit();
#endif
TARGET.vchi_instance = 0;
#endif
}
#ifdef USE_VC
unsigned target_vc_get_event(void)
{
unsigned counter;
pthread_mutex_lock(&vc_callback_mutex);
counter = TARGET.vc_callback_counter;
pthread_mutex_unlock(&vc_callback_mutex);
return counter;
}
int target_vc_wait_event(unsigned counter, unsigned timeout_ms)
{
struct timespec ts;
int ret;
target_clock_t start, stop;
start = target_clock();
pthread_mutex_lock(&vc_callback_mutex);
/* get present time */
clock_gettime(CLOCK_REALTIME, &ts);
/* set the absolute timeout */
ts.tv_sec += timeout_ms / 1000;
ts.tv_nsec += (timeout_ms % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
++ts.tv_sec;
ts.tv_nsec -= 1000000000;
}
ret = 0;
while (TARGET.vc_callback_counter < counter && ret == 0) {
ret = pthread_cond_timedwait(&vc_callback_cond, &vc_callback_mutex, &ts);
}
pthread_mutex_unlock(&vc_callback_mutex);
stop = target_clock();
if (ret == 0) {
log_std(("linux: vc event recevied after %llu [us]\n", stop - start));
} else if (ret == ETIMEDOUT) {
log_std(("WARNING:linux: vc event NOT recevied for TIMEOUT after %llu [us]\n", stop - start));
} else {
log_std(("WARNING:linux: vc event wait failed with error %d, %s\n", errno, strerror(errno)));
}
return ret;
}
#endif
/***************************************************************************/
/* Scheduling */
void target_yield(void)
{
#if HAVE_SCHED_YIELD && defined(_POSIX_PRIORITY_SCHEDULING) /* OSDEF Check for POSIX scheduling */
sched_yield();
#endif
}
void target_idle(void)
{
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 1000000; /* 1 ms */
nanosleep(&req, 0);
}
void target_usleep_granularity(unsigned us)
{
TARGET.usleep_granularity = us;
}
void target_usleep(unsigned us)
{
struct timeval start_tv;
struct timeval stop_tv;
struct timespec req;
unsigned requested;
unsigned effective;
/* if too short don't wait */
if (us <= TARGET.usleep_granularity) {
return;
}
requested = us - TARGET.usleep_granularity;
req.tv_sec = requested / 1000000;
req.tv_nsec = (requested % 1000000) * 1000;
gettimeofday(&start_tv, NULL);
nanosleep(&req, 0);
gettimeofday(&stop_tv, NULL);
effective = (stop_tv.tv_sec - start_tv.tv_sec) * 1000000 + (stop_tv.tv_usec - start_tv.tv_usec);
if (effective > us) {
/* don't adjust the granularity, it should be measured by the OS code */
/* TARGET.usleep_granularity += effective - us; */
log_std(("WARNING:linux: target_usleep() too long, granularity %d [us] (requested %d, tryed %d, effective %d)\n", TARGET.usleep_granularity, us, requested, effective));
}
}
/***************************************************************************/
/* Clock */
target_clock_t TARGET_CLOCKS_PER_SEC = 1000000LL;
target_clock_t target_clock(void)
{
struct timespec ts;
target_clock_t r;
clock_gettime(CLOCK_MONOTONIC, &ts);
r = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000;
return r;
}
/***************************************************************************/
/* Hardware */
static void dev_port_set(unsigned addr, unsigned value)
{
int f;
off_t o;
size_t s;
unsigned char c;
if (!TARGET.io_dev_port_flag)
goto err;
f = open("/dev/port", O_WRONLY);
if (f == -1) {
TARGET.io_dev_port_flag = 0;
log_std(("ERROR:linux: port_set failed, error %d open(/dev/port), %s\n", errno, strerror(errno)));
goto err;
}
o = lseek(f, addr, SEEK_SET);
if (o == -1) {
log_std(("ERROR:linux: port_set failed, error %d in lseek(0x%x) /dev/port\n", errno, addr));
goto err_close;
}
if (o != addr) {
log_std(("ERROR:linux: port_set failed, erroneous return value %d in lseek(0x%x) /dev/port\n", (int)o, addr));
goto err_close;
}
c = value;
s = write(f, &c, 1);
if (s == -1) {
log_std(("ERROR:linux: port_set failed, error %d in write(0x%x) /dev/port\n", errno, value));
goto err_close;
}
if (s != 1) {
log_std(("ERROR:linux: port_set failed, erroneous return value %d in write(0x%x) /dev/port\n", (int)s, value));
goto err_close;
}
close(f);
return;
err_close:
close(f);
err:
return;
}
static unsigned dev_port_get(unsigned addr)
{
int f;
off_t o;
size_t s;
unsigned char c;
if (!TARGET.io_dev_port_flag)
goto err;
f = open("/dev/port", O_RDONLY);
if (f == -1) {
TARGET.io_dev_port_flag = 0;
log_std(("ERROR:linux: port_get failed, error %d open(/dev/port), %s\n", errno, strerror(errno)));
goto err;
}
o = lseek(f, addr, SEEK_SET);
if (o == -1) {
log_std(("ERROR:linux: port_get failed, error %d in lseek(0x%x) /dev/port\n", errno, addr));
goto err_close;
}
if (o != addr) {
log_std(("ERROR:linux: port_get failed, erroneous return value %d in lseek(0x%x) /dev/port\n", (int)o, addr));
goto err_close;
}
s = read(f, &c, 1);
if (s == -1) {
log_std(("ERROR:linux: port_get failed, error %d in read() /dev/port\n", errno));
goto err_close;
}
if (s != 1) {
log_std(("ERROR:linux: port_get failed, erroneous return value %d in read() /dev/port\n", (int)s));
goto err_close;
}
close(f);
return c;
err_close:
close(f);
err:
return 0;
}
#ifdef USE_DIRECT_PORT
static adv_bool io_port(void)
{
/* call iopl at the first use */
if (!TARGET.io_perm_iopl_flag) {
TARGET.io_perm_iopl_flag = 1;
if (iopl(3) == 0) {
TARGET.io_perm_flag = 1;
log_std(("linux: iopl(3) success, using direct io port\n"));
} else {
TARGET.io_perm_flag = 0;
log_std(("WARNING:linux: iopl(3) failed, using /dev/port to io port programming\n"));
}
}
return TARGET.io_perm_flag;
}
#endif
void target_port_set(unsigned addr, unsigned value)
{
log_debug(("linux: port_set(0x%x,0x%02x)\n", addr, value));
#ifdef USE_DIRECT_PORT
if (io_port()) {
outb(addr, value);
return;
}
#endif
dev_port_set(addr, value);
}
unsigned target_port_get(unsigned addr)
{
unsigned v;
#ifdef USE_DIRECT_PORT
if (io_port()) {
v = inb(addr);
log_std(("linux: port_get(0x%x) = 0x%02x\n", addr, v));
return v;
}
#endif
v = dev_port_get(addr);
log_debug(("linux: port_get(0x%x) = 0x%02x\n", addr, v));
return v;
}
void target_writeb(unsigned addr, unsigned char c)
{
log_std(("ERROR:linux: write at address 0x%x not allowed in this architecture\n", addr));
}
unsigned char target_readb(unsigned addr)
{
log_std(("ERROR:linux: read at address 0x%x not allowed in this architecture\n", addr));
return 0;
}
/***************************************************************************/
/* Mode */
adv_bool target_wm(void)
{
return getenv("DISPLAY") != 0;
}
void target_mode_reset(void)
{
/* nothing */
}
/***************************************************************************/
/* Video */
unsigned target_size_x(void)
{
return TARGET.size_x;
}
unsigned target_size_y(void)
{
return TARGET.size_y;
}
void target_size_set(unsigned x, unsigned y)
{
TARGET.size_x = x;
TARGET.size_y = y;
}
unsigned target_aspect_x(void)
{
return TARGET.aspect_x;
}
unsigned target_aspect_y(void)
{
return TARGET.aspect_y;
}
void target_aspect_set(unsigned x, unsigned y)
{
TARGET.aspect_x = x;
TARGET.aspect_y = y;
}
static unsigned char* target_load(const char* file, unsigned* size)
{
FILE* f;
struct stat st;
unsigned char* data;
f = fopen(file, "r");
if (!f)
return 0;
if (fstat(fileno(f), &st) != 0) {
fclose(f);
return 0;
}
*size = st.st_size;
data = malloc(st.st_size);
if (!data) {
fclose(f);
return 0;
}
if (fread(data, st.st_size, 1, f) != 1) {
fclose(f);
return 0;
}
fclose(f);
return data;
}
unsigned char* target_edid(unsigned* size)
{
const char* file = "/boot/edid.dat";
unsigned char* data;
/* check if it already exist in the /boot dir */
if (access(file, R_OK) != 0) {
/* dump it */
file = "/tmp/edid.dat";
if (system("tvservice -d /tmp/edid.dat") != 0)
return 0;
}
data = target_load(file, size);
if (!data) {
log_std(("ERROR:linux: failed to load edid %s\n", file));
return 0;
}
log_std(("linux: using edid %s, %u\n", file, *size));
return data;
}
/***************************************************************************/
/* Sound */
void target_sound_error(void)
{
/* nothing */
}
void target_sound_warn(void)
{
/* nothing */
}
void target_sound_signal(void)
{
/* nothing */
}
/***************************************************************************/
/* APM */
adv_error target_apm_shutdown(void)
{
int r;
r = execl("/sbin/poweroff", "/sbin/poweroff", NULL);
if (!WIFEXITED(r) || WEXITSTATUS(r) != 0)
return -1;
return 0;
}
adv_error target_apm_standby(void)
{
/* nothing */
return 0;
}
adv_error target_apm_wakeup(void)
{
/* nothing */
return 0;
}
/***************************************************************************/
/* System */
char* target_system(const char* cmd)
{
FILE* f;
char buffer[512];
int read;
char* s;
size_t len;
f = popen(cmd, "r");
if (!f) {
log_std(("linux: ERROR running system(%s) -> FAILED on popen()\n", cmd));
return 0;
}
read = fread(buffer, 1, sizeof(buffer) - 1, f);
if (read < 0) {
log_std(("linux: ERROR running system(%s) -> FAILED on fread()\n", cmd));
pclose(f);
return 0;
}
if (pclose(f) != 0) {
log_std(("linux: ERROR running system(%s) -> FAILED on pclose()\n", cmd));
return 0;
}
s = buffer;
len = read;
/* trim */
while (len != 0 && isspace(s[0])) {
++s;
--len;
}
while (len != 0 && isspace(s[len - 1]))
--len;
/* terminate */
s[len] = 0;
return strdup(s);
}
adv_error target_script(const char* script)
{
char file[FILE_MAXPATH];
int f;
int r;
uid_t euid;
gid_t egid;
log_std(("linux: script\n%s\n", script));
/* get the effective user/group id */
euid = geteuid();
egid = getegid();
/* set the real user id, prevent chroot programs to propagate permissions */
if (seteuid(getuid()) != 0) {
log_std(("ERROR:linux: script seteuid(getuid()) failed\n"));
goto err;
}
/* set the real group id, prevent chroot programs to propagate permissions */
if (setegid(getgid()) != 0) {
log_std(("ERROR:linux: script setegid(getgid()) failed\n"));
goto err;
}
strcpy(file, "/tmp/advscriptXXXXXX");
f = mkstemp(file);
if (f == -1) {
log_std(("ERROR:linux: mkstemp() failed\n"));
goto err_priv;
}
/* set it executable */
if (fchmod(f, S_IRWXU) != 0) {
log_std(("ERROR:linux: script fchmod() failed\n"));
goto err_close;
}
if (write(f, script, strlen(script)) != strlen(script)) {
log_std(("ERROR:linux: script write() failed\n"));
goto err_close;
}
if (close(f) != 0) {
log_std(("ERROR:linux: script close()e failed\n"));
goto err_priv;
}
r = system(file);
log_std(("linux: system(script) return %d\n", r));
remove(file); /* ignore error */
/* restore privileges */
if (seteuid(euid) != 0) {
log_std(("ERROR:linux: script script seteuid(%d) failed\n", (int)euid));
}
if (setegid(egid) != 0) {
log_std(("ERROR:linux: script script setegid(%d) failed\n", (int)egid));
}
return r;
err_close:
close(f);
err_priv:
/* restore privileges */
if (seteuid(euid) != 0) {
log_std(("ERROR:linux: script script seteuid(%d) failed\n", (int)euid));
}
if (setegid(egid) != 0) {
log_std(("ERROR:linux: script script setegid(%d) failed\n", (int)egid));
}
err:
return -1;
}
adv_error target_spawn_redirect(const char* file, const char** argv, const char* output)
{
int r;
int i;
int p;
log_std(("linux: spawn_redirect %s\n", file));
for (i = 0; argv[i]; ++i)
log_std(("linux: spawn_redirect arg%d %s\n", i, argv[i]));
log_std(("linux: spawn_redirect input %s\n", output));
p = fork();
if (p == -1) {
log_std(("ERROR:linux: spawn_redirect fork() failed\n"));
return -1;
}
if (p == 0) {
int f;
/* open the output file */
f = open(output, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (f == -1) {
log_std(("ERROR:linux: spawn_redirect open failed\n"));
exit(127);
}
/* remap the output stream */
if (dup2(f, STDOUT_FILENO) == -1) {
log_std(("ERROR:linux: spawn_redirect dup2 failed\n"));
exit(127);
}
close(f);
/* set the real user id, prevent chroot programs to propagate permissions */
if (seteuid(getuid()) != 0) {
log_std(("ERROR:linux: spawn seteuid(getuid()) failed\n"));
exit(127);
}
/* set the real group id, prevent chroot programs to propagate permissions */
if (setegid(getgid()) != 0) {
log_std(("ERROR:linux: spawn setegid(getgid()) failed\n"));
exit(127);
}
/* exec the program */
execvp(file, (char**)argv);
exit(127);
} else {
while (1) {
if (waitpid(p, &r, 0) == -1) {
if (errno != EINTR) {
r = -1;
break;
}
} else
break;
}
log_std(("linux: spawn_redirect return %d\n", r));
return r;
}
}
adv_error target_spawn(const char* file, const char** argv)
{
int r;
int i;
int p;
log_std(("linux: spawn %s\n", file));
for (i = 0; argv[i]; ++i)
log_std(("linux: spawn arg%d %s\n", i, argv[i]));
p = fork();
if (p == -1) {
log_std(("ERROR:linux: spawn fork() failed\n"));
return -1;
}
if (p == 0) {
/* set the real user id, prevent chroot programs to propagate permissions */
if (seteuid(getuid()) != 0) {
log_std(("ERROR:linux: spawn seteuid(getuid()) failed\n"));
exit(127);
}
/* set the real group id, prevent chroot programs to propagate permissions */
if (setegid(getgid()) != 0) {
log_std(("ERROR:linux: spawn setegid(getgid()) failed\n"));
exit(127);
}
/* exec the program */
execvp(file, (char**)argv);
exit(127);
} else {
while (1) {
if (waitpid(p, &r, 0) == -1) {
if (errno != EINTR) {
r = -1;
break;
}
} else
break;
}
log_std(("linux: spawn return %d\n", r));
return r;
}
}
adv_error target_mkdir(const char* file)
{
return mkdir(file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
void target_sync(void)
{
sync();
}
adv_error target_search(char* path, unsigned path_size, const char* file)
{
const char* path_env;
char* path_list;
char* dir;
log_std(("linux: target_search(%s)\n", file));
/* if it's an absolute path */
if (file[0] == file_dir_slash()) {
sncpy(path, path_size, file);
if (access(path, F_OK) == 0) {
log_std(("linux: target_search() return %s\n", path));
return 0;
}
log_std(("linux: target_search failed\n"));
return -1;
}
/* get the path list */
path_env = getenv("PATH");
if (!path_env) {
log_std(("linux: genenv(PATH) failed\n"));
} else {
char separator[2];
separator[0] = file_dir_separator();
separator[1] = 0;
/* duplicate for the strtok use */
path_list = strdup(path_env);
dir = strtok(path_list, separator);
while (dir) {
sncpy(path, path_size, dir);
if (!path[0] || path[strlen(path) - 1] != file_dir_slash()) {
char slash[2];
slash[0] = file_dir_slash();
slash[1] = 0;
sncat(path, path_size, slash);
}
sncat(path, path_size, file);
if (access(path, F_OK) == 0) {
free(path_list);
log_std(("linux: target_search() return %s\n", path));
return 0;
}
dir = strtok(0, separator);
}
free(path_list);
}
log_std(("linux: target_search failed\n"));
return -1;
}
static void target_wrap(FILE* f, unsigned col, char* s)
{
unsigned i;
int p;
adv_bool has_space;
p = 0;
i = 0;
has_space = 1;
while (s[p]) {
const char* t;