summaryrefslogtreecommitdiffstats
path: root/logd/src/logd/cmdbuf.cpp
blob: 9412a9cd588e5f24a593b0f30c9406c6bec988e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

#include <vespa/log/log.h>
LOG_SETUP("");
LOG_RCSID("$Id$");

#include "errhandle.h"
#include "service.h"
#include "forward.h"
#include "perform.h"
#include "cmdbuf.h"

namespace logdemon {

CmdBuf::CmdBuf()
    : _size(1000),
      _buf((char *)malloc(_size)),
      _bp(_buf),
      _left(_size)
{ }


CmdBuf::~CmdBuf()
{
    free(_buf);
}


bool
CmdBuf::hasCmd()
{
    char *p = _buf;
    while (p < _bp) {
        if (*p == '\n') return true;
        p++;
    }
    return false;

}

void
CmdBuf::doCmd(Performer& via)
{
    char *p = _buf;
    while (p < _bp) {
        if (*p == '\n') {
            *p = '\0';
            LOG(spam, "doing command: '%s'", _buf);
            via.doCmd(_buf);

            ++p;
            int len = p - _buf;
            int movelen = _bp - p;
            memmove(_buf, p, movelen);
            _bp -= len;
            _left += len;

            p = _buf;
            continue;
        }
        p++;
    }
}


void
CmdBuf::extend()
{
    _size *= 2;
    int pos = _bp - _buf;
    char *nbuf = (char *)realloc(_buf, _size);
    if (nbuf == NULL) {
        free(_buf);
        LOG(error, "could not allocate %d bytes", _size);
        throw SomethingBad("realloc failed");
    }
    _buf = nbuf;
    _bp = _buf + pos;
    _left = _size - pos;
}

#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif

void
CmdBuf::maybeRead(int fd)
{
    struct timeval notime;
    notime.tv_sec = 0;
    notime.tv_usec = 0;

    fd_set fdset;
    FD_ZERO(&fdset);
    FD_SET(fd, &fdset);
        
    while (select(fd + 1, &fdset, NULL, NULL, &notime) > 0) {
        // usually loops just once
        int oflags = fcntl(fd, F_GETFL);
        int nbflags = oflags | O_NONBLOCK;

        if (fcntl(fd, F_SETFL, nbflags) != 0) {
            LOG(error, "could not fcntl logserver socket: %s",
                strerror(errno));
            throw SomethingBad("fcntl failed");
        }

        ssize_t len = ::read(fd, _bp, _left);
        if (len > 0) {
            _bp += len;
            _left -= len;
            if (_left < 80) {
                extend();
            }
        } else if (len < 0) {
            LOG(warning, "error reading from logserver socket: %s",
                strerror(errno));
            throw ConnectionException("error reading");
        }
        fcntl(fd, F_SETFL, oflags);
        if (len == 0) {
            LOG(warning, "read 0 bytes from logserver socket");
            throw ConnectionException("eof on socket");
            break;
        }
    }
    return;
}


bool
CmdBuf::readFile(int fd)
{
    ssize_t len = ::read(fd, _bp, _left);
    if (len > 0) {
        _bp += len;
        _left -= len;
        if (_left < 80) {
            extend();
        }
        return true;
    }
    if (len < 0) {
        LOG(error, "error reading file: %s",
            strerror(errno));
        throw SomethingBad("read failed");
    }
    return false;
}

} // namespace