// File: /workspace/github_c_makefile_star2_O2/ttoobne[P]bohttpd/src/core/utility.c
// Function: daemonize
void daemonize(const char *cmd)
{
    pid_t pid;
    struct rlimit rl;
    struct sigaction sa;
    int i;
    int fd0;
    int fd1;
    int fd2;

    /* 设置屏蔽字 */
    umask(0);

    if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
        log_error("%s: can't get file limit", cmd);
    }

    /* 调用 fork() ，使父进程 exit */
    if ((pid = fork()) < 0) {
        log_error("%s: can't fork", cmd);
    } else if (pid != 0) {
        exit(0);
    }

    /* 创建一个新会话 */
    setsid();

    /* 确保没有控制终端 */
    sa.sa_handler = SIG_IGN;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGHUP, &sa, NULL) < 0) {
        log_error("%s: can't ignore SIGHUP", cmd);
    }
        
    if ((pid = fork()) < 0) {
        log_error("%s: can't fork", cmd);
    } else if (pid != 0) {
        exit(0);
    }
        

    /* 切换当前工作目录为根目录 */
    if (chdir("/") < 0) {
        log_error("%s: can't change directory to /", cmd);
    }

    /* 关闭所有文件描述符 */
    if (rl.rlim_max == RLIM_INFINITY) {
        rl.rlim_max = 1024;
    }
        
    for (i = 0; i < rl.rlim_max; i++) {
        close(i);
    }

    /* 在终端看不到守护进程的输出，用户输入不会被守护进程读取 */
    fd0 = open("/dev/null", O_RDWR);
    fd1 = dup(0);
    fd2 = dup(0);

    openlog(cmd, LOG_CONS, LOG_DAEMON);
    if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
        syslog(LOG_ERR, "unexpected file descriptors %d %d %d",
               fd0, fd1, fd2);
        exit(1);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x2260
pushq $0x7
popq %rdi
leaq -0x38(%rbp), %rsi
callq 0x2410
testl %eax, %eax
jns 0x5d7a
leaq 0x127e(%rip), %rsi      # 0x6fda
leaq 0x12f3(%rip), %rdx      # 0x7056
leaq 0x12f6(%rip), %r8       # 0x7060
pushq $0x4
popq %rdi
pushq $0x52
popq %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0x4c60
callq 0x2460
testl %eax, %eax
js 0x5d8a
je 0x5daf
jmp 0x5e18
leaq 0x1249(%rip), %rsi      # 0x6fda
leaq 0x12be(%rip), %rdx      # 0x7056
leaq 0x12da(%rip), %r8       # 0x7079
pushq $0x4
popq %rdi
pushq $0x57
popq %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0x4c60
callq 0x21e0
leaq -0xc8(%rbp), %r14
movq $0x1, -0x8(%r14)
movq %r14, %rdi
callq 0x2250
andl $0x0, 0x80(%r14)
pushq $0x1
popq %rdi
leaq -0xd0(%rbp), %rsi
xorl %edx, %edx
callq 0x20b0
testl %eax, %eax
jns 0x5e0d
leaq 0x11eb(%rip), %rsi      # 0x6fda
leaq 0x1260(%rip), %rdx      # 0x7056
leaq 0x128b(%rip), %r8       # 0x7088
pushq $0x4
popq %rdi
pushq $0x64
popq %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0x4c60
callq 0x2460
testl %eax, %eax
js 0x5e1f
je 0x5e44
xorl %edi, %edi
callq 0x23f0
leaq 0x11b4(%rip), %rsi      # 0x6fda
leaq 0x1229(%rip), %rdx      # 0x7056
leaq 0x1245(%rip), %r8       # 0x7079
pushq $0x4
popq %rdi
pushq $0x68
popq %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0x4c60
leaq 0x1273(%rip), %rdi      # 0x70be
callq 0x2140
testl %eax, %eax
jns 0x5e79
leaq 0x117f(%rip), %rsi      # 0x6fda
leaq 0x11f4(%rip), %rdx      # 0x7056
leaq 0x1237(%rip), %r8       # 0x70a0
pushq $0x4
popq %rdi
pushq $0x70
popq %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0x4c60
movq -0x30(%rbp), %rax
cmpq $-0x1, %rax
jne 0x5e90
movq $0x400, -0x30(%rbp)     # imm = 0x400
movl $0x400, %eax            # imm = 0x400
xorl %r14d, %r14d
cmpq %r14, %rax
jbe 0x5ea9
movl %r14d, %edi
callq 0x21d0
incq %r14
movq -0x30(%rbp), %rax
jmp 0x5e93
leaq 0x1210(%rip), %rdi      # 0x70c0
pushq $0x2
popq %r13
movl %r13d, %esi
xorl %eax, %eax
callq 0x2380
movl %eax, %r14d
xorl %edi, %edi
callq 0x2190
movl %eax, %r15d
xorl %edi, %edi
callq 0x2190
movl %eax, %r12d
pushq $0x18
popq %rdx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x23d0
testl %r14d, %r14d
jne 0x5f06
cmpl $0x1, %r15d
jne 0x5f06
cmpl $0x2, %r12d
jne 0x5f06
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11bd(%rip), %rsi      # 0x70ca
pushq $0x3
popq %rdi
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22f0
pushq $0x1
popq %rdi
callq 0x23f0