summaryrefslogtreecommitdiff
path: root/src/main.c
blob: af86375a15501765112cc7af289891ad790473e3 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "umbrella.h"
#include "daemon.h"
#include "unit.h"
#include "client.h"
#include "log.h"
#include "log_tail.h"
#include "filter.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/wait.h>

/* Global daemon state — defined here, declared extern in umbrella.h */
Daemon g;

/* ── Signal handling ─────────────────────────────────────────────────────── */

static void handle_signal(void) {
    struct signalfd_siginfo info;
    ssize_t n = read(g.signal_fd, &info, sizeof(info));
    if (n != sizeof(info)) return;

    switch (info.ssi_signo) {
    case SIGTERM:
    case SIGINT:
        log_info("Received signal %d — shutting down", info.ssi_signo);
        g.running = 0;
        break;

    case SIGCHLD:
        /* Reap all finished child processes (action scripts, etc.) */
        while (1) {
            int status;
            pid_t pid = waitpid(-1, &status, WNOHANG);
            if (pid <= 0) break;

            /* Check if any managed STDIN unit matches this pid */
            for (int i = 0; i < g.unit_count; i++) {
                Unit *u = &g.units[i];
                if (u->console.type == CONSOLE_STDIN && u->pid == pid) {
                    if (WIFEXITED(status))
                        log_warn("Unit %s exited with code %d",
                                 u->name, WEXITSTATUS(status));
                    else if (WIFSIGNALED(status))
                        log_warn("Unit %s killed by signal %d",
                                 u->name, WTERMSIG(status));
                    u->state = STATE_CRASHED;
                    u->pid = 0;
                    break;
                }
            }
        }
        break;

    case SIGHUP:
        log_info("SIGHUP received — reloading units");
        log_tail_cleanup();
        filter_stop_all();
        /* Reload unit descriptors. Existing runtime state is preserved. */
        g.unit_count = 0;
        unit_load_all();
        for (int i = 0; i < g.unit_count; i++)
            filter_start(&g.units[i]);
        log_tail_init();
        break;
    }
}

/* ── Process stdout handling ─────────────────────────────────────────────── */
/*
 * Called when a STDIN-type unit's stdout fd is readable.
 * Reads available output, pushes to ring buffer, broadcasts to clients.
 */
static void handle_process_output(int fd) {
    /* Find which unit owns this fd */
    Unit *u = NULL;
    for (int i = 0; i < g.unit_count; i++) {
        if (g.units[i].stdout_fd == fd) {
            u = &g.units[i];
            break;
        }
    }
    if (!u) return;

    char buf[RING_BUF_LINE_MAX];
    ssize_t n = read(fd, buf, sizeof(buf) - 1);
    if (n <= 0) {
        /* Process closed its stdout — mark as stopped */
        log_info("Unit %s stdout closed", u->name);
        epoll_ctl(g.epoll_fd, EPOLL_CTL_DEL, fd, NULL);
        close(fd);
        u->stdout_fd = -1;
        return;
    }

    buf[n] = '\0';
    int nf = (int)n;
    filter_apply(u, buf, sizeof(buf), &nf);
    if (nf <= 0) return;
    ring_push(u->output, buf, nf);
    client_broadcast_output(u->name, buf, 0);
}

/* ── Main event loop ─────────────────────────────────────────────────────── */

static void event_loop(void) {
    struct epoll_event events[64];

    while (g.running) {
        int n = epoll_wait(g.epoll_fd, events, 64, -1);
        if (n < 0) {
            if (errno == EINTR) continue;
            log_error("epoll_wait: %s", strerror(errno));
            break;
        }

        for (int i = 0; i < n; i++) {
            int fd = events[i].data.fd;
            uint32_t ev = events[i].events;

            if (fd == g.signal_fd) {
                handle_signal();

            } else if (fd == g.listen_fd) {
                client_accept(g.listen_fd);

            } else if (log_tail_fd_to_unit(fd) != NULL) {
		    log_tail_handle(fd);

	    } else {
                /* Check if this is a unit stdout fd */
                int is_unit_stdout = 0;
                for (int j = 0; j < g.unit_count; j++) {
                    if (g.units[j].stdout_fd == fd) {
                        is_unit_stdout = 1;
                        break;
                    }
                }

                if (is_unit_stdout) {
                    handle_process_output(fd);
                } else {
                    /* It's a client fd */
                    if (ev & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
                        client_remove(fd);
                    } else if (ev & EPOLLIN) {
                        if (client_handle(fd) < 0)
                            client_remove(fd);
                    }
                }
            }
        }
    }
}

/* ── Entry point ─────────────────────────────────────────────────────────── */

static void usage(const char *prog) {
    fprintf(stderr,
            "Usage: %s [options]\n"
            "  -f    Run in foreground (don't daemonize)\n"
            "  -d    Enable debug logging\n"
            "  -h    Show this help\n",
            prog);
}

int main(int argc, char *argv[]) {
    int foreground = 0;
    int debug = 0;
    int opt;

    while ((opt = getopt(argc, argv, "fdh")) != -1) {
        switch (opt) {
            case 'f': foreground = 1; break;
            case 'd': debug = 1;      break;
            case 'h': usage(argv[0]); return 0;
            default:  usage(argv[0]); return 1;
        }
    }

    /* Initialize logging before anything else */
    log_init(foreground ? NULL : UMBRELLA_LOG_FILE,
             debug ? LOG_DEBUG : LOG_INFO);
    log_info("umbrella %s starting", UMBRELLA_VERSION);

    /* Daemonize if not running in foreground */
    if (daemon_daemonize(foreground) != 0) {
        log_error("daemonize failed");
        return 1;
    }

    /* Set up epoll */
    g.epoll_fd = epoll_create1(EPOLL_CLOEXEC);
    if (g.epoll_fd < 0) {
        log_error("epoll_create1: %s", strerror(errno));
        return 1;
    }

    /* Initialize daemon (signals, pid file, directories) */
    if (daemon_init() != 0) {
        log_error("daemon_init failed");
        return 1;
    }

    /* Register signal fd with epoll */
    struct epoll_event ev;
    ev.events  = EPOLLIN;
    ev.data.fd = g.signal_fd;
    epoll_ctl(g.epoll_fd, EPOLL_CTL_ADD, g.signal_fd, &ev);

    /* Load unit descriptors */
    if (unit_load_all() < 0) {
        log_error("Failed to load units");
        daemon_cleanup();
        return 1;
    }

    for (int i = 0; i < g.unit_count; i++)
        filter_start(&g.units[i]);

    log_tail_init();

    /* Set up listening socket */
    g.listen_fd = client_listen();
    if (g.listen_fd < 0) {
        log_error("Failed to create listen socket");
        daemon_cleanup();
        return 1;
    }

    ev.events  = EPOLLIN;
    ev.data.fd = g.listen_fd;
    epoll_ctl(g.epoll_fd, EPOLL_CTL_ADD, g.listen_fd, &ev);

    log_info("Ready. %d unit(s) loaded.", g.unit_count);
    g.running = 1;

    event_loop();

    log_info("Shutting down");
    log_tail_cleanup();
    filter_stop_all();
    daemon_cleanup();
    log_close();
    return 0;
}