@@ -13,23 +13,39 @@ import (
1313
1414 "github.com/erikdubbelboer/gspt"
1515 "github.com/jetkvm/kvm"
16+ "github.com/jetkvm/kvm/internal/native"
17+ "github.com/jetkvm/kvm/internal/supervisor"
1618)
1719
18- const (
19- envChildID = "JETKVM_CHILD_ID"
20- errorDumpDir = "/userdata/jetkvm/crashdump"
21- errorDumpLastFile = "last-crash.log"
22- errorDumpTemplate = "jetkvm-%s.log"
20+ var (
21+ subcomponent string
2322)
2423
2524func program () {
26- gspt .SetProcTitle (os .Args [0 ] + " [app]" )
27- kvm .Main ()
25+ subcomponentOverride := os .Getenv (supervisor .EnvSubcomponent )
26+ if subcomponentOverride != "" {
27+ subcomponent = subcomponentOverride
28+ }
29+ switch subcomponent {
30+ case "native" :
31+ native .RunNativeProcess (os .Args [0 ])
32+ default :
33+ kvm .Main ()
34+ }
35+ }
36+
37+ func setProcTitle (status string ) {
38+ if status != "" {
39+ status = " " + status
40+ }
41+ title := fmt .Sprintf ("jetkvm: [supervisor]%s" , status )
42+ gspt .SetProcTitle (title )
2843}
2944
3045func main () {
3146 versionPtr := flag .Bool ("version" , false , "print version and exit" )
3247 versionJSONPtr := flag .Bool ("version-json" , false , "print version as json and exit" )
48+ flag .StringVar (& subcomponent , "subcomponent" , "" , "subcomponent to run" )
3349 flag .Parse ()
3450
3551 if * versionPtr || * versionJSONPtr {
@@ -42,7 +58,7 @@ func main() {
4258 return
4359 }
4460
45- childID := os .Getenv (envChildID )
61+ childID := os .Getenv (supervisor . EnvChildID )
4662 switch childID {
4763 case "" :
4864 doSupervise ()
@@ -55,6 +71,8 @@ func main() {
5571}
5672
5773func supervise () error {
74+ setProcTitle ("" )
75+
5876 // check binary path
5977 binPath , err := os .Executable ()
6078 if err != nil {
@@ -74,7 +92,12 @@ func supervise() error {
7492 // run the child binary
7593 cmd := exec .Command (binPath )
7694
77- cmd .Env = append (os .Environ (), []string {envChildID + "=" + kvm .GetBuiltAppVersion ()}... )
95+ lastFilePath := filepath .Join (supervisor .ErrorDumpDir , supervisor .ErrorDumpLastFile )
96+
97+ cmd .Env = append (os .Environ (), []string {
98+ fmt .Sprintf ("%s=%s" , supervisor .EnvChildID , kvm .GetBuiltAppVersion ()),
99+ fmt .Sprintf ("%s=%s" , supervisor .ErrorDumpLastFile , lastFilePath ),
100+ }... )
78101 cmd .Args = os .Args
79102
80103 logFile , err := os .CreateTemp ("" , "jetkvm-stdout.log" )
@@ -94,6 +117,8 @@ func supervise() error {
94117 return fmt .Errorf ("failed to start command: %w" , startErr )
95118 }
96119
120+ setProcTitle (fmt .Sprintf ("started (pid=%d)" , cmd .Process .Pid ))
121+
97122 go func () {
98123 sigChan := make (chan os.Signal , 1 )
99124 signal .Notify (sigChan , syscall .SIGTERM )
@@ -102,8 +127,6 @@ func supervise() error {
102127 _ = cmd .Process .Signal (sig )
103128 }()
104129
105- gspt .SetProcTitle (os .Args [0 ] + " [sup]" )
106-
107130 cmdErr := cmd .Wait ()
108131 if cmdErr == nil {
109132 return nil
@@ -181,11 +204,11 @@ func renameFile(f *os.File, newName string) error {
181204
182205func ensureErrorDumpDir () error {
183206 // TODO: check if the directory is writable
184- f , err := os .Stat (errorDumpDir )
207+ f , err := os .Stat (supervisor . ErrorDumpDir )
185208 if err == nil && f .IsDir () {
186209 return nil
187210 }
188- if err := os .MkdirAll (errorDumpDir , 0755 ); err != nil {
211+ if err := os .MkdirAll (supervisor . ErrorDumpDir , 0755 ); err != nil {
189212 return fmt .Errorf ("failed to create error dump directory: %w" , err )
190213 }
191214 return nil
@@ -195,7 +218,7 @@ func createErrorDump(logFile *os.File) {
195218 fmt .Println ()
196219
197220 fileName := fmt .Sprintf (
198- errorDumpTemplate ,
221+ supervisor . ErrorDumpTemplate ,
199222 time .Now ().Format ("20060102-150405" ),
200223 )
201224
@@ -205,15 +228,15 @@ func createErrorDump(logFile *os.File) {
205228 return
206229 }
207230
208- filePath := filepath .Join (errorDumpDir , fileName )
231+ filePath := filepath .Join (supervisor . ErrorDumpDir , fileName )
209232 if err := renameFile (logFile , filePath ); err != nil {
210233 fmt .Printf ("failed to rename file: %v\n " , err )
211234 return
212235 }
213236
214237 fmt .Printf ("error dump copied: %s\n " , filePath )
215238
216- lastFilePath := filepath .Join (errorDumpDir , errorDumpLastFile )
239+ lastFilePath := filepath .Join (supervisor . ErrorDumpDir , supervisor . ErrorDumpLastFile )
217240
218241 if err := ensureSymlink (filePath , lastFilePath ); err != nil {
219242 fmt .Printf ("failed to create symlink: %v\n " , err )
0 commit comments