summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/in_tail/tail_signal.h
blob: 1a81fec64d6454eb7ebf44855f76d246b1776365 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef FLB_TAIL_SIGNAL_H
#define FLB_TAIL_SIGNAL_H

#include "tail_config.h"

static inline int tail_signal_manager(struct flb_tail_config *ctx)
{
    int n;
    uint64_t val = 0xc001;

    /*
     * The number of signal reads might be less than the written signals, this
     * means that some event is still pending in the queue. On that case we
     * don't need to signal it again.
     */
    if (ctx->ch_reads < ctx->ch_writes) {
        return 1;
    }

    /* Reset counters: prevent an overflow, unlikely..but let's keep safe */
    if (ctx->ch_reads == ctx->ch_writes) {
        ctx->ch_reads = 0;
        ctx->ch_writes = 0;
    }

    /* Insert a dummy event into the channel manager */
    n = flb_pipe_w(ctx->ch_manager[1], (const char *) &val, sizeof(val));
    if (n == -1) {
        flb_errno();
        return -1;
    }
    else {
        ctx->ch_writes++;
    }

    return n;
}

static inline int tail_signal_pending(struct flb_tail_config *ctx)
{
    int n;
    uint64_t val = 0xc002;

    /* Insert a dummy event into the 'pending' channel */
    n = flb_pipe_w(ctx->ch_pending[1], (const char *) &val, sizeof(val));

    /*
     * If we get EAGAIN, it simply means pending channel is full. As
     * notification is already pending, it's safe to ignore.
     */
    if (n == -1 && !FLB_PIPE_WOULDBLOCK()) {
        flb_errno();
        return -1;
    }

    return n;
}

static inline int tail_consume_pending(struct flb_tail_config *ctx)
{
    int ret;
    uint64_t val;

    /*
     * We need to consume the pending bytes. Loop until we would have
     * blocked (pipe is empty).
     */
    do {
        ret = flb_pipe_r(ctx->ch_pending[0], (char *) &val, sizeof(val));
        if (ret <= 0 && !FLB_PIPE_WOULDBLOCK()) {
            flb_errno();
            return -1;
        }
    } while (!FLB_PIPE_WOULDBLOCK());

    return 0;
}

#endif