// File: /workspace/github_cpp_makefile_star2_O3/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 $0x12d8, %rsp           # imm = 0x12D8
movl %edi, %ebx
leaq 0x230(%rsp), %r14
movq %r14, %rdi
callq 0x3090
movl $0x2, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3100
cmpb $0x3, %bl
leaq 0x4acc(%rip), %rax      # 0x8759
leaq 0x4abe(%rip), %rcx      # 0x8752
cmoveq %rax, %rcx
movl $0x4321fedc, %eax       # imm = 0x4321FEDC
movl $0x1234567, %edx        # imm = 0x1234567
cmovel %eax, %edx
cmpb $0x2, %bl
leaq 0x4ab3(%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 0x4a9a(%rip), %rdi      # 0x8767
movl $0x1, %esi
xorl %eax, %eax
callq 0x31c0
cmpl $-0x1, %eax
je 0x3cef
cmpl $0x1, %eax
je 0x3cef
movl %eax, %edi
movl $0x1, %esi
callq 0x3260
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x6666
movb $0x0, 0x160(%r14)
movaps 0x4305(%rip), %xmm0     # 0x8010
leaq 0x2b0(%rsp), %rcx
movups %xmm0, 0x1000(%rcx)
movq $0x0, 0x1010(%rcx)
movq %r14, 0x1018(%rcx)
movq %r14, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x49d4
leaq 0x2b0(%rsp), %rdi
movq %rax, 0x1020(%rdi)
leaq 0x4a20(%rip), %rsi      # 0x8774
callq 0x47f2
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xf, %esi
callq 0x3390
movb $0x0, 0xb(%rsp)
movl $0x58, %edi
callq 0x32a0
movq %rax, %r14
movl $0x4, 0x8(%rax)
leaq 0x7df0(%rip), %rax      # 0xbb78
movq %rax, (%r14)
leaq 0xb(%rsp), %rax
movq %rax, 0x50(%r14)
movb $0x0, 0xc(%r14)
movl $0x32, 0x18(%r14)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x48(%r14)
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x6f98
movl %ebp, 0xc(%rsp)
movl 0x4c(%r14), %r9d
addq $0x20, %r14
leaq 0x1a0(%rsp), %rdx
movq $0x1, (%rdx)
movq $0x0, 0x8(%rdx)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %r8d
callq 0x713c
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x48b0
cmpb $0x1, 0xb(%rsp)
jne 0x3e05
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x9, %esi
callq 0x3390
movb $0x1, %r13b
xorl %eax, %eax
leaq 0x7c7c(%rip), %rbp      # 0xbab0
leaq 0x1a0(%rsp), %r14
movq (%rbp,%rax,8), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3460
testl %eax, %eax
jne 0x3e5e
testb $0x49, 0x1b8(%rsp)
jne 0x3fb5
movl $0x1, %eax
testb $0x1, %r13b
movl $0x0, %r13d
jne 0x3e3c
leaq 0x4995(%rip), %rsi      # 0x880b
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
movq 0x7cd6(%rip), %rax      # 0xbb60
leaq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movaps 0x7cb6(%rip), %xmm0     # 0xbb50
movaps %xmm0, (%rdi)
leaq 0x38(%rsp), %rsi
leaq 0x2b0(%rsp), %rdx
callq 0x4190
leaq 0x496a(%rip), %rsi      # 0x8820
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
movaps 0x7c76(%rip), %xmm0     # 0xbb40
leaq 0x10(%rsp), %rdi
movaps %xmm0, 0x10(%rdi)
movaps 0x7c56(%rip), %xmm0     # 0xbb30
movaps %xmm0, (%rdi)
leaq 0x38(%rsp), %rsi
leaq 0x2b0(%rsp), %rdx
callq 0x4190
callq 0x3270
leaq 0x493a(%rip), %rsi      # 0x8835
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
leaq 0x38(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4938
movl 0xc(%rsp), %edi
callq 0x3220
testl %eax, %eax
je 0x3f81
leaq 0x492a(%rip), %rsi      # 0x8855
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
callq 0x3040
movl (%rax), %edi
callq 0x30b0
leaq 0x2b0(%rsp), %rdi
movq %rax, %rsi
callq 0x47f2
cmpb $0x6, %bl
jne 0x3f6d
leaq 0x48fe(%rip), %rsi      # 0x885e
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x48b0
jmp 0x3f72
movq 0x12c8(%rsp), %rdi
movq 0x12d0(%rsp), %rsi
movl 0x1c(%rsi), %edx
callq 0x56cc
leaq 0x38(%rsp), %rdi
callq 0x5808
addq $0x12d8, %rsp           # imm = 0x12D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4811(%rip), %rsi      # 0x87cd
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq %r12, 0x8(%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x38(%rsp), %rsi
leaq 0x2b0(%rsp), %rdx
callq 0x4190
cmpq $0x1, %rax
jne 0x3e6f
jmp 0x3eef
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4166
movq %r14, %rdi
callq 0x30e0
movq %rax, %r14
leaq 0x47cd(%rip), %rsi      # 0x87e9
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2b0(%rsp), %rdi
movq %rax, %rsi
callq 0x47f2
leaq 0x479e(%rip), %rsi      # 0x87e7
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
callq 0x3400
jmp 0x3e6f
jmp 0x4163
movq %rax, %r14
callq 0x3400
jmp 0x4166
jmp 0x4152
jmp 0x4163
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4166
movq %r14, %rdi
callq 0x30e0
movq %rax, %r14
leaq 0x48d0(%rip), %rsi      # 0x896a
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2b0(%rsp), %rdi
movq %rax, %rsi
callq 0x47f2
leaq 0x4720(%rip), %rsi      # 0x87e7
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
callq 0x3400
jmp 0x3eef
movq %rax, %r14
callq 0x3400
jmp 0x4166
jmp 0x4152
movq %rax, %r14
cmpl $0x1, %edx
jne 0x4166
movq %r14, %rdi
callq 0x30e0
movq %rax, %r14
leaq 0x488f(%rip), %rsi      # 0x8993
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x2b0(%rsp), %rdi
movq %rax, %rsi
callq 0x47f2
leaq 0x46b6(%rip), %rsi      # 0x87e7
leaq 0x2b0(%rsp), %rdi
callq 0x47f2
callq 0x3400
jmp 0x3eaf
movq %rax, %r14
callq 0x3400
jmp 0x4166
movq %rax, %rdi
callq 0x47e6
jmp 0x4163
movq %rax, %r14
jmp 0x417e
jmp 0x4163
movq %rax, %r14
movq 0x12c8(%rsp), %rdi
movq 0x12d0(%rsp), %rsi
movl 0x1c(%rsi), %edx
callq 0x56cc
leaq 0x38(%rsp), %rdi
callq 0x5808
movq %r14, %rdi
callq 0x3440