---
# System prepended metadata

title: gsoc log

---

# Week 5 log
## Some interesting codes
the whole code is the IPC solution I came up with which is replaced by one line of code setutxent()
details:
https://lists.freebsd.org/archives/soc-status/2024-July/000080.html
```clike
int
main()
{
        cap_channel_t *cap_casper, *cap_net;
        int s, sock1, sock, client_sock;
        struct sockaddr_un addr;
        socklen_t len;

        cap_casper = cap_init();
        cap_net = cap_service_open(chan: cap_casper, name: "system.net");

        cap_close(chan: cap_casper);
        //if (caph_enter() < 0)
                //err(1, "unable to enter capability mode");
        s = socket(PF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
                err(1, "socket\n");
                exit(1);
        }

        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
        unlink(SOCKET_PATH);

        if (cap_bind(cap_casper,s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
                err(1, "bind");
                printf("bind:\n");
                exit(1);
        }

        listen(s, 5);
        //if (caph_enter() < 0)
               //err(1, "unable to enter capability mode");

        pid_t pid = fork();
        if (pid == -1) {
                perror("fork");
                exit(1);
        } else if (pid == 0) { // Child
                printf("child\n");
                len = sizeof(addr);
                // close(s);
                sock = accept(s, (struct sockaddr *)&addr, &len);
                if (sock < 0) {
                    err(1, "accept\n");
                        exit(1);
                }
                send_utmpx(sock);
                // process_nvlist_data(sock);
                close(sock);
        } else { // Parent
                //if (caph_enter() < 0)
                        //err(1, "unable to enter capability mode");

                printf("parent\n");
                client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
                sock1 = cap_connect(cap_net, client_sock, (struct sockaddr                         *)&addr,
                    sizeof(addr));
                if (sock1 < 0) {
                        // printf("conncettest");
                        err(1, "connect\n");
                        exit(1);
                }

                receive_utmpx(sock: client_sock);
                // read_nvlist_data(client_sock);

                close(client_sock);
                // wait(NULL); // Wait for child to finish
        }

        return 0;
}
```