// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x78(%rsp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0x83e0
testl %eax, %eax
je 0x2535e
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0x108ce(%rip), %rdi     # 0x35c18
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
xorl %eax, %eax
jmp 0x25409
movq 0x2a0(%rbx), %rax
cmpb $0x0, (%rax)
leaq 0x108df(%rip), %r12     # 0x35c4e
cmovneq %rax, %r12
leaq 0x588(%rbx), %r14
movl 0x78(%rsp), %edx
andb $-0x20, 0x594(%rbx)
movl $0x32, 0x5a0(%rbx)
movl %edx, 0x5a4(%rbx)
movl $0x1, 0x5a8(%rbx)
xorl %ebp, %ebp
leaq 0x21688(%rip), %rdi     # 0x46a30
movq %r14, %rsi
movl $0x1, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xe198
leaq 0x540(%rbx), %rdi
movb $0x1, %bpl
movzbl 0x500(%rbx), %edx
andl $0x1, %edx
leaq 0x21654(%rip), %rsi     # 0x46a30
movl $0x28, %ecx
callq 0x258d4
orb $0x1, 0x500(%rbx)
testl %eax, %eax
je 0x2541b
movl %eax, 0x504(%rbx)
movl 0x7c(%rsp), %edi
callq 0x8b60
orb $-0x80, 0x10c(%rbx)
movb $0x1, %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x78(%rsp), %edi
callq 0x8b60
leaq 0x258(%rbx), %rax
leaq 0x100(%rbx), %rcx
cmpq $0x0, 0x260(%rbx)
cmoveq %rcx, %rax
movq (%rax), %rcx
movq (%r15), %rdx
movl 0x7c(%rsp), %edi
leaq 0x300(%rbx), %rax
movl 0x4e4(%rbx), %r8d
movq 0x278(%rbx), %r9
movb 0x2f0(%rbx), %r10b
movl 0x2f4(%rbx), %r11d
movq 0x318(%rbx), %r15
movb 0x2f8(%rbx), %bpl
movzwl 0x2fa(%rbx), %r13d
leaq 0x88(%rsp), %rsi
movq %rdx, (%rsi)
movq %rcx, 0x8(%rsi)
movq %r12, 0x10(%rsi)
movq %r9, 0x18(%rsi)
movq %r15, 0x20(%rsi)
xorl %ecx, %ecx
movb %cl, 0x28(%rsi)
movb %cl, 0x29(%rsi)
movb %cl, 0x2a(%rsi)
movb %r10b, 0x2b(%rsi)
movl %r11d, 0x2c(%rsi)
movb %bpl, 0x30(%rsi)
movw %r13w, 0x32(%rsi)
movl %edi, 0x34(%rsi)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x38(%rsi)
movl %r8d, 0x3c(%rsi)
movl %ecx, 0x40(%rsi)
movl %ecx, 0x44(%rsi)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, 0x48(%rsi)
movq $0x0, 0x50(%rsi)
movq 0x4a8(%rbx), %rdx
movq %rdx, 0x58(%rsi)
movq %rax, 0x60(%rsi)
movl %ecx, 0x68(%rsi)
movl $0xe, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
callq 0x2eebc
movl 0x5a4(%rbx), %edx
leaq 0x21520(%rip), %rdi     # 0x46a30
movq %r14, %rsi
callq 0xd520
movl 0x78(%rsp), %edi
callq 0x8b60
movl 0x7c(%rsp), %edi
callq 0x8b60
jmp 0x25357
cmpl $0x2, %edx
jne 0x25565
movq %rax, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x80(%rsp), %rsi
movq %rax, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x256b7
callq 0x8a50
testb %bpl, %bpl
jne 0x25503
jmp 0x25518
movq %rax, %rdi
callq 0xb74e
nop