// File: davmac314[P]dinit/src/dinit.cc
// Function: find_cgroup_path()
static void find_cgroup_path() noexcept
{
    if (have_cgroups_path) {
        return;
    }

    int pfd = open("/proc/self/cgroup", O_RDONLY);
    if (pfd == -1) {
        return;
    }

    try {
        size_t cgroup_line_sz = 64;
        size_t cur_read = 0;
        size_t line_end_pos = (size_t)-1;
        size_t colon_count = 0; // how many colons have we seen?
        size_t second_colon_pos = 0;
        std::vector<char, default_init_allocator<char>> cgroup_line(cgroup_line_sz);

        while (true) {
            ssize_t r = read(pfd, cgroup_line.data() + cur_read, cgroup_line_sz - cur_read);
            if (r == 0) {
                if (line_end_pos == (size_t)-1) {
                    line_end_pos = cur_read + 1;
                }
                break;
            }
            if (r == -1) {
                close(pfd);
                return;
            }

            size_t rr = (size_t)r;
            for (size_t i = 0; i < rr; ++i) {
                if (cgroup_line[cur_read + i] == '\n') {
                    line_end_pos = cur_read + i;
                }
                else if (line_end_pos != (size_t)-1) {
                    log(loglevel_t::WARN, "In multiple cgroups, cannot determine cgroup root path");
                    close(pfd);
                    return;
                }
                else if (cgroup_line[cur_read + i] == ':') {
                    if (++colon_count == 2) {
                        second_colon_pos = cur_read + i;
                    }
                }
            }

            cur_read += rr;
            if (line_end_pos != (size_t)-1) {
                break;
            }

            if (cur_read == cgroup_line_sz) {
                cgroup_line.resize(cgroup_line_sz * 2);
                cgroup_line_sz *= 2;
            }
        };

        close(pfd);
        pfd = -1;

        // Now extract the path
        // The group line should look something like:
        //
        //    0::/some/path
        //
        // We want "some/path", i.e. we'll skip the leading slash.
        if (colon_count < 2 || (line_end_pos - second_colon_pos) == 1
                || cgroup_line[second_colon_pos+1] != '/') {
            // path is from 2nd colon to end
            log(loglevel_t::WARN, "Could not determine cgroup root path");
            return;
        }

        cgroups_path.clear();
        size_t first_char_pos = second_colon_pos + 2;
        size_t root_path_len = line_end_pos - first_char_pos;
        cgroups_path.append(cgroup_line.data() + first_char_pos, root_path_len);
        have_cgroups_path = true;
        return;
    }
    catch (std::bad_alloc &b) {
        if (pfd != -1) {
            close(pfd);
        }
        log(loglevel_t::WARN, "Out-of-memory reading cgroup root path");
        return;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
testb $0x1, 0x9befe(%rip)     # 0xa8d90
je 0xce99
jmp 0xd20b
leaq 0x69f62(%rip), %rdi     # 0x76e02
xorl %esi, %esi
movb %sil, %al
callq 0xa520
movl %eax, -0x9c(%rbp)
jmp 0xceb2
movl -0x9c(%rbp), %eax
movl %eax, -0x4(%rbp)
cmpl $-0x1, -0x4(%rbp)
jne 0xcec6
jmp 0xd20b
movq $0x40, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $-0x1, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x18e70
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x18e90
jmp 0xcf22
leaq -0x49(%rbp), %rdi
callq 0x18f00
jmp 0xcf2d
movl -0x4(%rbp), %eax
movl %eax, -0xbc(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x18f20
movl -0xbc(%rbp), %edi
movq %rax, %rsi
movq -0x18(%rbp), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rdx
subq %rax, %rdx
callq 0xaac0
movq %rax, -0xb8(%rbp)
jmp 0xcf64
movq -0xb8(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xcff5
cmpq $-0x1, -0x20(%rbp)
jne 0xcff0
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xcff0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x18f00
jmp 0xcfb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x18ff0
movl -0x5c(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0xd21c
movq -0x58(%rbp), %rdi
callq 0xa240
movq %rax, -0x98(%rbp)
cmpl $-0x1, -0x4(%rbp)
je 0xd1ea
movl -0x4(%rbp), %edi
callq 0xad80
jmp 0xcfeb
jmp 0xd1ea
jmp 0xd116
cmpq $-0x1, -0x68(%rbp)
jne 0xd012
movl -0x4(%rbp), %edi
callq 0xad80
jmp 0xd006
movl $0x1, -0x6c(%rbp)
jmp 0xd1df
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jae 0xd0d5
movq -0x18(%rbp), %rsi
addq -0x80(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x18f40
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0xd057
movq -0x18(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xd0c2
cmpq $-0x1, -0x20(%rbp)
je 0xd085
leaq 0x69daf(%rip), %rsi     # 0x76e14
movl $0x2, %edi
callq 0x6b540
movl -0x4(%rbp), %edi
callq 0xad80
jmp 0xd079
movl $0x1, -0x6c(%rbp)
jmp 0xd1df
movq -0x18(%rbp), %rsi
addq -0x80(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x18f40
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0xd0be
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x2, %rax
jne 0xd0bc
movq -0x18(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xd0be
jmp 0xd0c0
jmp 0xd0c2
jmp 0xd0c4
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xd022
movq -0x78(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $-0x1, -0x20(%rbp)
je 0xd0ea
jmp 0xd116
movq -0x18(%rbp), %rax
cmpq -0x10(%rbp), %rax
jne 0xd111
movq -0x10(%rbp), %rsi
addq %rsi, %rsi
leaq -0x48(%rbp), %rdi
callq 0x18f60
jmp 0xd106
movq -0x10(%rbp), %rax
shlq %rax
movq %rax, -0x10(%rbp)
jmp 0xcf2b
movl -0x4(%rbp), %edi
callq 0xad80
jmp 0xd120
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
cmpq $0x2, -0x28(%rbp)
jb 0xd155
movq -0x20(%rbp), %rax
subq -0x30(%rbp), %rax
cmpq $0x1, %rax
je 0xd155
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
leaq -0x48(%rbp), %rdi
callq 0x18f40
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0xd16f
movl $0x2, %edi
leaq 0x69cea(%rip), %rsi     # 0x76e4b
callq 0x6b540
movl $0x1, -0x6c(%rbp)
jmp 0xd1df
leaq 0x9bbfa(%rip), %rdi     # 0xa8d70
movq %rdi, -0xc8(%rbp)
callq 0xa9a0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movq -0x88(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x18f20
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
movq -0x88(%rbp), %rax
addq %rax, %rsi
movq -0x90(%rbp), %rdx
callq 0xa090
jmp 0xd1d1
movb $0x1, 0x9bbb8(%rip)     # 0xa8d90
movl $0x1, -0x6c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x18ff0
jmp 0xd20b
leaq 0x69c7f(%rip), %rsi     # 0x76e70
movl $0x2, %edi
callq 0x6b540
movl $0x1, -0x6c(%rbp)
callq 0xac30
jmp 0xd209
jmp 0xd20b
addq $0xd0, %rsp
popq %rbp
retq
movq %rax, %rdi
callq 0xfa30
movq -0x58(%rbp), %rdi
callq 0xfa30
nopw %cs:(%rax,%rax)