// File: /workspace/github_cpp_makefile_star2_O2/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 0x20518
leaq 0x10(%rsp), %r12
pushq $0x14
popq %rdx
movl %ebp, %edi
movq %r12, %rsi
callq 0x210d6
movq %rax, %r15
testl %r15d, %r15d
js 0x2054c
movl %ebp, %edi
callq 0x8b50
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andq %rbp, %r15
movb $0x0, 0x10(%rsp,%r15)
callq 0x8040
movq %rax, %r15
andl $0x0, (%rax)
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8ba0
cmpl $0x0, (%r15)
setne %cl
cmpq %r12, (%r13)
sete %dl
cmpq %rbp, %rax
seta %sil
orb %dl, %sil
orb %cl, %sil
jne 0x20587
movl %eax, 0x4d8(%rbx)
leaq 0x28(%rsp), %rdx
andl $0x0, 0x10(%rdx)
pushq $0x1
popq %r13
pushq $0x5
popq %rcx
movl %r13d, %edi
movl %eax, %esi
callq 0x81b0
leaq 0x4d8(%rbx), %r12
testl %eax, %eax
je 0x205b5
cmpl $-0x1, %eax
jne 0x20587
cmpl $0xa, (%r15)
jne 0x20587
movl (%r12), %edi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x88c0
testl %eax, %eax
je 0x205fe
cmpl $0x3, (%r15)
jne 0x205fe
leaq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x209c6
orl $-0x1, 0x4d8(%rbx)
movl %r13d, %ebp
jmp 0x205a1
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r14
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x20e91
leaq 0xf600(%rip), %rdi      # 0x2fb3d
callq 0x28f17
movq %r14, %rdi
callq 0x28f31
jmp 0x2059e
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r14
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x20e91
leaq 0xf5de(%rip), %rdi      # 0x2fb4f
callq 0x28f17
movq %r14, %rdi
callq 0x28f31
movl %ebp, %edi
callq 0x8b50
jmp 0x2059e
leaq 0x8(%rbx), %rdi
leaq 0x4d8(%rbx), %rsi
callq 0x209c6
orl $-0x1, 0x4d8(%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x38(%rsp)
je 0x205d7
orl $-0x1, (%r12)
movl 0x30(%rsp), %eax
movl 0x40(%rsp), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r14)
pushq $0x2
jmp 0x205a0
leaq 0x338(%rbx), %rdi
movl 0x4d8(%rbx), %edx
leaq 0x25445(%rip), %rsi     # 0x45a30
pushq $0x28
popq %rcx
callq 0x21144
orb $0xc, 0x4f4(%rbx)
xorl %ebp, %ebp
jmp 0x205a1
leaq 0x8(%rbx), %rsi
pushq $0x2
popq %rdi
callq 0x20e91
leaq 0xf55e(%rip), %rdi      # 0x2fb6f
movq %r12, %rsi
callq 0x20ea1
andb $-0x9, 0x4f4(%rbx)
jmp 0x205a1
movq %rax, %rdi
callq 0xaebb
nop