driver.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. "syscall"
  12. "time"
  13. "github.com/Sirupsen/logrus"
  14. "github.com/docker/docker/daemon/execdriver"
  15. "github.com/docker/docker/pkg/reexec"
  16. sysinfo "github.com/docker/docker/pkg/system"
  17. "github.com/docker/docker/pkg/term"
  18. "github.com/docker/libcontainer"
  19. "github.com/docker/libcontainer/apparmor"
  20. "github.com/docker/libcontainer/cgroups/systemd"
  21. "github.com/docker/libcontainer/configs"
  22. "github.com/docker/libcontainer/system"
  23. "github.com/docker/libcontainer/utils"
  24. )
  25. const (
  26. DriverName = "native"
  27. Version = "0.2"
  28. )
  29. type driver struct {
  30. root string
  31. initPath string
  32. activeContainers map[string]libcontainer.Container
  33. machineMemory int64
  34. factory libcontainer.Factory
  35. sync.Mutex
  36. }
  37. func NewDriver(root, initPath string) (*driver, error) {
  38. meminfo, err := sysinfo.ReadMemInfo()
  39. if err != nil {
  40. return nil, err
  41. }
  42. if err := os.MkdirAll(root, 0700); err != nil {
  43. return nil, err
  44. }
  45. // native driver root is at docker_root/execdriver/native. Put apparmor at docker_root
  46. if err := apparmor.InstallDefaultProfile(); err != nil {
  47. return nil, err
  48. }
  49. cgm := libcontainer.Cgroupfs
  50. if systemd.UseSystemd() {
  51. cgm = libcontainer.SystemdCgroups
  52. }
  53. f, err := libcontainer.New(
  54. root,
  55. cgm,
  56. libcontainer.InitPath(reexec.Self(), DriverName),
  57. )
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &driver{
  62. root: root,
  63. initPath: initPath,
  64. activeContainers: make(map[string]libcontainer.Container),
  65. machineMemory: meminfo.MemTotal,
  66. factory: f,
  67. }, nil
  68. }
  69. type execOutput struct {
  70. exitCode int
  71. err error
  72. }
  73. func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (execdriver.ExitStatus, error) {
  74. // take the Command and populate the libcontainer.Config from it
  75. container, err := d.createContainer(c)
  76. if err != nil {
  77. return execdriver.ExitStatus{ExitCode: -1}, err
  78. }
  79. var term execdriver.Terminal
  80. p := &libcontainer.Process{
  81. Args: append([]string{c.ProcessConfig.Entrypoint}, c.ProcessConfig.Arguments...),
  82. Env: c.ProcessConfig.Env,
  83. Cwd: c.WorkingDir,
  84. User: c.ProcessConfig.User,
  85. }
  86. if c.ProcessConfig.Tty {
  87. rootuid, err := container.HostUID()
  88. if err != nil {
  89. return execdriver.ExitStatus{ExitCode: -1}, err
  90. }
  91. cons, err := p.NewConsole(rootuid)
  92. if err != nil {
  93. return execdriver.ExitStatus{ExitCode: -1}, err
  94. }
  95. term, err = NewTtyConsole(cons, pipes, rootuid)
  96. } else {
  97. p.Stdout = pipes.Stdout
  98. p.Stderr = pipes.Stderr
  99. r, w, err := os.Pipe()
  100. if err != nil {
  101. return execdriver.ExitStatus{ExitCode: -1}, err
  102. }
  103. if pipes.Stdin != nil {
  104. go func() {
  105. io.Copy(w, pipes.Stdin)
  106. w.Close()
  107. }()
  108. p.Stdin = r
  109. }
  110. term = &execdriver.StdConsole{}
  111. }
  112. if err != nil {
  113. return execdriver.ExitStatus{ExitCode: -1}, err
  114. }
  115. c.ProcessConfig.Terminal = term
  116. cont, err := d.factory.Create(c.ID, container)
  117. if err != nil {
  118. return execdriver.ExitStatus{ExitCode: -1}, err
  119. }
  120. d.Lock()
  121. d.activeContainers[c.ID] = cont
  122. d.Unlock()
  123. defer func() {
  124. cont.Destroy()
  125. d.cleanContainer(c.ID)
  126. }()
  127. if err := cont.Start(p); err != nil {
  128. return execdriver.ExitStatus{ExitCode: -1}, err
  129. }
  130. if startCallback != nil {
  131. pid, err := p.Pid()
  132. if err != nil {
  133. p.Signal(os.Kill)
  134. p.Wait()
  135. return execdriver.ExitStatus{ExitCode: -1}, err
  136. }
  137. startCallback(&c.ProcessConfig, pid)
  138. }
  139. oom := notifyOnOOM(cont)
  140. waitF := p.Wait
  141. if nss := cont.Config().Namespaces; !nss.Contains(configs.NEWPID) {
  142. // we need such hack for tracking processes with inherited fds,
  143. // because cmd.Wait() waiting for all streams to be copied
  144. waitF = waitInPIDHost(p, cont)
  145. }
  146. ps, err := waitF()
  147. if err != nil {
  148. execErr, ok := err.(*exec.ExitError)
  149. if !ok {
  150. return execdriver.ExitStatus{ExitCode: -1}, err
  151. }
  152. ps = execErr.ProcessState
  153. }
  154. cont.Destroy()
  155. _, oomKill := <-oom
  156. return execdriver.ExitStatus{ExitCode: utils.ExitStatus(ps.Sys().(syscall.WaitStatus)), OOMKilled: oomKill}, nil
  157. }
  158. // notifyOnOOM returns a channel that signals if the container received an OOM notification
  159. // for any process. If it is unable to subscribe to OOM notifications then a closed
  160. // channel is returned as it will be non-blocking and return the correct result when read.
  161. func notifyOnOOM(container libcontainer.Container) <-chan struct{} {
  162. oom, err := container.NotifyOOM()
  163. if err != nil {
  164. logrus.Warnf("Your kernel does not support OOM notifications: %s", err)
  165. c := make(chan struct{})
  166. close(c)
  167. return c
  168. }
  169. return oom
  170. }
  171. func killCgroupProcs(c libcontainer.Container) {
  172. var procs []*os.Process
  173. if err := c.Pause(); err != nil {
  174. logrus.Warn(err)
  175. }
  176. pids, err := c.Processes()
  177. if err != nil {
  178. // don't care about childs if we can't get them, this is mostly because cgroup already deleted
  179. logrus.Warnf("Failed to get processes from container %s: %v", c.ID(), err)
  180. }
  181. for _, pid := range pids {
  182. if p, err := os.FindProcess(pid); err == nil {
  183. procs = append(procs, p)
  184. if err := p.Kill(); err != nil {
  185. logrus.Warn(err)
  186. }
  187. }
  188. }
  189. if err := c.Resume(); err != nil {
  190. logrus.Warn(err)
  191. }
  192. for _, p := range procs {
  193. if _, err := p.Wait(); err != nil {
  194. logrus.Warn(err)
  195. }
  196. }
  197. }
  198. func waitInPIDHost(p *libcontainer.Process, c libcontainer.Container) func() (*os.ProcessState, error) {
  199. return func() (*os.ProcessState, error) {
  200. pid, err := p.Pid()
  201. if err != nil {
  202. return nil, err
  203. }
  204. process, err := os.FindProcess(pid)
  205. s, err := process.Wait()
  206. if err != nil {
  207. execErr, ok := err.(*exec.ExitError)
  208. if !ok {
  209. return s, err
  210. }
  211. s = execErr.ProcessState
  212. }
  213. killCgroupProcs(c)
  214. p.Wait()
  215. return s, err
  216. }
  217. }
  218. func (d *driver) Kill(c *execdriver.Command, sig int) error {
  219. active := d.activeContainers[c.ID]
  220. if active == nil {
  221. return fmt.Errorf("active container for %s does not exist", c.ID)
  222. }
  223. state, err := active.State()
  224. if err != nil {
  225. return err
  226. }
  227. return syscall.Kill(state.InitProcessPid, syscall.Signal(sig))
  228. }
  229. func (d *driver) Pause(c *execdriver.Command) error {
  230. active := d.activeContainers[c.ID]
  231. if active == nil {
  232. return fmt.Errorf("active container for %s does not exist", c.ID)
  233. }
  234. return active.Pause()
  235. }
  236. func (d *driver) Unpause(c *execdriver.Command) error {
  237. active := d.activeContainers[c.ID]
  238. if active == nil {
  239. return fmt.Errorf("active container for %s does not exist", c.ID)
  240. }
  241. return active.Resume()
  242. }
  243. func (d *driver) Terminate(c *execdriver.Command) error {
  244. defer d.cleanContainer(c.ID)
  245. container, err := d.factory.Load(c.ID)
  246. if err != nil {
  247. return err
  248. }
  249. defer container.Destroy()
  250. state, err := container.State()
  251. if err != nil {
  252. return err
  253. }
  254. pid := state.InitProcessPid
  255. currentStartTime, err := system.GetProcessStartTime(pid)
  256. if err != nil {
  257. return err
  258. }
  259. if state.InitProcessStartTime == currentStartTime {
  260. err = syscall.Kill(pid, 9)
  261. syscall.Wait4(pid, nil, 0, nil)
  262. }
  263. return err
  264. }
  265. func (d *driver) Info(id string) execdriver.Info {
  266. return &info{
  267. ID: id,
  268. driver: d,
  269. }
  270. }
  271. func (d *driver) Name() string {
  272. return fmt.Sprintf("%s-%s", DriverName, Version)
  273. }
  274. func (d *driver) GetPidsForContainer(id string) ([]int, error) {
  275. d.Lock()
  276. active := d.activeContainers[id]
  277. d.Unlock()
  278. if active == nil {
  279. return nil, fmt.Errorf("active container for %s does not exist", id)
  280. }
  281. return active.Processes()
  282. }
  283. func (d *driver) cleanContainer(id string) error {
  284. d.Lock()
  285. delete(d.activeContainers, id)
  286. d.Unlock()
  287. return os.RemoveAll(filepath.Join(d.root, id))
  288. }
  289. func (d *driver) createContainerRoot(id string) error {
  290. return os.MkdirAll(filepath.Join(d.root, id), 0655)
  291. }
  292. func (d *driver) Clean(id string) error {
  293. return os.RemoveAll(filepath.Join(d.root, id))
  294. }
  295. func (d *driver) Stats(id string) (*execdriver.ResourceStats, error) {
  296. c := d.activeContainers[id]
  297. if c == nil {
  298. return nil, execdriver.ErrNotRunning
  299. }
  300. now := time.Now()
  301. stats, err := c.Stats()
  302. if err != nil {
  303. return nil, err
  304. }
  305. memoryLimit := c.Config().Cgroups.Memory
  306. // if the container does not have any memory limit specified set the
  307. // limit to the machines memory
  308. if memoryLimit == 0 {
  309. memoryLimit = d.machineMemory
  310. }
  311. return &execdriver.ResourceStats{
  312. Stats: stats,
  313. Read: now,
  314. MemoryLimit: memoryLimit,
  315. }, nil
  316. }
  317. func getEnv(key string, env []string) string {
  318. for _, pair := range env {
  319. parts := strings.Split(pair, "=")
  320. if parts[0] == key {
  321. return parts[1]
  322. }
  323. }
  324. return ""
  325. }
  326. type TtyConsole struct {
  327. console libcontainer.Console
  328. }
  329. func NewTtyConsole(console libcontainer.Console, pipes *execdriver.Pipes, rootuid int) (*TtyConsole, error) {
  330. tty := &TtyConsole{
  331. console: console,
  332. }
  333. if err := tty.AttachPipes(pipes); err != nil {
  334. tty.Close()
  335. return nil, err
  336. }
  337. return tty, nil
  338. }
  339. func (t *TtyConsole) Master() libcontainer.Console {
  340. return t.console
  341. }
  342. func (t *TtyConsole) Resize(h, w int) error {
  343. return term.SetWinsize(t.console.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
  344. }
  345. func (t *TtyConsole) AttachPipes(pipes *execdriver.Pipes) error {
  346. go func() {
  347. if wb, ok := pipes.Stdout.(interface {
  348. CloseWriters() error
  349. }); ok {
  350. defer wb.CloseWriters()
  351. }
  352. io.Copy(pipes.Stdout, t.console)
  353. }()
  354. if pipes.Stdin != nil {
  355. go func() {
  356. io.Copy(t.console, pipes.Stdin)
  357. pipes.Stdin.Close()
  358. }()
  359. }
  360. return nil
  361. }
  362. func (t *TtyConsole) Close() error {
  363. return t.console.Close()
  364. }