-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathphpinfo.go
More file actions
199 lines (168 loc) · 4.79 KB
/
Copy pathphpinfo.go
File metadata and controls
199 lines (168 loc) · 4.79 KB
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
package frankenphp
// #include "frankenphp.h"
import "C"
import (
"runtime"
"runtime/debug"
"slices"
"sort"
"sync"
"unsafe"
)
type phpinfoEntry struct {
key, value string
}
var (
phpinfoMu sync.Mutex
phpinfoEntries []phpinfoEntry
phpinfoDirty = true
phpinfoBuildMu sync.Mutex
phpinfoCurrent *phpinfoTable
phpinfoRetired []*phpinfoTable
)
// one pinned generation of the phpinfo tables, held at a fixed address while
// C code may still be reading it
type phpinfoTable struct {
pinner runtime.Pinner
entries, modules **C.char
readers int
retired bool
}
func (t *phpinfoTable) matches(entries, modules **C.char) bool {
return t.entries == entries && t.modules == modules
}
// AddPHPInfoEntry adds an entry to the frankenphp section of phpinfo().
func AddPHPInfoEntry(key, value string) {
phpinfoMu.Lock()
defer phpinfoMu.Unlock()
phpinfoEntries = append(phpinfoEntries, phpinfoEntry{key, value})
phpinfoDirty = true
}
func collectPHPInfoEntries(buildInfo *debug.BuildInfo) (entries, modules []phpinfoEntry) {
phpinfoMu.Lock()
entries = slices.Clone(phpinfoEntries)
phpinfoMu.Unlock()
if buildInfo == nil {
return entries, nil
}
entries = append(entries, phpinfoEntry{"Go", buildInfo.GoVersion})
modules = buildGoModuleEntries(buildInfo)
moduleAliases := map[string]string{
"github.com/dunglas/mercure": "dunglas/mercure",
"github.com/e-dant/watcher": "e-dant/watcher",
"github.com/dunglas/caddy-cbrotli": "dunglas/caddy-cbrotli",
}
for _, module := range modules {
if alias, ok := moduleAliases[module.key]; ok {
entries = append(entries, phpinfoEntry{alias, module.value})
}
}
return entries, modules
}
func buildGoModuleEntries(buildInfo *debug.BuildInfo) []phpinfoEntry {
entries := make([]phpinfoEntry, 0, len(buildInfo.Deps)+1)
if buildInfo.Main.Path != "" {
entries = append(entries, phpinfoEntry{buildInfo.Main.Path, goModuleVersion(&buildInfo.Main)})
}
for _, dep := range buildInfo.Deps {
entries = append(entries, phpinfoEntry{dep.Path, goModuleVersion(dep)})
}
return entries
}
func goModuleVersion(module *debug.Module) string {
if module.Replace == nil {
return module.Version
}
if module.Replace.Version == "" {
// Replaced by a local directory
return module.Replace.Path
}
return module.Replace.Path + " " + module.Replace.Version
}
// The caller must hand the borrowed tables back to
// go_frankenphp_release_phpinfo, bailout included, or they stay pinned.
//
//export go_frankenphp_collect_phpinfo
func go_frankenphp_collect_phpinfo() (**C.char, **C.char) {
phpinfoBuildMu.Lock()
defer phpinfoBuildMu.Unlock()
phpinfoMu.Lock()
dirty := phpinfoDirty
phpinfoDirty = false
phpinfoMu.Unlock()
if !dirty && phpinfoCurrent != nil {
phpinfoCurrent.readers++
return phpinfoCurrent.entries, phpinfoCurrent.modules
}
buildInfo, _ := debug.ReadBuildInfo()
entries, modules := collectPHPInfoEntries(buildInfo)
table := new(phpinfoTable)
table.entries = pinPHPInfoEntries(entries, &table.pinner)
table.modules = pinPHPInfoEntries(modules, &table.pinner)
table.readers = 1
// readers of the previous table may still be printing it
retirePHPInfoTableLocked(phpinfoCurrent)
phpinfoCurrent = table
return table.entries, table.modules
}
// go_frankenphp_release_phpinfo ends a collect borrow; the table is unpinned
// once retired and its last reader is done.
//
//export go_frankenphp_release_phpinfo
func go_frankenphp_release_phpinfo(entries, modules **C.char) {
phpinfoBuildMu.Lock()
defer phpinfoBuildMu.Unlock()
table := phpinfoCurrent
if table == nil || !table.matches(entries, modules) {
table = nil
for _, t := range phpinfoRetired {
if t.matches(entries, modules) {
table = t
break
}
}
}
if table == nil {
return
}
if table.readers > 0 {
table.readers--
}
unpinPHPInfoTableLocked(table)
}
func retirePHPInfoTableLocked(table *phpinfoTable) {
if table == nil || table.retired {
return
}
table.retired = true
phpinfoRetired = append(phpinfoRetired, table)
unpinPHPInfoTableLocked(table)
}
func unpinPHPInfoTableLocked(table *phpinfoTable) {
if !table.retired || table.readers > 0 {
return
}
table.pinner.Unpin()
phpinfoRetired = slices.DeleteFunc(phpinfoRetired, func(t *phpinfoTable) bool {
return t == table
})
}
// pinPHPInfoEntries returns a sorted, NUL-terminated key/value array for C.
func pinPHPInfoEntries(entries []phpinfoEntry, pinner *runtime.Pinner) **C.char {
if len(entries) == 0 {
return nil
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].key < entries[j].key
})
arr := make([]*C.char, 2*len(entries)+1)
for i, e := range entries {
for j, s := range []string{e.key, e.value} {
data := unsafe.StringData(s + "\x00")
pinner.Pin(data)
arr[2*i+j] = (*C.char)(unsafe.Pointer(data))
}
}
pinner.Pin(&arr[0])
return &arr[0]
}