Pi66

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

sta.c

  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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

#define SOCKET_PATH "/tmp/sta.sock"

typedef struct{
    const char *(*func)(const char *);
    const char *fmt;
    const char *parm;
    int id;
    int delay;
}Args;

#include "util.h"
#include "config.h"

pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;

typedef struct {
    int id;
    char *name;
} Pro;

typedef struct {
    Pro *process;
    int count;
} Pros;

typedef struct {
    int id2;
    Pros *list;
} Potato;

Pros *init()
{
    Pros *list = malloc(sizeof(Pros));
    list->process = NULL;
    list->count = 0;
    return list;
}

void add_pro(Pros *list, int id, const char *name)
{
    for (int i = 0; i < list->count; ++i) {
        if (id == list->process[i].id) {
            free(list->process[i].name);
            list->process[i].name = strdup(name);
            return;
        }
    }
    list->count++;
    list->process = realloc(list->process, sizeof(Pro) * list->count);
    if (!list->process) {
        warn("Memory: memory allocation failed.");
        exit(1);
    }
    list->process[list->count - 1].name = strdup(name);
    list->process[list->count - 1].id = id;
}

void show_pros(Pros *list)
{
    for (int i = 0; i < list->count - 1; i++) {
        for (int j = i + 1; j < list->count; j++) {
            if (list->process[i].id > list->process[j].id) {
                Pro temp = list->process[i];
                list->process[i] = list->process[j];
                list->process[j] = temp;
            }
        }
    }
    for (int a = 0; a < list->count; ++a)
        printf("%s", list->process[a].name);
    printf("\n");
    fflush(stdout);
}

void usage()
{
    printf("Usage:\n");
    printf("\t-id        <number>       => process id (id >= 1)\n");
    printf("\t-name      <string>       => process name\n");
    printf("\t-h -help                  => print this help menu\n");
    exit(1);
}

void append(Args *command, Pros *list)
{
    const char *cmd = command->func(command->parm);
    if (!cmd) cmd = "N/A";
    char tmp[256];
    snprintf(tmp, sizeof(tmp), command->fmt, cmd);
    add_pro(list, command->id, tmp);
    show_pros(list);
}

void *worker(void *arg)
{
    Potato *data = arg;
    Args command = commands[data->id2];
    while (1) {
        pthread_mutex_lock(&list_mutex);
        append(&command, data->list);
        pthread_mutex_unlock(&list_mutex);
        usleep(command.delay * 1000);
    }
    return NULL;
}

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;
            if (pthread_create(&tids[i], NULL, worker, info))
                warn("Thread: can't create thread for id: %d", info->id2);
        } else {
            append(&commands[i], list);
        }
    }

    while (1) {
        int client = accept(sock, NULL, NULL);
        if (client < 0) continue;

        char buf[256], name[256];
        int n, id;

        while ((n = recv(client, buf, sizeof(buf) - 1, 0)) > 0) {
            buf[n] = '\0';
            char *s = strstr(buf, "|+|");
            if (s) {
                *s = '\0';
                id = atoi(buf);
                strncpy(name, s + 3, sizeof(name) - 1);
                name[sizeof(name) - 1] = '\0';
                add_pro(list, id, name);
                show_pros(list);
            }
        }
        close(client);
    }
}

void run_client(int argc, char const *argv[], struct sockaddr_un *addr, int sock)
{
    int id = -1;
    char *name = NULL;
    char buf[256];

    for (int i = 1; i < argc; i++) {
        if (!strcmp("-h", argv[i]) || !strcmp("-help", argv[i]))
            usage();
        if (!strcmp("-id", argv[i]) && i + 1 < argc)
            id = atoi(argv[++i]);
        else if (!strcmp("-name", argv[i]) && i + 1 < argc)
            name = (char *)argv[++i];
    }

    if (id <= 0)
        usage();

    if (name) {
        snprintf(buf, sizeof(buf), "%d|+|%s", id, name);
    } else {
        buf[0] = '\0';
        for (int i = 0; i < (int)(sizeof(commands) / sizeof(commands[0])); ++i) {
            if (id == commands[i].id) {
                const char *cmd = commands[i].func(commands[i].parm);
                if (!cmd) cmd = "N/A";
                char tmp[256];
                snprintf(tmp, sizeof(tmp), commands[i].fmt, cmd);
                snprintf(buf, sizeof(buf), "%d|+|%s", id, tmp);
                break;
            }
        }
        if (buf[0] == '\0') {
            warn("Client: no command found for id %d", id);
            return;
        }
    }

    if (connect(sock, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
        warn("Connect: failed: %s", strerror(errno));
        return;
    }
    write(sock, buf, strlen(buf));
}

int main(int argc, char const *argv[])
{
    struct sockaddr_un addr = {0};
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, SOCKET_PATH);

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

    Pros *list = init();

    for (int i = 1; i < argc; ++i) {
          if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--server") == 0) {
            if (access(SOCKET_PATH, F_OK) == 0){
                int checking_sock = socket(AF_UNIX, SOCK_STREAM, 0);
                if (checking_sock >= 0) {
                  if (connect(checking_sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
                      die("Socket: socket exists and connectable.");        
                  close(checking_sock);
                }
            if (unlink(SOCKET_PATH) == -1)
              die("Socket: socket clean up failed.");
            }
            run_server(&addr, sock, list);
        }
    }

    if (access(SOCKET_PATH, F_OK) == 0) {
        run_client(argc, argv, &addr, sock);
        goto clean_up;
    }

    warn("ERROR: server not running (no socket at " SOCKET_PATH ")");
    unlink(SOCKET_PATH);
clean_up:
    close(sock);
    return 0;
}