// File: /workspace/github_c_makefile_star2_O3/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 $0x418, %rsp            # imm = 0x418
cmpl $0x1, %edi
jne 0x1532
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
jne 0x1548
callq 0x13db
jmp 0x198d
leaq 0xacb(%rip), %rdi       # 0x2004
callq 0x1160
movl $0x1, %eax
jmp 0x1992
movl %eax, %ebx
leaq 0x12(%rsp), %rdi
movw $0x0, 0x6a(%rdi)
movw $0x1, -0x2(%rdi)
leaq 0xdbe(%rip), %rsi       # 0x2320
movl $0x6b, %edx
callq 0x10f0
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
movl $0x6e, %edx
callq 0x1140
cmpl $-0x1, %eax
jne 0x1591
leaq 0x10(%rsp), %rdi
callq 0x13ae
jmp 0x198d
leaq 0xae7(%rip), %rdi       # 0x207f
callq 0x1030
testq %rax, %rax
jne 0x15ac
callq 0x138c
jmp 0x198d
movq %rax, %r14
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11e0
cmpl $-0x1, %eax
jne 0x15cf
callq 0x135f
jmp 0x198d
movl %eax, %ebp
xorps %xmm0, %xmm0
movups %xmm0, 0x6e(%rsp)
movups %xmm0, 0x62(%rsp)
movups %xmm0, 0x52(%rsp)
movups %xmm0, 0x42(%rsp)
movups %xmm0, 0x32(%rsp)
movups %xmm0, 0x22(%rsp)
movups %xmm0, 0x12(%rsp)
movw $0x1, 0x10(%rsp)
movq %r14, %rdi
callq 0x1090
addq $-0x6c, %rax
cmpq $-0x6e, %rax
ja 0x161a
callq 0x133d
jmp 0x198d
leaq 0x12(%rsp), %rdi
movq %r14, %rsi
callq 0x1060
leaq 0x10(%rsp), %r15
movl %ebp, %edi
movq %r15, %rsi
movl $0x6e, %edx
callq 0x1190
cmpl $-0x1, %eax
jne 0x164a
callq 0x1310
jmp 0x198d
leaq 0xab8(%rip), %rsi       # 0x2109
movl %ebp, %edi
callq 0x1130
testq %rax, %rax
jne 0x1667
callq 0x12ee
jmp 0x198d
movq %rax, %r14
movq $0x0, (%rsp)
movl %ebx, %edi
movl $0x10, %esi
callq 0x1120
cmpl $-0x1, %eax
jne 0x168d
callq 0x12c1
jmp 0x198d
movq %r14, 0x8(%rsp)
leaq 0xc60(%rip), %rdi       # 0x22f9
callq 0x1070
leaq 0xc68(%rip), %rdi       # 0x230d
callq 0x1070
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %r13d
leaq 0xc58(%rip), %rdi       # 0x2317
callq 0x1070
cmpl $-0x1, %r13d
je 0x193e
movl %r13d, %edi
leaq 0xa31(%rip), %rsi       # 0x2109
callq 0x1130
testq %rax, %rax
je 0x18c5
movq %rax, %r12
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x10c0
testq %rax, %rax
je 0x18d2
movq %r15, %rdi
callq 0x1090
cmpb $0xa, -0x1(%rax,%r15)
jne 0x18d9
movb $0x0, 0xf(%rsp,%rax)
movq %r15, %rdi
callq 0x1090
cmpq $0x2, %rax
jb 0x18e2
movzbl 0x10(%rsp), %ebp
leaq 0xb08(%rip), %rdi       # 0x2241
movq %r15, %rsi
xorl %eax, %eax
callq 0x10a0
cmpl $0x2a, %ebp
je 0x1803
cmpl $0x5e, %ebp
jne 0x189f
callq 0x11d0
movq (%rax), %rcx
xorl %eax, %eax
movsbq 0x11(%rsp,%rax), %rdx
incq %rax
testb $0x8, 0x1(%rcx,%rdx,2)
jne 0x175f
cmpq $0x1, %rax
je 0x18f4
movq (%rsp), %rbp
testq %rbp, %rbp
je 0x18eb
leaq (%rsp,%rax), %r13
addq $0x10, %r13
movq (%rbp), %rsi
movq %r13, %rdi
callq 0x10d0
movq %rsp, %r14
testl %eax, %eax
je 0x17c5
movq %rbp, %r14
movq 0x10(%rbp), %rbp
testq %rbp, %rbp
je 0x18eb
movq (%rbp), %rsi
movq %r13, %rdi
callq 0x10d0
testl %eax, %eax
jne 0x17a1
addq $0x10, %r14
movl 0x8(%rbp), %ebp
leaq 0x11(%rsp), %rdi
callq 0x1180
movl %ebp, %edi
movl %eax, %esi
callq 0x1100
cmpl $-0x1, %eax
je 0x1934
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq (%r14), %rdi
movq 0x10(%rdi), %rax
movq %rax, (%r14)
callq 0x1040
jmp 0x189f
callq 0x11d0
movq (%rax), %rax
xorl %r14d, %r14d
movsbq 0x11(%rsp,%r14), %rcx
incq %r14
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x180e
cmpq $0x1, %r14
je 0x18fd
leaq (%rsp,%r14), %rbp
addq $0x10, %rbp
movq %rsp, %rax
movq %rax, %r15
movq (%rax), %rcx
leaq 0x10(%rcx), %rax
testq %rcx, %rcx
jne 0x1833
movl $0x18, %edi
callq 0x1110
testq %rax, %rax
je 0x190b
movq %rax, %r13
movq %rbp, %rdi
callq 0x11b0
movq %rax, (%r13)
testq %rax, %rax
je 0x1919
movq %rax, %rbp
movb $0x0, 0x10(%rsp,%r14)
leaq 0x11(%rsp), %rdi
callq 0x1180
movl %eax, 0x8(%r13)
leaq 0x9f4(%rip), %rdi       # 0x227f
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
movq %r13, (%r15)
leaq 0x10(%rsp), %r15
movl $0x3, %esi
movl $0x1, %edx
leaq 0xa45(%rip), %rdi       # 0x22f5
movq %r12, %rcx
callq 0x11a0
movq %r12, %rdi
callq 0x1080
jmp 0x169e
movl %r13d, %edi
callq 0x125a
jmp 0x169e
callq 0x122d
jmp 0x18b8
leaq 0x92f(%rip), %rdi       # 0x220f
jmp 0x1904
leaq 0x941(%rip), %rdi       # 0x222a
jmp 0x1904
leaq 0x9c6(%rip), %rdi       # 0x22b8
jmp 0x1904
leaq 0x996(%rip), %rdi       # 0x2291
jmp 0x1904
leaq 0x94a(%rip), %rdi       # 0x224e
callq 0x1160
jmp 0x18b8
leaq 0x960(%rip), %rdi       # 0x2272
callq 0x1160
jmp 0x192d
leaq 0x952(%rip), %rdi       # 0x2272
callq 0x1160
movq %r13, %rdi
callq 0x1040
leaq 0x10(%rsp), %r15
jmp 0x18b8
callq 0x1200
jmp 0x18b8
movq 0x26b3(%rip), %rax      # 0x3ff8
movq (%rax), %r14
callq 0x1050
movq %rax, %rbx
movl (%rax), %edi
callq 0x11c0
leaq 0x7f4(%rip), %rsi       # 0x2152
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
leaq 0x809(%rip), %rsi       # 0x217b
movq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
js 0x19a4
movq %r14, %rdi
callq 0x1080
movl $0x41, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x1299
jmp 0x198d