// File: /workspace/github_cpp_makefile_star2_O1/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 0x26777
movq %rdi, %rbx
cmpl $-0x1, 0x4e4(%rdi)
jne 0x26777
movq 0x198(%rbx), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x8540
testl %eax, %eax
je 0x26611
callq 0x8050
movl (%rax), %edi
cmpl $0x2, %edi
je 0x2661c
callq 0x81d0
movq %rax, %r14
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0xfa3f(%rip), %rdi      # 0x36043
callq 0x2e73d
movq %r14, %rdi
jmp 0x2670c
testb $-0x40, 0x29(%rsp)
je 0x266f7
movq %r15, %rdi
callq 0x8250
movl 0x1a0(%rbx), %r12d
addl $0x3, %r12d
movq %r12, %rdi
callq 0x8820
testq %rax, %rax
je 0x266e0
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 0x26713
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
movl %r12d, %edx
callq 0x80b0
cmpl $-0x1, %eax
je 0x26746
movq %r14, %rdi
callq 0x8720
movl 0x1bc(%rbx), %esi
movl 0x1c0(%rbx), %edx
movq %r15, %rdi
callq 0x83d0
testl %eax, %eax
je 0x26787
addq $0x8, %rbx
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xfa10(%rip), %rsi      # 0x360dc
movq %rbx, %rdi
callq 0x27aa4
movl %ebp, %edi
callq 0x8b60
jmp 0x26775
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0xf973(%rip), %rdi      # 0x36068
jmp 0x2670c
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0xf901(%rip), %rdi      # 0x3600d
callq 0x2e759
jmp 0x26775
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r15
movq 0x8(%rbx), %rsi
movl $0x3, %edi
callq 0x2e63a
leaq 0xf95c(%rip), %rdi      # 0x36093
callq 0x2e73d
movq %r15, %rdi
callq 0x2e759
jmp 0x2676d
addq $0x8, %rbx
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
callq 0x27a7e
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 0x267b5
movl %ebp, %edi
movl $0x80, %esi
callq 0x86a0
cmpl $-0x1, %eax
je 0x267d9
movl %ebp, 0x4e4(%rbx)
movb $0x1, %al
jmp 0x26777
addq $0x8, %rbx
callq 0x8050
movl (%rax), %edi
callq 0x81d0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xf938(%rip), %rsi      # 0x3610c
jmp 0x266cc
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %rbx
leaq 0xf94d(%rip), %rsi      # 0x3613c
movl $0x3, %edi
callq 0x2e63a
movq %rbx, %rdi
callq 0x2e759
jmp 0x266d4
movq %rax, %rdi
callq 0xb8be