Pi66

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

← back to log

fix: crashes and improve logging

author: pi66
date: 2026-04-28 22:33
hash: b587ef7ca41dab0a19410e5493842818a1bdfffe

Diffstat:

A

.gitignore
1 +

M

sta.c
30 +++++++++---------------------
 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0e56cf2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+config.h
diff --git a/sta.c b/sta.c
index a69c75d..ea7b2cd 100644
--- a/sta.c
+++ b/sta.c
@@ -58,7 +58,7 @@ void add_pro(Pros *list, int id, char *name)
    list->count++;
    list->process = realloc(list->process, sizeof(Pro) * list->count);
    if (!list->process) {
-       warn("Memory", "memory allocation failed.");
+       warn("Memory: memory allocation failed.");
        exit(1);
    }
    list->process[list->count - 1].name = strdup(name);
@@ -108,13 +108,6 @@ void append(Args *command, Pros *list)
    show_pros(list);
 }

-void clean_up(int s)
-{
-   (void)s;
-   unlink(SOCKET_PATH);
-   exit(0);
-}
-
 void *worker(void *arg)
 {
    Potato *data = arg;
@@ -130,10 +123,6 @@ void *worker(void *arg)

 void run_server(struct sockaddr_un *addr, int sock, Pros *list)
 {
-   signal(SIGTERM, clean_up);
-   signal(SIGINT, clean_up);
-   unlink(SOCKET_PATH);
-
    int cmds = sizeof(commands) / sizeof(commands[0]);
    pthread_t tids[cmds];
    for (int i = 0; i < cmds; ++i) {
@@ -148,10 +137,10 @@ void run_server(struct sockaddr_un *addr, int sock, Pros *list)
    }

    if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) < 0)
-       warn("Bind", "failed: %s", strerror(errno));
+       warn("Bind: failed: %s", strerror(errno));

    if (listen(sock, 5) < 0)
-       warn("Listen", "failed: %s", strerror(errno));
+       warn("Listen failed: %s", strerror(errno));

    while (1) {
        int client = accept(sock, NULL, NULL);
@@ -215,7 +204,7 @@ void run_client(int argc, char const *argv[], struct sockaddr_un *addr, int sock
    }

    if (connect(sock, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
-       warn("Connect", "failed: %s", strerror(errno));
+       warn("Connect: failed: %s", strerror(errno));
        return;
    }
    write(sock, buf, strlen(buf));
@@ -229,18 +218,17 @@ int main(int argc, char const *argv[])

    int sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock < 0)
-       warn("Socket", "failed to create socket: %s", strerror(errno));
+       warn("Socket: failed to create socket: %s", strerror(errno));

    Pros *list = init();

    for (int i = 1; i < argc; ++i) {
-       if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--server")) {
+         if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--server") == 0) {
            if (access(SOCKET_PATH, F_OK) == 0){
                if ( connect(sock, (struct sockaddr *)&addr, sizeof(addr) ) == 0 )
-                   unlink(SOCKET_PATH);
-               else
-                   die("ERROR: socket exists in " SOCKET_PATH);
-           }
+                   die("Socket: socket exists and connectable.");      
+           }else 
+             unlink(SOCKET_PATH);
            run_server(&addr, sock, list);
        }
    }