// File: /workspace/github_c_makefile_star2_O2/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 0x1332
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
jne 0x1342
movq 0x2cde(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x11c0
leaq 0xcf6(%rip), %rsi       # 0x2026
jmp 0x1394
leaq 0xccb(%rip), %rdi       # 0x2004
callq 0x1160
pushq $0x1
jmp 0x13a3
movl %eax, %ebx
leaq 0x22(%rsp), %rdi
andw $0x0, 0x6a(%rdi)
movw $0x1, -0x2(%rdi)
leaq 0xfc5(%rip), %rsi       # 0x2320
pushq $0x6b
popq %rdx
callq 0x10f0
leaq 0x20(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %ebx, %edi
callq 0x1140
cmpl $-0x1, %eax
jne 0x13b6
movq 0x2c7a(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x11c0
leaq 0xcbc(%rip), %rsi       # 0x2050
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x41
popq %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcc2(%rip), %rdi       # 0x207f
callq 0x1030
testq %rax, %rax
jne 0x13e5
movq 0x2c2a(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcb1(%rip), %rdi       # 0x2089
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
jmp 0x13a1
movq %rax, %r14
movl %ebx, 0xc(%rsp)
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
jne 0x141f
movq 0x2bf4(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x11c0
leaq 0xc8a(%rip), %rsi       # 0x20a4
jmp 0x1394
movl %eax, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x7e(%rsp)
movups %xmm0, 0x72(%rsp)
movups %xmm0, 0x62(%rsp)
movups %xmm0, 0x52(%rsp)
movups %xmm0, 0x42(%rsp)
movups %xmm0, 0x32(%rsp)
movups %xmm0, 0x22(%rsp)
movw $0x1, 0x20(%rsp)
movq %r14, %rdi
callq 0x1090
addq $-0x6c, %rax
cmpq $-0x6e, %rax
ja 0x1478
movq 0x2b91(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc58(%rip), %rdi       # 0x20c9
pushq $0x20
jmp 0x13da
leaq 0x22(%rsp), %rdi
movq %r14, %rsi
callq 0x1060
leaq 0x20(%rsp), %r15
pushq $0x6e
popq %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x1190
cmpl $-0x1, %eax
jne 0x14be
movq 0x2b55(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x11c0
leaq 0xc31(%rip), %rsi       # 0x20ea
jmp 0x1394
leaq 0xc44(%rip), %rsi       # 0x2109
movl %ebp, %edi
callq 0x1130
testq %rax, %rax
jne 0x14e9
movq 0x2b20(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc2a(%rip), %rdi       # 0x210c
pushq $0x27
jmp 0x13da
movq %rax, %rbx
andq $0x0, 0x10(%rsp)
pushq $0x10
popq %rsi
movl 0xc(%rsp), %ebp
movl %ebp, %edi
callq 0x1120
cmpl $-0x1, %eax
jne 0x1527
movq 0x2aec(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x11c0
leaq 0xc12(%rip), %rsi       # 0x2134
jmp 0x1394
movq %rbx, 0x18(%rsp)
leaq 0xdc6(%rip), %rdi       # 0x22f9
callq 0x1070
leaq 0xdce(%rip), %rdi       # 0x230d
callq 0x1070
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %r13d
leaq 0xdbe(%rip), %rdi       # 0x2317
callq 0x1070
cmpl $-0x1, %r13d
je 0x182b
movl %r13d, %edi
leaq 0xb97(%rip), %rsi       # 0x2109
callq 0x1130
testq %rax, %rax
je 0x173c
movq %rax, %r12
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x10c0
testq %rax, %rax
je 0x1773
movq %r15, %rdi
callq 0x1090
cmpb $0xa, -0x1(%rax,%r15)
jne 0x179f
movb $0x0, 0x1f(%rsp,%rax)
movq %r15, %rdi
callq 0x1090
cmpq $0x2, %rax
jb 0x17a8
movzbl 0x20(%rsp), %ebx
leaq 0xc6e(%rip), %rdi       # 0x2241
movq %r15, %rsi
xorl %eax, %eax
callq 0x10a0
cmpl $0x2a, %ebx
je 0x1644
cmpl $0x5e, %ebx
jne 0x171a
callq 0x11d0
movq (%rax), %rcx
xorl %eax, %eax
movsbq 0x21(%rsp,%rax), %rdx
incq %rax
testb $0x8, 0x1(%rcx,%rdx,2)
jne 0x15f5
cmpq $0x1, %rax
je 0x17b1
leaq (%rsp,%rax), %r13
addq $0x20, %r13
leaq 0x10(%rsp), %r14
movq (%r14), %rbx
testq %rbx, %rbx
je 0x17c3
movq (%rbx), %rsi
movq %r13, %rdi
callq 0x10d0
testl %eax, %eax
je 0x16dd
addq $0x10, %rbx
movq %rbx, %r14
jmp 0x161c
callq 0x11d0
movq (%rax), %rax
xorl %r14d, %r14d
movsbq 0x21(%rsp,%r14), %rcx
incq %r14
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x164f
cmpq $0x1, %r14
je 0x17ba
leaq (%rsp,%r14), %rbp
addq $0x20, %rbp
leaq 0x10(%rsp), %rax
movq %rax, %rbx
movq (%rax), %rcx
leaq 0x10(%rcx), %rax
testq %rcx, %rcx
jne 0x1676
pushq $0x18
popq %rdi
callq 0x1110
testq %rax, %rax
je 0x17d4
movq %rax, %r13
movq %rbp, %rdi
callq 0x11b0
movq %rax, (%r13)
testq %rax, %rax
je 0x17e2
movq %rax, %rbp
movb $0x0, 0x20(%rsp,%r14)
leaq 0x21(%rsp), %rdi
callq 0x1180
movl %eax, 0x8(%r13)
leaq 0xbb3(%rip), %rdi       # 0x227f
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
movq %r13, (%rbx)
jmp 0x1716
movl 0x8(%rbx), %ebp
leaq 0x21(%rsp), %rdi
callq 0x1180
movl %ebp, %edi
movl %eax, %esi
callq 0x1100
cmpl $-0x1, %eax
je 0x17f8
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq (%r14), %rdi
movq 0x10(%rdi), %rax
movq %rax, (%r14)
callq 0x1040
movl 0xc(%rsp), %ebp
leaq 0xbd4(%rip), %rdi       # 0x22f5
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x11a0
movq %r12, %rdi
callq 0x1080
jmp 0x1538
movq 0x28b5(%rip), %rax      # 0x3ff8
movq (%rax), %r12
callq 0x1050
movl (%rax), %edi
callq 0x11c0
movq %r12, %rdi
leaq 0xa68(%rip), %rsi       # 0x21c4
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
movl %r13d, %edi
callq 0x10b0
jmp 0x1538
movq 0x287e(%rip), %rax      # 0x3ff8
movq (%rax), %r13
callq 0x1050
movl (%rax), %edi
callq 0x11c0
movq %r13, %rdi
leaq 0xa54(%rip), %rsi       # 0x21e7
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x172f
leaq 0xa69(%rip), %rdi       # 0x220f
jmp 0x17ca
leaq 0xa7b(%rip), %rdi       # 0x222a
jmp 0x17ca
leaq 0xad9(%rip), %rdi       # 0x2291
jmp 0x17ca
leaq 0xa8d(%rip), %rdi       # 0x224e
jmp 0x17db
leaq 0xaee(%rip), %rdi       # 0x22b8
callq 0x1160
jmp 0x172f
leaq 0xa97(%rip), %rdi       # 0x2272
callq 0x1160
jmp 0x1822
leaq 0xa89(%rip), %rdi       # 0x2272
callq 0x1160
movq %r13, %rdi
callq 0x1040
jmp 0x1822
movq 0x27f9(%rip), %rax      # 0x3ff8
movq (%rax), %r13
callq 0x1050
movl (%rax), %edi
callq 0x11c0
movq %r13, %rdi
leaq 0xaba(%rip), %rsi       # 0x22d2
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %ebp
jmp 0x172f
movq 0x27c6(%rip), %r15      # 0x3ff8
movq (%r15), %r14
callq 0x1050
movq %rax, %rbx
movl (%rax), %edi
callq 0x11c0
leaq 0x907(%rip), %rsi       # 0x2152
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
leaq 0x91c(%rip), %rsi       # 0x217b
movq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
js 0x187f
movq %r14, %rdi
callq 0x1080
jmp 0x13a1
movq (%r15), %r14
movl (%rbx), %edi
callq 0x11c0
leaq 0x904(%rip), %rsi       # 0x2194
movq %r14, %rdi
jmp 0x1397