// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0xb0, %rsp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x2260
leaq -0x30(%rbp), %rsi
movl $0x7, %edi
callq 0x2410
testl %eax, %eax
jns 0x6279
leaq 0x1d83(%rip), %rsi      # 0x7fda
leaq 0x1df8(%rip), %rdx      # 0x8056
leaq 0x1dfb(%rip), %r8       # 0x8060
movl $0x4, %edi
movl $0x52, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
callq 0x2460
testl %eax, %eax
js 0x6289
je 0x62b2
jmp 0x6324
leaq 0x1d4a(%rip), %rsi      # 0x7fda
leaq 0x1dbf(%rip), %rdx      # 0x8056
leaq 0x1ddb(%rip), %r8       # 0x8079
movl $0x4, %edi
movl $0x57, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
callq 0x21e0
leaq -0xc0(%rbp), %r14
movq $0x1, -0x8(%r14)
movq %r14, %rdi
callq 0x2250
movl $0x0, 0x80(%r14)
leaq -0xc8(%rbp), %rsi
movl $0x1, %edi
xorl %edx, %edx
callq 0x20b0
testl %eax, %eax
jns 0x6319
leaq 0x1ce3(%rip), %rsi      # 0x7fda
leaq 0x1d58(%rip), %rdx      # 0x8056
leaq 0x1d83(%rip), %r8       # 0x8088
movl $0x4, %edi
movl $0x64, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
callq 0x2460
testl %eax, %eax
js 0x632b
je 0x6354
xorl %edi, %edi
callq 0x23f0
leaq 0x1ca8(%rip), %rsi      # 0x7fda
leaq 0x1d1d(%rip), %rdx      # 0x8056
leaq 0x1d39(%rip), %r8       # 0x8079
movl $0x4, %edi
movl $0x68, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
leaq 0x1d63(%rip), %rdi      # 0x80be
callq 0x2140
testl %eax, %eax
jns 0x638d
leaq 0x1c6f(%rip), %rsi      # 0x7fda
leaq 0x1ce4(%rip), %rdx      # 0x8056
leaq 0x1d27(%rip), %r8       # 0x80a0
movl $0x4, %edi
movl $0x70, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
movq -0x28(%rbp), %rax
testq %rax, %rax
je 0x63b8
cmpq $-0x1, %rax
jne 0x63a4
movq $0x400, -0x28(%rbp)     # imm = 0x400
xorl %r14d, %r14d
movl %r14d, %edi
callq 0x21d0
incq %r14
cmpq %r14, -0x28(%rbp)
ja 0x63a7
leaq 0x1d01(%rip), %rdi      # 0x80c0
movl $0x2, %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
movq %rbx, %rdi
movl $0x2, %esi
movl $0x18, %edx
callq 0x23d0
testl %r14d, %r14d
jne 0x6415
cmpl $0x1, %r15d
jne 0x6415
cmpl $0x2, %r12d
jne 0x6415
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1cae(%rip), %rsi      # 0x80ca
movl $0x3, %edi
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22f0
movl $0x1, %edi
callq 0x23f0