2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 21:03:21 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2014-07-31 21:03:21 +00:00
|
|
|
"strconv"
|
2016-01-27 22:09:42 +00:00
|
|
|
"time"
|
2014-07-31 21:03:21 +00:00
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-01-27 22:09:42 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2015-07-01 00:40:13 +00:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2018-04-09 19:35:24 +00:00
|
|
|
logcache "github.com/docker/docker/daemon/logger/loggerutils/cache"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2018-02-08 22:16:20 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-07-31 21:03:21 +00:00
|
|
|
)
|
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
// ContainerLogs copies the container's log channel to the channel provided in
|
|
|
|
// the config. If ContainerLogs returns an error, no messages have been copied.
|
|
|
|
// and the channel will be closed without data.
|
|
|
|
//
|
|
|
|
// if it returns nil, the config channel will be active and return log
|
|
|
|
// messages until it runs out or the context is canceled.
|
2018-05-04 16:41:42 +00:00
|
|
|
func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, config *types.ContainerLogsOptions) (messages <-chan *backend.LogMessage, isTTY bool, retErr error) {
|
2017-03-20 17:07:04 +00:00
|
|
|
lg := logrus.WithFields(logrus.Fields{
|
|
|
|
"module": "daemon",
|
|
|
|
"method": "(*Daemon).ContainerLogs",
|
|
|
|
"container": containerName,
|
|
|
|
})
|
|
|
|
|
2016-11-22 13:51:22 +00:00
|
|
|
if !(config.ShowStdout || config.ShowStderr) {
|
2017-11-29 04:09:37 +00:00
|
|
|
return nil, false, errdefs.InvalidParameter(errors.New("You must choose at least one stream"))
|
2016-11-22 13:51:22 +00:00
|
|
|
}
|
2015-12-11 17:39:28 +00:00
|
|
|
container, err := daemon.GetContainer(containerName)
|
2015-09-28 20:36:29 +00:00
|
|
|
if err != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, false, err
|
2015-09-28 20:36:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 07:55:55 +00:00
|
|
|
if container.RemovalInProgress || container.Dead {
|
2017-11-29 04:09:37 +00:00
|
|
|
return nil, false, errdefs.Conflict(errors.New("can not get logs from container which is dead or marked for removal"))
|
2017-03-24 07:55:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 13:51:22 +00:00
|
|
|
if container.HostConfig.LogConfig.Type == "none" {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, false, logger.ErrReadLogsNotSupported{}
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-04-11 21:49:14 +00:00
|
|
|
|
2017-04-20 10:17:06 +00:00
|
|
|
cLog, cLogCreated, err := daemon.getLogger(container)
|
2015-04-09 04:23:30 +00:00
|
|
|
if err != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, false, err
|
2015-07-01 00:40:13 +00:00
|
|
|
}
|
2017-04-20 10:17:06 +00:00
|
|
|
if cLogCreated {
|
|
|
|
defer func() {
|
2018-05-04 16:41:42 +00:00
|
|
|
if retErr != nil {
|
|
|
|
if err = cLog.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing logger: %v", err)
|
|
|
|
}
|
2017-04-20 10:17:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-03-20 17:07:04 +00:00
|
|
|
|
2015-07-03 13:50:06 +00:00
|
|
|
logReader, ok := cLog.(logger.LogReader)
|
2015-07-01 00:40:13 +00:00
|
|
|
if !ok {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, false, logger.ErrReadLogsNotSupported{}
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2015-05-07 01:09:27 +00:00
|
|
|
|
2017-03-15 15:41:12 +00:00
|
|
|
follow := config.Follow && !cLogCreated
|
2015-07-03 13:50:06 +00:00
|
|
|
tailLines, err := strconv.Atoi(config.Tail)
|
|
|
|
if err != nil {
|
|
|
|
tailLines = -1
|
|
|
|
}
|
2015-04-23 22:08:41 +00:00
|
|
|
|
2016-01-27 22:09:42 +00:00
|
|
|
var since time.Time
|
|
|
|
if config.Since != "" {
|
|
|
|
s, n, err := timetypes.ParseTimestamps(config.Since, 0)
|
|
|
|
if err != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, false, err
|
2016-01-27 22:09:42 +00:00
|
|
|
}
|
|
|
|
since = time.Unix(s, n)
|
|
|
|
}
|
2017-03-20 17:07:04 +00:00
|
|
|
|
2017-04-28 11:53:00 +00:00
|
|
|
var until time.Time
|
|
|
|
if config.Until != "" && config.Until != "0" {
|
|
|
|
s, n, err := timetypes.ParseTimestamps(config.Until, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
until = time.Unix(s, n)
|
|
|
|
}
|
|
|
|
|
2015-07-03 13:50:06 +00:00
|
|
|
readConfig := logger.ReadConfig{
|
2016-01-27 22:09:42 +00:00
|
|
|
Since: since,
|
2017-04-28 11:53:00 +00:00
|
|
|
Until: until,
|
2015-07-03 13:50:06 +00:00
|
|
|
Tail: tailLines,
|
|
|
|
Follow: follow,
|
|
|
|
}
|
2014-10-30 19:33:26 +00:00
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
logs := logReader.ReadLogs(readConfig)
|
2015-12-19 14:43:10 +00:00
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
// past this point, we can't possibly return any errors, so we can just
|
|
|
|
// start a goroutine and return to tell the caller not to expect errors
|
|
|
|
// (if the caller wants to give up on logs, they have to cancel the context)
|
|
|
|
// this goroutine functions as a shim between the logger and the caller.
|
|
|
|
messageChan := make(chan *backend.LogMessage, 1)
|
|
|
|
go func() {
|
2018-05-04 16:41:42 +00:00
|
|
|
if cLogCreated {
|
|
|
|
defer func() {
|
|
|
|
if err = cLog.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing logger: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
daemon.ContainerLogs(): fix resource leak on follow
When daemon.ContainerLogs() is called with options.follow=true
(as in "docker logs --follow"), the "loggerutils.followLogs()"
function never returns (even then the logs consumer is gone).
As a result, all the resources associated with it (including
an opened file descriptor for the log file being read, two FDs
for a pipe, and two FDs for inotify watch) are never released.
If this is repeated (such as by running "docker logs --follow"
and pressing Ctrl-C a few times), this results in DoS caused by
either hitting the limit of inotify watches, or the limit of
opened files. The only cure is daemon restart.
Apparently, what happens is:
1. logs producer (a container) is gone, calling (*LogWatcher).Close()
for all its readers (daemon/logger/jsonfilelog/jsonfilelog.go:175).
2. WatchClose() is properly handled by a dedicated goroutine in
followLogs(), cancelling the context.
3. Upon receiving the ctx.Done(), the code in followLogs()
(daemon/logger/loggerutils/logfile.go#L626-L638) keeps to
send messages _synchronously_ (which is OK for now).
4. Logs consumer is gone (Ctrl-C is pressed on a terminal running
"docker logs --follow"). Method (*LogWatcher).Close() is properly
called (see daemon/logs.go:114). Since it was called before and
due to to once.Do(), nothing happens (which is kinda good, as
otherwise it will panic on closing a closed channel).
5. A goroutine (see item 3 above) keeps sending log messages
synchronously to the logWatcher.Msg channel. Since the
channel reader is gone, the channel send operation blocks forever,
and resource cleanup set up in defer statements at the beginning
of followLogs() never happens.
Alas, the fix is somewhat complicated:
1. Distinguish between close from logs producer and logs consumer.
To that effect,
- yet another channel is added to LogWatcher();
- {Watch,}Close() are renamed to {Watch,}ProducerGone();
- {Watch,}ConsumerGone() are added;
*NOTE* that ProducerGone()/WatchProducerGone() pair is ONLY needed
in order to stop ConsumerLogs(follow=true) when a container is stopped;
otherwise we're not interested in it. In other words, we're only
using it in followLogs().
2. Code that was doing (logWatcher*).Close() is modified to either call
ProducerGone() or ConsumerGone(), depending on the context.
3. Code that was waiting for WatchClose() is modified to wait for
either ConsumerGone() or ProducerGone(), or both, depending on the
context.
4. followLogs() are modified accordingly:
- context cancellation is happening on WatchProducerGone(),
and once it's received the FileWatcher is closed and waitRead()
returns errDone on EOF (i.e. log rotation handling logic is disabled);
- due to this, code that was writing synchronously to logWatcher.Msg
can be and is removed as the code above it handles this case;
- function returns once ConsumerGone is received, freeing all the
resources -- this is the bugfix itself.
While at it,
1. Let's also remove the ctx usage to simplify the code a bit.
It was introduced by commit a69a59ffc7e3d ("Decouple removing the
fileWatcher from reading") in order to fix a bug. The bug was actually
a deadlock in fsnotify, and the fix was just a workaround. Since then
the fsnofify bug has been fixed, and a new fsnotify was vendored in.
For more details, please see
https://github.com/moby/moby/pull/27782#issuecomment-416794490
2. Since `(*filePoller).Close()` is fixed to remove all the files
being watched, there is no need to explicitly call
fileWatcher.Remove(name) anymore, so get rid of the extra code.
Should fix https://github.com/moby/moby/issues/37391
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-01 04:03:55 +00:00
|
|
|
// signal that the log reader is gone
|
|
|
|
defer logs.ConsumerGone()
|
2017-04-20 10:17:06 +00:00
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
// close the messages channel. closing is the only way to signal above
|
|
|
|
// that we're doing with logs (other than context cancel i guess).
|
|
|
|
defer close(messageChan)
|
|
|
|
|
|
|
|
lg.Debug("begin logs")
|
2018-08-01 03:50:45 +00:00
|
|
|
defer lg.Debugf("end logs (%v)", ctx.Err())
|
|
|
|
|
2017-03-20 17:07:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// i do not believe as the system is currently designed any error
|
|
|
|
// is possible, but we should be prepared to handle it anyway. if
|
|
|
|
// we do get an error, copy only the error field to a new object so
|
|
|
|
// we don't end up with partial data in the other fields
|
|
|
|
case err := <-logs.Err:
|
|
|
|
lg.Errorf("Error streaming logs: %v", err)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case messageChan <- &backend.LogMessage{Err: err}:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case msg, ok := <-logs.Msg:
|
|
|
|
// there is some kind of pool or ring buffer in the logger that
|
|
|
|
// produces these messages, and a possible future optimization
|
|
|
|
// might be to use that pool and reuse message objects
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := msg.AsLogMessage() // just a pointer conversion, does not copy data
|
|
|
|
|
|
|
|
// there could be a case where the reader stops accepting
|
|
|
|
// messages and the context is canceled. we need to check that
|
|
|
|
// here, or otherwise we risk blocking forever on the message
|
|
|
|
// send.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case messageChan <- m:
|
|
|
|
}
|
2014-10-30 19:33:26 +00:00
|
|
|
}
|
2014-07-31 21:03:21 +00:00
|
|
|
}
|
2017-03-20 17:07:04 +00:00
|
|
|
}()
|
2017-07-19 14:20:13 +00:00
|
|
|
return messageChan, container.Config.Tty, nil
|
2015-07-01 00:40:13 +00:00
|
|
|
}
|
2015-11-03 18:45:12 +00:00
|
|
|
|
2017-04-20 10:17:06 +00:00
|
|
|
func (daemon *Daemon) getLogger(container *container.Container) (l logger.Logger, created bool, err error) {
|
|
|
|
container.Lock()
|
|
|
|
if container.State.Running {
|
|
|
|
l = container.LogDriver
|
|
|
|
}
|
|
|
|
container.Unlock()
|
|
|
|
if l == nil {
|
|
|
|
created = true
|
|
|
|
l, err = container.StartLogger()
|
2015-11-03 18:45:12 +00:00
|
|
|
}
|
2017-04-20 10:17:06 +00:00
|
|
|
return
|
2015-11-03 18:45:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 12:50:37 +00:00
|
|
|
// mergeLogConfig merges the daemon log config to the container's log config if the container's log driver is not specified.
|
|
|
|
func (daemon *Daemon) mergeAndVerifyLogConfig(cfg *containertypes.LogConfig) error {
|
|
|
|
if cfg.Type == "" {
|
|
|
|
cfg.Type = daemon.defaultLogConfig.Type
|
|
|
|
}
|
|
|
|
|
2016-07-12 19:05:44 +00:00
|
|
|
if cfg.Config == nil {
|
|
|
|
cfg.Config = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2016-03-12 12:50:37 +00:00
|
|
|
if cfg.Type == daemon.defaultLogConfig.Type {
|
|
|
|
for k, v := range daemon.defaultLogConfig.Config {
|
|
|
|
if _, ok := cfg.Config[k]; !ok {
|
|
|
|
cfg.Config[k] = v
|
|
|
|
}
|
2016-03-02 12:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 19:35:24 +00:00
|
|
|
logcache.MergeDefaultLogConfig(cfg.Config, daemon.defaultLogConfig.Config)
|
|
|
|
|
2016-03-12 12:50:37 +00:00
|
|
|
return logger.ValidateLogOpts(cfg.Type, cfg.Config)
|
2016-03-02 12:22:18 +00:00
|
|
|
}
|
2018-02-08 22:16:20 +00:00
|
|
|
|
|
|
|
func (daemon *Daemon) setupDefaultLogConfig() error {
|
|
|
|
config := daemon.configStore
|
|
|
|
if len(config.LogConfig.Config) > 0 {
|
|
|
|
if err := logger.ValidateLogOpts(config.LogConfig.Type, config.LogConfig.Config); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to set log opts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
daemon.defaultLogConfig = containertypes.LogConfig{
|
|
|
|
Type: config.LogConfig.Type,
|
|
|
|
Config: config.LogConfig.Config,
|
|
|
|
}
|
2018-04-09 19:35:24 +00:00
|
|
|
|
2018-02-08 22:16:20 +00:00
|
|
|
logrus.Debugf("Using default logging driver %s", daemon.defaultLogConfig.Type)
|
|
|
|
return nil
|
|
|
|
}
|