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