// File: /workspace/github_c_makefile_star2_O0/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
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
xorl %edi, %edi
callq 0x2280
movl $0x7, %edi
leaq -0x20(%rbp), %rsi
callq 0x2430
cmpl $0x0, %eax
jge 0x82d3
movq -0x8(%rbp), %r9
movl $0x4, %edi
leaq 0x1d2a(%rip), %rsi      # 0x9fe3
leaq 0x1d9f(%rip), %rdx      # 0xa05f
movl $0x52, %ecx
leaq 0x1d9d(%rip), %r8       # 0xa069
movb $0x0, %al
callq 0x6450
callq 0x2480
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
jge 0x830c
movq -0x8(%rbp), %r9
movl $0x4, %edi
leaq 0x1cf3(%rip), %rsi      # 0x9fe3
leaq 0x1d68(%rip), %rdx      # 0xa05f
movl $0x57, %ecx
leaq 0x1d7f(%rip), %r8       # 0xa082
movb $0x0, %al
callq 0x6450
jmp 0x831b
cmpl $0x0, -0xc(%rbp)
je 0x8319
xorl %edi, %edi
callq 0x2420
jmp 0x831b
callq 0x2200
movl $0x1, %eax
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rdi
addq $0x8, %rdi
callq 0x2270
movl $0x0, -0x30(%rbp)
movl $0x1, %edi
leaq -0xb8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x20b0
cmpl $0x0, %eax
jge 0x8387
movq -0x8(%rbp), %r9
movl $0x4, %edi
leaq 0x1c76(%rip), %rsi      # 0x9fe3
leaq 0x1ceb(%rip), %rdx      # 0xa05f
movl $0x64, %ecx
leaq 0x1d11(%rip), %r8       # 0xa091
movb $0x0, %al
callq 0x6450
callq 0x2480
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
jge 0x83c0
movq -0x8(%rbp), %r9
movl $0x4, %edi
leaq 0x1c3f(%rip), %rsi      # 0x9fe3
leaq 0x1cb4(%rip), %rdx      # 0xa05f
movl $0x68, %ecx
leaq 0x1ccb(%rip), %r8       # 0xa082
movb $0x0, %al
callq 0x6450
jmp 0x83cf
cmpl $0x0, -0xc(%rbp)
je 0x83cd
xorl %edi, %edi
callq 0x2420
jmp 0x83cf
leaq 0x1cf1(%rip), %rdi      # 0xa0c7
callq 0x2140
cmpl $0x0, %eax
jge 0x840a
movq -0x8(%rbp), %r9
movl $0x4, %edi
leaq 0x1bf3(%rip), %rsi      # 0x9fe3
leaq 0x1c68(%rip), %rdx      # 0xa05f
movl $0x70, %ecx
leaq 0x1ca6(%rip), %r8       # 0xa0a9
movb $0x0, %al
callq 0x6450
cmpq $-0x1, -0x18(%rbp)
jne 0x8419
movq $0x400, -0x18(%rbp)     # imm = 0x400
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x844c
movl -0xbc(%rbp), %edi
callq 0x21f0
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x8423
leaq 0x1c76(%rip), %rdi      # 0xa0c9
movl $0x2, %esi
movb $0x0, %al
callq 0x23b0
movl %eax, -0xc0(%rbp)
xorl %edi, %edi
callq 0x21a0
movl %eax, -0xc4(%rbp)
xorl %edi, %edi
callq 0x21a0
movl %eax, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
movl $0x2, %esi
movl $0x18, %edx
callq 0x2400
cmpl $0x0, -0xc0(%rbp)
jne 0x84ad
cmpl $0x1, -0xc4(%rbp)
jne 0x84ad
cmpl $0x2, -0xc8(%rbp)
je 0x84dd
movl -0xc0(%rbp), %edx
movl -0xc4(%rbp), %ecx
movl -0xc8(%rbp), %r8d
movl $0x3, %edi
leaq 0x1c07(%rip), %rsi      # 0xa0d3
movb $0x0, %al
callq 0x2310
movl $0x1, %edi
callq 0x2420
addq $0xd0, %rsp
popq %rbp
retq