// File: /workspace/github_cpp_makefile_star2_O1/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 $0x12e8, %rsp           # imm = 0x12E8
movl %edi, %ebp
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
callq 0x3090
movl $0x2, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3100
cmpb $0x3, %bpl
leaq 0x5a2f(%rip), %rax      # 0x9775
leaq 0x5a21(%rip), %rcx      # 0x976e
cmoveq %rax, %rcx
movl $0x4321fedc, %eax       # imm = 0x4321FEDC
movl $0x1234567, %edx        # imm = 0x1234567
cmovel %eax, %edx
cmpb $0x2, %bpl
leaq 0x5a15(%rip), %rax      # 0x977e
cmovneq %rcx, %rax
movq %rax, 0x18(%rsp)
movl $0xcdef0123, %eax       # imm = 0xCDEF0123
cmovnel %edx, %eax
movl %ebp, 0x14(%rsp)
cmpb $0x6, %bpl
movl $0x45584543, %ebp       # imm = 0x45584543
cmovnel %eax, %ebp
leaq 0x59f2(%rip), %rdi      # 0x9783
movl $0x1, %esi
xorl %eax, %eax
callq 0x31c0
cmpl $-0x1, %eax
je 0x3db3
cmpl $0x1, %eax
je 0x3db3
movl %eax, %edi
movl $0x1, %esi
callq 0x3260
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0x686a
movb $0x0, 0x160(%rbx)
movaps 0x5242(%rip), %xmm0     # 0x9010
leaq 0x2c0(%rsp), %rcx
movups %xmm0, 0x1000(%rcx)
movq $0x0, 0x1010(%rcx)
movq %rbx, 0x1018(%rcx)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x4af2
leaq 0x2c0(%rsp), %rdi
movq %rax, 0x1020(%rdi)
leaq 0x5979(%rip), %rsi      # 0x9790
callq 0x4910
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xf, %esi
callq 0x3390
movb $0x0, 0xf(%rsp)
movl $0x58, %edi
callq 0x32a0
movq %rax, %r14
movl $0x4, 0x8(%rax)
leaq 0x9cbd(%rip), %rax      # 0xdb08
movq %rax, (%r14)
leaq 0xf(%rsp), %rax
movq %rax, 0x50(%r14)
andb $-0x20, 0xc(%r14)
movl $0x32, 0x18(%r14)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x48(%r14)
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x7200
movl %ebp, 0x10(%rsp)
movl 0x4c(%r14), %r9d
addq $0x20, %r14
leaq 0x1b0(%rsp), %rdx
movq $0x1, (%rdx)
movq $0x0, 0x8(%rdx)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %r8d
callq 0x73a4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x49ce
cmpb $0x1, 0xf(%rsp)
jne 0x3ec8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x9, %esi
callq 0x3390
movb $0x1, %r13b
leaq 0x9b4b(%rip), %rbp      # 0xda40
leaq 0x1b0(%rsp), %r14
leaq 0x2c0(%rsp), %r12
xorl %r15d, %r15d
movq (%r15,%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3460
testl %eax, %eax
je 0x3f1f
movb $0x1, %al
jmp 0x3f75
testb $0x49, 0x1c8(%rsp)
je 0x3f1b
movq %r12, %rdi
leaq 0x58b6(%rip), %rsi      # 0x97e9
callq 0x4910
movq %rbx, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %r12, %rdx
callq 0x4263
cmpq $0x1, %rax
movzbl %r13b, %r13d
movl $0x0, %eax
cmovel %eax, %r13d
xorl %eax, %eax
testb %al, %al
je 0x3fd7
leaq 0x8(%r15), %rax
testq %r15, %r15
movq %rax, %r15
je 0x3f08
jmp 0x3fd7
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x4239
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
movq %r12, %rdi
leaq 0x585d(%rip), %rsi      # 0x9805
callq 0x4910
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x4910
movq %r12, %rdi
leaq 0x5838(%rip), %rsi      # 0x9803
callq 0x4910
callq 0x3400
jmp 0x3f73
testb $0x1, %r13b
movl 0x10(%rsp), %ebx
je 0x4065
leaq 0x583b(%rip), %rsi      # 0x9827
leaq 0x2c0(%rsp), %rdi
callq 0x4910
movq 0x9af0(%rip), %rax      # 0xdaf0
leaq 0x20(%rsp), %rdi
movq %rax, 0x10(%rdi)
movaps 0x9ad0(%rip), %xmm0     # 0xdae0
movaps %xmm0, (%rdi)
leaq 0x48(%rsp), %rsi
leaq 0x2c0(%rsp), %rdx
callq 0x4263
leaq 0x5810(%rip), %rsi      # 0x983c
leaq 0x2c0(%rsp), %rdi
callq 0x4910
movaps 0x9a90(%rip), %xmm0     # 0xdad0
leaq 0x20(%rsp), %rdi
movaps %xmm0, 0x10(%rdi)
movaps 0x9a70(%rip), %xmm0     # 0xdac0
movaps %xmm0, (%rdi)
leaq 0x48(%rsp), %rsi
leaq 0x2c0(%rsp), %rdx
callq 0x4263
callq 0x3270
leaq 0x57e0(%rip), %rsi      # 0x9851
leaq 0x2c0(%rsp), %rdi
callq 0x4910
leaq 0x48(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4a56
movl %ebx, %edi
callq 0x3220
testl %eax, %eax
je 0x40f7
leaq 0x57d2(%rip), %rsi      # 0x9871
leaq 0x2c0(%rsp), %rdi
callq 0x4910
callq 0x3040
movl (%rax), %edi
callq 0x30b0
leaq 0x2c0(%rsp), %rdi
movq %rax, %rsi
callq 0x4910
cmpb $0x6, 0x14(%rsp)
jne 0x40e3
leaq 0x57a4(%rip), %rsi      # 0x987a
leaq 0x2c0(%rsp), %rdi
callq 0x4910
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x49ce
jmp 0x40e8
movq 0x12d8(%rsp), %rdi
movq 0x12e0(%rsp), %rsi
movl 0x1c(%rsi), %edx
callq 0x5848
leaq 0x48(%rsp), %rdi
callq 0x5994
addq $0x12e8, %rsp           # imm = 0x12E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4236
movq %rax, %rbx
callq 0x3400
jmp 0x4239
movq %rax, %rdi
callq 0x4904
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x4239
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
leaq 0x5823(%rip), %rsi      # 0x9986
leaq 0x2c0(%rsp), %rdi
callq 0x4910
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
leaq 0x2c0(%rsp), %rdi
movq %rax, %rsi
callq 0x4910
leaq 0x5673(%rip), %rsi      # 0x9803
leaq 0x2c0(%rsp), %rdi
callq 0x4910
callq 0x3400
movl 0x10(%rsp), %ebx
jmp 0x4065
movq %rax, %rbx
callq 0x3400
jmp 0x4239
jmp 0x413d
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x4239
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
leaq 0x57db(%rip), %rsi      # 0x99af
leaq 0x2c0(%rsp), %rdi
callq 0x4910
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
leaq 0x2c0(%rsp), %rdi
movq %rax, %rsi
callq 0x4910
leaq 0x5602(%rip), %rsi      # 0x9803
leaq 0x2c0(%rsp), %rdi
callq 0x4910
callq 0x3400
movl 0x10(%rsp), %ebx
jmp 0x4025
movq %rax, %rbx
callq 0x3400
jmp 0x4239
jmp 0x413d
jmp 0x4236
movq %rax, %rbx
jmp 0x4251
jmp 0x4236
jmp 0x4236
movq %rax, %rbx
movq 0x12d8(%rsp), %rdi
movq 0x12e0(%rsp), %rsi
movl 0x1c(%rsi), %edx
callq 0x5848
leaq 0x48(%rsp), %rdi
callq 0x5994
movq %rbx, %rdi
callq 0x3440