Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / nativeexecutor.patch
1 diff --git a/src/controller/servicemgr/executor/nativeexecutor/nativeexecutor.go b/src/controller/servicemgr/executor/nativeexecutor/nativeexecutor.go
2 index a62a768..49f529d 100755
3 --- a/src/controller/servicemgr/executor/nativeexecutor/nativeexecutor.go
4 +++ b/src/controller/servicemgr/executor/nativeexecutor/nativeexecutor.go
5 @@ -19,11 +19,11 @@
6  package nativeexecutor
7  
8  import (
9 -       "bufio"
10         "errors"
11         "log"
12         "os"
13 -       "os/exec"
14 +       "strings"
15 +       "sync"
16  
17         "controller/servicemgr"
18         "controller/servicemgr/executor"
19 @@ -31,14 +31,21 @@ import (
20  )
21  
22  var (
23 -       logPrefix     = "[nativeexecutor]"
24 +       logPrefix      = "[nativeexecutor]"
25         nativeexecutor = &NativeExecutor{}
26 +       handler        ExecuteHandler
27  )
28  
29 +// Execute callback
30 +type ExecuteHandler interface {
31 +       CmdExecute(pkgName string, args string) int
32 +}
33 +
34  // NativeExecutor struct
35  type NativeExecutor struct {
36         executor.ServiceExecutionInfo
37         executor.HasClientNotification
38 +       executeCB ExecuteHandler
39  }
40  
41  func init() {
42 @@ -50,6 +57,10 @@ func GetInstance() *NativeExecutor {
43         return nativeexecutor
44  }
45  
46 +func (t *NativeExecutor) SetExecuteHandler(h ExecuteHandler) {
47 +       t.executeCB = h
48 +}
49 +
50  // Execute executes native service application
51  func (t NativeExecutor) Execute(s executor.ServiceExecutionInfo) (err error) {
52         t.ServiceExecutionInfo = s
53 @@ -57,71 +68,58 @@ func (t NativeExecutor) Execute(s executor.ServiceExecutionInfo) (err error) {
54         log.Println(logPrefix, t.ServiceName, t.ParamStr)
55         log.Println(logPrefix, "parameter length :", len(t.ParamStr))
56  
57 -       cmd, pid, err := t.setService()
58 +       result, err := t.setService()
59         if err != nil {
60                 return
61         }
62  
63 -       log.Println(logPrefix, "Just ran subprocess ", pid)
64 +       log.Println(logPrefix, "Just ran subprocess [Result] ", result)
65 +
66 +       var wait sync.WaitGroup
67 +       wait.Add(1)
68  
69         executeCh := make(chan error)
70         go func() {
71 -               executeCh <- cmd.Wait()
72 +               status, _ := t.waitService(executeCh)
73 +               t.notifyServiceStatus(status)
74 +               wait.Done()
75         }()
76  
77 -       status, err := t.waitService(executeCh)
78 -       t.notifyServiceStatus(status)
79 +       switch {
80 +       case result >= 0:
81 +               executeCh <- nil
82 +       default:
83 +               executeCh <- err
84 +       }
85 +
86 +       wait.Wait()
87  
88         return
89  }
90  
91 -func (t NativeExecutor) setService() (cmd *exec.Cmd, pid int, err error) {
92 +func (t NativeExecutor) setService() (result int, err error) {
93         if len(t.ParamStr) < 1 {
94                 err = errors.New("error: empty parameter")
95                 return
96         }
97 -       cmd = exec.Command(t.ParamStr[0], t.ParamStr[1:]...)
98 -
99 -       // set "owner" account: need to execute user app
100 -       /*
101 -               execUser, _ := user.Lookup("owner")
102  
103 -               uid, _ := strconv.Atoi(execUser.Uid)
104 -               gid, _ := strconv.Atoi(execUser.Gid)
105 -               groups, _ := execUser.GroupIds()
106 -
107 -               var gids = []uint32{}
108 -               for _, i := range groups {
109 -                       id, _ := strconv.Atoi(i)
110 -                       gids = append(gids, uint32(id))
111 -               }
112 -
113 -               log.Printf("uid(%d), gid(%d)", uid, gid)
114 -               log.Printf("groupIds: %v", gids)
115 -
116 -               cmd.SysProcAttr = &syscall.SysProcAttr{}
117 -               cmd.SysProcAttr.Credential = &syscall.Credential{
118 -                       Uid:    uint32(uid),
119 -                       Gid:    uint32(gid),
120 -                       Groups: gids,
121 -               }
122 -       */
123 -
124 -       stdout, _ := cmd.StdoutPipe()
125 -       err = cmd.Start()
126 -       if err != nil {
127 -               log.Println(logPrefix, err.Error())
128 +       if nil == t.executeCB {
129 +               err = errors.New("Failed to execute: Native Callback is nil")
130                 return
131         }
132  
133 -       scanner := bufio.NewScanner(stdout)
134 -       for scanner.Scan() {
135 -               m := scanner.Text()
136 -               log.Println(m)
137 +       if len(t.ParamStr) < 2 {
138 +               result = t.executeCB.CmdExecute(t.ParamStr[0], "")
139 +       } else {
140 +               args := strings.Join(t.ParamStr[1:], " ")
141 +               result = t.executeCB.CmdExecute(t.ParamStr[0], args)
142         }
143  
144 -       pid = cmd.Process.Pid
145 -
146 +       if result != 0 {
147 +               err = errors.New("Failed to execute in native layer")
148 +               return
149 +       }
150 +       log.Println(logPrefix, "Successfully executed in native layer")
151         return
152  }
153