Pi66

sta
minimal & simple status monitoring app.
git clone https://git.pi66.xyz/sta

← back to log

fix: bind and listen before threads

author: pi66
date: 2026-05-21 18:58
hash: b4becd8f58564bba5c44886f5cc0f03fb613f41b

Diffstat:

M

sta.c
16 +++++++++++++++++-------------
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
diff --git a/sta.c b/sta.c
index fd6f8a4..d6e010d 100644
--- a/sta.c
+++ b/sta.c
@@ -117,24 +117,26 @@ void *worker(void *arg)
 void run_server(struct sockaddr_un *addr, int sock, Pros *list)
 {
    int cmds = sizeof(commands) / sizeof(commands[0]);
+
+   if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) < 0)
+       warn("Bind: failed: %s", strerror(errno));
+
+   if (listen(sock, 5) < 0)
+       warn("Listen failed: %s", strerror(errno));
+
    pthread_t tids[cmds];
    for (int i = 0; i < cmds; ++i) {
        if (commands[i].delay) {
            Potato *info = malloc(sizeof(Potato));
            info->list = list;
            info->id2 = i;
-           pthread_create(&tids[i], NULL, worker, info);
+           if (pthread_create(&tids[i], NULL, worker, info))
+               warn("Thread: can't create thread for id: %d", info->id2);
        } else {
            append(&commands[i], list);
        }
    }

-   if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) < 0)
-       warn("Bind: failed: %s", strerror(errno));
-
-   if (listen(sock, 5) < 0)
-       warn("Listen failed: %s", strerror(errno));
-
    while (1) {
        int client = accept(sock, NULL, NULL);
        if (client < 0) continue;