12 "github.com/gin-gonic/gin"
13 common "github.com/iotbzh/xds-common/golib"
14 "github.com/iotbzh/xds-common/golib/eows"
19 // ExecArgs JSON parameters of /exec command
21 ID string `json:"id" binding:"required"`
22 SdkID string `json:"sdkID"` // sdk ID to use for setting env
23 CmdID string `json:"cmdID"` // command unique ID
24 Cmd string `json:"cmd" binding:"required"`
25 Args []string `json:"args"`
26 Env []string `json:"env"`
27 RPath string `json:"rpath"` // relative path into project
28 TTY bool `json:"tty"` // Use a tty, specific to gdb --tty option
29 TTYGdbserverFix bool `json:"ttyGdbserverFix"` // Set to true to activate gdbserver workaround about inferior output
30 ExitImmediate bool `json:"exitImmediate"` // when true, exit event sent immediately when command exited (IOW, don't wait file synchronization)
31 CmdTimeout int `json:"timeout"` // command completion timeout in Second
34 // ExecInMsg Message used to received input characters (stdin)
36 CmdID string `json:"cmdID"`
37 Timestamp string `json:"timestamp"`
38 Stdin string `json:"stdin"`
41 // ExecOutMsg Message used to send output characters (stdout+stderr)
43 CmdID string `json:"cmdID"`
44 Timestamp string `json:"timestamp"`
45 Stdout string `json:"stdout"`
46 Stderr string `json:"stderr"`
49 // ExecExitMsg Message sent when executed command exited
51 CmdID string `json:"cmdID"`
52 Timestamp string `json:"timestamp"`
53 Code int `json:"code"`
54 Error error `json:"error"`
57 // ExecSignalArgs JSON parameters of /exec/signal command
58 ExecSignalArgs struct {
59 CmdID string `json:"cmdID" binding:"required"` // command id
60 Signal string `json:"signal" binding:"required"` // signal number
65 // ExecInEvent Event send in WS when characters are sent (stdin)
66 ExecInEvent = "exec:input"
68 // ExecOutEvent Event send in WS when characters are received (stdout or stderr)
69 ExecOutEvent = "exec:output"
71 // ExecExitEvent Event send in WS when program exited
72 ExecExitEvent = "exec:exit"
74 // ExecInferiorInEvent Event send in WS when characters are sent to an inferior (used by gdb inferior/tty)
75 ExecInferiorInEvent = "exec:inferior-input"
77 // ExecInferiorOutEvent Event send in WS when characters are received by an inferior
78 ExecInferiorOutEvent = "exec:inferior-output"
83 // ExecCmd executes remotely a command
84 func (s *APIService) execCmd(c *gin.Context) {
85 var gdbPty, gdbTty *os.File
88 if c.BindJSON(&args) != nil {
89 common.APIError(c, "Invalid arguments")
93 // TODO: add permission ?
95 // Retrieve session info
96 sess := s.sessions.Get(c)
98 common.APIError(c, "Unknown sessions")
103 common.APIError(c, "Websocket not established")
107 // Allow to pass id in url (/exec/:id) or as JSON argument
108 idArg := c.Param("id")
113 common.APIError(c, "Invalid id")
116 id, err := s.mfolders.ResolveID(idArg)
118 common.APIError(c, err.Error())
121 f := s.mfolders.Get(id)
123 common.APIError(c, "Unknown id")
127 prj := fld.GetConfig()
129 // Build command line
131 // Setup env var regarding Sdk ID (used for example to setup cross toolchain)
132 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
133 cmd = append(cmd, envCmd...)
134 cmd = append(cmd, "&&")
136 // It's an error if no envcmd found while a sdkid has been provided
137 if args.SdkID != "" {
138 common.APIError(c, "Unknown sdkid")
143 cmd = append(cmd, "cd", "\""+fld.GetFullPath(args.RPath)+"\"")
144 // FIXME - add 'exec' prevents to use syntax:
145 // xds-exec -l debug -c xds-config.env -- "cd build && cmake .."
146 // but exec is mandatory to allow to pass correctly signals
147 // As workaround, exec is set for now on client side (eg. in xds-gdb)
148 //cmd = append(cmd, "&&", "exec", args.Cmd)
149 cmd = append(cmd, "&&", args.Cmd)
151 // Process command arguments
152 cmdArgs := make([]string, len(args.Args)+1)
154 // Copy and Translate path from client to server
155 for _, aa := range args.Args {
156 if strings.Contains(aa, prj.ClientPath) {
157 cmdArgs = append(cmdArgs, fld.ConvPathCli2Svr(aa))
159 cmdArgs = append(cmdArgs, aa)
163 // Allocate pts if tty if used
165 gdbPty, gdbTty, err = pty.Open()
167 common.APIError(c, err.Error())
171 s.log.Debugf("Client command tty: %v %v\n", gdbTty.Name(), gdbTty.Name())
172 cmdArgs = append(cmdArgs, "--tty="+gdbTty.Name())
175 // Unique ID for each commands
176 if args.CmdID == "" {
177 args.CmdID = s.cfg.ServerUID[:18] + "_" + strconv.Itoa(execCommandID)
181 // Create new execution over WS context
182 execWS := eows.New(strings.Join(cmd, " "), cmdArgs, sop, sess.ID, args.CmdID)
185 // Append client project dir to environment
186 execWS.Env = append(args.Env, "CLIENT_PROJECT_DIR="+prj.ClientPath)
188 // Set command execution timeout
189 if args.CmdTimeout == 0 {
190 // 0 : default timeout
191 // TODO get default timeout from config.json file
192 execWS.CmdExecTimeout = 24 * 60 * 60 // 1 day
194 execWS.CmdExecTimeout = args.CmdTimeout
197 // Define callback for input (stdin)
198 execWS.InputEvent = ExecInEvent
199 execWS.InputCB = func(e *eows.ExecOverWS, stdin string) (string, error) {
200 s.log.Debugf("STDIN <<%v>>", strings.Replace(stdin, "\n", "\\n", -1))
203 if len(stdin) == 1 && stdin == "\x04" {
205 errMsg := fmt.Errorf("close stdin: %v", stdin)
211 prjID := (*data)["ID"].(string)
212 f := s.mfolders.Get(prjID)
214 s.log.Errorf("InputCB: Cannot get folder ID %s", prjID)
216 // Translate paths from client to server
217 stdin = (*f).ConvPathCli2Svr(stdin)
223 // Define callback for output (stdout+stderr)
224 execWS.OutputCB = func(e *eows.ExecOverWS, stdout, stderr string) {
225 // IO socket can be nil when disconnected
226 so := s.sessions.IOSocketGet(e.Sid)
228 s.log.Infof("%s not emitted: WS closed (sid:%s, msgid:%s)", ExecOutEvent, e.Sid, e.CmdID)
232 // Retrieve project ID and RootPath
234 prjID := (*data)["ID"].(string)
235 gdbServerTTY := (*data)["gdbServerTTY"].(string)
237 f := s.mfolders.Get(prjID)
239 s.log.Errorf("OutputCB: Cannot get folder ID %s", prjID)
241 // Translate paths from server to client
242 stdout = (*f).ConvPathSvr2Cli(stdout)
243 stderr = (*f).ConvPathSvr2Cli(stderr)
246 s.log.Debugf("%s emitted - WS sid[4:] %s - id:%s - prjID:%s", ExecOutEvent, e.Sid[4:], e.CmdID, prjID)
248 s.log.Debugf("STDOUT <<%v>>", strings.Replace(stdout, "\n", "\\n", -1))
251 s.log.Debugf("STDERR <<%v>>", strings.Replace(stderr, "\n", "\\n", -1))
254 // FIXME replace by .BroadcastTo a room
255 err := (*so).Emit(ExecOutEvent, ExecOutMsg{
257 Timestamp: time.Now().String(),
262 s.log.Errorf("WS Emit : %v", err)
265 // XXX - Workaround due to gdbserver bug that doesn't redirect
266 // inferior output (https://bugs.eclipse.org/bugs/show_bug.cgi?id=437532#c13)
267 if gdbServerTTY == "workaround" && len(stdout) > 1 && stdout[0] == '&' {
269 // Extract and cleanup string like &"bla bla\n"
270 re := regexp.MustCompile("&\"(.*)\"")
271 rer := re.FindAllStringSubmatch(stdout, -1)
273 if rer != nil && len(rer) > 0 {
274 for _, o := range rer {
276 out = strings.Replace(o[1], "\\n", "\n", -1)
277 out = strings.Replace(out, "\\r", "\r", -1)
278 out = strings.Replace(out, "\\t", "\t", -1)
280 s.log.Debugf("STDOUT INFERIOR: <<%v>>", out)
281 err := (*so).Emit(ExecInferiorOutEvent, ExecOutMsg{
283 Timestamp: time.Now().String(),
288 s.log.Errorf("WS Emit : %v", err)
293 s.log.Errorf("INFERIOR out parsing error: stdout=<%v>", stdout)
298 // Define callback for output
299 execWS.ExitCB = func(e *eows.ExecOverWS, code int, err error) {
300 s.log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", e.CmdID, code, err)
312 // IO socket can be nil when disconnected
313 so := s.sessions.IOSocketGet(e.Sid)
315 s.log.Infof("%s not emitted - WS closed (id:%s)", ExecExitEvent, e.CmdID)
319 // Retrieve project ID and RootPath
321 prjID := (*data)["ID"].(string)
322 exitImm := (*data)["ExitImmediate"].(bool)
324 // XXX - workaround to be sure that Syncthing detected all changes
325 if err := s.mfolders.ForceSync(prjID); err != nil {
326 s.log.Errorf("Error while syncing folder %s: %v", prjID, err)
329 // Wait end of file sync
330 // FIXME pass as argument
332 for t := tmo; t > 0; t-- {
333 s.log.Debugf("Wait file in-sync for %s (%d/%d)", prjID, t, tmo)
334 if sync, err := s.mfolders.IsFolderInSync(prjID); sync || err != nil {
336 s.log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
340 time.Sleep(time.Second)
344 // FIXME replace by .BroadcastTo a room
345 errSoEmit := (*so).Emit(ExecExitEvent, ExecExitMsg{
347 Timestamp: time.Now().String(),
351 if errSoEmit != nil {
352 s.log.Errorf("WS Emit : %v", errSoEmit)
356 // User data (used within callbacks)
357 data := make(map[string]interface{})
359 data["ExitImmediate"] = args.ExitImmediate
360 if args.TTY && args.TTYGdbserverFix {
361 data["gdbServerTTY"] = "workaround"
363 data["gdbServerTTY"] = ""
365 execWS.UserData = &data
367 // Start command execution
368 s.log.Infof("Execute [Cmd ID %s]: %v %v", execWS.CmdID, execWS.Cmd, execWS.Args)
372 common.APIError(c, err.Error())
376 c.JSON(http.StatusOK,
379 "cmdID": execWS.CmdID,
383 // ExecCmd executes remotely a command
384 func (s *APIService) execSignalCmd(c *gin.Context) {
385 var args ExecSignalArgs
387 if c.BindJSON(&args) != nil {
388 common.APIError(c, "Invalid arguments")
392 s.log.Debugf("Signal %s for command ID %s", args.Signal, args.CmdID)
394 e := eows.GetEows(args.CmdID)
396 common.APIError(c, "unknown cmdID")
400 err := e.Signal(args.Signal)
402 common.APIError(c, err.Error())
406 c.JSON(http.StatusOK,