// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0x1, %edi
callq 0x8560
leaq 0x84(%rsp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0x83e0
testl %eax, %eax
je 0x269ea
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf461(%rip), %rdi      # 0x35e39
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
jmp 0x26fc2
movq (%rbx), %rax
leaq 0x8c(%rsp), %rsi
movq %rbx, %rdi
callq *0x80(%rax)
testb %al, %al
je 0x26fc2
movq 0x2a0(%rbx), %r14
cmpl $0x1, 0x298(%rbx)
ja 0x26a27
cmpb $0x0, (%r14)
leaq 0xf22d(%rip), %rax      # 0x35c4e
cmoveq %rax, %r14
jmp 0x26a34
cmpl $-0x1, 0x4ec(%rbx)
je 0x26ae2
movq %r14, 0xb8(%rsp)
movq $-0x1, %rax
movq %rax, 0x70(%rsp)
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x4c0(%rbx)
setne %al
cmpl $-0x1, 0x4b0(%rbx)
setne %r15b
orb %al, %r15b
cmpb $0x1, %r15b
jne 0x26a7d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x90(%rax)
movq %rax, %r14
jmp 0x26a80
xorl %r14d, %r14d
testb $0x40, 0x2e(%rbx)
jne 0x26a8e
xorl %r13d, %r13d
jmp 0x26b7b
leaq 0x70(%rsp), %rcx
movl $0x1, %edi
movl $0x801, %esi            # imm = 0x801
xorl %edx, %edx
callq 0x8520
testl %eax, %eax
je 0x26b2f
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf3db(%rip), %rdi      # 0x35eab
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
jmp 0x26f67
leaq 0x70(%rsp), %rdi
movl $0x80000, %esi          # imm = 0x80000
callq 0x83e0
testl %eax, %eax
je 0x26fd6
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf33f(%rip), %rdi      # 0x35e5c
callq 0x2e520
movq %r14, %rdi
callq 0x2e53c
jmp 0x26faa
movl 0x70(%rsp), %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x8390
movl 0x70(%rsp), %edi
orl $0x1, %eax
movl $0x2, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x8390
movl $0x510, %edi            # imm = 0x510
callq 0x8600
movq %rax, %r13
movq 0x148(%rbx), %rdx
movl 0x70(%rsp), %ecx
leaq 0x1febd(%rip), %rsi     # 0x46a30
movq %rax, %rdi
callq 0xbe34
testb %r15b, %r15b
movq %r13, 0xa0(%rsp)
je 0x26c34
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0x83e0
testl %eax, %eax
je 0x26bd5
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r12
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf331(%rip), %rdi      # 0x35ef1
callq 0x2e520
movq %r12, %rdi
callq 0x2e53c
xorl %r15d, %r15d
jmp 0x26f02
movl 0x78(%rsp), %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x8390
movl 0x78(%rsp), %edi
orl $0x1, %eax
movl $0x2, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x8390
movl 0x78(%rsp), %edx
andb $-0x20, 0xc(%r14)
movl $0x32, 0x18(%r14)
movl %edx, 0x1c(%r14)
movl $0x1, 0x20(%r14)
leaq 0x1fe12(%rip), %rdi     # 0x46a30
movq %r14, %rsi
movl $0x1, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xe198
movq %r14, 0x98(%rsp)
cmpl $0x2, 0x298(%rbx)
jne 0x26cd0
movl 0x2d0(%rbx), %eax
testl %eax, %eax
je 0x26cb8
leal -0x1(%rax), %ecx
movq 0x2d8(%rbx), %rdx
xorl %r14d, %r14d
cmpb $0xa, (%rdx,%rcx)
leaq 0xf2cb(%rip), %rcx      # 0x35f36
leaq 0xf2c3(%rip), %r13      # 0x35f35
cmoveq %rcx, %r13
setne %r14b
leal (%r14,%rax), %esi
addl $0x21, %esi
cmpl 0x2cc(%rbx), %esi
jae 0x26cd0
movq %rbx, %rdi
callq 0x271e6
testb %al, %al
je 0x26cd0
addl $0x21, %r14d
movl 0x2d0(%rbx), %edi
addq 0x2d8(%rbx), %rdi
movl %r14d, %edx
movq %r13, %rsi
callq 0x84d0
addl %r14d, 0x2d0(%rbx)
leaq 0x408(%rbx), %rdi
leaq 0x1fd6a(%rip), %rsi     # 0x46a30
movl $0x1, %edx
callq 0xd486
leaq 0x380(%rbx), %r14
movl 0x84(%rsp), %edx
andb $-0x20, 0x38c(%rbx)
movl $0x32, 0x398(%rbx)
movl %edx, 0x39c(%rbx)
movl $0x1, 0x3a0(%rbx)
xorl %r13d, %r13d
leaq 0x1fd27(%rip), %rdi     # 0x46a30
movq %r14, %rsi
movl $0x1, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xe198
leaq 0x338(%rbx), %rdi
movb 0x4f4(%rbx), %al
andb $0x4, %al
shrb $0x2, %al
movb $0x1, %r13b
movzbl %al, %edx
leaq 0x1fcef(%rip), %rsi     # 0x46a30
movl $0x28, %ecx
callq 0x258d4
orb $0x4, 0x4f4(%rbx)
testl %eax, %eax
je 0x26d9c
movl %eax, 0x4d8(%rbx)
movl 0x88(%rsp), %edi
callq 0x8b60
movl 0x74(%rsp), %edi
cmpl $-0x1, %edi
je 0x26d76
callq 0x8b60
movl 0x7c(%rsp), %edi
cmpl $-0x1, %edi
je 0x26d84
callq 0x8b60
movl 0x78(%rsp), %eax
movl %eax, 0x4e8(%rbx)
orb $-0x80, 0x10c(%rbx)
movb $0x1, %al
jmp 0x26fc4
movq %r14, 0x90(%rsp)
leaq 0x258(%rbx), %rax
cmpq $0x0, 0x260(%rbx)
leaq 0x100(%rbx), %rcx
cmoveq %rcx, %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
callq 0x8930
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), %r14d
movq 0x4b8(%rbx), %rax
movq %rax, 0xa8(%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 0xb0(%rsp), %rdi
movq %rdi, 0x8(%rsi)
movq 0xb8(%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 0x88(%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 %r14d, 0x44(%rsi)
movl %r10d, 0x48(%rsi)
movq 0xa8(%rsp), %rax
movq %rax, 0x50(%rsi)
movq 0x4a8(%rbx), %rax
movq %rax, 0x58(%rsi)
leaq 0x300(%rbx), %rax
movq %rax, 0x60(%rsi)
movl 0x8c(%rsp), %eax
movl %eax, 0x68(%rsi)
movl $0xe, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
callq 0x2eebc
movl 0x39c(%rbx), %edx
leaq 0x1fb43(%rip), %rdi     # 0x46a30
movq 0x90(%rsp), %rsi
callq 0xd520
movq 0x98(%rsp), %r14
movl 0x78(%rsp), %edi
cmpl $-0x1, %edi
je 0x26f10
callq 0x8b60
movl 0x7c(%rsp), %edi
cmpl $-0x1, %edi
je 0x26f1e
callq 0x8b60
testb %r15b, %r15b
je 0x26f36
movl 0x1c(%r14), %edx
leaq 0x1fb02(%rip), %rdi     # 0x46a30
movq %r14, %rsi
callq 0xd520
testb $0x40, 0x2e(%rbx)
je 0x26f67
cmpq $0x0, 0xa0(%rsp)
je 0x26f55
movq 0xa0(%rsp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl 0x70(%rsp), %edi
callq 0x8b60
movl 0x74(%rsp), %edi
callq 0x8b60
cmpl $-0x1, 0x4f0(%rbx)
je 0x26faa
leaq 0x408(%rbx), %rsi
movl 0x424(%rbx), %edx
leaq 0x1faac(%rip), %rdi     # 0x46a30
callq 0xd520
movl 0x4f0(%rbx), %edi
callq 0x8b60
movl 0x4ec(%rbx), %edi
callq 0x8b60
movq $-0x1, 0x4ec(%rbx)
movl 0x84(%rsp), %edi
callq 0x8b60
movl 0x88(%rsp), %edi
callq 0x8b60
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 0x26a34
andb $-0x20, 0x414(%rbx)
leaq 0x408(%rbx), %rsi
movl $0x32, 0x420(%rbx)
movl %edx, 0x424(%rbx)
movl $0x1, 0x428(%rbx)
leaq 0x1fa0a(%rip), %rdi     # 0x46a30
movl $0x1, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xe198
jmp 0x26a34
movq %rax, %rdi
callq 0x8210
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xee1e(%rip), %rdi      # 0x35e79
callq 0x2e53c
movl 0x4f0(%rbx), %edi
callq 0x8b60
movl 0x4ec(%rbx), %edi
callq 0x8b60
movq $-0x1, 0x4ec(%rbx)
callq 0x8a50
jmp 0x26faa
movq %rax, %r12
cmpl $0x2, %edx
jne 0x2715b
movq %r12, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0xc0(%rsp), %rsi
movq %rax, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x271c0
callq 0x8a50
jmp 0x26bcd
movq %rdx, %r14
movq %rax, %r12
movl $0x510, %esi            # imm = 0x510
movq %r13, %rdi
callq 0x8620
jmp 0x270e1
movq %rdx, %r14
movq %rax, %r12
cmpl $0x2, %r14d
jne 0x2715b
movq %r12, %rdi
callq 0x8210
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xedc7(%rip), %rdi      # 0x35ecb
callq 0x2e53c
callq 0x8a50
jmp 0x26f55
movq %r14, 0x90(%rsp)
movq %rax, %r12
cmpl $0x2, %edx
jne 0x2715b
movq %r12, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0xc0(%rsp), %rsi
movq %rax, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x272a0
callq 0x8a50
testb %r13b, %r13b
jne 0x26ee0
jmp 0x26efa
movq %r12, %rdi
callq 0xb74e
movq %rax, %rdi
callq 0xb74e
nop