// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/baseproc-service.cc
// Function: base_process_service::open_socket()
bool base_process_service::open_socket() noexcept
{
    if (socket_path.empty() || socket_fd != -1) {
        // No socket, or already open
        return true;
    }

    const char * saddrname = socket_path.c_str();

    // Check the specified socket path
    struct stat stat_buf;
    if (stat(saddrname, &stat_buf) == 0) {
        if ((stat_buf.st_mode & S_IFSOCK) == 0) {
            // Not a socket
            log(loglevel_t::ERROR, get_name(), ": activation socket file exists (and is not a socket)");
            return false;
        }
    }
    else if (errno != ENOENT) {
        // Other error
        log(loglevel_t::ERROR, get_name(), ": error checking activation socket: ", strerror(errno));
        return false;
    }

    // Remove stale socket file (if it exists).
    // We won't test the return from unlink - if it fails other than due to ENOENT, we should get an
    // error when we try to create the socket anyway.
    unlink(saddrname);

    uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
    struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
    if (name == nullptr) {
        log(loglevel_t::ERROR, get_name(), ": opening activation socket: out of memory");
        return false;
    }

    name->sun_family = AF_UNIX;
    strcpy(name->sun_path, saddrname);

    int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
    if (sockfd == -1) {
        log(loglevel_t::ERROR, get_name(), ": error creating activation socket: ", strerror(errno));
        free(name);
        return false;
    }

    if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
        log(loglevel_t::ERROR, get_name(), ": error binding activation socket: ", strerror(errno));
        close(sockfd);
        free(name);
        return false;
    }

    free(name);

    // POSIX (1003.1, 2013) says that fchown and fchmod don't necessarily work on sockets. We have to
    // use chown and chmod instead.
    if (chown(saddrname, socket_uid, socket_gid)) {
        log(loglevel_t::ERROR, get_name(), ": error setting activation socket owner/group: ",
                strerror(errno));
        close(sockfd);
        return false;
    }

    if (chmod(saddrname, socket_perms) == -1) {
        log(loglevel_t::ERROR, get_name(), ": Error setting activation socket permissions: ",
                strerror(errno));
        close(sockfd);
        return false;
    }

    if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
        log(loglevel_t::ERROR, ": error listening on activation socket: ", strerror(errno));
        close(sockfd);
        return false;
    }

    socket_fd = sockfd;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xa0, %rsp
movb $0x1, %al
cmpq $0x0, 0x1a0(%rdi)
je 0x268dd
movq %rdi, %rbx
cmpl $-0x1, 0x4e4(%rdi)
jne 0x268dd
movq 0x198(%rbx), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x8540
testl %eax, %eax
je 0x2677d
callq 0x8050
movl (%rax), %edi
cmpl $0x2, %edi
je 0x26788
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf88a(%rip), %rdi      # 0x35ffa
callq 0x2e520
movq %r14, %rdi
jmp 0x26875
testb $-0x40, 0x29(%rsp)
je 0x26860
movq %r15, %rdi
callq 0x8250
movl 0x1a0(%rbx), %r12d
addl $0x3, %r12d
movq %r12, %rdi
callq 0x8820
testq %rax, %rax
je 0x26849
movq %rax, %r14
movw $0x1, (%rax)
movq %rax, %rdi
addq $0x2, %rdi
movq %r15, %rsi
callq 0x85a0
movl $0x1, %edi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
callq 0x8110
cmpl $-0x1, %eax
je 0x2687c
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
movl %r12d, %edx
callq 0x80b0
cmpl $-0x1, %eax
je 0x268af
movq %r14, %rdi
callq 0x8720
movl 0x1bc(%rbx), %esi
movl 0x1c0(%rbx), %edx
movq %r15, %rdi
callq 0x83d0
testl %eax, %eax
je 0x268ed
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
movq 0x8(%rbx), %rdi
leaq 0xf85b(%rip), %rsi      # 0x36093
callq 0x27b12
movl %ebp, %edi
callq 0x8b60
jmp 0x268db
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf7c1(%rip), %rdi      # 0x3601f
jmp 0x26875
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf74f(%rip), %rdi      # 0x35fc4
callq 0x2e53c
jmp 0x268db
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r15
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e412
leaq 0xf7aa(%rip), %rdi      # 0x3604a
callq 0x2e520
movq %r15, %rdi
callq 0x2e53c
jmp 0x268d3
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x27aec
movl %ebp, %edi
callq 0x8b60
movq %r14, %rdi
callq 0x8720
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x1b8(%rbx), %esi
movq %r15, %rdi
callq 0x8580
cmpl $-0x1, %eax
je 0x2691b
movl %ebp, %edi
movl $0x80, %esi
callq 0x86a0
cmpl $-0x1, %eax
je 0x2693f
movl %ebp, 0x4e4(%rbx)
movb $0x1, %al
jmp 0x268dd
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
movq 0x8(%rbx), %rdi
leaq 0xf789(%rip), %rsi      # 0x360c3
jmp 0x26838
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %rbx
leaq 0xf79e(%rip), %rsi      # 0x360f3
movl $0x3, %edi
callq 0x2e412
movq %rbx, %rdi
callq 0x2e53c
jmp 0x2683d
movq %rax, %rdi
callq 0xb74e