aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/logctl.cpp
blob: 59c02b57344fa21e95a81de8afe1a76710c3b8a6 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "logctl.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>

#include <vespa/log/log.h>
LOG_SETUP(".sentinel.logctl");

namespace config::sentinel {

void justRunLogctl(const char *cspec, const char *lspec)
{
    const char *progName = "vespa-logctl";
    pid_t pid = fork();
    if (pid == 0) {
        LOG(debug, "running '%s' '%s' '%s'", progName, cspec, lspec);
        int devnull = open("/dev/null", O_WRONLY);
        if (devnull > 1) {
            dup2(devnull, 1);
            close(devnull);
        }
        int rv = execlp(progName, progName, "-c", cspec, lspec, nullptr);
        if (rv != 0) {
            LOG(warning, "execlp of '%s' failed: %s", progName, strerror(errno));
        }
    } else if (pid > 0) {
        bool again;
        do {
            again = false;
            int wstatus = 0;
            pid_t got = waitpid(pid, &wstatus, 0);
            if (got == pid) {
                again = false;
                if (WIFEXITED(wstatus)) {
                    int exitCode = WEXITSTATUS(wstatus);
                    if (exitCode != 0) {
                        LOG(warning, "running '%s' failed (exit code %d)", progName, exitCode);
                    }
                } else if (WIFSIGNALED(wstatus)) {
                    int termSig = WTERMSIG(wstatus);
                    LOG(warning, "running '%s' failed (got signal %d)", progName, termSig);
                } else {
                    LOG(warning, "'%s' failure (wait status was %d)", progName, wstatus);
                }
            } else if (errno == EINTR) {
                again = true;
            } else {
                LOG(error, "waitpid() failed: %s", strerror(errno));
            }
        } while (again);
    } else {
        LOG(error, "fork() failed: %s", strerror(errno));
    }
}

}