// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/shutdown.cc
// Function: do_system_shutdown(shutdown_type_t)
void do_system_shutdown(shutdown_type_t shutdown_type)
{
    using namespace std;
    
    // Mask all signals to prevent death of our parent etc from terminating us
    sigset_t allsigs;
    sigfillset(&allsigs);
    sigprocmask(SIG_SETMASK, &allsigs, nullptr);
    
    int reboot_type = RB_AUTOBOOT; // reboot
    const char *shutdown_type_arg = "reboot";
#if defined(RB_POWER_OFF)
    if (shutdown_type == shutdown_type_t::POWEROFF) {
        reboot_type = RB_POWER_OFF;
        shutdown_type_arg = "poweroff";
    }
#elif defined(RB_POWEROFF)
    // FreeBSD spells it slightly differently
    if (shutdown_type == shutdown_type_t::POWEROFF) {
        reboot_type = RB_POWEROFF;
        shutdown_type_arg = "poweroff";
    }
#elif defined(RB_POWERDOWN)
    // NetBSD (at least) uses RB_POWERDOWN rather than RB_POWER_OFF
    if (shutdown_type == shutdown_type_t::POWEROFF) {
        reboot_type = RB_POWERDOWN;
        shutdown_type_arg = "poweroff";
    }
#endif
#if defined(RB_HALT_SYSTEM)
    // Linux
    if (shutdown_type == shutdown_type_t::HALT) {
        reboot_type = RB_HALT_SYSTEM;
        shutdown_type_arg = "halt";
    }
#elif defined(RB_HALT)
    // NetBSD, FreeBSD
    if (shutdown_type == shutdown_type_t::HALT) {
        reboot_type = RB_HALT;
        shutdown_type_arg = "halt";
    }
#endif
#if defined(RB_KEXEC)
    if (shutdown_type == shutdown_type_t::KEXEC) reboot_type = RB_KEXEC;
#endif
    
    // Write to console rather than any terminal, since we lose the terminal it seems:
    int consfd = open("/dev/console", O_WRONLY);
    if (consfd != STDOUT_FILENO && consfd != -1) {
        dup2(consfd, STDOUT_FILENO);
    }
    
    loop_t loop;
    subproc_buffer sub_buf {loop, STDOUT_FILENO};

    sub_buf.append("Sending TERM/KILL to all processes...\n");
    
    // Send TERM/KILL to all (remaining) processes
    kill(-1, SIGTERM);

    // 1 second delay (while outputting from sub_buf):
    bool timeout_reached = false;
    dasynq::time_val timeout {1, 0};
    dasynq::time_val interval {0,0};
    loop_t::timer::add_timer(loop, clock_type::MONOTONIC, true /* relative */,
            timeout.get_timespec(), interval.get_timespec(),
            [&](loop_t &eloop, int expiry_count) -> rearm {

        timeout_reached = true;
        return rearm::REMOVE;
    });

    do {
      loop.run();
    } while (! timeout_reached);

    kill(-1, SIGKILL);

    // Attempt to execute shutdown hook at three possible locations:
    const char * const hook_paths[] = {
            "/etc/dinit/shutdown-hook",
            "/lib/dinit/shutdown-hook"
    };
    
    bool do_unmount_ourself = true;
    const int execmask = S_IXOTH | S_IXGRP | S_IXUSR;
    struct stat statbuf;

    for (size_t i = 0; i < sizeof(hook_paths) / sizeof(hook_paths[0]); ++i) {
        int stat_r = lstat(hook_paths[i], &statbuf);
        if (stat_r == 0 && (statbuf.st_mode & execmask) != 0) {
            sub_buf.append("Executing shutdown hook...\n");
            const char *prog_args[] = { hook_paths[i], shutdown_type_arg, nullptr };
            try {
                auto r = run_process(prog_args, loop, sub_buf);
                if (r.did_exit() && r.get_exit_status() == 0) {
                    do_unmount_ourself = false;
                }
            }
            catch (std::exception &e) {
                sub_buf.append("Couldn't fork for shutdown-hook: ");
                sub_buf.append(e.what());
                sub_buf.append("\n");
            }
            break;
        }
    }

    // perform shutdown
    if (do_unmount_ourself) {
        sub_buf.append("Turning off swap...\n");
        swap_off(loop, sub_buf);
        sub_buf.append("Unmounting disks...\n");
        unmount_disks(loop, sub_buf);
    }

    sync();
    
    sub_buf.append("Issuing shutdown via kernel...\n");
    loop.poll();  // give message a chance to get to console
#ifdef __NetBSD__
    reboot(reboot_type, NULL);
#else
    if (reboot(reboot_type)) {
        // we're in trouble now
        sub_buf.append("reboot: ");
        sub_buf.append(strerror(errno));
        if (shutdown_type == shutdown_type_t::KEXEC) {
            sub_buf.append(
                    "\nIt is possible that no suitable kernel image was loaded before"
                    "\nreboot with kexec was attempted.\n"
            );
        }
        while (true) loop.run();
    }
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x12f8, %rsp           # imm = 0x12F8
movl %edi, %ebx
leaq 0x250(%rsp), %r14
movq %r14, %rdi
callq 0x3080
pushq $0x2
popq %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3100
cmpb $0x3, %bl
leaq 0x4dbd(%rip), %rax      # 0x8759
leaq 0x4daf(%rip), %rcx      # 0x8752
cmoveq %rax, %rcx
movl $0x4321fedc, %eax       # imm = 0x4321FEDC
movl $0x1234567, %edx        # imm = 0x1234567
cmovel %eax, %edx
cmpb $0x2, %bl
leaq 0x4da4(%rip), %r12      # 0x8762
cmovneq %rcx, %r12
movl $0xcdef0123, %eax       # imm = 0xCDEF0123
cmovnel %edx, %eax
cmpb $0x6, %bl
movl $0x45584543, %ebp       # imm = 0x45584543
cmovnel %eax, %ebp
leaq 0x4d8b(%rip), %rdi      # 0x8767
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x31b0
cmpl $-0x1, %eax
je 0x39fa
cmpl $0x1, %eax
je 0x39fa
pushq $0x1
popq %rsi
movl %eax, %edi
callq 0x3250
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x42ce
leaq 0x2d0(%rsp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0x42e0
leaq 0x4d53(%rip), %rsi      # 0x8774
leaq 0x2d0(%rsp), %rdi
callq 0x431c
pushq $-0x1
popq %rdi
pushq $0xf
popq %rsi
callq 0x3360
movq $0x1, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movb $0x0, 0xb(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
pushq $0x58
popq %rdi
callq 0x3280
movq %rax, %r14
movl $0x4, 0x8(%rax)
leaq 0x910a(%rip), %rax      # 0xcb78
movq %rax, (%r14)
leaq 0xb(%rsp), %rax
movq %rax, 0x50(%r14)
leaq 0x58(%rsp), %rsi
pushq $0x1
popq %rdx
pushq $0x32
popq %rcx
movq %r14, %rdi
callq 0x6ad4
movl %ebp, 0xc(%rsp)
leaq 0x58(%rsp), %r15
leaq 0x10(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x6af0
pushq $-0x1
popq %r14
movq %r15, %rdi
movl %r14d, %esi
callq 0x4374
cmpb $0x1, 0xb(%rsp)
jne 0x3aaf
pushq $-0x1
popq %rdi
pushq $0x9
popq %rsi
callq 0x3360
xorl %r13d, %r13d
leaq 0x8fda(%rip), %rbp      # 0xcab0
leaq 0x1c0(%rsp), %r15
cmpq $0x10, %r13
je 0x3b49
movq (%r13,%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3420
testl %eax, %eax
jne 0x3b02
testb $0x49, 0x1d8(%rsp)
jne 0x3b08
addq $0x8, %r13
jmp 0x3ade
leaq 0x4cbe(%rip), %rsi      # 0x87cd
leaq 0x2d0(%rsp), %rdi
callq 0x431c
leaq 0x30(%rsp), %rdi
movq %r14, (%rdi)
movq %r12, 0x8(%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x58(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
callq 0x3e09
cmpq $0x1, %rax
je 0x3bc9
leaq 0x4cbb(%rip), %rsi      # 0x880b
leaq 0x2d0(%rsp), %rdi
callq 0x431c
movq 0x8ffc(%rip), %rax      # 0xcb60
leaq 0x30(%rsp), %rdi
movq %rax, 0x10(%rdi)
movaps 0x8fdc(%rip), %xmm0     # 0xcb50
movaps %xmm0, (%rdi)
leaq 0x58(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
callq 0x3e09
leaq 0x4c90(%rip), %rsi      # 0x8820
leaq 0x2d0(%rsp), %rdi
callq 0x431c
movaps 0x8f9c(%rip), %xmm0     # 0xcb40
leaq 0x30(%rsp), %rdi
movaps %xmm0, 0x10(%rdi)
movaps 0x8f7c(%rip), %xmm0     # 0xcb30
movaps %xmm0, (%rdi)
leaq 0x58(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
callq 0x3e09
callq 0x3260
leaq 0x4c60(%rip), %rsi      # 0x8835
leaq 0x2d0(%rsp), %rdi
callq 0x431c
leaq 0x58(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0x43e2
movl 0xc(%rsp), %edi
callq 0x3210
testl %eax, %eax
je 0x3c59
leaq 0x4c52(%rip), %rsi      # 0x8855
leaq 0x2d0(%rsp), %rdi
callq 0x431c
callq 0x3030
movl (%rax), %edi
callq 0x30a0
leaq 0x2d0(%rsp), %rdi
movq %rax, %rsi
callq 0x431c
cmpb $0x6, %bl
jne 0x3c45
leaq 0x4c26(%rip), %rsi      # 0x885e
leaq 0x2d0(%rsp), %rdi
callq 0x431c
leaq 0x58(%rsp), %rbx
pushq $-0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4374
jmp 0x3c4d
leaq 0x2d0(%rsp), %rdi
callq 0x442a
leaq 0x58(%rsp), %rdi
callq 0x54ba
addq $0x12f8, %rsp           # imm = 0x12F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
cmpl $0x1, %edx
jne 0x3dea
movq %r14, %rdi
callq 0x30d0
movq %rax, %r14
leaq 0x4b49(%rip), %rsi      # 0x87e9
leaq 0x2d0(%rsp), %rdi
callq 0x431c
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2d0(%rsp), %rdi
movq %rax, %rsi
callq 0x431c
leaq 0x4b1a(%rip), %rsi      # 0x87e7
leaq 0x2d0(%rsp), %rdi
callq 0x431c
callq 0x33d0
jmp 0x3b49
jmp 0x3de7
movq %rax, %r14
callq 0x33d0
jmp 0x3dea
jmp 0x3dd6
jmp 0x3de7
movq %rax, %r14
cmpl $0x1, %edx
jne 0x3dea
movq %r14, %rdi
callq 0x30d0
movq %rax, %r14
leaq 0x4c4c(%rip), %rsi      # 0x896a
leaq 0x2d0(%rsp), %rdi
callq 0x431c
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2d0(%rsp), %rdi
movq %rax, %rsi
callq 0x431c
leaq 0x4a9c(%rip), %rsi      # 0x87e7
leaq 0x2d0(%rsp), %rdi
callq 0x431c
callq 0x33d0
jmp 0x3bc9
movq %rax, %r14
callq 0x33d0
jmp 0x3dea
jmp 0x3dd6
movq %rax, %r14
cmpl $0x1, %edx
jne 0x3dea
movq %r14, %rdi
callq 0x30d0
movq %rax, %r14
leaq 0x4c0b(%rip), %rsi      # 0x8993
leaq 0x2d0(%rsp), %rdi
callq 0x431c
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2d0(%rsp), %rdi
movq %rax, %rsi
callq 0x431c
leaq 0x4a32(%rip), %rsi      # 0x87e7
leaq 0x2d0(%rsp), %rdi
callq 0x431c
callq 0x33d0
jmp 0x3b89
movq %rax, %r14
callq 0x33d0
jmp 0x3dea
movq %rax, %rdi
callq 0x42c3
jmp 0x3de7
movq %rax, %r14
jmp 0x3df7
jmp 0x3de7
movq %rax, %r14
leaq 0x2d0(%rsp), %rdi
callq 0x442a
leaq 0x58(%rsp), %rdi
callq 0x54ba
movq %r14, %rdi
callq 0x3400