// File: /workspace/github_cpp_makefile_star2_O1/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 0x24a0b
xorl %r12d, %r12d
leaq (%rsp,%r12), %rsi
movl $0x14, %edx
subq %r12, %rdx
movl %ebp, %edi
callq 0x88f0
movl $0x1, %r13d
testq %rax, %rax
je 0x248e6
js 0x248eb
addq %rax, %r12
xorl %r13d, %r13d
jmp 0x2490d
movq %r12, %r15
jmp 0x2490d
callq 0x8050
cmpl $0x4, (%rax)
jne 0x248fd
movl $0x2, %r13d
jmp 0x2490d
cmpq $0x1, %r12
movl $0x0, %r15d
sbbq %r15, %r15
orq %r12, %r15
testb $0x1, %r13b
jne 0x2491f
cmpq $0x14, %r12
jb 0x248be
movl $0x14, %r15d
testl %r15d, %r15d
js 0x24a41
movl %ebp, %edi
callq 0x8b60
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andq %rbp, %r15
movb $0x0, (%rsp,%r15)
callq 0x8050
movq %rax, %r12
movl $0x0, (%rax)
movq %rsp, %r15
leaq 0x20(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8830
cmpl $0x0, (%r12)
setne %cl
cmpq %r15, (%r13)
sete %dl
cmpq %rbp, %rax
seta %sil
orb %dl, %sil
orb %cl, %sil
jne 0x24b23
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), %r13
testl %eax, %eax
je 0x24a91
movb $0x1, %cl
cmpl $-0x1, %eax
jne 0x24a84
cmpl $0xa, (%r12)
jne 0x24a89
movl (%r13), %edi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x88d0
testl %eax, %eax
je 0x24b0a
cmpl $0x3, (%r12)
jne 0x24b0a
leaq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x24fe4
movl $0xffffffff, 0x4d8(%rbx) # imm = 0xFFFFFFFF
movl $0x1, %r15d
jmp 0x24b1d
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0x1116f(%rip), %rdi     # 0x35b9e
callq 0x2e73d
movq %r14, %rdi
callq 0x2e759
jmp 0x24b7c
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0x1114b(%rip), %rdi     # 0x35bb0
callq 0x2e73d
movq %r14, %rdi
callq 0x2e759
movl $0x1, %r15d
movl %ebp, %edi
callq 0x8b60
jmp 0x24b82
jmp 0x24b1f
testl %eax, %eax
jne 0x24b1f
cmpl $0x0, 0x38(%rsp)
je 0x24aba
movl $0xffffffff, (%r13)     # imm = 0xFFFFFFFF
movl 0x30(%rsp), %eax
movl 0x40(%rsp), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r14)
movl $0x2, %r15d
jmp 0x24b1d
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 0x21f3d(%rip), %rdi     # 0x46a30
addq $0x28, %rdi
callq 0x25edc
orb $0xc, 0x4f4(%rbx)
xorl %ecx, %ecx
xorl %r15d, %r15d
jmp 0x24b1f
leaq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x24f98
andb $-0x9, 0x4f4(%rbx)
xorl %ecx, %ecx
testb %cl, %cl
je 0x24b82
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0x110c0(%rip), %rdi     # 0x35bf8
callq 0x2e73d
movl 0x4d8(%rbx), %ecx
leaq 0x11c92(%rip), %rdx     # 0x367dc
leaq 0x28(%rsp), %r14
movl $0xc, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x89e0
movq %r14, %rdi
callq 0x2e73d
leaq 0x110a5(%rip), %rdi     # 0x35c12
callq 0x2e759
movl $0xffffffff, 0x4d8(%rbx) # imm = 0xFFFFFFFF
movl $0x1, %r15d
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24b99
movq %rax, %rdi
callq 0xb8be
nop