// File: /workspace/github_cpp_makefile_star2_O2/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 0x21fda
movq %rdi, %rbx
cmpl $-0x1, 0x4e4(%rdi)
jne 0x21fda
movq 0x198(%rbx), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x8540
testl %eax, %eax
je 0x21e6c
callq 0x8040
movl (%rax), %edi
cmpl $0x2, %edi
je 0x21e77
addq $0x8, %rbx
callq 0x81c0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xe1a2(%rip), %rsi      # 0x30001
movq %rbx, %rdi
callq 0x22faf
jmp 0x21fd8
testb $-0x40, 0x29(%rsp)
je 0x21f51
movq %r15, %rdi
callq 0x8250
movl 0x1a0(%rbx), %r12d
addl $0x3, %r12d
movq %r12, %rdi
callq 0x8820
testq %rax, %rax
je 0x21f39
movq %rax, %r14
movw $0x1, (%rax)
movq %rax, %rdi
addq $0x2, %rdi
movq %r15, %rsi
callq 0x85a0
pushq $0x1
popq %rdi
movl $0x80801, %esi          # imm = 0x80801
xorl %edx, %edx
callq 0x8100
cmpl $-0x1, %eax
je 0x21f6e
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
movl %r12d, %edx
callq 0x8090
cmpl $-0x1, %eax
je 0x21f97
movq %r14, %rdi
callq 0x8710
movl 0x1bc(%rbx), %esi
movl 0x1c0(%rbx), %edx
movq %r15, %rdi
callq 0x83d0
testl %eax, %eax
je 0x21fea
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xe175(%rip), %rsi      # 0x3009a
movq %rbx, %rdi
callq 0x22fd8
movl %ebp, %edi
callq 0x8b50
jmp 0x21fd8
addq $0x8, %rbx
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x23001
leaq 0xe0d7(%rip), %rdi      # 0x30026
jmp 0x21f67
addq $0x8, %rbx
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x23001
leaq 0xe064(%rip), %rdi      # 0x2ffcb
callq 0x28f31
jmp 0x21fd8
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xe0c4(%rip), %rsi      # 0x30051
movq %rbx, %rdi
callq 0x22faf
jmp 0x21fd0
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r15
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x23001
leaq 0xe0ba(%rip), %rdi      # 0x30076
callq 0x28f17
movq %r15, %rdi
callq 0x28f31
movl %ebp, %edi
callq 0x8b50
movq %r14, %rdi
callq 0x8710
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 0x22018
movl %ebp, %edi
movl $0x80, %esi
callq 0x86a0
cmpl $-0x1, %eax
je 0x2203c
movl %ebp, 0x4e4(%rbx)
movb $0x1, %al
jmp 0x21fda
addq $0x8, %rbx
callq 0x8040
movl (%rax), %edi
callq 0x81c0
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xe093(%rip), %rsi      # 0x300ca
jmp 0x21f25
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %rbx
leaq 0xe0a8(%rip), %rsi      # 0x300fa
pushq $0x3
popq %rdi
callq 0x28e0a
movq %rbx, %rdi
callq 0x28f31
jmp 0x21f2d
movq %rax, %rdi
callq 0xaebb
nop