// File: /workspace/github_c_makefile_star2_O1/panux[P]linit/src/linit-supd.c
// Function: main
int main(int argc, char **argv) {
    if(argc != 1) {
        perror("[FATAL] Should have no arguments\n");
        return 1;
    }
    //create socket
    int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    {
        if(sockfd == -1) {
            fprintf(stderr, "[FATAL] Failed to create unix socket: %s\n", strerror(errno));
            return 65;
        }
        struct sockaddr_un addr;
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, "/tmp/linit-supd.sock", sizeof(addr.sun_path)-1);
        if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
            fprintf(stderr, "[FATAL] Failed to bind unix domain socket: %s\n", strerror(errno));
            return 65;
        }
    }
    //connect to linitd
    FILE* linitdconn;
    {
        char *sp = getenv("LINITSOCK");
        if(sp == NULL) {
            fprintf(stderr, "[FATAL] LINITSOCK not set\n");
            return 65;
        }
        int fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if(fd == -1) {
            fprintf(stderr, "[FATAL] Failed to create socket: %s\n", strerror(errno));
            return 65;
        }
        struct sockaddr_un addr = { .sun_family = AF_UNIX };
        if((strlen(sp) + 1) > sizeof(addr.sun_path)) {
            fprintf(stderr, "[FATAL] Socket path is too long\n");
            return 65;
        }
        strcpy(addr.sun_path, sp);
        if(connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
            fprintf(stderr, "[FATAL] Failed to connect: %s\n", strerror(errno));
            return 65;
        }
        linitdconn = fdopen(fd, "r+");
        if(linitdconn == NULL) {
            fprintf(stderr, "[FATAL] Failed to convert fd to stream\n");
            return 65;
        }
    }
    //chain (starts empty)
    struct chain *ch = NULL;
    //listen
    if(listen(sockfd, 16) == -1) {
        fprintf(stderr, "[FATAL] Failed to listen: %s\n", strerror(errno));
        return 65;
    }
    //handle connections
    printf("Starting linit-supd\n");
    for(;;) {
        printf("Accepting\n");
        int fd = accept(sockfd, NULL, NULL);
        printf("Accepted\n");
        if(fd == -1) {  //something went wrong - notify linitd and exit
            fprintf(stderr, "[FATAL] Failed to accept connection: %s\n", strerror(errno));
            if(fprintf(linitdconn, "state linit-supd failed\n") < 0) {  //failed to notify linitd
                fprintf(stderr, "[FATAL] Failed to notify linitd of failure: %s\n", strerror(errno));
            } else {
                fclose(linitdconn);
            }
            return 65;
        }
        FILE *stream = fdopen(fd, "r+");
        if(stream == NULL) {
            fprintf(stderr, "[ERROR] Failed to open stream: %s\n", strerror(errno));
            close(fd);
            continue;
        }
        char buf[1024];
        if(fgets(buf, 1024, stream) == NULL) {
            fprintf(stderr, "[ERROR] Failed to read from stream: %s\n", strerror(errno));
            fclose(stream);
            continue;
        }
        //strip trailing newline
        if(buf[strlen(buf) - 1] == '\n') {
            buf[strlen(buf) - 1] = '\0';
        } else {
            perror("[ERROR] Oversized command\n");
            fclose(stream);
            continue;
        }
        if(strlen(buf) < 2) {
            perror("[ERROR] Short command\n");
            fclose(stream);
            continue;
        }
        char cmd = buf[0];
        char *arg = buf + 1;
        printf("Recieved %s\n", buf);
        switch(cmd) {
        case '*':   //new supervision
            {
                char *pidnum = arg;
                char *name = arg;
                while(isdigit(*name)) { name++; }
                if(name == arg) {
                    perror("[ERROR] Command missing process ID\n");
                    fclose(stream);
                    continue;
                }
                struct chain **chainend = &ch;
                while(*chainend != NULL) { chainend = &((*chainend)->next); }
                struct chain *link = malloc(sizeof(struct chain));
                if(link == NULL) {
                    perror("[ERROR] OOM\n");
                    fclose(stream);
                    continue;
                }
                link->name = strdup(name);
                if(link->name == NULL) {
                    perror("[ERROR] OOM\n");
                    free(link);
                    fclose(stream);
                    continue;
                }
                *name = '\0';
                link->pid = atoi(pidnum);
                printf("Name: %s PID: %d\n", link->name, (int)link->pid);
                *chainend = link;
            }
            break;
        case '^':   //terminate & delete supervision
            {
                char *signum = arg;
                char *name = arg;
                while(isdigit(*name)) { name++; }
                if(name == arg) {
                    perror("[ERROR] Command missing signal number\n");
                    fclose(stream);
                    continue;
                }
                //find in chain
                struct chain **chainpos = &ch;
                while((*chainpos != NULL) && !streq(name, (*chainpos)->name)) {
                    chainpos = &((*chainpos)->next);
                }
                if(*chainpos == NULL) { //not found
                    perror("[ERROR] process not found");
                    fclose(stream);
                    continue;
                }
                //send signal
                pid_t pid = (**chainpos).pid;
                if(kill(pid, atoi(signum)) == -1) {
                    fprintf(stderr, "[ERROR] Failed to send signal: %s\n", strerror(errno));
                    fclose(stream);
                    continue;
                }
                //wait for exit
                waitpid(pid, NULL, 0);  //note: error ignored because race condition
                //remove from chain
                struct chain *c = *chainpos;
                *chainpos = (**chainpos).next;
                free(c);
            }
            break;
        }
        fprintf(stream, "ok\n");
        fclose(stream);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
cmpl $0x1, %edi
jne 0x16e3
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
je 0x16f9
movl %eax, %r13d
xorps %xmm0, %xmm0
leaq 0x22(%rsp), %rdi
movaps %xmm0, -0x2(%rdi)
movups %xmm0, 0x5c(%rdi)
movaps %xmm0, 0x4e(%rdi)
movaps %xmm0, 0x3e(%rdi)
movaps %xmm0, 0x2e(%rdi)
movaps %xmm0, 0x1e(%rdi)
movaps %xmm0, 0xe(%rdi)
movw $0x1, -0x2(%rdi)
leaq 0xd2a(%rip), %rsi       # 0x2320
movl $0x6b, %edx
callq 0x10f0
leaq 0x20(%rsp), %r14
movl %r13d, %edi
movq %r14, %rsi
movl $0x6e, %edx
callq 0x1140
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x1703
cmpl $-0x1, %ebp
je 0x1ab6
leaq 0xa4f(%rip), %rdi       # 0x207f
callq 0x1030
testq %rax, %rax
je 0x1712
movq %rax, %r15
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
je 0x1719
movl %eax, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movups %xmm0, 0x7e(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movw $0x1, 0x20(%rsp)
movq %r15, %rdi
callq 0x1090
addq $-0x6c, %rax
cmpq $-0x6e, %rax
jbe 0x1720
leaq 0x22(%rsp), %rdi
movq %r15, %rsi
callq 0x1060
leaq 0x20(%rsp), %rsi
movl %ebp, %edi
movl $0x6e, %edx
callq 0x1190
cmpl $-0x1, %eax
je 0x1727
leaq 0xa3f(%rip), %rsi       # 0x2109
movl %ebp, %edi
callq 0x1130
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x1a92
movb $0x1, %al
jmp 0x1733
leaq 0x91a(%rip), %rdi       # 0x2004
callq 0x1160
movl $0x1, %ebx
jmp 0x1ab6
callq 0x1465
jmp 0x1ab1
callq 0x1200
movl $0x41, %ebx
jmp 0x1620
callq 0x12cb
jmp 0x172c
callq 0x129e
jmp 0x172c
callq 0x127c
jmp 0x172c
callq 0x124f
movl $0x41, %ebx
xorl %eax, %eax
testb %al, %al
je 0x1ab6
movq $0x0, 0x8(%rsp)
movl %r13d, %edi
movl $0x10, %esi
callq 0x1120
cmpl $-0x1, %eax
je 0x1aac
movl %ebx, 0x4(%rsp)
leaq 0xb94(%rip), %rdi       # 0x22f9
callq 0x1070
movl %r13d, (%rsp)
leaq 0xb98(%rip), %rdi       # 0x230d
callq 0x1070
movl %r13d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %ebp
leaq 0xb88(%rip), %rdi       # 0x2317
callq 0x1070
cmpl $-0x1, %ebp
je 0x19a9
movl %ebp, %edi
leaq 0x963(%rip), %rsi       # 0x2109
callq 0x1130
testq %rax, %rax
je 0x19ca
movq %rax, %r12
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x10c0
testq %rax, %rax
je 0x19d9
movq %r14, %rdi
callq 0x1090
cmpb $0xa, -0x1(%rax,%r14)
jne 0x19e0
movb $0x0, 0x1f(%rsp,%rax)
movq %r14, %rdi
callq 0x1090
cmpq $0x2, %rax
jb 0x19e9
movzbl 0x20(%rsp), %ebx
leaq 0xa3a(%rip), %rdi       # 0x2241
movq %r14, %rsi
xorl %eax, %eax
callq 0x10a0
cmpl $0x2a, %ebx
je 0x18e6
cmpl $0x5e, %ebx
jne 0x198b
callq 0x11d0
movq (%rax), %rcx
xorl %eax, %eax
movsbq 0x21(%rsp,%rax), %rdx
incq %rax
testb $0x8, 0x1(%rcx,%rdx,2)
jne 0x182d
cmpq $0x1, %rax
je 0x1a21
movq 0x8(%rsp), %r15
testq %r15, %r15
sete %bl
je 0x1a18
leaq (%rsp,%rax), %rbp
addq $0x20, %rbp
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x10d0
leaq 0x8(%rsp), %r13
testl %eax, %eax
je 0x189a
movq %r15, %r13
movq 0x10(%r15), %r15
testq %r15, %r15
sete %bl
je 0x1a18
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x10d0
testl %eax, %eax
jne 0x1874
addq $0x10, %r13
testb %bl, %bl
jne 0x1a18
movl 0x8(%r15), %ebp
leaq 0x21(%rsp), %rdi
callq 0x1180
movl %ebp, %edi
movl %eax, %esi
callq 0x1100
cmpl $-0x1, %eax
je 0x1a82
xorl %r15d, %r15d
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq (%r13), %rdi
movq 0x10(%rdi), %rax
movq %rax, (%r13)
callq 0x1040
jmp 0x1981
callq 0x11d0
movq (%rax), %rax
xorl %r15d, %r15d
movsbq 0x21(%rsp,%r15), %rcx
incq %r15
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x18f1
cmpq $0x1, %r15
je 0x1a2a
leaq (%rsp,%r15), %r13
addq $0x20, %r13
leaq 0x8(%rsp), %rax
movq %rax, %rbx
movq (%rax), %rcx
leaq 0x10(%rcx), %rax
testq %rcx, %rcx
jne 0x1918
movl $0x18, %edi
callq 0x1110
testq %rax, %rax
je 0x1a4b
movq %rax, %rbp
movq %r13, %rdi
callq 0x11b0
movq %rax, (%rbp)
testq %rax, %rax
je 0x1a59
movq %rax, %r13
movb $0x0, 0x20(%rsp,%r15)
leaq 0x21(%rsp), %rdi
callq 0x1180
movl %eax, 0x8(%rbp)
xorl %r15d, %r15d
leaq 0x90d(%rip), %rdi       # 0x227f
movq %r13, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
movq %rbp, (%rbx)
movb $0x1, %al
movl (%rsp), %r13d
testb %al, %al
je 0x1a03
movl $0x3, %esi
movl $0x1, %edx
leaq 0x959(%rip), %rdi       # 0x22f5
movq %r12, %rcx
callq 0x11a0
xorl %r15d, %r15d
jmp 0x19fb
movq 0x18(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movq %r14, %rdx
callq 0x139a
movl 0x14(%rsp), %r15d
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rsp)
jmp 0x1a03
movl %ebp, %edi
callq 0x135b
movl $0x3, %r15d
jmp 0x1a03
callq 0x132e
jmp 0x19f5
leaq 0x828(%rip), %rdi       # 0x220f
jmp 0x19f0
leaq 0x83a(%rip), %rdi       # 0x222a
callq 0x1160
movl $0x3, %r15d
movq %r12, %rdi
callq 0x1080
testl %r15d, %r15d
je 0x176e
cmpl $0x3, %r15d
je 0x176e
jmp 0x1a8c
leaq 0x899(%rip), %rdi       # 0x22b8
jmp 0x1a52
leaq 0x869(%rip), %rdi       # 0x2291
jmp 0x1a31
leaq 0x81d(%rip), %rdi       # 0x224e
callq 0x1160
movq %r12, %rdi
callq 0x1080
movl $0x3, %r15d
xorl %eax, %eax
jmp 0x1987
leaq 0x820(%rip), %rdi       # 0x2272
callq 0x1160
jmp 0x1a6d
leaq 0x812(%rip), %rdi       # 0x2272
callq 0x1160
movq %rbp, %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1080
movl $0x3, %r15d
xorl %eax, %eax
jmp 0x1983
movq %r12, %rdi
callq 0x12ed
jmp 0x1a75
movl 0x4(%rsp), %ebx
jmp 0x1ab6
callq 0x122d
movl $0x41, %ebx
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
jmp 0x1733
callq 0x1438
movl $0x41, %ebx
movl %ebx, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq