// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
cmpl $0x1, %esi
jg 0x55ee
leaq 0x1f76(%rip), %rdi      # 0x755a
callq 0x3550
jmp 0x56ba
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, -0x30(%rbp)
leaq 0x46e1(%rip), %rsi      # 0x9ce0
leaq -0x110(%rbp), %rdi
movl $0xa0, %edx
callq 0x32e0
leaq 0x1cca(%rip), %r13      # 0x72e1
leaq -0x38(%rbp), %rbx
leaq 0x19fe(%rip), %r12      # 0x7020
andl $0x0, -0x38(%rbp)
movl %r15d, %edi
movq %r14, %rsi
movq %r13, %rdx
leaq -0x110(%rbp), %rcx
movq %rbx, %r8
callq 0x35d0
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x565f
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq -0x30(%rbp), %rax
movl $0x1, 0x80(%rax)
jmp 0x5622
testl %eax, %eax
je 0x5622
jmp 0x5697
movq -0x30(%rbp), %rax
movb $0x1, 0x84(%rax)
jmp 0x5622
movq -0x30(%rbp), %rax
movl $0x1, 0x7c(%rax)
jmp 0x5622
movq 0x492a(%rip), %rax      # 0x9fb0
movq (%rax), %rdi
callq 0x3570
movq -0x30(%rbp), %rcx
movl %eax, 0x78(%rcx)
jmp 0x5622
cmpl $-0x1, %eax
jne 0x56ba
movq 0x492d(%rip), %rbx      # 0x9fd0
movl (%rbx), %eax
cmpl %r15d, %eax
jne 0x56d0
leaq 0x1c39(%rip), %rsi      # 0x72ea
movq -0x30(%rbp), %rdi
callq 0x5f68
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x50(%rbp), %rcx
andq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq -0x70(%rbp), %r12
leaq -0x31(%rbp), %r13
cmpl %r15d, %eax
jge 0x571a
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
cltq
movq (%r14,%rax,8), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x3110
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x69ee
movq %r12, %rdi
callq 0x3160
movl (%rbx), %eax
jmp 0x56e7
movq -0x50(%rbp), %rax
leaq 0x1bdb(%rip), %rsi      # 0x7300
cmpq $0x0, 0x8(%rax)
je 0x5753
movq (%rax), %rax
cmpb $0x2f, (%rax)
movq -0x30(%rbp), %rbx
jne 0x5757
callq 0x31a0
testl %eax, %eax
je 0x576f
callq 0x31a0
testl %eax, %eax
je 0x576f
leaq 0x1bce(%rip), %rsi      # 0x731f
jmp 0x5757
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x5f68
xorl %ebx, %ebx
leaq -0x50(%rbp), %rdi
callq 0x5cca
jmp 0x56bc
cmpb $0x1, 0x84(%rbx)
jne 0x5786
pushq $0x1
popq %rdi
movl %edi, %esi
callq 0x3430
testl %eax, %eax
js 0x57ef
movl $0x2, 0x20(%rbx)
movl $0x88, %edi
callq 0x33c0
movq %rax, %r14
leaq -0x50(%rbp), %rcx
movq (%rcx), %rdx
leaq 0x50(%rbx), %r8
movq %rax, %rdi
movq %rbx, %rsi
callq 0x51c6
addq $0x28, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6b82
xorl %r14d, %r14d
leaq 0x1f73(%rip), %r15      # 0x773c
leaq 0x88(%rip), %rbx        # 0x5858
cmpq $0x20, %r14
je 0x57e8
movl (%r14,%r15), %edi
movq %rbx, %rsi
callq 0x3420
addq $0x4, %r14
jmp 0x57d0
movb $0x1, %bl
jmp 0x5761
callq 0x3030
movl (%rax), %edi
callq 0x30e0
leaq 0x1b50(%rip), %rsi      # 0x7352
movq %rbx, %rdi
movq %rax, %rdx
callq 0x5e62
jmp 0x575f
jmp 0x5831
movq %rax, %rbx
movq %r14, %rdi
callq 0x3370
jmp 0x5834
jmp 0x5831
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x3160
jmp 0x5834
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x5cca
movq %rbx, %rdi
callq 0x3600
nop