// File: mrGoncharuk[P]foldermonitor/src/main.cpp
// Function: daemonize()
int	daemonize()
{
	pid_t pid, sid;

	pid = fork();
	if(pid > 0)
	{
		exit(EXIT_SUCCESS);
	}
	else if(pid < 0)
	{
		exit(EXIT_FAILURE);
	}
	umask(0);

	openlog("daemon-foldermonitor", LOG_NOWAIT | LOG_PID, LOG_MAIL);
	syslog(LOG_NOTICE, "Staring daemon-foldermonitor...");

	sid = setsid();
	if(sid < 0)
	{
		syslog(LOG_ERR, "Could not generate session ID for child process");
		exit(EXIT_FAILURE);
	}

	if((chdir("/")) < 0)
	{
		syslog(LOG_ERR, "Could not change working directory to /");
		exit(EXIT_FAILURE);
	}


	const char *pidfile = "/var/run/daemon-foldermonitor.pid";
	int pidFilehandle = open(pidfile, O_RDWR | O_CREAT, 0666);
	if (pidFilehandle == -1 )
	{
		syslog(LOG_INFO, "Could not open PID lock file %s, exiting", pidfile);
		exit(EXIT_FAILURE);
	}
	int fr = flock(pidFilehandle, LOCK_EX | LOCK_NB);
	if (fr < 0)
	{
		syslog(LOG_INFO, "Daemon already running. Terminating current daemon with pid <%d>.", getpid());
		exit(EXIT_FAILURE);
	}
	std::string strpid = std::to_string(getpid());
	write(pidFilehandle, strpid.c_str(), strpid.length());
	
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);
	return pidFilehandle;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
callq 0x3090
movl %eax, -0x4(%rbp)
cmpl $0x0, -0x4(%rbp)
jle 0x35ed
xorl %edi, %edi
callq 0x3250
cmpl $0x0, -0x4(%rbp)
jge 0x35fd
movl $0x1, %edi
callq 0x3250
jmp 0x35ff
xorl %edi, %edi
callq 0x3380
leaq 0x49f7(%rip), %rdi      # 0x8004
movl $0x11, %esi
movl $0x10, %edx
callq 0x31d0
movl $0x5, %edi
leaq 0x49f1(%rip), %rsi      # 0x8019
movb $0x0, %al
callq 0x3060
callq 0x3240
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jge 0x365a
movl $0x3, %edi
leaq 0x49f0(%rip), %rsi      # 0x8039
movb $0x0, %al
callq 0x3060
movl $0x1, %edi
callq 0x3250
leaq 0x4a2e(%rip), %rdi      # 0x808f
callq 0x3300
cmpl $0x0, %eax
jge 0x3688
movl $0x3, %edi
leaq 0x49f2(%rip), %rsi      # 0x8069
movb $0x0, %al
callq 0x3060
movl $0x1, %edi
callq 0x3250
leaq 0x4a02(%rip), %rax      # 0x8091
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movl $0x42, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x31a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x36d2
movq -0x10(%rbp), %rdx
movl $0x6, %edi
leaq 0x49f2(%rip), %rsi      # 0x80b3
movb $0x0, %al
callq 0x3060
movl $0x1, %edi
callq 0x3250
movl -0x14(%rbp), %edi
movl $0x6, %esi
callq 0x33c0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x370c
callq 0x32e0
movl %eax, %edx
movl $0x6, %edi
leaq 0x49e1(%rip), %rsi      # 0x80dc
movb $0x0, %al
callq 0x3060
movl $0x1, %edi
callq 0x3250
callq 0x32e0
movl %eax, %esi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x60(%rbp)
callq 0x3ba0
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
callq 0x30f0
movq -0x60(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x3310
movl -0x54(%rbp), %edi
movq -0x50(%rbp), %rsi
movq %rax, %rdx
callq 0x30c0
jmp 0x374d
xorl %edi, %edi
callq 0x33f0
jmp 0x3756
movl $0x1, %edi
callq 0x33f0
jmp 0x3762
movl $0x2, %edi
callq 0x33f0
jmp 0x376e
movl -0x14(%rbp), %eax
movl %eax, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3170
movl -0x64(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3170
movq -0x40(%rbp), %rdi
callq 0x3360
nopw %cs:(%rax,%rax)