summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/win32/winsvc.c
blob: 9e0942ee5f7a0b7ae2c1673ec659dca0cb58d0ef (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019-2020 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.
 */

#include <Windows.h>
#include <Shlwapi.h>

struct flb_config;
extern struct flb_config *config;
extern int flb_engine_exit(struct flb_config*);
extern int flb_main(int, char**);

/* Windows Service utils */
#define svc_name "fluent-bit"
static SERVICE_STATUS_HANDLE hstatus;
static int win32_argc;
static char **win32_argv;

/*
 * A Windows Service uses 'C:\Windows\System32' as working directory
 * by default. Here we use a more intuitive default path (where
 * fluent-bit.exe exists).
 */
static int update_default_workdir(void)
{
    char path[MAX_PATH];

    if (win32_argc < 1) {
        return -1;
    }

    if (strcpy_s(path, MAX_PATH, win32_argv[0])) {
        return -1;
    }

    if (!PathRemoveFileSpecA(path)) {
        return -1;
    }

    if (!SetCurrentDirectoryA(path)) {
        return -1;
    }

    return 0;
}

static void svc_notify(DWORD status)
{
    SERVICE_STATUS ss;

    ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    ss.dwCurrentState = status;
    ss.dwWin32ExitCode = NO_ERROR;
    ss.dwServiceSpecificExitCode = NO_ERROR;
    ss.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    ss.dwWaitHint = 30000;
    ss.dwCheckPoint = 0;

    /*
     * According to MSDN (SetServiceStatus), accepting control on
     * SERVICE_START_PENDING can crash the service.
     */
    if (status == SERVICE_START_PENDING) {
        ss.dwControlsAccepted = 0;
    }

    SetServiceStatus(hstatus, &ss);
}

static void WINAPI svc_handler(DWORD ctrl)
{
    switch (ctrl)
    {
    case SERVICE_CONTROL_STOP:
        svc_notify(SERVICE_STOP_PENDING);
        flb_engine_exit(config);
        return;
    default:
        break;
    }
}

static void WINAPI svc_main(DWORD svc_argc, LPTSTR *svc_argv)
{
    hstatus = RegisterServiceCtrlHandler(svc_name, svc_handler);
    if (!hstatus) {
        return;
    }

    update_default_workdir();

    svc_notify(SERVICE_START_PENDING);
    flb_main(win32_argc, win32_argv);
    svc_notify(SERVICE_STOPPED);
}

/*
 * Notify SCM that Fluent Bit is running.
 *
 * Note: Call this function in the main execution flow (immediately
 * before the engine is starting).
 */
void win32_started(void)
{
    if (hstatus) {
        svc_notify(SERVICE_RUNNING);
    }
}

static const SERVICE_TABLE_ENTRY svc_table[] = {
    {svc_name, svc_main},
    {NULL, NULL}
};

int win32_main(int argc, char **argv)
{
    win32_argc = argc;
    win32_argv = argv;

    if (StartServiceCtrlDispatcher(svc_table)) {
        return 0;
    }

    if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
        return -1;
    }

    /*
     * If we cannot connect to SCM, we assume that "fluent-bit.exe"
     * was invoked from the command line.
     */
    return flb_main(argc, argv);
}