summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/monkey/plugins/cheetah/cheetah.c
blob: 489e73422b59beb22ac62de93e13219103fefae3 (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
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
160
161
162
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Server
 *  ==================
 *  Copyright 2001-2017 Eduardo Silva <eduardo@monkey.io>
 *
 *  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.
 */

/* Monkey Plugin Interface */
#include <monkey/mk_api.h>

/* Local header files */
#include "cmd.h"
#include "cutils.h"
#include "cheetah.h"
#include "loop.h"

void mk_cheetah_welcome_msg()
{
    CHEETAH_WRITE("\n%s%s***%s Welcome to %sCheetah!%s, the %sMonkey Shell %s:) %s***%s\n",
                  ANSI_BOLD, ANSI_YELLOW,
                  ANSI_WHITE, ANSI_GREEN,
                  ANSI_WHITE, ANSI_RED, ANSI_WHITE, ANSI_YELLOW, ANSI_RESET);
    CHEETAH_WRITE("\n      << %sType 'help' or '\\h' for help%s >>\n\n",
                  ANSI_BLUE, ANSI_RESET);
    CHEETAH_FLUSH();
}

static int mk_cheetah_config(char *path)
{
    unsigned long len;
    char *listen = NULL;
    char *default_file = NULL;
    struct mk_rconf *conf;
    struct mk_rconf_section *section;

    /* this variable is defined in cheetah.h and points to
     * the FILE *descriptor where to write out the data
     */
    cheetah_output = NULL;

    /* read configuration file */
    mk_api->str_build(&default_file, &len, "%scheetah.conf", path);
    conf = mk_api->config_open(default_file);
    if (!conf) {
        return -1;
    }

    section = mk_api->config_section_get(conf, "CHEETAH");

    if (!section) {
        CHEETAH_WRITE("\nError, could not find CHEETAH tag");
        return -1;
    }

    /* no longer needed */
    mk_api->mem_free(default_file);

    /* Listen directive */
    listen = mk_api->config_section_get_key(section, "Listen", MK_RCONF_STR);

    if (strcasecmp(listen, LISTEN_STDIN_STR) == 0) {
        listen_mode = LISTEN_STDIN;
    }
    else if (strcasecmp(listen, LISTEN_SERVER_STR) == 0) {
        listen_mode = LISTEN_SERVER;
    }
    else {
        printf("\nCheetah! Error: Invalid LISTEN value");
        return -1;
    }

    /* Cheetah cannot work in STDIN mode if Monkey is working in background */
    if (listen_mode == LISTEN_STDIN && mk_api->config->is_daemon == MK_TRUE) {
        printf("\nCheetah!: Forcing SERVER mode as Monkey is running in background\n");
        fflush(stdout);
        listen_mode = LISTEN_SERVER;
    }

    return 0;
}

static void mk_cheetah_init(void *args)
{
    struct mk_server *server = args;

    /* Rename worker */
    mk_api->worker_rename("monkey: cheetah");

    /* Open right FDs for I/O */
    if (listen_mode == LISTEN_STDIN) {
        cheetah_input = stdin;
        cheetah_output = stdout;
        mk_cheetah_loop_stdin(server);
    }
    else if (listen_mode == LISTEN_SERVER) {
        mk_cheetah_loop_server(server);
    }
}

/* This function is called when the plugin is loaded, it must
 * return
 */
int mk_cheetah_plugin_init(struct plugin_api **api, char *confdir)
{
    int ret;
    mk_api = *api;
    init_time = time(NULL);

    ret = mk_cheetah_config(confdir);
    return ret;
}

int mk_cheetah_plugin_exit()
{
    if (listen_mode == LISTEN_SERVER) {
        /* Remote named pipe */
        unlink(cheetah_server);
        mk_api->mem_free(cheetah_server);
    }

    return 0;
}

int mk_cheetah_master_init(struct mk_server *server)
{
    int ret;
    pthread_t tid;

    ret = mk_api->worker_spawn(mk_cheetah_init, server, &tid);
    if (ret != 0) {
        return -1;
    }

    return 0;
}

struct mk_plugin mk_plugin_cheetah = {
    /* Identification */
    .shortname     = "cheetah",
    .name          = "Cheetah! Shell",
    .version       = MK_VERSION_STR,

    /* Init / Exit */
    .init_plugin   = mk_cheetah_plugin_init,
    .exit_plugin   = mk_cheetah_plugin_exit,

    /* Init Levels */
    .master_init   = mk_cheetah_master_init,
    .worker_init   = NULL
};