// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/baseproc-service.cc
// Function: base_process_service::start_ps_process(std::vector<char const*, std::allocator<char const*>> const&, bool)
bool base_process_service::start_ps_process(const std::vector<const char *> &cmd, bool on_console) 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.

    event_loop.get_time(last_start_time, clock_type::MONOTONIC);

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

    int input_fd;
    if (!get_input_fd(&input_fd)) {
        return false;
    }

    const char * logfile = this->logfile.c_str();
    if (this->log_type == log_type_id::LOGFILE || this->log_type == log_type_id::NONE) {
        // Note: if log_type == NONE, logfile should be empty.
        if (*logfile == 0) {
            logfile = "/dev/null";
        }
    }
    else /* log_type_id::BUFFER or ::PIPE */ {
        if (this->log_output_fd == -1) {
            int logfd[2];
            // Note: we set CLOEXEC on the file descriptors here; when the output file descriptor is dup'd
            // to stdout, this will be effectively removed for the output end
            if (bp_sys::pipe2(logfd, O_CLOEXEC)) {
                log(loglevel_t::ERROR, get_name(), ": can't create output pipe: ", strerror(errno));
                goto out_p;
            }
            this->log_input_fd = logfd[0];
            this->log_output_fd = logfd[1];
            if (this->log_type == log_type_id::BUFFER) {
                try {
                    this->log_output_listener.add_watch(event_loop, logfd[0], dasynq::IN_EVENTS,
                            false /* not enabled */);
                }
                catch (...) {
                    log(loglevel_t::ERROR, get_name(), ": can't add output watch (insufficient resources)");
                    bp_sys::close(this->log_input_fd);
                    bp_sys::close(this->log_output_fd);
                    this->log_input_fd = -1;
                    this->log_output_fd = -1;
                    goto out_p;
                }
            }
        }
        // (More is done below, after we have performed additional setup)
    }

    {
        bool child_status_registered = false;
        control_conn_t *control_conn = nullptr;

        int control_socket[2] = {-1, -1};
        int notify_pipe[2] = {-1, -1};
        bool have_notify = !notification_var.empty() || force_notification_fd != -1;
        ready_notify_watcher * rwatcher = have_notify ? get_ready_watcher() : nullptr;
        bool ready_watcher_registered = false;

        if (onstart_flags.pass_cs_fd) {
            if (dinit_socketpair(AF_UNIX, SOCK_STREAM, /* protocol */ 0, control_socket, SOCK_NONBLOCK)) {
                log(loglevel_t::ERROR, get_name(), ": can't create control socket: ", strerror(errno));
                goto out_lfd;
            }

            // Make the server side socket close-on-exec:
            int fdflags = bp_sys::fcntl(control_socket[0], F_GETFD);
            bp_sys::fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);

            try {
                control_conn = new control_conn_t(event_loop, services, control_socket[0]);
            }
            catch (std::exception &exc) {
                log(loglevel_t::ERROR, get_name(), ": can't launch process; out of memory");
                goto out_cs;
            }
        }

        if (have_notify) {
            // Create a notification pipe:
            if (bp_sys::pipe2(notify_pipe, 0) != 0) {
                log(loglevel_t::ERROR, get_name(), ": can't create notification pipe: ", strerror(errno));
                goto out_cs_h;
            }

            // Set the read side as close-on-exec:
            int fdflags = bp_sys::fcntl(notify_pipe[0], F_GETFD);
            bp_sys::fcntl(notify_pipe[0], F_SETFD, fdflags | FD_CLOEXEC);

            // add, but don't yet enable, readiness watcher:
            try {
                rwatcher->add_watch(event_loop, notify_pipe[0], dasynq::IN_EVENTS, false);
                ready_watcher_registered = true;
            }
            catch (std::exception &exc) {
                log(loglevel_t::ERROR, get_name(), ": can't add notification watch: ", exc.what());
                goto out_cs_h;
            }
        }

        if (log_type == log_type_id::BUFFER) {
            // Set watcher enabled if space in buffer
            if (log_buf_size > 0) {
                // Append a "restarted" message to buffer contents
                const char *restarting_msg = "\n(dinit: note: service restarted)\n";
                unsigned restarting_msg_len = strlen(restarting_msg);
                bool trailing_nl = log_buffer[log_buf_size - 1] == '\n';
                if (trailing_nl) {
                    ++restarting_msg; // trim leading newline
                    --restarting_msg_len;
                }
                if (log_buf_size + restarting_msg_len >= log_buf_max) {
                    goto skip_enable_log_watch;
                }
                if (!ensure_log_buffer_backing(log_buf_size + restarting_msg_len)) {
                    goto skip_enable_log_watch;
                }
                memcpy(log_buffer.data() + log_buf_size, restarting_msg, restarting_msg_len);
                log_buf_size += restarting_msg_len;
            }
            log_output_listener.set_enabled(event_loop, true);
        }
        skip_enable_log_watch: ;

        // Set up complete, now fork and exec:

        pid_t forkpid;

        try {
            child_status_listener.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 = child_listener.fork(event_loop, reserved_child_watch, dasynq::DEFAULT_PRIORITY - 10);
            reserved_child_watch = true;
        }
        catch (std::exception &e) {
            log(loglevel_t::ERROR, get_name(), ": could not fork: ", e.what());
            goto out_cs_h;
        }

        if (forkpid == 0) {
            const char * working_dir_c = service_dsc_dir;
            if (!working_dir.empty()) working_dir_c = working_dir.c_str();
            after_fork(getpid());
            run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits};
            run_params.on_console = on_console;
            run_params.in_foreground = !onstart_flags.shares_console;
            run_params.unmask_sigint = onstart_flags.unmask_intr;
            run_params.csfd = control_socket[1];
            run_params.socket_fd = socket_fd;
            run_params.notify_fd = notify_pipe[1];
            run_params.force_notify_fd = force_notification_fd;
            run_params.notify_var = notification_var.c_str();
            run_params.env_file = env_file.c_str();
            run_params.output_fd = log_output_fd;
            run_params.input_fd = input_fd;
            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
            pid = forkpid;

            bp_sys::close(pipefd[1]); // close the 'other end' fd
            if (control_socket[1] != -1) bp_sys::close(control_socket[1]);
            if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
            notification_fd = notify_pipe[0];
            waiting_for_execstat = true;
            return true;
        }

        // Failure exit:

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

        if (notify_pipe[0] != -1) bp_sys::close(notify_pipe[0]);
        if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
        if (ready_watcher_registered) {
            rwatcher->deregister(event_loop);
        }

        if (onstart_flags.pass_cs_fd) {
            delete control_conn;

            out_cs:
            bp_sys::close(control_socket[0]);
            bp_sys::close(control_socket[1]);
        }
    }

    out_lfd:
    if (log_input_fd != -1) {
        log_output_listener.deregister(event_loop);
        bp_sys::close(log_input_fd);
        bp_sys::close(log_output_fd);
        log_input_fd = -1;
        log_output_fd = -1;
    }

    out_p:
    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 $0x138, %rsp            # imm = 0x138
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x438(%rdi), %rsi
pushq $0x1
popq %rdi
callq 0x8560
leaq 0x80(%rsp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0x83e0
testl %eax, %eax
je 0x220da
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
leaq 0x70(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xdd73(%rip), %rsi      # 0x2fe40
movq %rbx, %rdi
callq 0x2283a
jmp 0x22640
movq (%rbx), %rax
leaq 0x94(%rsp), %rsi
movq %rbx, %rdi
callq *0x80(%rax)
testb %al, %al
je 0x22640
movq 0x2a0(%rbx), %r14
cmpl $0x1, 0x298(%rbx)
ja 0x22117
cmpb $0x0, (%r14)
leaq 0xdb25(%rip), %rax      # 0x2fc36
cmoveq %rax, %r14
jmp 0x22124
cmpl $-0x1, 0x4ec(%rbx)
je 0x221cc
movq %r14, 0x88(%rsp)
orq $-0x1, 0x70(%rsp)
orq $-0x1, 0x78(%rsp)
cmpq $0x0, 0x4c0(%rbx)
setne %al
cmpl $-0x1, 0x4b0(%rbx)
setne %r14b
orb %al, %r14b
cmpb $0x1, %r14b
jne 0x22168
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x90(%rax)
movq %rax, %r15
jmp 0x2216b
xorl %r15d, %r15d
testb $0x40, 0x2e(%rbx)
jne 0x22179
xorl %r13d, %r13d
jmp 0x22262
pushq $0x1
popq %rdi
leaq 0x70(%rsp), %rcx
movl $0x801, %esi            # imm = 0x801
xorl %edx, %edx
callq 0x8520
testl %eax, %eax
je 0x2221a
leaq 0x8(%rbx), %r14
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r15
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x23001
leaq 0xdcf8(%rip), %rdi      # 0x2feb2
callq 0x28f17
movq %r15, %rdi
callq 0x28f31
jmp 0x225ee
leaq 0x70(%rsp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0x83e0
testl %eax, %eax
je 0x22654
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r14
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x23001
leaq 0xdc5b(%rip), %rdi      # 0x2fe63
callq 0x28f17
movq %r14, %rdi
callq 0x28f31
jmp 0x22628
movl 0x70(%rsp), %edi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x8380
movl 0x70(%rsp), %edi
orl $0x1, %eax
pushq $0x2
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x8380
movl $0x510, %edi            # imm = 0x510
callq 0x85f0
movq %rax, %r13
movq 0x148(%rbx), %rdx
movl 0x70(%rsp), %ecx
leaq 0x237d6(%rip), %rsi     # 0x45a30
movq %rax, %rdi
callq 0xb642
testb %r14b, %r14b
movq %r13, 0xa8(%rsp)
je 0x222f3
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0x83e0
testl %eax, %eax
je 0x222b5
leaq 0x8(%rbx), %r12
callq 0x8040
movl (%rax), %edi
callq 0x81c0
leaq 0xc0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xdc53(%rip), %rsi      # 0x2fef8
movq %r12, %rdi
callq 0x2283a
xorl %r14d, %r14d
jmp 0x2258d
movl 0x78(%rsp), %edi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x8380
movl 0x78(%rsp), %edi
orl $0x1, %eax
pushq $0x2
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x8380
movl 0x78(%rsp), %edx
leaq 0x2374f(%rip), %rsi     # 0x45a30
pushq $0x1
popq %rcx
pushq $0x32
popq %r9
movq %r15, %rdi
xorl %r8d, %r8d
callq 0xad4e
movq %r15, 0xa0(%rsp)
cmpl $0x2, 0x298(%rbx)
jne 0x2238d
movl 0x2d0(%rbx), %eax
testl %eax, %eax
je 0x22377
leal -0x1(%rax), %ecx
movq 0x2d8(%rbx), %rdx
xorl %r15d, %r15d
cmpb $0xa, (%rdx,%rcx)
leaq 0xdc13(%rip), %rcx      # 0x2ff3d
leaq 0xdc0b(%rip), %r13      # 0x2ff3c
cmoveq %rcx, %r13
setne %r15b
leal (%r15,%rax), %esi
addl $0x21, %esi
cmpl 0x2cc(%rbx), %esi
jae 0x2238d
movq %rbx, %rdi
callq 0x22866
testb %al, %al
je 0x2238d
addl $0x21, %r15d
movl 0x2d0(%rbx), %edi
addq 0x2d8(%rbx), %rdi
movl %r15d, %edx
movq %r13, %rsi
callq 0x84d0
addl %r15d, 0x2d0(%rbx)
leaq 0x408(%rbx), %rdi
leaq 0x236ab(%rip), %rsi     # 0x45a30
pushq $0x1
popq %rdx
callq 0xcf5c
leaq 0x380(%rbx), %r15
xorl %r13d, %r13d
movl 0x80(%rsp), %edx
leaq 0x2368b(%rip), %rsi     # 0x45a30
pushq $0x1
popq %rcx
pushq $0x32
popq %r9
movq %r15, %rdi
movl %ecx, %r8d
callq 0xad4e
leaq 0x338(%rbx), %rdi
movb 0x4f4(%rbx), %al
andb $0x4, %al
shrb $0x2, %al
movb $0x1, %r13b
movzbl %al, %edx
leaq 0x2365a(%rip), %rsi     # 0x45a30
pushq $0x28
popq %rcx
callq 0x2115c
orb $0x4, 0x4f4(%rbx)
testl %eax, %eax
je 0x2242f
movl %eax, 0x4d8(%rbx)
movl 0x84(%rsp), %edi
callq 0x8b50
movl 0x74(%rsp), %edi
cmpl $-0x1, %edi
je 0x22409
callq 0x8b50
movl 0x7c(%rsp), %edi
cmpl $-0x1, %edi
je 0x22417
callq 0x8b50
movl 0x78(%rsp), %eax
movl %eax, 0x4e8(%rbx)
orb $-0x80, 0x10c(%rbx)
movb $0x1, %al
jmp 0x22642
movq %r15, 0x98(%rsp)
leaq 0x258(%rbx), %rax
cmpq $0x0, 0x260(%rbx)
leaq 0x100(%rbx), %rcx
cmoveq %rcx, %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
callq 0x8920
movq (%rbx), %rcx
movq %rbx, %rdi
movl %eax, %esi
callq *0x60(%rcx)
movq (%r12), %rdi
movzwl 0x2e(%rbx), %eax
movl %eax, %edx
shrb $0x5, %dl
andb $0x1, %dl
testb $0x10, %al
movl 0x4e4(%rbx), %r8d
movl 0x4b0(%rbx), %r15d
movq 0x4b8(%rbx), %rax
movq %rax, 0xb0(%rsp)
movq 0x278(%rbx), %r9
movl 0x4ec(%rbx), %r10d
movb 0x2f0(%rbx), %r11b
movl 0x2f4(%rbx), %r12d
movq 0x318(%rbx), %r13
movb 0x2f8(%rbx), %al
movzwl 0x2fa(%rbx), %ecx
leaq 0xc8(%rsp), %rsi
movq %rdi, (%rsi)
movq 0xb8(%rsp), %rdi
movq %rdi, 0x8(%rsi)
movq 0x88(%rsp), %rdi
movq %rdi, 0x10(%rsi)
movq %r9, 0x18(%rsi)
movq %r13, 0x20(%rsi)
movb %bpl, 0x28(%rsi)
sete 0x29(%rsi)
movb %dl, 0x2a(%rsi)
movb %r11b, 0x2b(%rsi)
movl %r12d, 0x2c(%rsi)
movb %al, 0x30(%rsi)
movw %cx, 0x32(%rsi)
movl 0x84(%rsp), %eax
movl %eax, 0x34(%rsi)
movl 0x74(%rsp), %eax
movl %eax, 0x38(%rsi)
movl %r8d, 0x3c(%rsi)
movl 0x7c(%rsp), %eax
movl %eax, 0x40(%rsi)
movl %r15d, 0x44(%rsi)
movl %r10d, 0x48(%rsi)
movq 0xb0(%rsp), %rax
movq %rax, 0x50(%rsi)
movq 0x4a8(%rbx), %rax
movq %rax, 0x58(%rsi)
leaq 0x300(%rbx), %rax
movq %rax, 0x60(%rsi)
movl 0x94(%rsp), %eax
movl %eax, 0x68(%rsi)
pushq $0xe
popq %rcx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
callq 0x297ec
leaq 0x234b8(%rip), %rsi     # 0x45a30
movq 0x98(%rsp), %rdi
callq 0xd002
movq 0xa0(%rsp), %r15
movl 0x78(%rsp), %edi
cmpl $-0x1, %edi
je 0x2259b
callq 0x8b50
movl 0x7c(%rsp), %edi
cmpl $-0x1, %edi
je 0x225a9
callq 0x8b50
testb %r14b, %r14b
je 0x225bd
leaq 0x2347b(%rip), %rsi     # 0x45a30
movq %r15, %rdi
callq 0xd002
testb $0x40, 0x2e(%rbx)
je 0x225ee
cmpq $0x0, 0xa8(%rsp)
je 0x225dc
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl 0x70(%rsp), %edi
callq 0x8b50
movl 0x74(%rsp), %edi
callq 0x8b50
cmpl $-0x1, 0x4f0(%rbx)
je 0x22628
leaq 0x408(%rbx), %rdi
leaq 0x2342b(%rip), %rsi     # 0x45a30
callq 0xd002
movl 0x4f0(%rbx), %edi
callq 0x8b50
movl 0x4ec(%rbx), %edi
callq 0x8b50
orq $-0x1, 0x4ec(%rbx)
movl 0x80(%rsp), %edi
callq 0x8b50
movl 0x84(%rsp), %edi
callq 0x8b50
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x70(%rsp), %edx
movl %edx, 0x4f0(%rbx)
movl 0x74(%rsp), %eax
movl %eax, 0x4ec(%rbx)
cmpl $0x2, 0x298(%rbx)
jne 0x22124
leaq 0x408(%rbx), %rdi
leaq 0x233ad(%rip), %rsi     # 0x45a30
pushq $0x1
popq %rcx
pushq $0x32
popq %r9
xorl %r8d, %r8d
callq 0xad4e
jmp 0x22124
movq %rax, %rdi
callq 0x8200
leaq 0x8(%rbx), %rsi
pushq $0x3
popq %rdi
callq 0x23001
leaq 0xd7ce(%rip), %rdi      # 0x2fe80
callq 0x28f31
movl 0x4f0(%rbx), %edi
callq 0x8b50
movl 0x4ec(%rbx), %edi
callq 0x8b50
orq $-0x1, 0x4ec(%rbx)
callq 0x8a40
jmp 0x22628
movq %rax, %r12
cmpl $0x2, %edx
jne 0x227d5
movq %r12, %rdi
callq 0x8200
leaq 0x8(%rbx), %r12
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %r13
pushq $0x3
popq %rdi
movq %r12, %rsi
callq 0x23001
leaq 0xd806(%rip), %rdi      # 0x2ff1b
callq 0x28f17
movq %r13, %rdi
callq 0x28f31
callq 0x8a40
jmp 0x222ad
movq %rdx, %r14
movq %rax, %r12
movl $0x510, %esi            # imm = 0x510
movq %r13, %rdi
callq 0x8610
jmp 0x22747
movq %rdx, %r14
movq %rax, %r12
cmpl $0x2, %r14d
jne 0x227d5
movq %r12, %rdi
callq 0x8200
leaq 0x8(%rbx), %rsi
pushq $0x3
popq %rdi
callq 0x23001
leaq 0xd766(%rip), %rdi      # 0x2fed2
callq 0x28f31
callq 0x8a40
jmp 0x225dc
movq %r15, 0x98(%rsp)
movq %rax, %r12
cmpl $0x2, %edx
jne 0x227d5
movq %r12, %rdi
callq 0x8200
leaq 0x8(%rbx), %r12
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %rbp
pushq $0x3
popq %rdi
movq %r12, %rsi
callq 0x23001
leaq 0xd7aa(%rip), %rdi      # 0x2ff5f
callq 0x28f17
movq %rbp, %rdi
callq 0x28f31
callq 0x8a40
testb %r13b, %r13b
jne 0x22571
jmp 0x22585
movq %r12, %rdi
callq 0xaebb
movq %rax, %rdi
callq 0xaebb
nop