// File: merelin[P]mediasmartserverd/src/update_monitor.cpp
// Function: UpdateMonitor::MonitorThreadProc(void*)
void* UpdateMonitor::MonitorThreadProc(void* /*arg*/)
{
	pthread_cleanup_push(MonitorThreadCleanupHandler, NULL);
	while(true)
	{
		if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
		{
			throw ErrnoException("pthread_setcancelstate");
		}
		int update_count = -1, security_update_count = -1;
		if(!GetUpdateStatus(&update_count, &security_update_count))
		{
			break;
		}
		bool reboot_required = IsRebootRequired();
		if (verbose > 1)
		{
			std::cout << "--- Update Monitor ---\n";
			std::cout << "  Updates          : " << update_count << "\n"; 
			std::cout << "  Security Updates : " << security_update_count << "\n";
			std::cout << "  Reboot Required  : " << (reboot_required ? "YES" : "NO") << "\n";
		}
		if (reboot_required)
		{
			//Red
			leds_->SetSystemLed(LED_RED, true);
			leds_->SetSystemLed(LED_BLUE, false);
		}
		else if (security_update_count > 0)
		{
			//Purple
			leds_->SetSystemLed(LED_BLUE | LED_RED, true);
		}
		else if (update_count > 0)
		{
			//Blue
			leds_->SetSystemLed(LED_BLUE, true);
			leds_->SetSystemLed(LED_RED, false);
		}
		else //Nothing
		{
			//Off
			leds_->SetSystemLed(LED_BLUE | LED_RED, false);
		}
		if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
		{
			throw ErrnoException("pthread_setcancelstate");
		}
		sleep(900); //Sleep 15 minutes. //A cancellation point.
	}
	std::cout << "Update monitor thread stopped from within monitoring loop.\n";
	pthread_cleanup_pop(1); //Remove handler and execute it.
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
leaq -0x20(%rbp), %rdi
leaq 0x4f6(%rip), %rsi       # 0x8e90
xorl %eax, %eax
movl %eax, %edx
callq 0x9260
jmp 0x89a5
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x55d0
movl %eax, -0x88(%rbp)
jmp 0x89bb
movl -0x88(%rbp), %eax
cmpl $0x0, %eax
je 0x8a9a
movb $0x1, -0x52(%rbp)
movl $0x10, %edi
callq 0x51b0
movq %rax, -0x98(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x56c0
movq -0x90(%rbp), %rdx
leaq 0x9a5f(%rip), %rsi      # 0x1245c
leaq -0x50(%rbp), %rdi
callq 0x7600
jmp 0x8a08
callq 0x5030
movq -0x98(%rbp), %rdi
movl (%rax), %edx
leaq -0x50(%rbp), %rsi
callq 0x76b0
jmp 0x8a21
movq -0x98(%rbp), %rdi
movb $0x0, -0x52(%rbp)
leaq 0xebcd(%rip), %rsi      # 0x17600
leaq -0x12ba(%rip), %rdx     # 0x7780
callq 0x5680
jmp 0x8e82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x8e70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x8a78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x5230
leaq -0x51(%rbp), %rdi
callq 0x53f0
testb $0x1, -0x52(%rbp)
jne 0x8a89
jmp 0x8a95
movq -0x98(%rbp), %rdi
callq 0x52a0
jmp 0x8e70
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
leaq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x8ef0
movb %al, -0x99(%rbp)
jmp 0x8abd
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x8ace
jmp 0x8ac9
jmp 0x8e37
callq 0x91d0
movb %al, -0x9a(%rbp)
jmp 0x8adb
movb -0x9a(%rbp), %al
andb $0x1, %al
movb %al, -0x5d(%rbp)
leaq 0xf92f(%rip), %rax      # 0x1841c
cmpl $0x1, (%rax)
jle 0x8bfd
movq 0xf4b3(%rip), %rdi      # 0x17fb0
leaq 0x996f(%rip), %rsi      # 0x12473
callq 0x5380
jmp 0x8b0b
movq 0xf49e(%rip), %rdi      # 0x17fb0
leaq 0x9972(%rip), %rsi      # 0x1248b
callq 0x5380
movq %rax, -0xa8(%rbp)
jmp 0x8b27
movq -0xa8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x5690
movq %rax, -0xb0(%rbp)
jmp 0x8b3f
movq -0xb0(%rbp), %rdi
leaq 0x9c82(%rip), %rsi      # 0x127cf
callq 0x5380
jmp 0x8b54
movq 0xf455(%rip), %rdi      # 0x17fb0
leaq 0x993f(%rip), %rsi      # 0x124a1
callq 0x5380
movq %rax, -0xb8(%rbp)
jmp 0x8b70
movq -0xb8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x5690
movq %rax, -0xc0(%rbp)
jmp 0x8b88
movq -0xc0(%rbp), %rdi
leaq 0x9c39(%rip), %rsi      # 0x127cf
callq 0x5380
jmp 0x8b9d
movq 0xf40c(%rip), %rdi      # 0x17fb0
leaq 0x990c(%rip), %rsi      # 0x124b7
callq 0x5380
movq %rax, -0xc8(%rbp)
jmp 0x8bb9
movq -0xc8(%rbp), %rdi
movb -0x5d(%rbp), %cl
leaq 0x9903(%rip), %rax      # 0x124cd
leaq 0x9900(%rip), %rsi      # 0x124d1
testb $0x1, %cl
cmovneq %rax, %rsi
callq 0x5380
movq %rax, -0xd0(%rbp)
jmp 0x8be6
movq -0xd0(%rbp), %rdi
leaq 0x9bdb(%rip), %rsi      # 0x127cf
callq 0x5380
jmp 0x8bfb
jmp 0x8bfd
testb $0x1, -0x5d(%rbp)
je 0x8c67
leaq 0xf7f6(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0xd8(%rbp)
jmp 0x8c18
movq -0xd8(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x2, %esi
movl $0x1, %edx
callq *%rax
jmp 0x8c34
leaq 0xf7c5(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0xe0(%rbp)
jmp 0x8c49
movq -0xe0(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x1, %esi
xorl %edx, %edx
callq *%rax
jmp 0x8c62
jmp 0x8d3b
cmpl $0x0, -0x5c(%rbp)
jle 0x8ca3
leaq 0xf78c(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0xe8(%rbp)
jmp 0x8c82
movq -0xe8(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x3, %esi
movl $0x1, %edx
callq *%rax
jmp 0x8c9e
jmp 0x8d39
cmpl $0x0, -0x58(%rbp)
jle 0x8d07
leaq 0xf750(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0xf0(%rbp)
jmp 0x8cbe
movq -0xf0(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x1, %edx
movl %edx, %esi
callq *%rax
jmp 0x8cd7
leaq 0xf722(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0xf8(%rbp)
jmp 0x8cec
movq -0xf8(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x2, %esi
xorl %edx, %edx
callq *%rax
jmp 0x8d05
jmp 0x8d37
leaq 0xf6f2(%rip), %rdi      # 0x18400
callq 0x7880
movq %rax, -0x100(%rbp)
jmp 0x8d1c
movq -0x100(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
movl $0x3, %esi
xorl %edx, %edx
callq *%rax
jmp 0x8d35
jmp 0x8d37
jmp 0x8d39
jmp 0x8d3b
xorl %edi, %edi
movl %edi, %esi
callq 0x55d0
movl %eax, -0x104(%rbp)
jmp 0x8d4c
movl -0x104(%rbp), %eax
cmpl $0x0, %eax
je 0x8e26
movb $0x1, -0x82(%rbp)
movl $0x10, %edi
callq 0x51b0
movq %rax, -0x118(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x56c0
movq -0x110(%rbp), %rdx
leaq 0x96c8(%rip), %rsi      # 0x1245c
leaq -0x80(%rbp), %rdi
callq 0x7600
jmp 0x8d9f
callq 0x5030
movq -0x118(%rbp), %rdi
movl (%rax), %edx
leaq -0x80(%rbp), %rsi
callq 0x76b0
jmp 0x8db8
movq -0x118(%rbp), %rdi
movb $0x0, -0x82(%rbp)
leaq 0xe833(%rip), %rsi      # 0x17600
leaq -0x1654(%rip), %rdx     # 0x7780
callq 0x5680
jmp 0x8e82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x8e01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5230
leaq -0x81(%rbp), %rdi
callq 0x53f0
testb $0x1, -0x82(%rbp)
jne 0x8e18
jmp 0x8e24
movq -0x118(%rbp), %rdi
callq 0x52a0
jmp 0x8e70
movl $0x384, %edi            # imm = 0x384
callq 0x52b0
jmp 0x8e32
jmp 0x89a3
movq 0xf172(%rip), %rdi      # 0x17fb0
leaq 0x968f(%rip), %rsi      # 0x124d4
callq 0x5380
jmp 0x8e4c
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x9290
jmp 0x8e5c
leaq -0x20(%rbp), %rdi
callq 0x92b0
xorl %eax, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x20(%rbp), %rdi
callq 0x92b0
movq -0x28(%rbp), %rdi
callq 0x56b0
nopw %cs:(%rax,%rax)