-
Notifications
You must be signed in to change notification settings - Fork 119
/
target.cpp
80 lines (67 loc) · 1.53 KB
/
target.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
#include "pxt.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
namespace pxt {
void target_exit() {
kill(getpid(), SIGTERM);
}
extern "C" void target_reset() {
for (int i = 3; i < 1000; ++i)
close(i);
if (!fork()) {
execv(initialArgv[0], initialArgv);
exit(127);
} else {
target_exit();
}
}
void target_startup() {
int pid = getpid();
DMESG("runtime starting, pid=%d...", pid);
FILE *pf = fopen("/tmp/pxt-pid", "r");
if (pf) {
int p2 = 0;
fscanf(pf, "%d", &p2);
if (p2)
kill(p2, SIGTERM);
fclose(pf);
}
pf = fopen("/tmp/pxt-pid", "w");
fprintf(pf, "%d", pid);
fclose(pf);
}
uint64_t readSerialNumber() {
static uint64_t bigSerialNumber;
if (bigSerialNumber)
return bigSerialNumber;
char buf[1024];
int fd = open("/proc/cpuinfo", O_RDONLY);
int len = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (len < 0)
len = 0;
buf[len] = 0;
auto p = strstr(buf, "Serial\t");
if (p) {
p += 6;
while (*p && strchr(" \t:", *p))
p++;
uint64_t s = 0;
sscanf(p, "%llu", &s);
bigSerialNumber = s;
}
if (!bigSerialNumber)
bigSerialNumber = 0xf00d0042f00d0042;
return bigSerialNumber;
}
uint64_t getLongSerialNumber() {
static uint64_t serial;
if (serial == 0)
serial = readSerialNumber();
return serial;
}
} // namespace pxt