// File: wangyufei95[P]watchdog_linux/watchdog.cpp
// Function: Supervisor::parseArgs(int, char**)
bool Supervisor::parseArgs(int argc, char* argv[]) 
	{
	  if (argc <= 1) 
	  {
		printHelp();
		return false;
	  }

	  struct option opts[] = {
		  					  {"fork",no_argument,nullptr,'f'},
							  {"delay-limit", required_argument, nullptr, 'l'},
							  {"restart-on-error", no_argument, nullptr, 'e'},
							  {"restart-on-crash", no_argument, nullptr, 'c'},
							  {0, 0, 0, 0}
							  };

	  int logLevel = 2;

	  for (;;) 
	  {
		int long_index = 0;
		switch (getopt_long(argc, argv, "fp:l:fec", opts, &long_index)) 
		{
			case 'f':
				fork_= true;
				break;
		  	case 'l':
			// TODO: ensure optarg is a number
				restartDelayLimit_ = atoi(optarg);
        		break;
      		case 'e':
        		restartOnError_ = true;
        		break;
      		case 'c':
        		restartOnCrash_ = true;
        		break;
      		case 0:  // long option with (val!=nullptr && flag=0)
        		break;
      		case -1: {
        // EOF - everything parsed.
        		if (optind == argc) 
				{
          			logger()->error("no program path given");
          			return false;
        		}

        std::vector<std::string> args;
        while (optind < argc) 
		{
          args.push_back(argv[optind++]);
        }

        if (args[0].empty() || args[0][0] != '/') 
		{
          logger()->error("program path must be absolute.");
          return false;
        }

        if (getuid() && getuid()) 
		{
          logger()->error("Must run as (setuid) root. Please fix permissions.");
          return false;
        }

        if (fork_) 
		{
          int rv = daemon(1, 1);   //�ɹ�Ϊ0  ʧ��Ϊ-1
          if (rv < 0) 
		  {
            logger()->error("Could not daemonize into background. %s",
                            strerror(errno));

            return false;
          }
        }

        logger_.setLogLevel(logLevel);

        program_.reset(new Program(logger(), args[0], args, mainPidfile_));

        auto signals = {SIGINT,  SIGQUIT, SIGTERM, SIGCONT,
                        SIGUSR1, SIGUSR2, SIGTTIN, SIGTTOU};

        for (int sig : signals) 
		{
          signal(sig, &Supervisor::sighandler);
        }

        return true;
      }
      case '?':  // ambiguous match / unknown arg
      default:
        return false;
    }
  }
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpl $0x1, -0x14(%rbp)
jg 0x6a8c
movq -0x178(%rbp), %rdi
callq 0x7010
movb $0x0, -0x1(%rbp)
jmp 0x6ff3
leaq -0xc0(%rbp), %rdi
leaq 0xa236(%rip), %rsi      # 0x10cd0
movl $0xa0, %edx
callq 0x3320
movl $0x2, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0x14(%rbp), %edi
movq -0x20(%rbp), %rsi
leaq 0x57df(%rip), %rdx      # 0xc2a5
leaq -0xc0(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0x36a0
movl %eax, %ecx
movl %ecx, -0x17c(%rbp)
subl $-0x1, %eax
je 0x6ba5
jmp 0x6aec
movl -0x17c(%rbp), %eax
testl %eax, %eax
je 0x6ba0
jmp 0x6afc
movl -0x17c(%rbp), %eax
subl $0x3f, %eax
je 0x6fe6
jmp 0x6b0d
movl -0x17c(%rbp), %eax
subl $0x63, %eax
je 0x6b8a
jmp 0x6b1a
movl -0x17c(%rbp), %eax
subl $0x65, %eax
je 0x6b77
jmp 0x6b27
movl -0x17c(%rbp), %eax
subl $0x66, %eax
je 0x6b44
jmp 0x6b34
movl -0x17c(%rbp), %eax
subl $0x6c, %eax
je 0x6b57
jmp 0x6fe8
movq -0x178(%rbp), %rax
movb $0x1, 0x84(%rax)
jmp 0x6fee
movq 0xa44a(%rip), %rax      # 0x10fa8
movq (%rax), %rdi
callq 0x3620
movl %eax, %ecx
movq -0x178(%rbp), %rax
movl %ecx, 0x78(%rax)
jmp 0x6fee
movq -0x178(%rbp), %rax
movl $0x1, 0x7c(%rax)
jmp 0x6fee
movq -0x178(%rbp), %rax
movl $0x1, 0x80(%rax)
jmp 0x6fee
jmp 0x6fee
movq 0xa41c(%rip), %rax      # 0x10fc8
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x6bd7
movq -0x178(%rbp), %rdi
callq 0x8320
movq %rax, %rdi
leaq 0x56e5(%rip), %rsi      # 0xc2ae
callq 0x8330
movb $0x0, -0x1(%rbp)
jmp 0x6ff3
leaq -0xe0(%rbp), %rdi
callq 0x8450
movq 0xa3de(%rip), %rax      # 0x10fc8
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jge 0x6cb8
movq -0x20(%rbp), %rax
movq 0xa3c8(%rip), %rdx      # 0x10fc8
movslq (%rdx), %rcx
movl %ecx, %esi
incl %esi
movl %esi, (%rdx)
movq (%rax,%rcx,8), %rax
movq %rax, -0x190(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x36f0
movq -0x190(%rbp), %rsi
movq -0x188(%rbp), %rdx
leaq -0x100(%rbp), %rdi
callq 0x3540
jmp 0x6c43
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x8470
jmp 0x6c58
leaq -0x100(%rbp), %rdi
callq 0x3260
leaq -0x101(%rbp), %rdi
callq 0x3430
jmp 0x6be3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
jmp 0x6ca7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x3260
leaq -0x101(%rbp), %rdi
callq 0x3430
jmp 0x6fd8
leaq -0xe0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x84a0
movq %rax, %rdi
callq 0x36b0
testb $0x1, %al
jne 0x6d12
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x1a0(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x84a0
movq -0x1a0(%rbp), %rsi
movq %rax, %rdi
callq 0x3790
movq %rax, -0x198(%rbp)
jmp 0x6d03
movq -0x198(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x6d66
movq -0x178(%rbp), %rdi
callq 0x8320
movq %rax, -0x1a8(%rbp)
jmp 0x6d27
movq -0x1a8(%rbp), %rdi
leaq 0x558f(%rip), %rsi      # 0xc2c4
callq 0x8330
jmp 0x6d3c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x118(%rbp)
jmp 0x6fca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
jmp 0x6fd8
callq 0x31e0
cmpl $0x0, %eax
je 0x6db7
callq 0x31e0
cmpl $0x0, %eax
je 0x6db7
movq -0x178(%rbp), %rdi
callq 0x8320
movq %rax, -0x1b0(%rbp)
jmp 0x6d8f
movq -0x1b0(%rbp), %rdi
leaq 0x5546(%rip), %rsi      # 0xc2e3
callq 0x8330
jmp 0x6da4
movb $0x0, -0x1(%rbp)
movl $0x1, -0x118(%rbp)
jmp 0x6fca
movq -0x178(%rbp), %rax
testb $0x1, 0x84(%rax)
je 0x6e30
movl $0x1, %esi
movl %esi, %edi
callq 0x34b0
movl %eax, -0x11c(%rbp)
cmpl $0x0, -0x11c(%rbp)
jge 0x6e2e
movq -0x178(%rbp), %rdi
callq 0x8320
movq %rax, -0x1b8(%rbp)
jmp 0x6df7
callq 0x3040
movl (%rax), %edi
callq 0x3100
movq -0x1b8(%rbp), %rdi
movq %rax, %rdx
leaq 0x5502(%rip), %rsi      # 0xc316
callq 0x7f00
jmp 0x6e1b
movb $0x0, -0x1(%rbp)
movl $0x1, -0x118(%rbp)
jmp 0x6fca
jmp 0x6e30
movq -0x178(%rbp), %rdi
movl -0xc4(%rbp), %esi
callq 0x84c0
jmp 0x6e44
movq -0x178(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x1c8(%rbp)
movl $0x88, %edi
callq 0x3400
movq %rax, -0x1c0(%rbp)
jmp 0x6e69
movq -0x178(%rbp), %rdi
callq 0x8320
movq %rax, -0x1d0(%rbp)
jmp 0x6e7e
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x84a0
movq -0x178(%rbp), %r8
movq -0x1c0(%rbp), %rdi
movq -0x1d0(%rbp), %rsi
movq -0x1d8(%rbp), %rcx
movq %rax, %rdx
addq $0x50, %r8
callq 0x6400
jmp 0x6ebf
movq -0x1c0(%rbp), %rsi
movq -0x1c8(%rbp), %rdi
callq 0x84e0
movq 0x576f(%rip), %rax      # 0xc648
movq %rax, -0x150(%rbp)
movq 0x5769(%rip), %rax      # 0xc650
movq %rax, -0x148(%rbp)
movq 0x5763(%rip), %rax      # 0xc658
movq %rax, -0x140(%rbp)
movq 0x575d(%rip), %rax      # 0xc660
movq %rax, -0x138(%rbp)
leaq -0x150(%rbp), %rax
movq %rax, -0x130(%rbp)
movq $0x8, -0x128(%rbp)
leaq -0x130(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rdi
callq 0x8510
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rdi
callq 0x8530
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rax
cmpq -0x168(%rbp), %rax
je 0x6fbc
movq -0x160(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x16c(%rbp)
movl -0x16c(%rbp), %edi
leaq 0xad(%rip), %rsi        # 0x7030
callq 0x34a0
movq -0x160(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x160(%rbp)
jmp 0x6f57
movq -0x1c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
callq 0x33b0
jmp 0x6fd8
movb $0x1, -0x1(%rbp)
movl $0x1, -0x118(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x7c40
jmp 0x6ff3
leaq -0xe0(%rbp), %rdi
callq 0x7c40
jmp 0x7001
jmp 0x6fe8
movb $0x0, -0x1(%rbp)
jmp 0x6ff3
jmp 0x6aae
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0x110(%rbp), %rdi
callq 0x36e0
nopl (%rax)