// File: davmac314[P]dinit/src/proc-service.cc
// Function: process_service::start_stop_process(std::vector<char const*, std::allocator<char const*>> const&)
bool process_service::start_stop_process(const std::vector<const char *> &cmd) noexcept
{
    // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
    // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
    // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
    // is written to the pipe, and the parent can read it.

    int pipefd[2];
    if (bp_sys::pipe2(pipefd, O_CLOEXEC)) {
        log(loglevel_t::ERROR, get_name(), ": can't create status check pipe (for stop command): ",
                strerror(errno));
        return false;
    }

    const char * logfile = this->logfile.c_str();
    if (*logfile == 0) {
        logfile = "/dev/null";
    }

    bool child_status_registered = false;

    // Set up complete, now fork and exec:

    pid_t forkpid;

    try {
        stop_pipe_watcher.add_watch(event_loop, pipefd[0], dasynq::IN_EVENTS);
        child_status_registered = true;

        // We specify a high priority (i.e. low priority value) so that process termination is
        // handled early. This means we have always recorded that the process is terminated by the
        // time that we handle events that might otherwise cause us to signal the process, so we
        // avoid sending a signal to an invalid (and possibly recycled) process ID.
        forkpid = stop_watcher.fork(event_loop, reserved_stop_watch, dasynq::DEFAULT_PRIORITY - 10);
        reserved_stop_watch = true;
    }
    catch (std::exception &e) {
        log(loglevel_t::ERROR, get_name(), ": could not fork (for stop command): ", e.what());
        goto out_cs_h;
    }

    if (forkpid == 0) {
        close(pipefd[0]);
        const char * working_dir_c = service_dsc_dir;
        if (! working_dir.empty()) working_dir_c = working_dir.c_str();
        run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits};
        run_params.on_console = false;
        run_params.in_foreground = false;
        run_params.csfd = -1;
        run_params.socket_fd = socket_fd;
        run_params.notify_fd = -1;
        run_params.force_notify_fd = -1;
        run_params.notify_var = nullptr;
        run_params.env_file = env_file.c_str();
        run_params.nice_is_set = nice_is_set;
        run_params.nice = nice;
        #if SUPPORT_CGROUPS
        run_params.run_in_cgroup = run_in_cgroup.c_str();
        #endif
        #if SUPPORT_CAPABILITIES
        run_params.cap_iab = cap_iab.get();
        run_params.secbits = secbits;
        run_params.no_new_privs = onstart_flags.no_new_privs;
        #endif
        #if SUPPORT_IOPRIO
        run_params.ioprio = ioprio;
        #endif
        #if SUPPORT_OOM_ADJ
        run_params.oom_adj_is_set = oom_adj_is_set;
        run_params.oom_adj = oom_adj;
        #endif
        run_child_proc(run_params);
    }
    else {
        // Parent process
        stop_pid = forkpid;

        bp_sys::close(pipefd[1]); // close the 'other end' fd
        waiting_for_execstat = true;
        return true;
    }

    // Failure exit:

    out_cs_h:
    if (child_status_registered) {
        stop_pipe_watcher.deregister(event_loop);
    }

    bp_sys::close(pipefd[0]);
    bp_sys::close(pipefd[1]);

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0xa490
cmpl $0x0, %eax
je 0x51849
movq -0x150(%rbp), %rdi
callq 0x2d420
movq %rax, -0x158(%rbp)
callq 0xa060
movl (%rax), %edi
callq 0xa1f0
movq -0x158(%rbp), %rsi
movq %rax, -0x28(%rbp)
movl $0x3, %edi
leaq 0x273a1(%rip), %rdx     # 0x78bd8
leaq -0x28(%rbp), %rcx
callq 0x51e00
movb $0x0, -0x1(%rbp)
jmp 0x51bbf
movq -0x150(%rbp), %rdi
addq $0x2a0, %rdi            # imm = 0x2A0
callq 0xa250
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x51877
leaq 0x2739b(%rip), %rax     # 0x78c0e
movq %rax, -0x30(%rbp)
movq -0x150(%rbp), %rdi
movb $0x0, -0x31(%rbp)
addq $0x588, %rdi            # imm = 0x588
movl -0x20(%rbp), %edx
leaq 0x572cd(%rip), %rsi     # 0xa8b60
movl $0x1, %r8d
movl $0x32, %r9d
movl %r8d, %ecx
callq 0xf690
jmp 0x518a9
movq -0x150(%rbp), %rax
movb $0x1, -0x31(%rbp)
movq %rax, %rdi
addq $0x540, %rdi            # imm = 0x540
movzbl 0x500(%rax), %edx
andl $0x1, %edx
leaq 0x57291(%rip), %rsi     # 0xa8b60
movl $0x28, %ecx
callq 0x52e80
movl %eax, -0x15c(%rbp)
jmp 0x518e1
movq -0x150(%rbp), %rax
movl -0x15c(%rbp), %ecx
movl %ecx, -0x38(%rbp)
movb 0x500(%rax), %cl
andb $-0x2, %cl
orb $0x1, %cl
movb %cl, 0x500(%rax)
jmp 0x51981
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x51bd5
movq -0x40(%rbp), %rdi
callq 0xa240
movq -0x150(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x2d420
movq %rax, -0x168(%rbp)
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0x168(%rbp), %rsi
movq %rax, -0x58(%rbp)
leaq 0x272b8(%rip), %rdx     # 0x78c18
movl $0x3, %edi
leaq -0x58(%rbp), %rcx
callq 0x51e40
movl $0x2, -0x5c(%rbp)
callq 0xac30
jmp 0x5197c
jmp 0x51b87
cmpl $0x0, -0x38(%rbp)
jne 0x51b46
movl -0x20(%rbp), %edi
callq 0xad80
jmp 0x51995
movq -0x150(%rbp), %rdi
movq 0x100(%rdi), %rax
movq %rax, -0x68(%rbp)
addq $0x258, %rdi            # imm = 0x258
callq 0xac70
testb $0x1, %al
jne 0x519ce
movq -0x150(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
callq 0xa250
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
callq 0x52fb0
movq -0x150(%rbp), %r10
movq %rax, %rsi
movq -0x68(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl 0x4a8(%r10), %r9d
movl 0x4ac(%r10), %edi
addq $0x300, %r10            # imm = 0x300
movq %rsp, %rax
movq %r10, 0x8(%rax)
movl %edi, (%rax)
leaq -0xd8(%rbp), %rdi
callq 0x52fd0
jmp 0x51a19
movq -0x150(%rbp), %rdi
movb $0x0, -0xb0(%rbp)
movb $0x0, -0xaf(%rbp)
movl $0xffffffff, -0xa0(%rbp) # imm = 0xFFFFFFFF
movl 0x4e4(%rdi), %eax
movl %eax, -0x9c(%rbp)
movl $0xffffffff, -0x98(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x94(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x88(%rbp)
addq $0x278, %rdi            # imm = 0x278
callq 0xa250
movq -0x150(%rbp), %rdi
movq %rax, -0xc0(%rbp)
movb 0x2f0(%rdi), %al
andb $0x1, %al
movb %al, -0xad(%rbp)
movl 0x2f4(%rdi), %eax
movl %eax, -0xac(%rbp)
addq $0x318, %rdi            # imm = 0x318
callq 0xa250
movq %rax, %rcx
movq -0x150(%rbp), %rax
movq %rcx, -0xb8(%rbp)
movb 0x2f8(%rax), %cl
andb $0x1, %cl
movb %cl, -0xa8(%rbp)
movw 0x2fa(%rax), %ax
movw %ax, -0xa6(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
movl $0x70, %edx
callq 0xa5b0
movq -0x150(%rbp), %rdi
movups -0xe8(%rbp), %xmm0
movq %rsp, %rax
movups %xmm0, 0x60(%rax)
movups -0xf8(%rbp), %xmm0
movups %xmm0, 0x50(%rax)
movups -0x108(%rbp), %xmm0
movups %xmm0, 0x40(%rax)
movups -0x148(%rbp), %xmm0
movups -0x138(%rbp), %xmm1
movups -0x128(%rbp), %xmm2
movups -0x118(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x6ceb0
jmp 0x51b85
movq -0x150(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, 0x504(%rax)
movl -0x1c(%rbp), %edi
callq 0xad80
jmp 0x51b60
movq -0x150(%rbp), %rax
movl 0x10c(%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
orl $0x80, %ecx
movl %ecx, 0x10c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x51bbf
jmp 0x51b87
testb $0x1, -0x31(%rbp)
je 0x51ba7
movq -0x150(%rbp), %rdi
addq $0x588, %rdi            # imm = 0x588
leaq 0x56fbe(%rip), %rsi     # 0xa8b60
callq 0x17d20
movl -0x20(%rbp), %edi
callq 0xad80
jmp 0x51bb1
movl -0x1c(%rbp), %edi
callq 0xad80
jmp 0x51bbb
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq %rax, %rdi
callq 0xfa30
movq -0x40(%rbp), %rdi
callq 0xfa30
nop