// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/proc-service.cc
// Function: bgproc_service::read_pid_file(dasynq::dprivate::proc_status*)
bgproc_service::pid_result_t
bgproc_service::read_pid_file(proc_status_t *exit_status) noexcept
{
    const char *pid_file_c = pid_file.c_str();
    int fd = bp_sys::open(pid_file_c, O_CLOEXEC);
    if (fd == -1) {
        log(loglevel_t::ERROR, get_name(), ": read pid file: ", strerror(errno));
        return pid_result_t::FAILED;
    }

    char pidbuf[21]; // just enough to hold any 64-bit integer
    int r = complete_read(fd, pidbuf, 20);
    if (r < 0) {
        // Could not read from PID file
        log(loglevel_t::ERROR, get_name(), ": could not read from pidfile; ", strerror(errno));
        bp_sys::close(fd);
        return pid_result_t::FAILED;
    }

    bp_sys::close(fd);
    pidbuf[r] = 0; // store nul terminator

    bool valid_pid = false;
    errno = 0;
    char *eptr;
    unsigned long long v = std::strtoull(pidbuf, &eptr, 0);
    if (errno == 0 && eptr != pidbuf) {
        if (v <= make_unsigned_val(std::numeric_limits<pid_t>::max())) {
            pid = (pid_t) v;
            valid_pid = true;
        }
    }

    if (valid_pid) {
        siginfo_t child_info;
        child_info.si_pid = 0; // for portability
        int waitid_r = bp_sys::waitid(P_PID, pid, &child_info, WNOHANG | WEXITED);
        if (waitid_r == -1 && errno == ECHILD) {
            // We can't track this child - check process exists:
            if (bp_sys::kill(pid, 0) == 0 || errno != ESRCH) {
                log(loglevel_t::WARN, get_name(), ": unable to supervise process with pid ", pid);
                tracking_child = false;
                return pid_result_t::OK;
            }

            log(loglevel_t::ERROR, get_name(), ": pid read from pidfile (", pid, ") is not valid");
            pid = -1;
            return pid_result_t::FAILED;
        }
        else if (waitid_r == 0) {
            if (child_info.si_pid != 0) {
                pid = -1;
                *exit_status = proc_status_t(child_info.si_code, child_info.si_status);
                return pid_result_t::TERMINATED;
            }

            // Child hasn't terminated, but we can track it (no error)
            child_listener.add_reserved(event_loop, pid, dasynq::DEFAULT_PRIORITY - 10);
            tracking_child = true;
            reserved_child_watch = true;
            return pid_result_t::OK;
        }
    }

    log(loglevel_t::ERROR, get_name(), ": pid read from pidfile (", pid, ") is not valid");
    pid = -1;
    return pid_result_t::FAILED;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x5f0(%rdi), %rdi
movl $0x80000, %esi          # imm = 0x80000
xorl %eax, %eax
callq 0x8450
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x24ae1
xorl %r15d, %r15d
leaq (%rsp,%r15), %rsi
movl $0x14, %edx
subq %r15, %rdx
movl %ebp, %edi
callq 0x88f0
testq %rax, %rax
je 0x24b23
js 0x24ac9
addq %rax, %r15
jmp 0x24ad3
callq 0x8050
cmpl $0x4, (%rax)
jne 0x24b17
cmpq $0x14, %r15
jb 0x24aaa
movl $0x14, %r15d
jmp 0x24b2c
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0x11050(%rip), %rdi     # 0x35b55
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
jmp 0x24c3f
xorl %eax, %eax
cmpq $0x1, %r15
sbbq %rax, %rax
orq %rax, %r15
testl %r15d, %r15d
js 0x24c4a
movl %ebp, %edi
callq 0x8b60
movb $0x0, (%rsp,%r15)
callq 0x8050
movq %rax, %r15
movl $0x0, (%rax)
movq %rsp, %r12
leaq 0x20(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8830
cmpl $0x0, (%r15)
setne %cl
cmpq %r12, (%r13)
sete %dl
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
seta %sil
orb %dl, %sil
orb %cl, %sil
jne 0x24be6
movl %eax, 0x4d8(%rbx)
leaq 0x28(%rsp), %rdx
movl $0x0, 0x10(%rdx)
movl $0x1, %edi
movl %eax, %esi
movl $0x5, %ecx
callq 0x81c0
leaq 0x4d8(%rbx), %r12
testl %eax, %eax
je 0x24c8d
cmpl $-0x1, %eax
jne 0x24be6
cmpl $0xa, (%r15)
jne 0x24be6
movl (%r12), %edi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x88d0
testl %eax, %eax
je 0x24d04
cmpl $0x3, (%r15)
jne 0x24d04
movq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x2517a
jmp 0x24c35
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0x10fb4(%rip), %rdi     # 0x35baf
callq 0x2e520
movl 0x4d8(%rbx), %ecx
leaq 0x11b9f(%rip), %rdx     # 0x367ac
leaq 0x28(%rsp), %r14
movl $0xc, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x89e0
movq %r14, %rdi
callq 0x2e520
leaq 0x10f99(%rip), %rdi     # 0x35bc9
callq 0x2e53c
movl $0xffffffff, 0x4d8(%rbx) # imm = 0xFFFFFFFF
movl $0x1, %r14d
jmp 0x24d17
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0x10ef9(%rip), %rdi     # 0x35b67
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
movl $0x1, %r14d
movl %ebp, %edi
callq 0x8b60
jmp 0x24d17
cmpl $0x0, 0x38(%rsp)
je 0x24cb6
movl $0xffffffff, (%r12)     # imm = 0xFFFFFFFF
movl 0x30(%rsp), %eax
movl 0x40(%rsp), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r14)
movl $0x2, %r14d
jmp 0x24d17
leaq 0x338(%rbx), %rax
movl 0x4d8(%rbx), %edx
andb $-0x20, 0x344(%rbx)
movl %edx, 0x368(%rbx)
movl $0x28, 0x350(%rbx)
leaq 0x358(%rbx), %rsi
movq %rax, 0x358(%rbx)
leaq 0x21d41(%rip), %rdi     # 0x46a30
addq $0x28, %rdi
callq 0x2604a
orb $0xc, 0x4f4(%rbx)
xorl %r14d, %r14d
jmp 0x24d17
movq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x2512e
andb $-0x9, 0x4f4(%rbx)
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24d2e
movq %rax, %rdi
callq 0xb74e