// File: /workspace/github_c_makefile_star2_O1/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 0x6127
leaq 0x1ed5(%rip), %rsi      # 0x7fda
leaq 0x1f4a(%rip), %rdx      # 0x8056
leaq 0x1f4d(%rip), %r8       # 0x8060
movl $0x4, %edi
movl $0x52, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
callq 0x2460
testl %eax, %eax
js 0x6137
je 0x6160
jmp 0x61d2
leaq 0x1e9c(%rip), %rsi      # 0x7fda
leaq 0x1f11(%rip), %rdx      # 0x8056
leaq 0x1f2d(%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 0x61c7
leaq 0x1e35(%rip), %rsi      # 0x7fda
leaq 0x1eaa(%rip), %rdx      # 0x8056
leaq 0x1ed5(%rip), %r8       # 0x8088
movl $0x4, %edi
movl $0x64, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
callq 0x2460
testl %eax, %eax
js 0x61d9
je 0x6202
xorl %edi, %edi
callq 0x23f0
leaq 0x1dfa(%rip), %rsi      # 0x7fda
leaq 0x1e6f(%rip), %rdx      # 0x8056
leaq 0x1e8b(%rip), %r8       # 0x8079
movl $0x4, %edi
movl $0x68, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
leaq 0x1eb5(%rip), %rdi      # 0x80be
callq 0x2140
testl %eax, %eax
jns 0x623b
leaq 0x1dc1(%rip), %rsi      # 0x7fda
leaq 0x1e36(%rip), %rdx      # 0x8056
leaq 0x1e79(%rip), %r8       # 0x80a0
movl $0x4, %edi
movl $0x70, %ecx
movq %rbx, %r9
xorl %eax, %eax
callq 0x252a
cmpq $-0x1, -0x28(%rbp)
jne 0x624a
movq $0x400, -0x28(%rbp)     # imm = 0x400
cmpq $0x0, -0x28(%rbp)
je 0x6265
xorl %r14d, %r14d
movl %r14d, %edi
callq 0x21d0
incq %r14
cmpq %r14, -0x28(%rbp)
ja 0x6254
leaq 0x1e54(%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 0x62c2
cmpl $0x1, %r15d
jne 0x62c2
cmpl $0x2, %r12d
jne 0x62c2
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e01(%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