Source file
src/runtime/write_err_android.go
1
2
3
4
5 package runtime
6
7 import "unsafe"
8
9 var (
10 writeHeader = []byte{6 , 'G', 'o', 0}
11 writePath = []byte("/dev/log/main\x00")
12 writeLogd = []byte("/dev/socket/logdw\x00")
13
14
15 writeFD uintptr
16 writeBuf [1024]byte
17 writePos int
18 )
19
20
21
22
23
24
25 type loggerType int32
26
27 const (
28 unknown loggerType = iota
29 legacy
30 logd
31
32 )
33
34 var logger loggerType
35
36 func writeErr(b []byte) {
37 if logger == unknown {
38
39 if v := uintptr(access(&writeLogd[0], 0x02 )); v == 0 {
40 logger = logd
41 initLogd()
42 } else {
43 logger = legacy
44 initLegacy()
45 }
46 }
47
48
49 write(2, unsafe.Pointer(&b[0]), int32(len(b)))
50
51
52
53
54
55
56
57
58
59
60 var hlen int
61 switch logger {
62 case logd:
63 hlen = writeLogdHeader()
64 case legacy:
65 hlen = len(writeHeader)
66 }
67
68 dst := writeBuf[hlen:]
69 for _, v := range b {
70 if v == 0 {
71 v = '0'
72 }
73 dst[writePos] = v
74 writePos++
75 if v == '\n' || writePos == len(dst)-1 {
76 dst[writePos] = 0
77 write(writeFD, unsafe.Pointer(&writeBuf[0]), int32(hlen+writePos))
78 for i := range dst {
79 dst[i] = 0
80 }
81 writePos = 0
82 }
83 }
84 }
85
86 func initLegacy() {
87
88 writeFD = uintptr(open(&writePath[0], 0x1 , 0))
89 if writeFD == 0 {
90
91
92
93 msg := []byte("runtime: cannot open /dev/log/main\x00")
94 write(2, unsafe.Pointer(&msg[0]), int32(len(msg)))
95 exit(2)
96 }
97
98
99 copy(writeBuf[:len(writeHeader)], writeHeader)
100 }
101
102
103 var logdAddr sockaddr_un
104
105 func initLogd() {
106
107 logdAddr.family = _AF_UNIX
108 copy(logdAddr.path[:], writeLogd)
109
110
111
112
113
114 fd := socket(_AF_UNIX, _SOCK_DGRAM|_O_CLOEXEC, 0)
115 if fd < 0 {
116 msg := []byte("runtime: cannot create a socket for logging\x00")
117 write(2, unsafe.Pointer(&msg[0]), int32(len(msg)))
118 exit(2)
119 }
120
121 errno := connect(fd, unsafe.Pointer(&logdAddr), int32(unsafe.Sizeof(logdAddr)))
122 if errno < 0 {
123 msg := []byte("runtime: cannot connect to /dev/socket/logdw\x00")
124 write(2, unsafe.Pointer(&msg[0]), int32(len(msg)))
125
126 exit(2)
127 }
128 writeFD = uintptr(fd)
129
130
131
132 copy(writeBuf[11:11+len(writeHeader)], writeHeader)
133 }
134
135
136 func writeLogdHeader() int {
137 hdr := writeBuf[:11]
138
139
140
141
142
143
144
145
146 hdr[0] = 0
147 sec, nsec, _ := time_now()
148 packUint32(hdr[3:7], uint32(sec))
149 packUint32(hdr[7:11], uint32(nsec))
150
151
152
153 return 11 + len(writeHeader)
154 }
155
156 func packUint32(b []byte, v uint32) {
157
158 b[0] = byte(v)
159 b[1] = byte(v >> 8)
160 b[2] = byte(v >> 16)
161 b[3] = byte(v >> 24)
162 }
163
View as plain text