Debian Codegraph @

launch_helper/legacy/legacy.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // package legacy implements a HelperLauncher for “legacy” applications.
18 package legacy
19
20 import (
21 "bytes"
22 "os"
23 "os/exec"
24 "path/filepath"
25 "strconv"
26
27 "gitlab.com/ubports/development/core/lomiri-push-service/click"
28 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
29 )
30
31 const legacyHelperDir = "/usr/lib/lomiri-push-service/legacy-helpers"
32
33 type legacyHelperLauncher struct {
34 helperDir string
35 log logger.Logger
36 done func(string)
37 }
38
39 func New(log logger.Logger) *legacyHelperLauncher {
40 return &legacyHelperLauncher{helperDir: legacyHelperDir, log: log}
41 }
42
43 func (lhl *legacyHelperLauncher) SetHelperDir(helperDir string) {
44 lhl.helperDir = helperDir
45 }
46
47 func (lhl *legacyHelperLauncher) InstallObserver(done func(string)) error {
48 lhl.done = done
49 return nil
50 }
51
52 func (lhl *legacyHelperLauncher) HelperAppID(app *click.AppId) string {
53 return app.Application
54 }
55
56 func (*legacyHelperLauncher) RemoveObserver() error { return nil }
57
58 type msg struct {
59 id string
60 err error
61 }
62
63 func (lhl *legacyHelperLauncher) Launch(appId, f1, f2 string) (string, error) {
64 comm := make(chan msg)
65
66 go func() {
67 progname := filepath.Join(lhl.helperDir, appId)
68 cmd := exec.Command(progname, f1, f2)
69 var stdout bytes.Buffer
70 cmd.Stdout = &stdout
71 var stderr bytes.Buffer
72 cmd.Stderr = &stderr
73 err := cmd.Start()
74 if err != nil {
75 comm <- msg{"", err}
76 return
77 }
78 proc := cmd.Process
79 if proc == nil {
80 panic("cmd.Process is nil after successful cmd.Start()??")
81 }
82 id := strconv.FormatInt((int64)(proc.Pid), 36)
83 comm <- msg{id, nil}
84 p_err := cmd.Wait()
85 if p_err != nil {
86 // Helper failed or got killed, log output/errors
87 lhl.log.Errorf("legacy helper failed: appId: %v, helper: %v, pid: %v, error: %v, stdout: %#v, stderr: %#v.",
88 appId, progname, id, p_err, stdout.String(), stderr.String())
89 }
90 lhl.done(id)
91 }()
92 msg := <-comm
93 return msg.id, msg.err
94 }
95
96 func (lhl *legacyHelperLauncher) Stop(_, id string) error {
97 pid, err := strconv.ParseInt(id, 36, 0)
98 if err != nil {
99 return err
100 }
101 proc, err := os.FindProcess(int(pid))
102 if err != nil {
103 return err
104 }
105 return proc.Kill()
106 }