summaryrefslogtreecommitdiffstats
path: root/src/detection/initsystem/initsystem_linux.c
blob: 445657bad2d4aef71a10b0a478c2daad640e6ef2 (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
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
#include "initsystem.h"
#include "common/processing.h"
#include "common/binary.h"
#include "common/strutil.h"

#include <libgen.h>
#include <unistd.h>

FF_A_UNUSED static bool extractSystemdVersion(const char* str, uint32_t len, void* userdata) {
    if (!ffStrStartsWith(str, "systemd ")) {
        return true;
    }
    const char* pstart = str + strlen("systemd ");
    const char* pend = memmem(pstart, len - strlen("systemd "), " running in ", strlen(" running in "));
    if (!pend) {
        return true;
    }
    ffStrbufSetNS((FFstrbuf*) userdata, (uint32_t) (pend - pstart), pstart);
    return false;
}

const char* ffDetectInitSystem(FFInitSystemResult* result) {
    const char* error = ffProcessGetBasicInfoLinux((int) result->pid, &result->name, NULL, NULL);
    if (error) {
#ifdef __ANDROID__
        if (access("/system/bin/init", F_OK) == 0) {
            ffStrbufSetStatic(&result->exe, "/system/bin/init");
            ffStrbufSetStatic(&result->name, "init");
            return NULL;
        }
#endif
        return error;
    }

    const char* _;
    // In linux /proc/1/exe is not readable
    ffProcessGetInfoLinux((int) result->pid, &result->name, &result->exe, &_, NULL);
    if (result->exe.chars[0] == '/') {
        // In some old system, /sbin/init is a symlink
        char buf[PATH_MAX];
        if (realpath(result->exe.chars, buf)) {
            ffStrbufSetS(&result->exe, buf);
            ffStrbufSetS(&result->name, basename(result->exe.chars));
        }
    }

    if (instance.config.general.detectVersion) {
#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__GNU__)
        if (ffStrbufEqualS(&result->name, "systemd")) {
            ffBinaryExtractStrings(result->exe.chars, extractSystemdVersion, &result->version, (uint32_t) strlen("systemd 0.0 running in x"));
            if (result->version.length == 0) {
                if (ffProcessAppendStdOut(&result->version, (char* const[]) {
                                                                ffStrbufEndsWithS(&result->exe, "/systemd") ? result->exe.chars : "systemctl", // use exe path in case users have another systemd installed
                                                                "--version",
                                                                NULL,
                                                            }) == NULL &&
                    result->version.length) {
                    uint32_t iStart = ffStrbufFirstIndexC(&result->version, '(');
                    if (iStart < result->version.length) {
                        uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ')');
                        ffStrbufSubstrBefore(&result->version, iEnd);
                        ffStrbufSubstrAfter(&result->version, iStart);
                    }
                }
            }
        } else if (ffStrbufEqualS(&result->name, "dinit")) {
            if (ffProcessAppendStdOut(&result->version, (char* const[]) {
                                                            ffStrbufEndsWithS(&result->exe, "/dinit") ? result->exe.chars : "dinit",
                                                            "--version",
                                                            NULL,
                                                        }) == NULL &&
                result->version.length) {
                // Dinit version 0.18.0.
                ffStrbufSubstrBeforeFirstC(&result->version, '\n');
                ffStrbufTrimRight(&result->version, '.');
                ffStrbufSubstrAfterLastC(&result->version, ' ');
            }
        } else if (ffStrbufEqualS(&result->name, "shepherd")) {
            if (ffProcessAppendStdOut(&result->version, (char* const[]) {
                                                            ffStrbufEndsWithS(&result->exe, "/shepherd") ? result->exe.chars : "shepherd",
                                                            "--version",
                                                            NULL,
                                                        }) == NULL &&
                result->version.length) {
                // shepherd (GNU Shepherd) 1.0.6
                // The first line in the output might not contain the version
                if (!ffStrbufStartsWithS(&result->version, "shepherd")) {
                    ffStrbufSubstrAfterFirstC(&result->version, '\n');
                }

                ffStrbufSubstrBeforeFirstC(&result->version, '\n');
                ffStrbufSubstrAfterLastC(&result->version, ' ');
            }
        }
#elif __APPLE__
        if (ffStrbufEqualS(&result->name, "launchd")) {
            if (ffProcessAppendStdOut(&result->version, (char* const[]) {
                                                            "/bin/launchctl",
                                                            "version",
                                                            NULL,
                                                        }) == NULL &&
                result->version.length) {
                uint32_t iStart = ffStrbufFirstIndexS(&result->version, "Version ");
                if (iStart < result->version.length) {
                    iStart += (uint32_t) strlen("Version");
                    uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ':');
                    ffStrbufSubstrBefore(&result->version, iEnd);
                    ffStrbufSubstrAfter(&result->version, iStart);
                }
            }
        }
#endif
    }

    return NULL;
}